2020.1.3 • Published 4 years ago

@4react/forms v2020.1.3

Weekly downloads
-
License
ISC
Repository
github
Last release
4 years ago

@4react / forms

Ready-to-use form context and validation for React Applications.

Usage

Import dependency

npm i @4react/forms

Compose fields

import { Form, FormField, FormData } from '@4react/forms'

const App = () => (
  <Form>
    <FormField name="username" accept={/^\w+$/}>
      {({ update, valid }) => <input ... />}
    </FormField>
    <FormField name="password">
      {({ update, valid }) => <input ... />}
    </FormField>
    <FormData>
      {({ values, valid }) => <button ... />}
    </FormData>
  </Form>
)

Live example

API

Form

Component that provides a context for a form.

import { Form } from '@4react/forms'

const App = () => (
  <Form>
    ...
  </Form>
)
NameTypeDefaultDescription
initialValuesobject-optional - Initial values for the form fields.

FormField

Component that registers a new field for the surrounding form.

import { FormField } from '@4react/forms'

...

<FormField name="email">
  {({ update, valid }) => (
    <input
      type="text"
      onChange={e => update(e.target.value)}
      style={{ borderColor: valid ? 'gray' : 'red' }}
    />
  )}
</FormField>
NameTypeDefaultDescription
namestring-Name of the field
defaultValueany-optional - Default value of the field.
acceptFunction, RegExp-optional - If provided, this function is used to validate the value of the field. For string fields (and compatible types fields) a RegExp can be use also.
validateOn"init", "update""update"optional - Use to control which moments are take into consideration for validation. A list of values is also accepted.

This components accept a child function, and provides to it an object with the following properties:

NameTypeDescription
updateFunctionRequired to update the value of the field.
validbooleanPoints if last validation is passed or none is executed yet.
valueanyThe stored value of the field (for controlled inputs).

FormData

Component that retrieves all values of the current form.

import { FormData } from '@4react/forms'

...
    
<FormData>
  {({ values, valid }) => (
    <button
      disabled={!valid}
      onClick={() => sendData(values)}
    >
      SEND DATA
    </button>
  )}
</FormData>
NameTypeDefaultDescription
validatebooleantrueoptional - Used for performance issues, setting this property to false will disable automatic validation. Manual validation can be triggered by calling the validate method passed to children.

This components accept a child function, and provides to it the following properties:

NameTypeDescription
valuesobjectObject containing all values of the form.
validbooleanPoints if the values in all the fields are considered valid. Returns false if no validation is done yet.
validateFunctionFor performance reasons it's possible to force validation only on demand. In these cases invoking this function will validate the entire form.

Hooks

Alternatively to the use of the above components, it's possible to create custom fields and data consumers, using the following hooks:

useFormField

This hook will register a new field with the specified name, and returns the same set of properties of the FormField component (see FormField).

import { useFormField } form '@4react/forms'

const MyCustomField = ({ name }) => {
  const { update, valid } = useFormField(name)

  return <input ... />
}
NameTypeDefaultDescription
namestring-Name of the field.
optionsobject{}optional - Object accepting all optional props of the FormField component (see FormField).

useFormData

This hook retrieves collected values of the current form, and returns the same set of properties of the FormData component (see FormData).

import { useFormData } from '@4react/forms'

const MyCustomSubmit = () => {
  const { values, valid } = useFormData()

  return <button ... />
}
NameTypeDefaultDescription
optionsobject{}optional - Object accepting all optional props of the FormData component (see FormData).