react-form-validator-components
Version:
Components for react-form-validator-core
63 lines (51 loc) • 1.2 kB
JavaScript
import React, {Component} from 'react';
import PropTypes from 'prop-types';
import TextValidatorCore from './TextValidatorCore';
class TextValidator extends Component {
constructor(props) {
super(props);
this.state = {
value: props.value,
};
this.onChange = this.onChange.bind(this);
}
componentDidMount() {
if (this.props.value == null && this.props.defaultValue !== null) {
setTimeout(() => {
this.onChange(this.props.defaultValue);
});
}
}
onChange(value) {
if (this.state.value === value) {
return;
}
this.setState({
value,
});
if (this.props.onChange) {
this.props.onChange(value);
}
}
render() {
const {value, defaultValue, onChange, ...rest} = this.props;
const currentValue = this.state.value;
if (currentValue != null) {
return (
<TextValidatorCore
onChange={this.onChange}
value={currentValue}
{...rest}
/>
);
} else {
return (<div></div>);
}
}
}
TextValidator.propTypes = {
value: PropTypes.any,
defaultValue: PropTypes.any,
onChange: PropTypes.func,
}
export default TextValidator;