Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return the input state and form state using hooks #42

Open
tkrotoff opened this issue Jun 16, 2019 · 1 comment
Open

Return the input state and form state using hooks #42

tkrotoff opened this issue Jun 16, 2019 · 1 comment

Comments

@tkrotoff
Copy link
Owner

tkrotoff commented Jun 16, 2019

With v0.12, to stylize an input given its state (pending for example), you have to catch the events:

export class Input extends React.Component {
  [...]

  state: InputState = {
    field: undefined
  };

  componentWillMount() {
    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);
  }

  fieldWillValidate = (fieldName: string) => {
    // Ignore the event if it's not for us
    if (fieldName === this.props.name) {
      this.setState({ field: 'pending' });
    }
  };

  fieldDidValidate = (field: Field) => {
    // Ignore the event if it's not for us
    if (field.name === this.props.name) {
      this.setState({ field });
    }
  };

  fieldDidReset = (field: Field) => {
    // Ignore the event if it's not for us
    if (field.name === this.props.name) {
      this.setState({ field: undefined });
    }
  };

  [...]
}

There is probably a better story here thx to hooks.

const field = useField('fieldName');
return (
  <input className={field.state === 'PENDING' ? 'is-pending' : ''} />
);
enum FieldValidationState {
  Untouched,
  ValidationPending,
  ValidationPerformed
};

See https://www.google.com/search?q=field+validation+status

Work on field lifecycle: untouched => willValidate => didValidate (hasErrors, hasWarnings, hasInfos, isValid) => didReset

Related issues: #40

We should also do the same for the form itself:

const form = useForm(...);
if (form.state === 'VALID') ...
enum FormState {
  Untouched,
  ValidationPending,
  ValidationPerformed
};

Work on form lifecycle: untouched => willValidate => didValidate (hasErrors, hasWarnings, hasInfos, isValid) => didReset

Check for inspiration here: https://github.com/bluebill1049/react-hook-form

@tkrotoff tkrotoff changed the title Return the input state with hooks Return the input state and form state using hooks Jun 17, 2019
@tkrotoff
Copy link
Owner Author

tkrotoff commented Jun 21, 2019

Use case:

For a captcha with an image, an input and a button to check the entered matches the image.
The input is required.

  • We want to turn the input red with ✕ mark if not filled
  • We don't want the input to be green with a ✓ if filled because the user could understand the text he entered matches the image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant