2017-08-22 21:49:50 +03:00
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import React from 'react';
|
2016-07-31 16:53:16 +03:00
|
|
|
|
2016-08-05 08:58:24 +03:00
|
|
|
import classNames from 'classnames';
|
|
|
|
|
2016-07-31 16:53:16 +03:00
|
|
|
import captcha from 'services/captcha';
|
|
|
|
import { skins, SKIN_DARK } from 'components/ui';
|
2016-08-14 11:59:23 +03:00
|
|
|
import { ComponentLoader } from 'components/ui/loader';
|
2016-07-31 16:53:16 +03:00
|
|
|
|
|
|
|
import styles from './form.scss';
|
|
|
|
import FormInputComponent from './FormInputComponent';
|
|
|
|
|
|
|
|
export default class Captcha extends FormInputComponent {
|
|
|
|
static displayName = 'Captcha';
|
|
|
|
|
|
|
|
static propTypes = {
|
2016-08-14 11:59:23 +03:00
|
|
|
skin: PropTypes.oneOf(skins),
|
|
|
|
delay: PropTypes.number
|
2016-07-31 16:53:16 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
static defaultProps = {
|
2016-08-14 11:59:23 +03:00
|
|
|
skin: SKIN_DARK,
|
|
|
|
delay: 0
|
2016-07-31 16:53:16 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
componentDidMount() {
|
2017-08-23 00:00:10 +03:00
|
|
|
setTimeout(() => {
|
2016-08-14 13:10:59 +03:00
|
|
|
captcha.render(this.el, {
|
|
|
|
skin: this.props.skin,
|
|
|
|
onSetCode: this.setCode
|
2017-08-23 00:00:10 +03:00
|
|
|
}).then((captchaId) => this.captchaId = captchaId);
|
|
|
|
}, this.props.delay);
|
2016-07-31 16:53:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
2016-08-05 08:58:24 +03:00
|
|
|
const {skin} = this.props;
|
|
|
|
|
2016-07-31 16:53:16 +03:00
|
|
|
return (
|
2016-08-14 11:59:23 +03:00
|
|
|
<div className={styles.captchaContainer}>
|
|
|
|
<div className={styles.captchaLoader}>
|
|
|
|
<ComponentLoader />
|
|
|
|
</div>
|
2016-08-14 13:10:59 +03:00
|
|
|
|
2016-08-05 08:58:24 +03:00
|
|
|
<div ref={this.setEl} className={classNames(
|
|
|
|
styles.captcha,
|
|
|
|
styles[`${skin}Captcha`]
|
|
|
|
)} />
|
2016-08-14 13:10:59 +03:00
|
|
|
|
2016-08-05 08:41:33 +03:00
|
|
|
{this.renderError()}
|
|
|
|
</div>
|
2016-07-31 16:53:16 +03:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2016-08-14 13:10:59 +03:00
|
|
|
reset() {
|
|
|
|
captcha.reset(this.captchaId);
|
|
|
|
}
|
|
|
|
|
2016-08-22 22:18:11 +03:00
|
|
|
getValue() {
|
|
|
|
return this.state && this.state.code;
|
|
|
|
}
|
2016-08-14 13:10:59 +03:00
|
|
|
|
2016-08-22 22:18:11 +03:00
|
|
|
onFormInvalid() {
|
2016-08-14 13:10:59 +03:00
|
|
|
this.reset();
|
|
|
|
}
|
|
|
|
|
2016-07-31 16:53:16 +03:00
|
|
|
setCode = (code) => this.setState({code});
|
|
|
|
}
|