usetheform
Edit page
Introduction
Components
CollectionFormFormContextInputPropsBasic usageReducersValidation - SyncValidation - AsyncPersistStateOnUnmountSelectTextArea
Hooks
Material UI - React Select

Input

Renders all the inputs of the types listed at: W3schools Input Types and accepts as props any html attribute listed at: Html Input Attributes.

Props

type: string

  • Type listed at: W3schools Input Types.

name: string

  • A field's name in Usetheform state.
  • If the Input is rendered within a <Collection array />, name is not allowed as prop.

index: string

  • A field's index in an array Collection.
  • index is only allowed if your Input is rendered within a <Collection array /> .

value: string | number

  • Specifies the initial value of an input element.

checked: boolean

  • Specifies whether an input element should be pre-selected or not (for type="checkbox" or type="radio").

touched: boolean

  • A field that has been touched/visited. Default value of false.
  • If true, validation messages (sync and async) will be shown but only when the event onBlur of the field is triggered by a user action.

reducers: array | function

(nextValue, prevValue, formState) => nextValue
  • An array whose values correspond to different reducing functions.
  • Reducer functions specify how the Input's value changes.

innerRef: object (a mutable ref object)

  • When you need to access the underlying DOM node created by an Input (e.g. to call focus), you can use a ref to store a reference to the input dom node.
const ref = useRef(null)
<Input innerRef={ref} type="text" name="test" />

Basic usage

import { Form, Input, Collection } from 'usetheform'
  • {} 0 keys

    Reducers

    import { Form, Input } from 'usetheform'
    • {} 0 keys

      Validation - Sync

      import { useValidation } from 'usetheform'
      • {} 0 keys

        Validation - Async

        import { Form, Input } from 'usetheform';

        • {} 0 keys

          Detailed Explanation:

          import { useForm } from 'usetheform'
          export const asyncTestInput = value =>
          new Promise((resolve, reject) => {
          // it could be an API call or any async operation
          setTimeout(() => {
          if (value === "foo") {
          reject("username already in use");
          } else {
          resolve("Success");
          }
          }, 1000);
          });
          export const Submit = () => {
          const { isValid } = useForm();
          return (
          <button disabled={!isValid} type="submit">
          Submit
          </button>
          );
          };