UNPKG

react-form-with-constraints

Version:
50 lines (49 loc) 2.17 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.FieldFeedbackWhenValid = void 0; const React = require("react"); const prop_types_1 = require("prop-types"); const FieldFeedbacks_1 = require("./FieldFeedbacks"); const FormWithConstraints_1 = require("./FormWithConstraints"); class FieldFeedbackWhenValid extends React.Component { constructor() { super(...arguments); this.state = { fieldIsValid: undefined }; this.fieldWillValidate = (fieldName) => { if (fieldName === this.context.fieldFeedbacks.fieldName) { this.setState({ fieldIsValid: undefined }); } }; this.fieldDidValidate = (field) => { if (field.name === this.context.fieldFeedbacks.fieldName) { this.setState({ fieldIsValid: field.isValid() }); } }; this.fieldDidReset = (field) => { if (field.name === this.context.fieldFeedbacks.fieldName) { this.setState({ fieldIsValid: undefined }); } }; } componentDidMount() { this.context.form.addFieldWillValidateEventListener(this.fieldWillValidate); this.context.form.addFieldDidValidateEventListener(this.fieldDidValidate); this.context.form.addFieldDidResetEventListener(this.fieldDidReset); } componentWillUnmount() { this.context.form.removeFieldWillValidateEventListener(this.fieldWillValidate); this.context.form.removeFieldDidValidateEventListener(this.fieldDidValidate); this.context.form.removeFieldDidResetEventListener(this.fieldDidReset); } render() { const { style, ...otherProps } = this.props; return this.state.fieldIsValid ? (React.createElement("span", { ...otherProps, style: { display: 'block', ...style } })) : null; } } exports.FieldFeedbackWhenValid = FieldFeedbackWhenValid; FieldFeedbackWhenValid.contextTypes = { form: (0, prop_types_1.instanceOf)(FormWithConstraints_1.FormWithConstraints).isRequired, fieldFeedbacks: (0, prop_types_1.instanceOf)(FieldFeedbacks_1.FieldFeedbacks).isRequired };