Skip to content

Commit f793a85

Browse files
committed
doc: update form doc
1 parent 4914864 commit f793a85

File tree

17 files changed

+192
-43
lines changed

17 files changed

+192
-43
lines changed

components/form/__tests__/__snapshots__/demo.test.js.snap

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,42 @@ exports[`renders ./components/form/demo/custom-validation.vue correctly 1`] = `
197197
</form>
198198
`;
199199
200+
exports[`renders ./components/form/demo/customized-form-controls.vue correctly 1`] = `
201+
<form class="ant-form ant-form-inline">
202+
<div class="ant-row ant-form-item">
203+
<div class="ant-col ant-form-item-label"><label html-for="customized_form_controls_price" class="" title="Price">Price
204+
<!---->
205+
</label></div>
206+
<div class="ant-col ant-form-item-control">
207+
<div class="ant-form-item-control-input">
208+
<div class="ant-form-item-control-input-content"><span><input type="text" style="width: 100px;" class="ant-input"><div style="width: 80px; margin: 0px 8px;" class="ant-select ant-select-single ant-select-show-arrow"><!----><!----><div class="ant-select-selector"><span class="ant-select-selection-search"><input id="rc_select_TEST_OR_SSR" autocomplete="off" class="ant-select-selection-search-input" style="opacity: 0;" role="combobox" aria-haspopup="listbox" aria-owns="rc_select_TEST_OR_SSR_list" aria-autocomplete="list" aria-controls="rc_select_TEST_OR_SSR_list" aria-activedescendant="rc_select_TEST_OR_SSR_list_0" readonly="" unselectable="on" type="search"></span><span class="ant-select-selection-item" title="RMB">RMB</span>
209+
<!---->
210+
</div><span class="ant-select-arrow" style="user-select: none;" unselectable="on" aria-hidden="true"><span role="img" aria-label="down" class="anticon anticon-down ant-select-suffix"><svg focusable="false" class="" data-icon="down" width="1em" height="1em" fill="currentColor" aria-hidden="true" viewBox="64 64 896 896"><path d="M884 256h-75c-5.1 0-9.9 2.5-12.9 6.6L512 654.2 227.9 262.6c-3-4.1-7.8-6.6-12.9-6.6h-75c-6.5 0-10.3 7.4-6.5 12.7l352.6 486.1c12.8 17.6 39 17.6 51.7 0l352.6-486.1c3.9-5.3.1-12.7-6.4-12.7z"></path></svg></span></span>
211+
<!---->
212+
</div></span>
213+
</div>
214+
<!---->
215+
</div>
216+
<!---->
217+
<!---->
218+
</div>
219+
</div>
220+
<div class="ant-row ant-form-item">
221+
<!---->
222+
<div class="ant-col ant-form-item-control">
223+
<div class="ant-form-item-control-input">
224+
<div class="ant-form-item-control-input-content"><button class="ant-btn ant-btn-primary" type="submit">
225+
<!----><span>Submit</span>
226+
</button></div>
227+
<!---->
228+
</div>
229+
<!---->
230+
<!---->
231+
</div>
232+
</div>
233+
</form>
234+
`;
235+
200236
exports[`renders ./components/form/demo/dynamic-form-item.vue correctly 1`] = `
201237
<form class="ant-form ant-form-horizontal">
202238
<div class="ant-row ant-form-item">
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
import demoTest from '../../../tests/shared/demoTest';
22

3-
demoTest('form');
3+
demoTest('form', { skip: ['price-input'] });

components/form/demo/basic.vue

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,9 @@ Basic Form data control. Includes layout, initial values, validation and submit.
6161
</template>
6262
<script lang="ts">
6363
import { Dayjs } from 'dayjs';
64-
import { defineComponent, reactive, toRaw, UnwrapRef } from 'vue';
64+
import { defineComponent, reactive, toRaw } from 'vue';
65+
import type { UnwrapRef } from 'vue';
66+
6567
interface FormState {
6668
name: string;
6769
region: string | undefined;

components/form/demo/custom-validation.vue

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,9 @@ See more advanced usage at [async-validator](https://github.com/yiminghe/async-v
4444
</a-form>
4545
</template>
4646
<script lang="ts">
47-
import { RuleObject, ValidateErrorEntity } from 'ant-design-vue/es/form/interface';
48-
import { defineComponent, reactive, ref, UnwrapRef } from 'vue';
47+
import { RuleObject } from 'ant-design-vue/es/form';
48+
import { defineComponent, reactive, ref } from 'vue';
49+
import type { UnwrapRef } from 'vue';
4950
interface FormState {
5051
pass: string;
5152
checkPass: string;
@@ -59,7 +60,7 @@ export default defineComponent({
5960
checkPass: '',
6061
age: undefined,
6162
});
62-
let checkAge = async (rule: RuleObject, value: number) => {
63+
let checkAge = async (_rule: RuleObject, value: number) => {
6364
if (!value) {
6465
return Promise.reject('Please input the age');
6566
}
@@ -73,7 +74,7 @@ export default defineComponent({
7374
}
7475
}
7576
};
76-
let validatePass = async (rule: RuleObject, value: string) => {
77+
let validatePass = async (_rule: RuleObject, value: string) => {
7778
if (value === '') {
7879
return Promise.reject('Please input the password');
7980
} else {
@@ -83,7 +84,7 @@ export default defineComponent({
8384
return Promise.resolve();
8485
}
8586
};
86-
let validatePass2 = async (rule: RuleObject, value: string) => {
87+
let validatePass2 = async (_rule: RuleObject, value: string) => {
8788
if (value === '') {
8889
return Promise.reject('Please input the password again');
8990
} else if (value !== formState.pass) {
@@ -105,7 +106,7 @@ export default defineComponent({
105106
const handleFinish = (values: FormState) => {
106107
console.log(values, formState);
107108
};
108-
const handleFinishFailed = (errors: ValidateErrorEntity<FormState>) => {
109+
const handleFinishFailed = errors => {
109110
console.log(errors);
110111
};
111112
const resetForm = () => {

components/form/demo/dynamic-form-item.vue

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,8 @@ Add or remove form items dynamically.
5555

5656
<script lang="ts">
5757
import { MinusCircleOutlined, PlusOutlined } from '@ant-design/icons-vue';
58-
import { ValidateErrorEntity } from 'ant-design-vue/es/form/interface';
59-
import { defineComponent, reactive, ref, UnwrapRef } from 'vue';
58+
import { defineComponent, reactive, ref } from 'vue';
59+
import type { UnwrapRef } from 'vue';
6060
6161
interface Domain {
6262
value: string;
@@ -94,7 +94,7 @@ export default defineComponent({
9494
.then(() => {
9595
console.log('values', dynamicValidateForm.domains);
9696
})
97-
.catch((error: ValidateErrorEntity<any>) => {
97+
.catch(error => {
9898
console.log('error', error);
9999
});
100100
};

components/form/demo/horizontal-login.vue

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,10 @@ Inline login form is often used in navigation bar.
4545
</template>
4646
<script lang="ts">
4747
import { UserOutlined, LockOutlined } from '@ant-design/icons-vue';
48-
import { ValidateErrorEntity } from 'ant-design-vue/es/form/interface';
49-
import { defineComponent, reactive, UnwrapRef } from 'vue';
48+
import { defineComponent, reactive } from 'vue';
49+
import type { UnwrapRef } from 'vue';
50+
import type { FormProps } from 'ant-design-vue';
51+
5052
interface FormState {
5153
user: string;
5254
password: string;
@@ -61,10 +63,10 @@ export default defineComponent({
6163
user: '',
6264
password: '',
6365
});
64-
const handleFinish = (values: FormState) => {
66+
const handleFinish: FormProps['onFinish'] = values => {
6567
console.log(values, formState);
6668
};
67-
const handleFinishFailed = (errors: ValidateErrorEntity<FormState>) => {
69+
const handleFinishFailed: FormProps['onFinishFailed'] = errors => {
6870
console.log(errors);
6971
};
7072
return {

components/form/demo/lable-width.vue

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,9 @@ Set label width by labelCol.style
4545
</a-form>
4646
</template>
4747
<script lang="ts">
48-
import { defineComponent, reactive, toRaw, UnwrapRef } from 'vue';
48+
import { defineComponent, reactive, toRaw } from 'vue';
49+
import type { UnwrapRef } from 'vue';
50+
4951
interface FormState {
5052
name: string;
5153
delivery: boolean;

components/form/demo/layout.vue

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ There are three layout for form: `horizontal`, `vertical`, `inline`.
3636
</a-form>
3737
</template>
3838
<script lang="ts">
39-
import { computed, defineComponent, reactive, UnwrapRef } from 'vue';
39+
import { computed, defineComponent, reactive } from 'vue';
40+
import type { UnwrapRef } from 'vue';
4041
4142
interface FormState {
4243
layout: 'horizontal' | 'vertical' | 'inline';

components/form/demo/validation.vue

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,10 @@ Just add the `rules` attribute for `Form` component, pass validation rules, and
6767
</a-form>
6868
</template>
6969
<script lang="ts">
70-
import { ValidateErrorEntity } from 'ant-design-vue/es/form/interface';
7170
import { Dayjs } from 'dayjs';
72-
import { defineComponent, reactive, ref, toRaw, UnwrapRef } from 'vue';
71+
import { defineComponent, reactive, ref, toRaw } from 'vue';
72+
import type { UnwrapRef } from 'vue';
73+
7374
interface FormState {
7475
name: string;
7576
region: string | undefined;
@@ -115,7 +116,7 @@ export default defineComponent({
115116
.then(() => {
116117
console.log('values', formState, toRaw(formState));
117118
})
118-
.catch((error: ValidateErrorEntity<FormState>) => {
119+
.catch(error => {
119120
console.log('error', error);
120121
});
121122
};

components/form/index.en-US.md

Lines changed: 61 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ You can align the controls of a `form` using the `layout` prop:
2525

2626
A form consists of one or more form fields whose type includes input, textarea, checkbox, radio, select, tag, and more. A form field is defined using `<a-form-item />`.
2727

28-
2928
## API
3029

3130
### Form
@@ -48,7 +47,7 @@ A form consists of one or more form fields whose type includes input, textarea,
4847
### Events
4948

5049
| Events Name | Description | Arguments | Version |
51-
| --- | --- | --- | --- |
50+
| --- | --- | --- | --- | --- |
5251
| submit | Defines a function will be called if form data validation is successful. | Function(e:Event) | |
5352
| validate | triggers after a form item is validated | name of the form item being validated, whether validation is passed and the error message if not | |
5453
| finish | Trigger after submitting the form and verifying data successfully | function(values) | - | 2.0.0 |
@@ -57,7 +56,7 @@ A form consists of one or more form fields whose type includes input, textarea,
5756
### Methods
5857

5958
| Method | Description | Parameters |
60-
| --- | --- | --- |
59+
| --- | --- | --- | --- |
6160
| validate | Validate fields, it is same as validateFields | (nameList?: [NamePath](#NamePath)[]) => Promise | |
6261
| validateFields | Validate fields | (nameList?: [NamePath](#NamePath)[]) => Promise | |
6362
| scrollToField | Scroll to field position | (name: [NamePath](#NamePath), options: [[ScrollOptions](https://github.com/stipsan/scroll-into-view-if-needed/tree/ece40bd9143f48caf4b99503425ecb16b0ad8249#options)]) => void | |
@@ -85,11 +84,65 @@ A form consists of one or more form fields whose type includes input, textarea,
8584
| validateFirst | Whether stop validate on first rule of error for this field. | boolean | false | |
8685
| validateTrigger | When to validate the value of children node | string \| string[] | `change` | |
8786

88-
#### Note
87+
### Note
88+
89+
#### 3.x
90+
91+
Since version 3.0, Form.Item no longer hijacks child elements, but automatically checks through provider/inject dependency injection. This method can improve component performance, and there is no limit to the number of child elements. The same is true for child elements. It can be a high-level component that is further encapsulated.
92+
93+
You can reference [Customized Form Controls](#components-form-demo-customized-form-controls)
94+
95+
But it also has some disadvantages:
96+
97+
1. If the custom component wants Form.Item to be verified and displayed, you need to inject `const {id, onFieldChange, onFieldBlur} = useFormItemContext` and call the corresponding method.
98+
99+
2. A Form.Item can only collect the data of one form item. If there are multiple form items, it will cause collection confusion, for example,
100+
101+
```html
102+
<a-form-item>
103+
<a-input name="a"></a-input>
104+
<a-input name="b"></a-input>
105+
</a-form-item>
106+
```
107+
108+
As above Form.Item does not know whether to collect `name="a"` or `name=`b``, you can solve this kind of problem in the following two ways:
109+
110+
The first is to use multiple `a-form-item`:
111+
112+
```html
113+
<a-form-item>
114+
<a-input name="a"></a-input>
115+
<a-form-item><a-input name="b"></a-input></a-form-item>
116+
</a-form-item>
117+
```
89118

90-
Form.Item will hijack the only child element and listen to the `blur` and`change` events to achieve the purpose of automatic verification, so please ensure that the form field is not wrapped by other elements. If there are multiple child elements, only the first child element will be monitored for changes.
119+
The second way is to wrap it with a custom component and call `useFormItemContext` in the custom component
91120

92-
If the form field to be monitored does not meet the conditions for automatic monitoring, you can associate the form field as follows:
121+
```html
122+
<script>
123+
import { Form } from 'ant-desing-vue';
124+
export default {
125+
setup() {
126+
const formItemContext = Form.useFormItemContext();
127+
},
128+
};
129+
</script>
130+
```
131+
132+
```html
133+
<a-form-item>
134+
<custom-com>
135+
<a-input name="a"></a-input>
136+
<a-input name="b"></a-input>
137+
</custom-com>
138+
</a-form-item>
139+
```
140+
141+
#### 2.x
142+
143+
Form.Item hijacks the only child element and listens to the `blur` and `change` events to achieve the purpose of automatic verification, so please make sure that the form field is not wrapped by other elements. If there are multiple child elements, only the change of the first child element will be monitored.
144+
145+
If the form field to be monitored does not meet the conditions of automatic monitoring, you can associate the form field as follows:
93146

94147
```html
95148
<a-form-item name="form.name" ref="name" :autoLink="false">
@@ -124,20 +177,17 @@ If the form field to be monitored does not meet the conditions for automatic mon
124177

125178
See more advanced usage at [async-validator](https://github.com/yiminghe/async-validator).
126179

127-
128180
### useForm (v2.2)
129181

130182
`useForm` is a method that can run independently of the Form component. It uses the Vue response mechanism to monitor and verify data, and returns the verification result. You can bind the verification result to any component, `Form. Item` only displays the results.
131183

132184
The following versions need to be provided separately by `@ant-design-vue/use` library, it is not recommended to continue to use, you should upgrade to version 2.2+ as soon as possible
133185

134-
135186
```ts
136187
import { Form } from 'ant-design-vue';
137188
const useForm = Form.useForm;
138189

139190
useForm(modelRef, ruleRef, [options]);
140-
141191
```
142192

143193
参数说明:
@@ -174,5 +224,5 @@ function useForm(
174224
) => Promise<RuleError[]>;
175225
mergeValidateInfo: (items: ValidateInfo | ValidateInfo[]) => ValidateInfo;
176226
clearValidate: (names?: namesType) => void;
177-
}
178-
```
227+
};
228+
```

0 commit comments

Comments
 (0)