1.0.17 • Published 8 months ago

react-tiny-hook-form v1.0.17

Weekly downloads
-
License
MIT
Repository
github
Last release
8 months ago

React Tiny Hook Form

Install

npm i react-tiny-hook-form

Quickstart

import { useCallback, useId } from 'react';
import { useForm, ValidityStatesEnum } from 'react-tiny-hook-form';

const INPUT_NAME = 'input';
const INPUT_PATTERN = '^[a-zA-Z\u00C0-\u024F]+$';
const INPUT_ERROR = ValidityStatesEnum.CUSTOM_ERROR;

function App() {
  const id = useId();
  const hintId = useId();

  const { errors, handleSubmit, validate, setError, clearErrors } = useForm({
    fields: [INPUT_NAME],
  });

  const onSetError = useCallback(() => {
    setError(INPUT_NAME, INPUT_ERROR);
  }, [errors, setError]);

  return (
    <form
      noValidate
      autoComplete="off"
      onSubmit={handleSubmit(console.debug)}
      onReset={() => clearErrors()}
    >
      <fieldset>
        <legend>Form</legend>

        <div>
          <label htmlFor={id}>
            {INPUT_NAME} <b>*</b>
          </label>

          <input
            name={INPUT_NAME}
            pattern={INPUT_PATTERN}
            id={id}
            required
            aria-describedby={hintId}
            onChange={validate}
          />

          <div id={hintId}>
            {errors[INPUT_NAME] && <p>{errors[INPUT_NAME]}</p>}
          </div>
        </div>
      </fieldset>

      <button type="submit">
        Submit
      </button>

      <button type="button" onClick={onSetError}>
        Set "{INPUT_ERROR}" error at "{INPUT_NAME}" field
      </button>

      <button type="button" onClick={() => clearErrors()}>
        Clear errors
      </button>

      <button type="reset">Reset</button>
    </form>
  );
}

export default App;
1.0.2

8 months ago

1.0.1

9 months ago

1.0.17

8 months ago

1.0.16

8 months ago

1.0.9

8 months ago

1.0.8

8 months ago

1.0.7

8 months ago

1.0.6

8 months ago

1.0.4

8 months ago

1.0.11

8 months ago

1.0.10

8 months ago

1.0.15

8 months ago

1.0.14

8 months ago

1.0.13

8 months ago

1.0.12

8 months ago

1.0.0

12 months ago