---
localeCode: en-US
order: 24
category: Input
title: Form
subTitle: Form
icon: doc-form
dir: column
---
## Form
- **Rerender on demand**, avoids unnecessary full-volume rendering, higher performance
- Easy to use, **simple structure**, avoids unnecessary hierarchical nesting
- Perfect accessibility support
- FormState / FieldState can also be easily obtained from outside the Form
Provides an external method to operate inside the form: formApi / fieldApi
- Support for encapsulating custom components into form controls, and you can quickly access your team's components through the extension mechanism provided by Form (through `withField` HOC)
- Support Form level / Field level assignment, verification (synchronous / asynchronous)
## Field
Semi encapsulates all form field component (Input、Select、Checkbox、DatePicker etc.) with `withField` once.
Taking over their data flow (`props.value` & `props.onChange`)
When in use, you need to import from the Form (note: only the control imported from the Form has data synchronization)
### Supported Field Component
- `Input`, `InputNumber`, `TextArea`, `Select`, `Checkbox`, `Radio`, `RadioGroup`, `Switch`, `DatePicker`, `TimePicker`, `Slider`, `InputGroup`, `TreeSelect`, `Cascader`, `Rating`, `AutoComplete`, `Upload`, `Label`, `ErrorMessage`, `Section`、`TagInput`
All mounted under Form and declared directly in `
` and `` when used.
```javascript
import { Form } from '@douyinfe/semi-ui';
const FormInput = Form.Input;
const FormSelect = Form.Select;
const Option = FormSelect.Option;
```
The Field level component provided by Form, its `value` (or other properties specified by `valueKey`), onChange (or other callback functions specified by `onKeyChangeFnName`)
Properties are hijacked by Form, so
1. No longer need to manually bind the onChange event and update the value as controled component. But you can continue to listen onChange events for the latest values if you want
2. You cannot set the state of component with attributes such as `value`, `defaultValue`, `checked`, `defaultChecked`, etc. The default value can be set by Field's `initValue` or Form's `unitValues`
3. You should not modify the value of Form State directly, all changes to the data in the Form should be done by providing `formApi`, `fieldApi`
## Demos
### Various ways to declare form
Semi Form supports multiple writing at the same time.
#### Basic Usage
Add `field` property to each field component.
You can also set `label` properties for each field, by default is the same as field
`label` can be passed in a string directly, or declared in the form of an object, configure `extra`, `required`, `optional` and other attributes to deal with more complex scenarios
The field attribute is required props
```jsx live=true dir="column"
import React from 'react';
import { Form, Tooltip } from '@douyinfe/semi-ui';
import { IconHelpCircle } from '@douyinfe/semi-icons';
() => (
}}
style={{ width: 176 }}
/>
);
```
#### Other declaration methods
When you need to get `formState`, `formApi`, `values`, etc. directly inside the Form structure, you can use the following writing
#### Via render props
```jsx live=true dir="column"
import React from 'react';
import { Form } from '@douyinfe/semi-ui';
() => (
AdminUserGuest{JSON.stringify(formState)}
>
)} layout='horizontal'>
);
```
#### Via children function
declare children as a function that returns all field components
```jsx live=true dir="column"
import React from 'react';
import { Form } from '@douyinfe/semi-ui';
() => (
AdminUserGuest{JSON.stringify(formState)}
>
)
}
);
```
#### Via props.component
Pass the entire internal structure directly in the form through `component` attribute.
```jsx live=true dir="column"
import React from 'react';
import { Form } from '@douyinfe/semi-ui';
class Demo extends React.Component {
constructor() { super(); }
render() {
const fields = ({ formState, formApi, values }) => (
<>
{JSON.stringify(formState)}
>
);
return ;
}
}
```
### All supported field components
```jsx live=true dir="column"
import React from 'react';
import { Form, Col, Row, Button } from '@douyinfe/semi-ui';
import { IconUpload } from '@douyinfe/semi-icons';
class BasicDemoWithInit extends React.Component {
constructor() {
super();
this.state = {
initValues: {
name: 'semi',
business: ['ulikeCam'],
role: 'ued',
switch: true,
files: [
{
uid: '1',
name: 'vigo.png',
status: 'success',
size: '130KB',
preview: true,
url: 'https://lf3-static.bytednsdoc.com/obj/eden-cn/ptlz_zlp/ljhwZthlaukjlkulzlp/root-web-sites/vigo.png'
},
{
uid: '2',
name: 'resso.jpeg',
status: 'validateFail',
size: '222KB',
percent: 50,
preview: true,
fileInstance: new File([new ArrayBuffer(2048)], 'resso.jpeg', { type: 'image/jpeg' }),
url: 'https://lf3-static.bytednsdoc.com/obj/eden-cn/ptlz_zlp/ljhwZthlaukjlkulzlp/root-web-sites/Resso.png'
},
{
uid: '3',
name: 'dy.jpeg',
status: 'uploading',
size: '222KB',
percent: 50,
preview: true,
fileInstance: new File([new ArrayBuffer(2048)], 'dy.jpeg', { type: 'image/jpeg' }),
url: 'https://lf3-static.bytednsdoc.com/obj/eden-cn/ptlz_zlp/ljhwZthlaukjlkulzlp/root-web-sites/dy.png'
}
]
}
};
}
render() {
const { Input, InputNumber, AutoComplete, Select, TreeSelect, Cascader, DatePicker, TimePicker, TextArea, CheckboxGroup, Checkbox, RadioGroup, Radio, Slider, Rating, Switch, TagInput, Section } = Form;
const { initValues } = this.state;
const plainOptions = ['A', 'B', 'C'];
const style = { width: '90%' };
const treeData = [
{
label: 'Asia',
value: 'Asia',
key: '0',
children: [
{
label: 'China',
value: 'China',
key: '0-0',
children: [
{
label: 'Beijing',
value: 'Beijing',
key: '0-0-0',
},
{
label: 'Shanghai',
value: 'Shanghai',
key: '0-0-1',
},
],
},
],
},
{
label: 'North America',
value: 'North America',
key: '1',
}
];
return (
);
}
}
```
### Field binding syntax
Every Field component must have a `field` property. This is how the form manages the state of this field.
See the field syntax section below for additional details on what you can pass in for field.
The field can be a simple string, can be contained`.`Or`[]`String that supports multi-level nesting
Below is an example of the field name and their mapping path in FormState
| Field | Resolution |
| ---------------------- | ---------------------------------- |
| username | formState.values.username |
| user\[0\] | formState.values.user\[0\] |
| siblings.1 | formState.values.siblings\[1\] |
| siblings\['2'\] | formState.values.siblings\[2\] |
| parents\[0\].name | formState.values.parents\[0\].name |
| parents\[1\]\['name'\] | formState.values.parents\[1\].name |
```jsx live=true dir="column"
import React from 'react';
import { Form, Row, Col, Toast, TextArea } from '@douyinfe/semi-ui';
() => (
);
```
### Form layout
- Vertical Layout: Arrange each field vertically (By default)
Semi Design recommends a vertical layout.
```jsx live=true dir="column"
import React from 'react';
import { Form, Button, Toast } from '@douyinfe/semi-ui';
() => {
const handleSubmit = (values) => {
console.log(values);
Toast.info('Submit Success');
};
return (
I have read and agree to the terms of service
Or
>
)}
);
};
```
- Horizontal Layout: Arrange each field horizontally
You can use the horizontal layout by setting `layout='horizontal'`
```jsx live=true dir="column"
import React from 'react';
import { Form } from '@douyinfe/semi-ui';
() => (
);
```
- Label Position, Label Align
You can control the position of the label in the Field and the direction of text alignment by setting `labelPosition`, `labelAlign`
```jsx live=true dir="column"
import React from 'react';
import { Form, Select, Checkbox, Radio } from '@douyinfe/semi-ui';
class BasicDemo extends React.Component {
constructor(props) {
super(props);
this.state = {
labelPosition: 'left',
labelAlign: 'left',
};
}
render() {
const { labelPosition, labelAlign } = this.state;
const labelWidth = 120;
return (
<>
value === 'semi', message: 'not semi' }
]}
/>
mikejanekateadminuserguestrootmanwoman
>
);
}
}
```
- A more complex layout.
You can also combine the `Row` and `Col` provided by the `Grid` to arrange the form structure as you want.
```jsx live=true dir="column"
import React from 'react';
import { Form, Row, Col } from '@douyinfe/semi-ui';
() => (
);
```
### wrapper Col / label Col
When you need to set a uniform layout for all Fields in a Form, you can set `wrapperCol` and `labelCol` on the `Form` to quickly generate the layout. No need to manually use `Row`, `Col` manual layout.
`wrapperCol`,`labelCol`Property Configuration Reference [Col components](/en-US/basic/grid#Col)
```jsx live=true dir="column"
import React from 'react';
import { Form } from '@douyinfe/semi-ui';
() => (
Quality AssuranceSoftware EngineerProduct ManagerDesigner
);
```
### Remove automatically added Label
Form will automatically insert `Label` for Field control. If you don't need to automatically insert the Label module, you can turn off the automatic label insertion function by setting `noLabel=true` in the Field (at this time, the Field still has the ability to automatically display ErrorMessage, so the DOM structure is still different from the original component)
If you want to keep the DOM structure consistent with the original component, you can use `pure=true`. At this time, the DOM structure will not change except that the data flow is taken over (you need to be responsible for the rendering of ErrorMessage, and it cannot be used by formProps.wrapperCol property impact)
```jsx live=true dir="column"
import React from 'react';
import { Form } from '@douyinfe/semi-ui';
() => (
val !== 'semi' ? 'not semi' : '' }
placeholder='Type your name'
/>
);
```
### Embedded Label
A Label can be inlined in a field control by setting labelPosition to `inset`. Components currently supporting this feature include `Input`, `InputNumber`, `DatePicker`, `TimePicker`, `Select`, `TreeSelect`, `Cascader`, `TagInput`
```jsx live=true dir="column"
import React from 'react';
import { Form } from '@douyinfe/semi-ui';
() => (
operaterdpmued
);
```
### Export Label, ErrorMessage use
When the built-in Label and ErrorMessage layout does not meet the business requirements, you need to combine the positions yourself, but you want to use the default styles of Label and ErrorMessage directly.
you can import them from the `Form` module, and combine `Form.Label` / `Form.ErrorMessage` by yourself.
For details of their API, refer to [Label](#Form.Label) / [ErrorMessage](#Form.ErrorMessage)
```jsx
import { Form } from '@douyinfe/semi-ui';
const { Label, ErrorMessage } = Form;
```
### Use Form.Slot
When your custom component needs to maintain the same layout style as the Field component, you can place your custom component in `Form.Slot`
`labelWidth`, `labelAlign`, `wrapperCol`, `labelCol` set on the Form component automatically acts on `Form.Slot`
For the Slot property configuration, refer to [Form.Slot](#Form.Slot)
```jsx live=true dir="column"
import React from 'react';
import { Form } from '@douyinfe/semi-ui';
class AssistComponent extends React.Component {
render() {
return (
{`I'm Semi Form SlotA, a custom ReactNode`}
{`I'm Semi Form SlotA, i have different labelWidth and textAlign.`}
);}
}
```
### Use helpText、extraText set prompt information
You can place custom prompt information through `helpText`, and display it in the same block as the verification information (error). When both have values, the verification information will be displayed first.
Additional prompt information can be placed through `extraText`. When the error message and prompt text need to appear at the same time, this configuration can be used. It is always displayed and located after helpText/error
When `validateStatus` is passed in, the UI style corresponding to the value of validateStatus will be displayed first. If not passed in, the internal verification status of the field shall prevail.
```jsx live=true dir="column"
import React from 'react';
import { Form } from '@douyinfe/semi-ui';
() => {
const [helpText, setHelpText] = useState('');
const [validateStatus, setValidateStatus] = useState('default');
const formRef = useRef();
const validate = (val, values) => {
if (!val) {
setValidateStatus('error');
return Password can not be blank;
} else if (val && val.length <= 3) {
setValidateStatus('warning');
setHelpText(Password Strength: Weak); // show helpText
return ''; // validate pass
} else {
setHelpText('');
setValidateStatus('success');
return '';
}
};
const random = () => {
let pw = (Math.random() * 100000).toString().slice(0, 5);
formRef.current.formApi.setValue('Password', pw);
formRef.current.formApi.setError('Password', '');
setHelpText('');
setValidateStatus('success');
};
return (
Don't have a suitable password? Click to generate a random
}
>
);
};
```
By configuring `extraTextPosition`, you can control the display position of extraText. Optional values `bottom`, `middle`
For example, when you want to display the extraText prompt information between the Label and Field component.
This attribute can be configured uniformly on the Form or individually on each Field. When passing in at the same time, the configuration of the Field shall prevail.
```jsx live=true dir="column"
import React from 'react';
import { Form } from '@douyinfe/semi-ui';
() => {
const options = [
{ label: 'Lark Notification', value: 'lark' },
{ label: 'Email Notification', value: 'email' },
{ label: 'Banner Notification', value: 'notification' }
];
const notifyText = "When unchecked, the default is a red dot reminder, and the message enters the recipient's message list by default. For important notifications, you can check the corresponding notification methods at the same time";
const forceText = "For dialog notifications, you can specify that the message must wait for a specified amount of time before it can be marked as read.";
return (
);
};
```
### Using Input Group
When you need to combine some fields to use, you can use `Form.InputGroup` to wrap them.
In Semi Form, when you using field components like `Form.Input`、`Form.Select`, Form will insert Label module automatically for them.
But usually, in`InputGroup` you only need a Label belonging to the entire Group.
You can set the label property in the `InputGroup` to insert a Label belonging to the Group
`label` configurable properties, see [Label](#Form.Label)
```jsx live=true dir="column"
import React from 'react';
import { Form, Button } from '@douyinfe/semi-ui';
() => (
PhoneNumber), required: true }} labelPosition='top'>
USA +1China +86Japan+81
);
```
### Form in the Modal pop-up layer
You can place the Form in Modal and load it as a popup.
When submitting, use `formApi.validate()` to centrally verify the Field
```jsx live=true dir="column"
import React from 'react';
import { Form, Modal, Select, Button, Row, Col } from '@douyinfe/semi-ui';
class ModalFormDemo extends React.Component {
constructor(props) {
super(props);
this.state = {
visible: false,
};
this.showDialog = this.showDialog.bind(this);
this.handleOk = this.handleOk.bind(this);
this.handleCancel = this.handleCancel.bind(this);
this.getFormApi = this.getFormApi.bind(this);
}
showDialog() {
this.setState({ visible: true });
}
handleOk() {
this.formApi.validate()
.then((values) => {
console.log(values);
})
.catch((errors) => {
console.log(errors);
});
}
handleCancel() {
this.setState({ visible: false });
}
getFormApi(formApi) {
this.formApi = formApi;
}
render(){
const { visible } = this.state;
let message = 'Required';
return (
<>
>
);
}
}
```
### Configure initial values and verification rules
- You can configure check rules for each Field through `rules`
The verification library inside the Form is based on `async-validator`, and more configuration rules can be found in its [official documentation](https://github.com/yiminghe/async-validator)
- You can uniformly set the initial value for the entire form through the `initValues` of form, or you can set the initial value through `initValue` in each field (the latter has a higher priority)
```jsx live=true dir="column"
import React from 'react';
import { Form } from '@douyinfe/semi-ui';
class BasicDemoWithInit extends React.Component {
constructor() {
super();
this.state = {
initValues: {
name: 'semi',
role: 'rd'
}
};
this.getFormApi = this.getFormApi.bind(this);
}
getFormApi(formApi) { this.formApi = formApi; }
render() {
const { Select, Input } = Form;
const style = { width: '100%' };
return (
);
}
}
```
### Custom Validate (Form Level)
You can set a custom validation function validateFields for the `form` as a whole, which will be called when submit
#### Synchronous Validate
When validate success, you should return an empty string.
When validate fails, you should return the error message (Object, key is fieldName, value is the corresponding error message)
```jsx live=true dir="column"
import React from 'react';
import { Form, Button } from '@douyinfe/semi-ui';
class FormLevelValidateSync extends React.Component {
constructor() {
super();
this.syncValidate = this.syncValidate.bind(this);
}
syncValidate(values) {
const errors = {};
if (values.name !== 'mike') {
errors.name = 'you must name mike';
}
if (values.sex !== 'female') {
errors.sex = 'must be woman';
}
errors.familyName = [
{ before: 'before errror balabala ', after: 'after error balabala' },
'familyName[1] error balabala'
];
return errors;
}
render() {
return (
);
}
}
```
#### Asynchronous Validate
For asynchronous validation, you should return a promise. In promise.then() you need to return the corresponding error message.
```jsx live=true dir="column"
import React from 'react';
import { Form, Button } from '@douyinfe/semi-ui';
class FormLevelValidateAsync extends React.Component {
constructor() {
super();
this.asyncValidate = this.asyncValidate.bind(this);
}
asyncValidate(values) {
const sleep = ms => new Promise(resolve => setTimeout(resolve, ms));
return sleep(2000).then(() => {
let errors = {};
if (values.name !== 'mike') {
errors.name = 'you must name mike';
}
if (values.sex !== 'female') {
errors.sex = 'sex not valid';
}
return errors;
});
}
render() {
return (
);
}
}
```
### Custom Validate (Field Level)
You can specify a custom validation function for field. Supports synchronous and asynchronous validation (by returning promises)
```jsx live=true dir="column"
import React from 'react';
import { Form, Button } from '@douyinfe/semi-ui';
class FieldLevelValidateDemo extends React.Component {
constructor() {
super();
this.validateName = this.validateName.bind(this);
this.asyncValidate = this.asyncValidate.bind(this);
}
validateName(val) {
if (!val) {
return '【sync】can\'t be empty';
} else if (val.length <= 5) {
return '【sync】must more than 5';
}
return '';
}
asyncValidate(val, values) {
const sleep = ms => new Promise(resolve => setTimeout(resolve, ms));
return sleep(2000).then(() => {
if (!val) {
return '【async】can\'t be empty';
} else if (val.length <= 5) {
return '【async】must more than 5';
} else {
return '';
}
});
}
render() {
return (
);
}
}
```
### Manually Trigger specified validation
When you want to manually trigger the validation of some specific Field, you can do it through `formApi.validate`.
When no parameters are passed in, all Fields are checked by default. When parameters are passed in, the parameters specified shall prevail
```jsx live=true dir="column"
import React from 'react';
import { Form, Button, Space } from '@douyinfe/semi-ui';
class PartValidAndResetDemo extends React.Component {
constructor() {
super();
this.validate = this.validate.bind(this);
this.getFormApi = this.getFormApi.bind(this);
this.validatePartial = this.validatePartial.bind(this);
this.resetPartial = this.resetPartial.bind(this);
}
getFormApi(formApi) {
this.formApi = formApi;
}
validate(val) {
if (!val) {
return 'can\'t be empty';
} else if (val.length <= 5) {
return (i am incoming reactNode);
}
return;
}
validatePartial(type) {
let scope = this.formApi.getValue('validateScope');
!scope ? scope = [] : null;
type === 'all' ? scope = ['a', 'b', 'c', 'd', 'b.name'] : null;
this.formApi.validate(scope)
.then(values => {
console.log(values);
Toast.success('pass');
}).catch(error => {
Toast.error('error');
console.log(error);
});
}
resetPartial() {
let scope = this.formApi.getValue('resetScope');
this.formApi.reset(scope);
}
render() {
let options = ['a', 'b', 'c', 'd', 'b.name'].map(item => ({ label: item, value: item }));
return (
);
}
}
```
### Linkage Fields
You can achieve the linkage between Fields by listening to the `onChange` of Field and then using formApi to make modifications.
```jsx live=true dir="column"
import React from 'react';
import { Form, Button, Row } from '@douyinfe/semi-ui';
class LinkFieldForm extends React.Component {
constructor() {
super();
this.getFormApi = this.getFormApi.bind(this);
this.handleSelectChange = this.handleSelectChange.bind(this);
}
handleSelectChange(value) {
let text = value === 'male' ? 'Hi male' : 'Hi female!';
this.formApi.setValue('Note', text);
}
getFormApi(formApi) { this.formApi = formApi; }
render() {
return (
femalemale
);
}
}
```
### Dynamic form
#### Dynamically add and delete fields
```jsx live=true dir="column"
import React from 'react';
import { Form, Button } from '@douyinfe/semi-ui';
() => (
);
```
#### Add or delete form items dynamically - by use ArrayField
For array items that are dynamically added or deleted, we provide the `ArrayField` component to simplify the operation of add / remove
For the detailed API of ArrayField, please refer to [ArrayField Props](#arrayfield-props) below
Note: The initValue type of ArrayField must be an array
```jsx live=true dir="column"
import React from 'react';
import { ArrayField, TextArea, Form, Button, useFormState } from '@douyinfe/semi-ui';
import { IconPlusCircle, IconMinusCircle } from '@douyinfe/semi-icons';
class ArrayFieldDemo extends React.Component {
constructor() {
super();
this.state = {
data: [
{ name: 'Semi D2C', role: 'Engineer' },
{ name: 'Semi C2D', role: 'Designer' },
]
};
}
render() {
let { data } = this.state;
const ComponentUsingFormState = () => {
const formState = useFormState();
return (
);
};
return (
);
}
}
```
#### Add or delete form items dynamically - by use formApi
If you don't use ArrayField, you can use the provided formApi to manually add or delete formState.
```jsx live=true dir="column"
import React from 'react';
import { Form, Button, TextArea } from '@douyinfe/semi-ui';
class ArrayDemo extends React.Component {
constructor() {
super();
this.state = {
initValues: {
effects: [
{ name: 'Face stickers', type: '2D', key: 1 },
{ name: 'Background sticker', type: '3D', key: 2 },
]
}
};
this.id = 3;
this.getFormApi = this.getFormApi.bind(this);
this.add = this.add.bind(this);
this.remove = this.remove.bind(this);
this.renderItems = this.renderItems.bind(this);
}
getFormApi(formApi) {
this.formApi = formApi;
}
add(obj) {
let effects = this.formApi.getValue('effects');
if (!effects) {
effects = [];
}
effects.push({ name: '', type: '', key: this.id++ });
this.formApi.setValue('effects', effects);
}
remove(key) {
let effects = this.formApi.getValue('effects');
effects = effects.filter((effect, index) => key !== effect.key);
if (!effects.length) {
effects = undefined;
}
this.formApi.setValue('effects', effects);
}
renderItems(formState, values) {
return values.effects && values.effects.map((effect, i) => (
2D3D
));
}
render() {
let { initValues } = this.state;
return (
);
}
}
```
### Use of Hook
We provide four Hooks so that you can easily access Form internal state and call Form and Field related api in Functional Component which placed inside the Form structure without passing through props.
```jsx
import { useFormApi, useFormState, useFieldApi, useFieldState } from '@douyinfe/semi-ui';
```
#### useFormApi
`useFormApi` allows you to directly access the formApi of the parent Form component within Functional Component via hook
```jsx live=true dir="column" noInline=true
import React from 'react';
import { useFormApi, Form, Button } from '@douyinfe/semi-ui';
const ComponentUsingFormApi = () => {
const formApi = useFormApi();
const change = () => {
formApi.setValue('name', Math.random());
};
return (
);
};
class UseFromApiDemo extends React.Component {
render() {
return (
);
}
}
render(UseFromApiDemo);
```
#### useFormState
`useFormState` allows you to directly access the form State of the parent Form component within Functional Component via hook
```jsx live=true dir="column" noInline=true
import React from 'react';
import { useFormState, Form } from '@douyinfe/semi-ui';
const ComponentUsingFormState = () => {
const formState = useFormState();
return (
);
}
}
render(UseFieldStateDemo);
```
### Use of HOC
We provided two HOC: `withFormApi`、`withFormState`, you can access the API of the Form and the internal state within other components
Provided HOC: `withField`, to encapsulating custom components as Field that conform the Semi Form data flow.
```jsx
import { withFormApi, withFormState, withField } from '@douyinfe/semi-ui';
```
#### HOC - withFormApi
You can encapsulate the component via `withFormApi` HOC so that the formApi of the parent Form component can be called directly inside the component
Note that the encapsulated components must be placed inside the Form structure.
```jsx live=true dir="column" noInline=true
import React from 'react';
import { withFormApi, Form, Button } from '@douyinfe/semi-ui';
const SomeComponetInsideForm = props => (
);
const ComponentWithFormApi = withFormApi(SomeComponetInsideForm);
class WithFormApiDemo extends React.Component {
render() {
return (
);
}
}
render(WithFormApiDemo);
```
#### HOC - withFormState
You can encapsulate the component via `withFormState` HOC so that the component has direct access to the Form State of the parent Form component.
Note that the encapsulated components must be placed inside the Form structure.
```jsx live=true dir="column" noInline=true
import React from 'react';
import { withFormState, Form } from '@douyinfe/semi-ui';
const SomeComponentInsideForm = props => (
{JSON.stringify(props.formState)}
);
const ComponentWithFormState = withFormState(SomeComponentInsideForm);
class WithFormStateDemo extends React.Component {
render() {
return (
);
}
}
render(WithFormStateDemo);
```
### Take over custom components
Via `withField`, you can extend other custom components into Field. Form will taking over its behavior.
Custom components must be controlled components.
With Field did the following things.
- Take over the `value` of the component (or other properties specified by valueKey), `onChange` (or other callback functions specified by onKeyChangeFnName)
- Insert Field's ``above the field
- Insert Field's `` under the field
- Insert Field's extraText under the field
With Field Options specific configuration can be consulted [withFieldOption](#WithFieldOption)
Your custom controlled component needs to do the following:
- When the value changes, call `props.onChange` and use the latest value as an input parameter
- Respond to changes in `props.value` and update your component UI rendering results
```jsx
withField(YourComponent, withFieldOption);
```
```jsx live=true dir="column" noInline=true
import React from 'react';
import { withField, Form } from '@douyinfe/semi-ui';
// encapsulated html native input
const htmlInput = (props) => {
let value = props.value || '';
let { validateStatus, ...rest } = props; // prevent props being transparently transmitted to DOM
return ;
};
const CustomInput = withField(htmlInput, { valueKey: 'value', onKeyChangeFnName: 'onChange', valuePath: 'target.value' });
// This component is used as an example, you can observe the formState here to see if the input data flow has been taken over by the form
const ComponentUsingFormState = () => {
const formState = useFormState();
return (
{JSON.stringify(formState)}
);
};
class WithFieldDemo1 extends React.Component {
render() {
return (
);
}
}
render(WithFieldDemo1);
```
```jsx live=true dir="column" noInline=true
import React from 'react';
import { withField, Input, Select, Form } from '@douyinfe/semi-ui';
const MyComponent = (props) => {
const { onChange, value } = props;
const { name, role } = value || {};
const handleChange = (v, type) => {
let newValue = { ...value, [type==='name' ? 'name' : 'role']: v };
onChange(newValue);
};
return (
);
};
class WithFieldDemo2 extends React.Component {
render() {
return (
);
}
}
render(WithFieldDemo2);
```
## API reference
## Form Props
| Properties | Instructions | Type | Default |
| ----------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ----------------------------------------------- | ---------- |
| autoScrollToError | If setting true,when submit or call formApi.validate () fails verification, it will automatically scroll to the wrong field, object config refer to [options](https://github.com/stipsan/scroll-into-view-if-needed#options) | boolean\| object | false |
| allowEmpty | Whether to keep the key of the null field in the values, keep the key when true, and remove the key when false | boolean | false |
| component | For declaring fields, not used at the same time as render, props.children | ReactNode |
| className | Classname for form tag | string |
| disabled | If true, all fields inside the form structure will automatically inherit the disabled attribute | boolean | false |
| extraTextPosition | The extraTextPosition property applied to each Field uniformly controls the display position of extraText. Middle (the vertical direction is displayed in the order of Label, extraText, and Field), bottom (the vertical direction is displayed in the order of Label, Field, and extraText) **since v1.9.0** | string | 'bottom' |
| getFormApi | This function will be executed once when the form is mounted and returns formApi. formApi can be used to modify the internal state of the form (value, touched, error) | function (formApi: object) | |
| initValues | Used to uniformly set the initial value of the form (will be consumed only once when form is mount) | object | |
| layout | The layout of fields, optional `horizontal` or `vertical` | string | 'vertical' |
| labelCol | Uniformly applied to the label label layout of each Field, with [Col Component](/en-US/basic/grid#Col), set `span`, `span` values, such as {span: 6, selected: 2} | object |
| labelAlign | Text-align value of label | string | 'left' |
| labelPosition | Location of label in Field, optional 'top', 'left', 'inset' (inset label only partial component support) | string | 'top' |
| labelWidth | Width of field'r label | string\|number | |
| onChange | Callback invoked when form update, including Fields mount/unmount / value change / blur / validation status change / error status change. | function (formState: object) | |
| onValueChange | Callback invoked when form values update | function (values: object, changedValue: object) |
| onReset | Callback invoked after clicked on reset button or executed `formApi.reset()` | function () | |
| onSubmit | Callback invoked after clicked on submit button or executed `formApi.submit()`, and all validation pass. | function (values: object) | |
| onSubmitFail | Callback invoked after clicked on submit button or executed `formApi.submit()`, but validate failed. | function (object, values: object) | |
| render | For declaring fields, not used at the same time as component, props.children | function |
| showValidateIcon | Whether the verification information block in the field automatically adds the corresponding status icon display **since v1.0.0** | boolean | true |
| validateFields | Form-level custom validate functions are called at submit or formApi.validate(). Supported synchronous / asynchronous function | function (values) | |
| wrapperCol | Uniformly apply the layout on each Field, with [Col component](/en-US/basic/grid#Col), set `span`, `span` values, such as {span: 20, offset: 4} | object |
## FormState
FormState stores all the state values within the Form, including the values of each field, error information, touched status
| Name | Instructions | Initial value | Example |
| ------- | -------------------------------------------------------------------------------------------------------------------------------- | ------------- | ----------------------------- |
| values | Value Collection of the form | {} | {fieldA: 'str', fieldB: true} |
| errors | Form error information collection, you can decide whether to allow users to submit by judging whether there is error information | {} | {fieldA: 'length not valid'} |
| touched | The collection of fields the user has clicked on | {} | {fieldA: true} |
### How to access the form state
- By calling `formApi.getFormState()`
- By declaring fields through [child render function](/en-US/input/form#Various%20ways%20to%20declare%20form), formState will injected as a parameter
- By declaring fields through [render props](/en-US/input/form#Various%20ways%20to%20declare%20form), formState will injected as a parameter
- Via [useFormState](#useFormState) hook
- Via [withFormState](#withFormState) HOC
## FormApi
We provide FormApi. You have easy access to FormApi both inside and outside the Form, which allows you to use getter and setter to get and manipulate the values of FormState.
The table below describes the features available in the formApi.
In order to prevent the user from accidentally modifying the internal state of the Form component after reading the internal state of formState, values
Semi will automatically execute deepClone once for input parameters of `formApi.setValue` and `setValues` and the return results of `formApi.getFormState`, `getValue` and `getValues `
| Function | Description | example |
| ------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------- |
| getFormState | Get FormState | formApi.getFormState() |
| submitForm | Manually submit form operation | formApi.submitForm() |
| reset | Reset the form manually | formApi.reset() |
| validate | Manually trigger validation of the entire form. the verification of the entire Field will be triggered by default when no parameters are passed , if you want to trigger the verification of some fields, pass in the target field array
After the Form level validator is configured, the Field level validator will not be triggered again when submit or formApi.validate() | formApi.validate() .then(values => {}) .catch(errors => {}) OR formApi.validate(['fieldA','fieldB']) |
| setValues | Set the values of the entire form. The isOverride in the second parameter is false by default. By default, only the values of the existing field in the Form are updated from `newValues` to`formState.values`. When isOverride is `true`, the newValues will be overwritten and assigned to formState.values | formApi.setValues(newValues: object, {isOverride: boolean}) |
| getValues | Get the values of all Field | formApi.getValues() |
| setValue | provides direct modification of formState.values method. The difference from `setValues` is that it only modifies a single field. | formApi.setValue(field: string, newFieldValue: any) |
| getValue | Get the value of all / single Field | formApi.getValue() formApi.getValue(field: string) |
| setTouched | Modify formState.touched | formApi.setTouched(field: string, isTouched: boolean) |
| getTouched | Get the touched state of the Field | formApi.getTouched(field: string) |
| setError | Modify the error information of a field | formApi.setError(field: string, fieldErrorMessage: string) |
| getError | Get Error Status of Field | formApi.getError(field: string) |
| getFieldExist | Get whether the field exists in the Form | formApi.getFieldExist(field: string) |
| scrollToField | Scroll to field | formApi.scrollToField(field: string, scrollOpts: [object](<(https://github.com/stipsan/scroll-into-view-if-needed#options)>)) |
### How to access formApi
- The Form component in the `ComponentDidMount` phase will execute the `getFormApi` callback passed in by props. You can save a reference to formApi in the callback function for subsequent calls (example code below)
In addition, we provide other ways to get formApi, and you can choose different ways of calling according to your preference.
- Use reference to get form instance,you can access form instance & its formApi
- By declaring fields through "child render function", formApi will injected as a parameter
- By declaring fields through "render props", formApi will injected as a parameter
- Via [useFormApi](#useFormApi) hook for children component of Form
- Via [withFormApi](#withFormApi) HOC for children component of Form
```jsx
import React from 'react';
import { Form, Button } from '@douyinfe/semi-ui';
class FormApiDemo extends React.Component {
constructor() {
super();
this.getFormApi = this.getFormApi.bind(this);
this.formBRef = React.createRef();
}
getFormApi(formApi) {
this.formApi = formApi;
// After getting the formApi object, you can use it to make any changes you want to the form
}
changeValues() {
// use formApi to update formA
this.formApi.setValues({ a: 1 });
// use formApi to update formB
this.formBRef.current.formApi.setValues({ b: 2 });
}
render() {
return (
<>
>
);
}
}
```
```jsx
import React from 'react';
import { Form, Button } from '@douyinfe/semi-ui';
() => {
// functional compoentn usage
const api = useRef();
return (
<>
>
);
};
```
## Field Props
Versions before v1.30.0, the Field component will not do ref forwarding
After v1.30, the underlying component instance can be obtained directly through ref, such as specifying ref to Form.Input and Form.Select, and directly obtaining the ref reference of the underlying original Input and Select components
| Properties | Description | Type | Default | Examples |
| --------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -------------------------------------- | --------- | ------------------------------------------------------------------ |
| field | The mapping path of the field's value in formState.values. Form will use this value to distinguish the internal form control. **Required!!!** | string | | |
| label | The label text for this field. When not passed, it defaults to the same name as field | string | |
| labelPosition | Label position of this field, optional 'top' / 'left' / 'inset' | string | |
| labelAlign | Text-align of the label text of this field | string | |
| labelWidth | The width of the label text of this field | string\|number | |
| noLabel | When you don't need to add label automatically, you can set this value to true | boolean | |
| name | Field name. When passed in, the corresponding className will be automatically added to the field wrapper div, such as: money => '.semi-form-field-money'. After v2.24, the name will also be transparently transmitted to the underlying component for consumption. For example, you can configure the name attribute of input | string | |
| fieldClassName | The className of the entire fieldWrapper is the same as the name parameter, except that the prefix is not automatically appended | string | |
| fieldStyle | The inline style of the entire fieldWrapper **since v1.9.0** | object | |
| initValue | The initial value of the field (consumed only once when Field mounted, subsequent updates are invalid), it has higher priority than the values in Form's initValues | any(type depends on current component) | |
| validate | The custom validation function for this form control. Supports synchronous and asynchronous verification. Rules does not take effect when validate is set | function(fieldValue, values) | | (fieldValue) => fieldValue.length>5? 'error balabala': '' |
| rules | validation rules, validation library based on [async-validator](https://github.com/yiminghe/async-validator) | array | | const rules = \[{type:' string ', message:' invalidate string'} \] |
| validateStatus | The validation result status of this form control, optional: `success` / `error` / `warning` / `default` | string | 'default' |
| trigger | The timing of triggering the verification, optional: `blur` / `change` / `custom` / `mount` 1. When set to custom, only formApi will trigger the verification 2.mount (triggered once when mounting) | string | 'change' |
| onChange | Callback invoked when this field value changes |
| transform | transform field values before validation | function(fieldValue) | | (value) => Number(value) |
| allowEmptyString | Whether to allow values to be empty strings. When the value is '' by default, the key corresponding to this field will be removed from `values`. If you want to keep the key, you need to set allowEmptyString to true | boolean | | false |
| convert | After the field value changes, before rerender, update the value of filed | function(fieldValue) | | (value) => newValue |
| stopValidateWithError | When it is true, the rules check is used. After encountering the first rule that fails the check, it will no longer trigger the check of subsequent rules **since v0.35.0** | boolean | | false |
| helpText | Custom prompt information, which is displayed in the same block as the verification information. When both have values, the verification information is displayed first **since v1.0.0** | ReactNode | | |
| extraText | Additional prompt information, you can use this when both error information and prompt copy are required, after helpText/errorMessage **since v1.0.0** | ReactNode | | |
| pure | Whether to only take over the data stream, when true, it will not automatically insert modules such as ErrorMessage, Label, extraText, etc. The style and DOM structure are consistent with the original components **since v1.1.0** | boolean | false | |
| extraTextPosition | controls the display position of extraText. Middle (the vertical direction is displayed in the order of Label, extraText, and Field), bottom (the vertical direction is displayed in the order of Label, Field, and extraText) **since v1.9.0** | string | 'bottom' | |
| ...other | The other configurable properties of the component can be passed in together with the above properties, such as the size / placeholder of Input,**Field passes it to the component itself** |
## Field Api
We also provide `fieldApi`, most of which is similar to `formApi`, with the difference that fieldApi limits the scope of modification, and it can only modify the bound field
| Function | Instructions | example |
| ---------- | ------------------------------------------------- | ------------------------------------------ |
| setValue | Modify the value of the current Field | fieldApi.setValue(newValue: any) |
| getValue | Gets the value of the current Field | fieldApi.getValue() |
| setTouched | Modify the value of the current Field | fieldApi.setTouched(true) |
| getTouched | Get Field's status | fieldApi.getTouched() |
| setError | Modify the error information of the current Field | fieldApi.setError(newErrorMessage: string) |
| getError | Gets field's error status | fieldApi.getError() |
## ArrayField Props
For dynamically added and deleted array form items, we provide the ArrayField scope to simplify add/remove operations
| Properties | Description | Type | Default |
| --------------------- | ---------------------------------------------------------------- | -------- | --------- |
| field | The mapping path of the value of the form control in formState.values Required, for example, there is an ArrayField responsible for rendering a[0].name, a[1].name, a[2].name three lines, their The parent is a, here props.field should be `a` | string | |
| initValue | The initial value of ArrayField, if the initial value is configured in both formProps.initValues and arrayFieldProps.initValue, the priority of the latter is higher | Array | []
| children | The content of ArrayField, the type is Function, the function input parameters are operation functions such as add, addWithInitValue and arrayFields, and it should return ReactNode after execution | Function(ArrayFieldChildrenProps) => ReactNode |
```ts
interface ArrayFieldChildrenProps {
arrayFields: ArrayFieldItem<>; // The current array form, which can be used to perform map operations to render each row
add: () => void; // Add blank line
addWithInitValue: (lineObject: Record) => void; // Add a new row with an initial value
}
interface ArrayFieldItem {
key: string; // A key used to identify the current row, which should be bound to the wrapper of the current row
field: string; // This row fieldPath, which is equivalent to ArrayFieldProps.field + [index]
remove: () => void; // Remove operation function of this line, when called, this line will be deleted directly
}
```
## Form.Section
```jsx
import { Form } from '@douyinfe/semi-ui';
const { Section } = Form;
```
| Properties | Description | Type |
| ---------- | ------------------ | --------- |
| text | Title of section | ReactNode |
| className | Classname | string |
| style | Inline style | object |
| children | Content of section | ReactNode |
## Form.Label
By default, `Label` is self-inserted into each `Field` by `Form`.
If you need to self-insert Label elsewhere, we have provided the `Label` component for you.
```jsx
import { Form } from '@douyinfe/semi-ui';
const { Label } = Form;
```
| Properties | Description | Type | Default |
| ---------- | ------------------------------- | --------- | ------- |
| text | Label content | ReactNode | |
| required | Whether to show the required \* | boolean | false |
| extra | Content after required | ReactNode | |
| align | Text-align | string | 'left' |
| className | Classname of label wrapper | string | |
| style | Inline style | string | |
| width | Label width | number | |
| optional | Whether to automatically append the "(optional)" text mark after the text (automatically switch the same semantic text according to different languages configured by Locale). When this item is true, the required \* will no longer be displayed. | boolean | false | v2.18.0 |
## Form.InputGroup
| Properties | Description | Type | Default | Version |
| ---------------- | --------------------------------------------------------- | ------------------------ |--- |--- |
| className | Classname of Form.InputGroup | string | |
| style | Inline style | object ||
| label | Label text of Form.InputGroup | Label \| string | |
| labelPosition | Label position,optional: 'top'/'left'/'inset'. When Form and InputGroup are passed in at the same time, the InputGroup props shall prevail | string | 'top'|
| extraText | Additional prompt information, when the error message and prompt text need to appear at the same time, you can use this, located after errorMessage | ReactNode | | v2.29.0 |
| extraTextPosition| Control the display position of extraText, optional `middle` (vertical direction is displayed in the order of Label, extraText, Group), `bottom` (vertical direction is displayed in the order of Label, Group, extraText)| string | 'bottom' | v2.29.0|
When extraTextPositon is middle and labelPosition is left. Since extraText is allowed to be ReactNode, the height of the content is variable, and the Label will no longer ensure that it can be aligned with the first line of text in the Field / InputGroup.
## Form.Slot
```jsx
import { Form } from '@douyinfe/semi-ui';
const { Slot } = Form;
```
| Properties | Instructions | Type | Default |
| ---------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -------------- | ------- |
| label | Slot's [Label configuration](#Form.Label), for example {text: 'semi', align: 'left'}; can also be passed directly into string, inside the Slot will be automatically encapsulated in legal Label format | object\|string | |
| className | Classname of Slot Wrapper | string | |
| style | Slot inline style | object | |
| children | Content of slot. You can place your custom component here | ReactNode | |
| error | ErrorMessage of Slot | ErrorMessage\|ReactNode |
## Form.ErrorMessage
```jsx
import { Form } from '@douyinfe/semi-ui';
const { ErrorMessage } = Form;
```
- When the error is React Node, String, boolean, render directly
- When the error is an array, the join operation is automatically performed to aggregate the error information in the array
| Properties | Instructions | Type | Default |
| ---------- | --------------------------------- | ------------------------ | ------- |
| error | Error message content | string\|array\|ReactNode\|undefined\|boolean | {} |
| className | Classname of ErrorMessage wrapper | string | |
| style | Inline style | object | |
## withFieldOption
| key | Description | Default |
| ----------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ---------- |
| valueKey | The component represents the property of the value, such as Switch, Radio is' checked 'and Input is' value ' | 'value' |
| onKeyChangeFnName | The callback function when the component value changes, generally 'onChange' | 'onChange' |
| valuePath | The path of the value attribute to the first parameter in the callback function, such as Radio's onChange (e.target. checked), then the value needs to be set to target .checked; Radio Group's onChange (e.target. value), which is' target .value '; if the first parameter is the value itself, there is no need to take the value down, the item does not need to be set | |
| withCursor | Do you need to maintain a cursor for Input class components | false |
## Accessibility
### ARIA
- [aria-labelledby](https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/Attributes/aria-labelledby)、for
- Field component will automatically add label DOM. The for attribute of label is the same as `props.id` or `props.name` or `props.field`; the `id` attribute of label is determined by `props.id` or `props.name` or `props.field`, and the value format is `${props.field}-label`;
- When the props.labelPosition of the Form or Field is set to `inset`, there is no label tag at this time, but a div tag. The div tag corresponding to insetLabel will be automatically appended with `id`, the value is the same as the id of the above label, corresponding to the `aria-labelledby` of the Field component
- The Field component will be automatically appended with `aria-labelledby`, the value is the same as the id of the above label
- [aria-required](https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/ARIA_Techniques/Using_the_aria-required_attribute)
- When the Field is configured with required fields (that is, props.rules contains require: true or props.label is configured with required: true), the Field component will be automatically appended with `aria-required = true` (except Form.Switch, Form.CheckboxGroup)
- [aria-invalid](https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/ARIA_Techniques/Using_the_aria-invalid_attribute) 、[aria-errormessage](https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/Attributes/aria-errormessage)
- When the Field check fails, the Field component will be automatically added with the `aria-invalid` = true attribute, except for Form.CheckboxGroup.
- When the Field check fails, the Field component will be automatically appended with the `aria-errormessage` attribute, the value of which is the id of the DOM element corresponding to the errorMessage (format like: `${props.field}-errormessage`), except for Form.CheckboxGroup.
- [aria-describedby](https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/ARIA_Techniques/Using_the_aria-describedby_attribute)
- When the Field is configured with `helpText` or `extraText`, the Field component will be automatically added with the `aria-describedby` attribute, whose value is the id of the DOM element corresponding to helpText and extraText (format like: `${props.field}-helpText` , `${props.field}-extraText`)
## Content Guidelines
- Form title
- The title of the form needs to follow the writing specification of the title
- Form label
- The label is a short description of the input box. The label is not a help text, so it should not be a description of the input
- Labels must:
- Place it above or below the input box
- Short (1-3 words)
- Use case conventions for statements (first letter uppercase, others lowercase)
- Help text
- Help text use statement writing conventions, capitalized
- Form button
- For the content Guidelines of the form button, refer to [Button component](/en-US/input/button)
## Design Tokens
## FAQ
- **Why did I declare the form, modify the value, and the data is not automatically mapped to formState.values?**
Check that the field has been passed correctly, and the `field` attribute on the Field component is a must-fill property !
- **Why doesn't the passed `defaultValue` or `defaultChecked` take effect?**
Refer to the beginning of the document [Field](#Field). The Form.Field component unifies the default value. You should pass the default value using `initValue` or `initValues`
- **Why did the component not change and the value not take effect after `initValue` and `initValues` were updated asynchronously?**
`initValue`, `initValues` are only consumed once when Field and Form mount, and subsequent asynchronous updates will not take effect.
If your initial value needs to be taken remotely, you can update it using `formApi.setValue / setValues` after you get the value
Or send a new `key` directly to Form or Field to force it to remount.
- **Why can't getValues get a certain field?**
If the field has no initial value, `getValues` cannot get this item. You can set `initValues`/`initValue` or set the `allowEmpty` attribute to the form.
- **Why does hitting enter on the input box trigger the form's submit?**
This is standard HTML behavior. We do not plan to intervene and we will remain the same as the HTML. If there is really only one input element in the form, and you don't want to trigger the submit callback when you press Enter, it is recommended to use preventDefault for the enter of the keydown event of input to prevent the default behavior.
Click #767 for background and content.
- **The form will automatically save the historical input items, what should I do if I don't want this function?**
Before v2.3, Form did not configure `for`, `name`, `id` and other attributes for input controls strictly according to the A11y accessibility standard, so this function was not available in previous versions. After v2.3, we implemented it strictly according to the W3C standard. If you don't want the browser to automatically save history input items, you can also turn it off by setting `autoComplete=off` at the Form level or Field level
- **[🔍 🧾 More FAQ](https://bytedance.feishu.cn/docs/doccnNKaGhZMqyu0FufD1JGHOjf)**