UNPKG

react-form-validator-components

Version:

Components for react-form-validator-core

77 lines (64 loc) 1.55 kB
import React from 'react'; import PropTypes from 'prop-types'; import {DatePicker} from '@material-ui/pickers'; import {ValidatorComponent} from 'react-form-validator-core'; class DateValidatorCore extends ValidatorComponent { componentDidMount() { super.componentDidMount(); if (this.props.doInitialValidation) { this.validate(this.props.value, true); } } onChange(value) { if (this.props.onChange) { this.props.onChange(value); } } render() { const { error, errorMessages, validators, requiredError, helperText, children, onChange, validatorListener, withRequiredValidator, // custom doInitialValidation, ...rest } = this.props; const { isValid } = this.state; return ( <DatePicker {...rest} onChange={this.onChange.bind(this)} error={!isValid || error} helperText={!isValid && this.getErrorMessage()} /> ); } } DateValidatorCore.propTypes = { // ValidatorComponent errorMessages: PropTypes.oneOfType([ PropTypes.array, PropTypes.string ]), validators: PropTypes.array, value: PropTypes.any, validatorListener: PropTypes.func, withRequiredValidator: PropTypes.bool, // Custom doInitialValidation: PropTypes.bool, }; DateValidatorCore.defaultProps = { // ValidatorComponent errorMessages: 'error', validators: [], validatorListener: () => {}, // Custom doInitialValidation: false }; export default DateValidatorCore;