2017-08-10 01:56:27 +00:00
|
|
|
import React, {PureComponent} from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
2017-03-03 01:44:07 +00:00
|
|
|
import autobind from 'autobind-decorator';
|
2016-08-15 17:04:36 +00:00
|
|
|
import {Cookie} from 'tough-cookie';
|
|
|
|
|
2017-03-03 01:44:07 +00:00
|
|
|
@autobind
|
2017-02-28 21:32:23 +00:00
|
|
|
class CookieInput extends PureComponent {
|
2017-03-03 01:44:07 +00:00
|
|
|
constructor (props) {
|
|
|
|
super(props);
|
|
|
|
|
|
|
|
this.state = {
|
2017-03-03 20:09:08 +00:00
|
|
|
isValid: true
|
2017-03-03 01:44:07 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
_setInputRef (n) {
|
|
|
|
this._input = n;
|
|
|
|
}
|
2016-08-15 17:04:36 +00:00
|
|
|
|
2017-03-03 01:44:07 +00:00
|
|
|
_handleChange (e) {
|
2016-08-15 17:04:36 +00:00
|
|
|
const isValid = this._isValid();
|
|
|
|
|
|
|
|
if (isValid) {
|
2017-03-03 01:44:07 +00:00
|
|
|
const value = e.target.value;
|
|
|
|
this.props.onChange(value);
|
2016-08-15 17:04:36 +00:00
|
|
|
}
|
|
|
|
|
2017-03-03 20:09:08 +00:00
|
|
|
this.setState({isValid});
|
2016-08-15 17:04:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_isValid () {
|
|
|
|
try {
|
|
|
|
const cookie = Cookie.parse(this._input.value);
|
|
|
|
return !!(cookie && cookie.domain);
|
|
|
|
} catch (e) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
render () {
|
|
|
|
const {defaultValue} = this.props;
|
|
|
|
const {isValid} = this.state;
|
|
|
|
|
|
|
|
return (
|
|
|
|
<input
|
|
|
|
className={isValid ? '' : 'input--error'}
|
2017-03-03 01:44:07 +00:00
|
|
|
ref={this._setInputRef}
|
2016-08-15 17:04:36 +00:00
|
|
|
type="text"
|
|
|
|
defaultValue={defaultValue}
|
2017-03-03 01:44:07 +00:00
|
|
|
onChange={this._handleChange}
|
2016-08-15 17:04:36 +00:00
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
CookieInput.propTypes = {
|
2017-03-08 05:52:17 +00:00
|
|
|
onChange: PropTypes.func.isRequired,
|
|
|
|
defaultValue: PropTypes.string.isRequired
|
2016-08-15 17:04:36 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export default CookieInput;
|