react-form-validator-components
Version:
Components for react-form-validator-core
76 lines (64 loc) • 1.56 kB
JavaScript
import React from 'react';
import PropTypes from 'prop-types';
import TextField from '@material-ui/core/TextField';
import {ValidatorComponent} from 'react-form-validator-core';
class TextValidatorCore extends ValidatorComponent {
componentDidMount() {
super.componentDidMount();
if (this.props.doInitialValidation) {
this.validate(this.props.value, true);
}
}
onChange(event) {
if (this.props.onChange) {
this.props.onChange(event.target.value);
}
}
render() {
const {
error,
errorMessages,
validators,
requiredError,
helperText,
validatorListener,
withRequiredValidator,
// custom
onChange,
children,
doInitialValidation,
...rest
} = this.props;
const {isValid} = this.state;
return (
<TextField
{...rest}
onChange={this.onChange.bind(this)}
helperText={!isValid && this.getErrorMessage()}
error={!isValid || error}
/>
);
}
}
TextValidatorCore.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
};
TextValidatorCore.defaultProps = {
// ValidatorComponent
errorMessages: 'error',
validators: [],
validatorListener: () => {},
// Custom
doInitialValidation: false
};
export default TextValidatorCore;