High performance Form component with data scope management. Including data collection, verification, and styles.
Note that useForm is a React Hooks that only works in functional component.
- <Form.Item label="Field" name="field">
- <Input />
- </Form.Item>
+ <Form.Item label="Field">
+ <Form.Item name="field" noStyle><Input /></Form.Item> // that will bind input
+ <span>description</span>
+ </Form.Item>
Note, in this case, no more name property should be left in Form.Item with label.See Customized Form Controls demo below for further advanced usage.
[
{
"name": [
"username"
],
"value": "Store value"
}
]| Property | Description | Type | Default |
|---|---|---|---|
component | Set the Form rendering element. Do not create a DOM node for false | ComponentType | false | form |
colon | Configure the default value of colon for Form.Item. Indicates whether the colon after the label is displayed (only effective when prop layout is horizontal) | boolean | true |
fields | Control of form fields through state management (such as redux). Not recommended for non-strong demand. View example | - | |
form | Form control instance created by Form.useForm(). Automatically created when not provided | - | |
hideRequiredMark | Hide required mark for all form items | boolean | false |
initialValues | Set value by Form initialization or reset | object | - |
labelAlign | The text align of label of all items | left | right | right |
labelCol | Label layout, like <Col> component. Set span offset value like {span: 3, offset: 12} or sm: {span: 3, offset: 12} | - | |
layout | Form layout | horizontal | vertical | inline | horizontal |
name | Form name. Will be the prefix of Field id | string | - |
preserve | Keep field value even when field removed | boolean | true |
scrollToFirstError | Auto scroll to first failed field when submit | boolean | false |
size | Set field component size (antd components only) | small | middle | large | - |
validateMessages | Validation prompt template, description see below | - | |
validateTrigger | Config field validate trigger | string | string[] | onChange |
wrapperCol | The layout for input controls, same as labelCol | - | |
onFinish | Trigger after submitting the form and verifying data successfully | function(values) | - |
onFinishFailed | Trigger after submitting the form and verifying data failed | function({ values, errorFields, outOfDate }) | - |
onFieldsChange | Trigger when field updated | function(changedFields, allFields) | - |
onValuesChange | Trigger when value updated | function(changedValues, allValues) | - |
| Property | Description | Type | Default |
|---|---|---|---|
colon | Used with label, whether to display : after label text. | boolean | true |
dependencies | Set the dependency field. See below | NamePath[] | - |
extra | The extra prompt message. It is similar to help. Usage example: to display error message and prompt message at the same time | string | ReactNode | - |
getValueFromEvent | Specify how to get value from event or other onChange arguments | (..args: any[]) => any | - |
getValueProps | Additional props with sub component | (value: any) => any | - |
hasFeedback | Used with validateStatus, this option specifies the validation status icon. Recommended to be used only with Input | boolean | false |
help | The prompt message. If not provided, the prompt message will be generated by the validation rule. | string | ReactNode | - |
htmlFor | Set sub label htmlFor | string | - |
initialValue | Config sub default value. Form initialValues get higher priority when conflict | string | - |
noStyle | No style for true, used as a pure field control | boolean | false |
label | Label text | string | ReactNode | - |
labelAlign | The text align of label | left | right | right |
labelCol | The layout of label. You can set span offset to something like {span: 3, offset: 12} or sm: {span: 3, offset: 12} same as with <Col>. You can set labelCol on Form. If both exists, use Item first', | - | |
name | Field name, support array | - | |
normalize | Normalize value from component value before passing to Form instance | (value, prevValue, prevValues) => any | - |
preserve | Keep field value even when field removed | boolean | true |
required | Display required style. It will be generated by the validation rule | boolean | false |
rules | Rules for field validation. Click here to see an example | Rule[] | - |
shouldUpdate | Custom field update logic. See below | boolean | (prevValue, curValue) => boolean | false |
trigger | When to collect the value of children node | string | onChange |
validateFirst | Whether stop validate on first rule of error for this field | boolean | false |
validateStatus | The validation status. If not provided, it will be generated by validation rule. options: success warning error validating | string | - |
validateTrigger | When to validate the value of children node | string | string[] | onChange |
valuePropName | Props of children node, for example, the prop of Switch is 'checked'. This prop is an encapsulation of getValueProps, which will be invalid after customizing getValueProps | string | value |
wrapperCol | The layout for input controls, same as labelCol. You can set wrapperCol on Form. If both exists, use Item first | - | |
hidden | Whether to hide Form.Item (still collect and validate value) | boolean | false |
| Property | Description | Type | Default |
|---|---|---|---|
name | Field name, support array | - | |
children | Render function | (fields: Field[], operation: { add, remove, move }) => React.ReactNode | - |
| Property | Description | Type | Default |
|---|---|---|---|
onFormChange | Triggered when a sub form field updates | Function(formName: string, info: { changedFields, forms }) | - |
onFormFinish | Triggered when a sub form submits | Function(formName: string, info: { values, forms }) | - |
| Property | Description | Type | Default |
|---|---|---|---|
getFieldInstance | Get field instance | (name: NamePath) => any | |
getFieldValue | Get the value by the field name | (name: NamePath) => any | |
getFieldsValue | Get values by a set of field names. Return according to the corresponding structure | (nameList?: NamePath[], filterFunc?: (meta: { touched: boolean, validating: boolean }) => boolean) => any | |
getFieldError | Get the error messages by the field name | (name: NamePath) => string[] | |
getFieldsError | Get the error messages by the fields name. Return as an array | (nameList?: NamePath[]) => FieldError[] | |
isFieldTouched | Check if a field has been operated | (name: NamePath) => boolean | |
isFieldsTouched | Check if fields have been operated. Check if all fields is touched when allTouched is true | (nameList?: NamePath[], allTouched?: boolean) => boolean | |
isFieldValidating | Check fields if is in validating | (name: NamePath) => boolean | |
resetFields | Reset fields to initialValues | (fields?: NamePath[]) => void | |
scrollToField | Scroll to field position | (name: NamePath, options: [ScrollOptions]) => void | |
setFields | Set fields status | (fields: FieldData[]) => void | |
setFieldsValue | Set fields value | (values) => void | |
submit | Submit the form. It`s same as click submit button | () => void | |
validateFields | Validate fields | (nameList?: NamePath[]) => Promise |
| Property | Description | Type | Default |
|---|---|---|---|
touched | Whether is operated | boolean | |
validating | Whether is in validating | boolean | |
errors | Error messages | string[] | |
name | Field name path | NamePath[] | |
value | Field value | any |
| Property | Description | Type | Default |
|---|---|---|---|
enum | Match enum value | any[] | |
len | Length of string, number, array | number | |
max | type required: max length of string, number, array | number | |
message | Error message. Will auto generate by template if not provided | string | |
min | type required: min length of string, number, array | number | |
pattern | Regex pattern | RegExp | |
required | Required field | boolean | |
transform | Transform value to the rule before validation | (value) => any | |
type | Normally string |number |boolean |url | email. More type to ref here | string | |
validator | Customize validation rule. Accept Promise as return. See example | (rule, value) => Promise | |
whitespace | Failed if only has whitespace | boolean | |
validateTrigger | Set validate trigger event. Must be the sub set of validateTrigger in Form.Item | string | string[] |