UNPKG

2.17 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.FieldFeedbackWhenValid = void 0;
4const React = require("react");
5const prop_types_1 = require("prop-types");
6const FieldFeedbacks_1 = require("./FieldFeedbacks");
7const FormWithConstraints_1 = require("./FormWithConstraints");
8class FieldFeedbackWhenValid extends React.Component {
9 constructor() {
10 super(...arguments);
11 this.state = {
12 fieldIsValid: undefined
13 };
14 this.fieldWillValidate = (fieldName) => {
15 if (fieldName === this.context.fieldFeedbacks.fieldName) {
16 this.setState({ fieldIsValid: undefined });
17 }
18 };
19 this.fieldDidValidate = (field) => {
20 if (field.name === this.context.fieldFeedbacks.fieldName) {
21 this.setState({ fieldIsValid: field.isValid() });
22 }
23 };
24 this.fieldDidReset = (field) => {
25 if (field.name === this.context.fieldFeedbacks.fieldName) {
26 this.setState({ fieldIsValid: undefined });
27 }
28 };
29 }
30 componentDidMount() {
31 this.context.form.addFieldWillValidateEventListener(this.fieldWillValidate);
32 this.context.form.addFieldDidValidateEventListener(this.fieldDidValidate);
33 this.context.form.addFieldDidResetEventListener(this.fieldDidReset);
34 }
35 componentWillUnmount() {
36 this.context.form.removeFieldWillValidateEventListener(this.fieldWillValidate);
37 this.context.form.removeFieldDidValidateEventListener(this.fieldDidValidate);
38 this.context.form.removeFieldDidResetEventListener(this.fieldDidReset);
39 }
40 render() {
41 const { style, ...otherProps } = this.props;
42 return this.state.fieldIsValid ? (React.createElement("span", { ...otherProps, style: { display: 'block', ...style } })) : null;
43 }
44}
45exports.FieldFeedbackWhenValid = FieldFeedbackWhenValid;
46FieldFeedbackWhenValid.contextTypes = {
47 form: (0, prop_types_1.instanceOf)(FormWithConstraints_1.FormWithConstraints).isRequired,
48 fieldFeedbacks: (0, prop_types_1.instanceOf)(FieldFeedbacks_1.FieldFeedbacks).isRequired
49};