UNPKG

react-form-validator-components

Version:

Components for react-form-validator-core

62 lines (51 loc) 1.2 kB
import React, {Component} from 'react'; import DateValidatorCore from './DateValidatorCore'; import PropTypes from 'prop-types'; class DateValidator 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 {value, onChange, defaultValue, ...rest} = this.props; const currentValue = this.state.value; if (currentValue !== null) { return ( <DateValidatorCore onChange={this.onChange} value={currentValue} {...rest} /> ); } else { return (<div></div>); } } } DateValidator.propTypes = { value: PropTypes.any, defaultValue: PropTypes.object, onChange: PropTypes.func }; export default DateValidator;