usetheform
Edit page
Introduction
Components
CollectionPropsBasic usageArray CollectionReducersValidation - SyncValidation - AsyncFormFormContextInputPersistStateOnUnmountSelectTextArea
Hooks
Material UI - React Select

Collection

Creates a nested piece of state within a Form.
A Collection can be of type: object or array.

Props

object: boolean

  • Creates a collection of type object if "true".

array: boolean

  • Creates a collection of type array if "true".

name: string

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

index: string

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

touched: boolean

  • Default value is false.

    • true: sync validation messages will be showing only when the event onBlur of any collection's field is triggered by the user action at any level of nesting. The async validation messages will be showing only at form submission.
    • false: validation messages (sync and async) will be showing only at form submission.
const [status, validation] = useValidation([anyValidationFunc])
<Collection touched object name="myObject" {...validation}>
<Input type="text" name="name" value="BeBo" />
</Collection>
{status.error && <label>{status.error}</label>}

value: array | object

  • Specifies the initial value of a Collection.

reducers: array | function

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

Basic usage

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

    Nested Collections

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

      Array Collection

      Array Collection of Input fields with indexes handled automatically.

      import React from "react";
      import { Form, Input, Collection } from "usetheform";
      • {} 0 keys

        Array Collection of Input fields with indexes handled automatically for custom Inputs.
        import React from "react";
        import { withIndex, useField, Collection } from "usetheform";
        const CustomInput = withIndex(({ type, name, value, index, ...restAttr }) => {
        const props = useField({ type, name, value, index });
        return <input {...restAttr} {...props}></input>;
        });
        • {} 0 keys

          Array Collection of Input fields with indexes handled maunally.
          import { Form, Input, Collection } from 'usetheform'
          • {} 0 keys

            Reducers

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

              Validation - Sync

              Validation at Collection level starts only on form submission if the prop touched is false.

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

                Validation - Async

                Async Validation for Collections is triggered on Submit event. The form submission is prevented if the validation fails. This means that the onSubmit function passed as prop to the Form component will not be invoked.

                import { useAsyncValidation, useForm } from 'usetheform'
                const Submit = () => {
                const { isValid } = useForm();
                return (
                <button disabled={!isValid} type="submit">
                Submit
                </button>
                );
                };
                • {} 0 keys