antd-mini
Version:
antd-mini 是支付宝小程序 UI 组件库,遵循 Ant Design 规范。
437 lines (315 loc) • 17.3 kB
Markdown
---
nav:
path: /components
group:
title: Information Entry
order: 12
toc: 'content'
---
The Form form contains data entry, validation, and corresponding styles. The Form component requires [component2](https://opendocs.alipay.com/mini/framework/custom-component-overview) Support.
- Used to create entities or collect information.
- When the input data type needs to be verified.
> Take the input box as an example
In `index.json` Introducing Components in
```json
"usingComponents": {
"form-input": "antd-mini/es/Form/FormInput/index"
"form-input": "antd-mini/Form/FormInput/index"
}
```
The logic layer registers the input box component ref into the Form
```xml
<form-input
label="用户名"
name="account"
placeholder="请输入用户名"
tooltip="用户名Description"
allowClear
ref="handleRef"
/>
```
```js
import { Form } from 'antd-mini/es/Form/form';
import { Form } from 'antd-mini/Form/form';
Page({
handleRef(ref) {
this.form.addItem(ref);
if (!this.formRefList) {
this.formRefList = [];
}
this.formRefList.push(ref.detail);
},
});
```
<code src='../../demo/pages/Form/FormBasic/index'></code>
<code src='../../demo/pages/Form/FormLayout/index'></code>
<code src='../../demo/pages/Form/FormInitialValues/index'></code>
<code src='../../demo/pages/Form/FormInitialValuesAsync/index'></code>
<code src='../../demo/pages/Form/FormWatch/index'></code>
<code src='../../demo/pages/Form/FormRules/index'></code>
<code src='../../demo/pages/Form/FormDynamic/index'></code>
<code src='../../demo/pages/Form/FormDependency/index'></code>
<code src='../../demo/pages/Form/FormValidate/index'></code>
<code src='../../demo/pages/Form/FormValidateMessages/index'></code>
<code src='../../demo/pages/Form/FormMultiple/index'></code>
<code src='../../demo/pages/Form/FormReadonly/index'></code>
<code src='../../demo/pages/Form/FormImageUploadRules/index'></code>
<code src='../../demo/pages/Form/FormJSON/index'></code>
Use `validateStatus: success` and `footer slot` Customize the error style.
<code src='../../demo/pages/Form/FormCustomError/index'></code>
by using [FormItem](
<code src='../../demo/pages/Form/FormCustom/index'></code>
<code src='../../demo/pages/Form/index'></code>
Properties Included in All Form Components
| Property | Description | Type | Default Value |
| -----|-----|-----|-----|
| dependencies | Set Dependent Fields, View[Detailed Description](
| footer | Bottom slot, receiving errors, status | slot | - |
| name | Name | string | - |
| label | Text | string | - |
| labelWidth | Text Width | string | - |
| position | layout, optional `horizontal` `vertical` | string | horizontal |
| validateStatus | The verification status. If it is not set, it will be automatically generated according to the verification rules. Optional `default` `success` `error` `validating` | string | - |
| help | Prompt information, if not set, will be automatically generated according to the verification rules | string | - |
| header | Top slot, receiving errors, status | slot | - |
| tooltip | Form Item Prompt Information | string\|slot | - |
| required | Required style settings. If it is not set, it will be automatically generated according to the verification rules. | boolean | false |
| message | Verify the error message. If it is not set, it will be automatically generated according to the verification rules. | string | false |
| requiredMark | Required optional tag style, optional `asterisk` `text-required` `text-optional` | string | asterisk |
| Property | Description | Type |
| ---------------- | -------------- | ----------------------------------------- |
| rules | Optional, Validation Rules | View[rules](
| initialValues | Optional, initial value | Record<string, any> |
| validateMessages | Optional, Verify Message | View[validateMessages](
| Property | Description | Type |
| ------------------------ | -------------------------------------------------------------------- | -------------------------------------------------------------------------------------- |
| addItem | Add Form Item | (formItem: Ref) => void |
| updateRules | To update the verification rules of the form, you need to pass in the full number of rules each time. | (rules: Rules) => void, the type of Rules can be viewed[rules](
| getFieldValue | Get the value of a form item | (name: string) => any |
| getFieldsValue | Gets the value for a set of field names. If no nameList is passed, all fields pairs are returned. | (nameList?: string[]) => Record<string, any> |
| getFieldValidatorStatus | Get form check status | (name: string) => [ValidatorStatus](
| getFieldsValidatorStatus | Get a set of form validation states. If no nameList is passed, all fields pairs are returned. | (nameList?: string[]) => Record<string, [ValidatorStatus](
| reset | Reset form to initial value | () => void |
| isFieldTouched | Determine whether a form item has been modified | () => boolean |
| onValueChange | Listen for the value modification of the specified form item, view[Detailed Description](
| onValuesChange | Listen for the value modification of a form item, view[Detailed Description](
| setFieldValue | Set the value of a form item | (name: string, value: any) => void; |
| setFieldsValue | Set the value of a form item | (values: Record<string, any>) => void; |
| setFieldValidatorStatus | Set Form Verification Status | (name: string, validatorStatus: [ValidatorStatus](
| setFieldsValidatorStatus | Set a set of form validation states | (fieldsValidatorStatus: Record<string, [ValidatorStatus](
| setInitialValues | Set Form Initial Values | (initialValues: Record<string, any>) => void |
| submit | Submit the form, return the promise form value, and the verification error will be thrown. | () => Promise<Record<string, any>> |
Used when there is a dependency between fields. For example, the "Password" and "Confirm Password" fields of the registered user form, where the "Confirm Password" verification depends on the "Password" field. Setup `dependencies` After that, the "Password" field update will automatically trigger the "Confirm Password" verification.
Example:
```javascript
{
account: [
{
required: true,
message: '需要输入用户名'
},
],
password: [
{
required: true,
},
],
confirm: [
{
required: true,
message: '需要输入确认密码'
},
(form) => ({
async validator(_, value) {
if (!value || form.getFieldValue('password') === value) {
return;
}
throw new Error('两次密码需一致');
},
}),
]
}
```
`rules` can be in `new Form` can also be set in `FormItem` By `required` or `message` Property settings.
```html
<form-input
label="用户名"
name="account"
required
message="请输入用户名"
ref="handleRef"
></form-input>
```
can refer [Asynchronous validator](https://github.com/yiminghe/async-validator/blob/master/src/messages.ts#L4-L55) message,antd-mini added on this basis. `${label}`,`${len}`,`${min}`,`${max}`,`${pattern}`。
Example:
```javascript
{
required: '需要输入${label}',
string: {
min: '${label}最少${min}个字符',
},
pattern: {
mismatch: '${label}需要满足${pattern}模式',
},
}
```
`setFieldValue` and `setFieldsValue` Will not trigger `onValueChange` and `onValuesChange`。`onValueChange` and `onValuesChange` It is only triggered when a user action is taken. If in `setFieldValue` or `setFieldsValue` then want to trigger `onValueChange` or `onValuesChange`you need to call these methods manually.
**Example**:
```js
const onValuesChangeCallback = (changedValues) => {
console.log(changedValues);
};
this.form.onValuesChange(onValuesChangeCallback);
this.form.setFieldValue(name, value);
onValuesChangeCallback({
[ ]: value,
});
```
```js
type ValidatorStatus = {
status: 'default' | 'success' | 'error' | 'validating',
errors: string[],
};
```
```js
{
values: Record<string, any>,
errorFields: {
name: string;
errors: string[];
}[]
}
```
with `Input` Same.
with `Textarea` Same.
with `Switch` Same.
with `Stepper` Same, but with the following added attributes:
| Property | Description | Type | Default Value |
| ---------------- | --------------------------------- | ------ | ------ |
| stepperClassName | Corresponding `Stepper` Components `className` | string | - |
| stepperStyle | Corresponding `Stepper` Components `style` | string | - |
with `CheckGroup` Same, but with the following added attributes:
| Property | Description | Type | Default Value |
| ---------------- | ----------------------------------- | ------ | ------ |
| checkboxLabel | Corresponding `CheckGroup` Components `label` | slot | - |
| checkboxPosition | Corresponding `CheckGroup` Components `position` | string | - |
with `RadioGroup` Same, but with the following added attributes:
| Property | Description | Type | Default Value |
| ------------- | ----------------------------------- | ------ | ------ |
| radioLabel | Corresponding `RadioGroup` Components `label` | slot | - |
| radioPosition | Corresponding `RadioGroup` Components `position` | string | - |
with `Picker` Same, but with the following added attributes:
| Property | Description | Type | Default Value |
| ----- | -------------------------------------------------------- | ----------------- | ------ |
| arrow | right arrow, optional `right`、`up`、`down`, pass true `right` | string \| boolean | - |
with `DatePicker` Same, but with the following added attributes:
| Property | Description | Type | Default Value |
| ----- | -------------------------------------------------------- | ----------------- | ------ |
| arrow | right arrow, optional `right`、`up`、`down`, pass true `right` | string \| boolean | - |
with `RangePicker` Same, but with the following added attributes:
| Property | Description | Type | Default Value |
| ----- | -------------------------------------------------------- | ----------------- | ------ |
| arrow | right arrow, optional `right`、`up`、`down`, pass true `right` | string \| boolean | - |
with `CascaderPicker` Same, but with the following added attributes:
| Property | Description | Type | Default Value |
| ----- | -------------------------------------------------------- | ----------------- | ------ |
| arrow | right arrow, optional `right`、`up`、`down`, pass true `right` | string \| boolean | - |
with `Slider` Same.
with `Selector` Same.
with `ImageUpload` Same.
`createForm` is a `mixin`for custom form items.
```js
import { createForm } from 'antd-mini/es/Form/form';
Component({
mixins: [createForm()],
methods: {
onChange(value) {
this.emit('onChange', value);
},
},
});
```
`createForm` The following is added to the component:
- `data`
```js
{
formData: {
value: undefined, // 表单项的值
status: 'default', // 表单项的Calibration状态,包括 `default`、`success`、`error`、`validating`
errors: [], // 错误信息
},
}
```
- `methods`
```js
// 修改表单项时,需调用 `emit` 方法。Custom Form Item组件In值改变时,应该调用此方法。
function emit(trigger: 'onChange' | 'onBlur' | 'onFocus', value: any): void;
```
For more methods, please refer `createForm` Method-related documentation. Use `formData` and `emit` The implementation of the custom form item is complete.
Component provides the following CSS variables, which can be used to customize styles. For more information, see ConfigProvider Components.
| Variable name | Default Value | Dark Mode Default | Remarks |
| --------------------- | ------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------- | ---------------- |
| --form-text-color | <div style="width: 150px; height: 30px; background-color: #cccccc; color: #333333;">
| --form-item-color | <div style="width: 150px; height: 30px; background-color: #666666; color: #ffffff;">
| --form-item-bg | <div style="width: 150px; height: 30px; background-color: #ffffff; color: #333333;">
| --form-error-color | <div style="width: 150px; height: 30px; background-color: #ff3141; color: #ffffff;">
| --form-extra-color | <div style="width: 150px; height: 30px; background-color: #999999; color: #ffffff;">
| --form-asterisk-color | <div style="width: 150px; height: 30px; background-color: #ff3b30; color: #ffffff;">