react-form-validator-components
Version:
Components for react-form-validator-core
60 lines (48 loc) • 1.17 kB
JavaScript
import React, {Component} from 'react';
import PropTypes from 'prop-types';
import SelectValidatorCore from './SelectValidatorCore';
class SelectValidator extends Component {
constructor(props) {
super(props);
this.state = {
value: props.value,
};
this.onChange = this.onChange.bind(this);
}
componentDidMount() {
if (!this.props.value && 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 {children, value, onChange, defaultValue, ...rest} = this.props;
const currentValue = this.state.value;
return (
<SelectValidatorCore
onChange={this.onChange}
value={currentValue}
{...rest}>
{children}
</SelectValidatorCore>
);
}
}
SelectValidator.propTypes = {
value: PropTypes.any,
defaultValue: PropTypes.any,
onChange: PropTypes.func
};
export default SelectValidator;