@4react/forms 中文文档教程

发布于 5年前 浏览 26 项目主页 更新于 3年前

@4react / forms

React 应用程序的即用型表单上下文和验证。

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>
)

实例

API

Form

为表单提供上下文的组件。

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

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

FormField

为周围表单注册新字段的组件。

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.

该组件接受子函数,并向其提供具有以下属性的对象:

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

检索当前表单的所有值的组件。

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.

该组件接受一个子函数,并为其提供以下属性:

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

除了使用上述组件之外,还可以使用以下挂钩创建自定义字段和数据使用者:

useFormField

该挂钩将注册一个具有指定名称的新字段,并返回 FormField 组件的同一组属性(请参阅 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

此挂钩检索当前表单的收集值,并返回 FormData 组件的同一组属性(请参阅 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).

@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).
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文