forked from vueComponent/ant-design-vue
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathForm.tsx
executable file
·389 lines (359 loc) · 12.9 KB
/
Form.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
import type { PropType, ExtractPropTypes, HTMLAttributes, ComponentPublicInstance } from 'vue';
import { defineComponent, computed, watch, ref } from 'vue';
import PropTypes from '../_util/vue-types';
import classNames from '../_util/classNames';
import warning from '../_util/warning';
import type { FieldExpose } from './FormItem';
import FormItem from './FormItem';
import { getNamePath, containsNamePath, cloneByNamePathList } from './utils/valueUtil';
import { defaultValidateMessages } from './utils/messages';
import { allPromiseFinish } from './utils/asyncUtil';
import { toArray } from './utils/typeUtil';
import isEqual from 'lodash-es/isEqual';
import type { Options } from 'scroll-into-view-if-needed';
import scrollIntoView from 'scroll-into-view-if-needed';
import initDefaultProps from '../_util/props-util/initDefaultProps';
import { tuple } from '../_util/type';
import type { ColProps } from '../grid/Col';
import type {
InternalNamePath,
NamePath,
RuleError,
ValidateErrorEntity,
ValidateOptions,
Callbacks,
ValidateMessages,
Rule,
} from './interface';
import { useInjectSize } from '../_util/hooks/useSize';
import useConfigInject from '../_util/hooks/useConfigInject';
import { useProvideForm } from './context';
import type { SizeType } from '../config-provider';
import useForm from './useForm';
export type RequiredMark = boolean | 'optional';
export type FormLayout = 'horizontal' | 'inline' | 'vertical';
/** @deprecated Will warning in future branch. Pls use `Rule` instead. */
export type ValidationRule = Rule;
export const formProps = () => ({
layout: PropTypes.oneOf(tuple('horizontal', 'inline', 'vertical')),
labelCol: { type: Object as PropType<ColProps & HTMLAttributes> },
wrapperCol: { type: Object as PropType<ColProps & HTMLAttributes> },
colon: { type: Boolean, default: undefined },
labelAlign: PropTypes.oneOf(tuple('left', 'right')),
labelWrap: { type: Boolean, default: undefined },
prefixCls: String,
requiredMark: { type: [String, Boolean] as PropType<RequiredMark | ''>, default: undefined },
/** @deprecated Will warning in future branch. Pls use `requiredMark` instead. */
hideRequiredMark: { type: Boolean, default: undefined },
model: PropTypes.object,
rules: { type: Object as PropType<{ [k: string]: Rule[] | Rule }> },
validateMessages: {
type: Object as PropType<ValidateMessages>,
default: undefined as ValidateMessages,
},
validateOnRuleChange: { type: Boolean, default: undefined },
// 提交失败自动滚动到第一个错误字段
scrollToFirstError: { type: [Boolean, Object] as PropType<boolean | Options> },
onSubmit: Function as PropType<(e: Event) => void>,
name: String,
validateTrigger: { type: [String, Array] as PropType<string | string[]> },
size: { type: String as PropType<SizeType> },
onValuesChange: { type: Function as PropType<Callbacks['onValuesChange']> },
onFieldsChange: { type: Function as PropType<Callbacks['onFieldsChange']> },
onFinish: { type: Function as PropType<Callbacks['onFinish']> },
onFinishFailed: { type: Function as PropType<Callbacks['onFinishFailed']> },
onValidate: { type: Function as PropType<Callbacks['onValidate']> },
});
export type FormProps = Partial<ExtractPropTypes<ReturnType<typeof formProps>>>;
export type FormExpose = {
resetFields: (name?: NamePath) => void;
clearValidate: (name?: NamePath) => void;
validateFields: (
nameList?: NamePath[] | string,
options?: ValidateOptions,
) => Promise<{
[key: string]: any;
}>;
getFieldsValue: (nameList?: InternalNamePath[] | true) => {
[key: string]: any;
};
validate: (
nameList?: NamePath[] | string,
options?: ValidateOptions,
) => Promise<{
[key: string]: any;
}>;
scrollToField: (name: NamePath, options?: {}) => void;
};
export type FormInstance = ComponentPublicInstance<FormProps, FormExpose>;
function isEqualName(name1: NamePath, name2: NamePath) {
return isEqual(toArray(name1), toArray(name2));
}
const Form = defineComponent({
name: 'AForm',
inheritAttrs: false,
props: initDefaultProps(formProps(), {
layout: 'horizontal',
hideRequiredMark: false,
colon: true,
}),
Item: FormItem,
useForm,
// emits: ['finishFailed', 'submit', 'finish', 'validate'],
setup(props, { emit, slots, expose, attrs }) {
const size = useInjectSize(props);
const { prefixCls, direction, form: contextForm } = useConfigInject('form', props);
const requiredMark = computed(() => props.requiredMark === '' || props.requiredMark);
const mergedRequiredMark = computed(() => {
if (requiredMark.value !== undefined) {
return requiredMark.value;
}
if (contextForm && contextForm.value?.requiredMark !== undefined) {
return contextForm.value.requiredMark;
}
if (props.hideRequiredMark) {
return false;
}
return true;
});
const mergedColon = computed(() => props.colon ?? contextForm.value?.colon);
const validateMessages = computed(() => {
return {
...defaultValidateMessages,
...props.validateMessages,
};
});
const formClassName = computed(() =>
classNames(prefixCls.value, {
[`${prefixCls.value}-${props.layout}`]: true,
[`${prefixCls.value}-hide-required-mark`]: mergedRequiredMark.value === false,
[`${prefixCls.value}-rtl`]: direction.value === 'rtl',
[`${prefixCls.value}-${size.value}`]: size.value,
}),
);
const lastValidatePromise = ref();
const fields: Record<string, FieldExpose> = {};
const addField = (eventKey: string, field: FieldExpose) => {
fields[eventKey] = field;
};
const removeField = (eventKey: string) => {
delete fields[eventKey];
};
const getFieldsByNameList = (nameList: NamePath[]) => {
const provideNameList = !!nameList;
const namePathList = provideNameList ? toArray(nameList).map(getNamePath) : [];
if (!provideNameList) {
return Object.values(fields);
} else {
return Object.values(fields).filter(
field =>
namePathList.findIndex(namePath => isEqualName(namePath, field.fieldName.value)) > -1,
);
}
};
const resetFields = (name?: NamePath) => {
if (!props.model) {
warning(false, 'Form', 'model is required for resetFields to work.');
return;
}
getFieldsByNameList(name ? [name] : undefined).forEach(field => {
field.resetField();
});
};
const clearValidate = (name?: NamePath) => {
getFieldsByNameList(name ? [name] : undefined).forEach(field => {
field.clearValidate();
});
};
const handleFinishFailed = (errorInfo: ValidateErrorEntity) => {
const { scrollToFirstError } = props;
emit('finishFailed', errorInfo);
if (scrollToFirstError && errorInfo.errorFields.length) {
let scrollToFieldOptions: Options = {};
if (typeof scrollToFirstError === 'object') {
scrollToFieldOptions = scrollToFirstError;
}
scrollToField(errorInfo.errorFields[0].name, scrollToFieldOptions);
}
};
const validate = (...args: any[]) => {
return validateField(...args);
};
const scrollToField = (name?: NamePath, options = {}) => {
const fields = getFieldsByNameList(name ? [name] : undefined);
if (fields.length) {
const fieldId = fields[0].fieldId.value;
const node = fieldId ? document.getElementById(fieldId) : null;
if (node) {
scrollIntoView(node, {
scrollMode: 'if-needed',
block: 'nearest',
...options,
});
}
}
};
// eslint-disable-next-line no-unused-vars
const getFieldsValue = (nameList: InternalNamePath[] | true = true) => {
if (nameList === true) {
const allNameList = [];
Object.values(fields).forEach(({ namePath }) => {
allNameList.push(namePath.value);
});
return cloneByNamePathList(props.model, allNameList);
} else {
return cloneByNamePathList(props.model, nameList);
}
};
const validateFields = (nameList?: NamePath[], options?: ValidateOptions) => {
warning(
!(nameList instanceof Function),
'Form',
'validateFields/validateField/validate not support callback, please use promise instead',
);
if (!props.model) {
warning(false, 'Form', 'model is required for validateFields to work.');
return Promise.reject('Form `model` is required for validateFields to work.');
}
const provideNameList = !!nameList;
const namePathList: InternalNamePath[] = provideNameList
? toArray(nameList).map(getNamePath)
: [];
// Collect result in promise list
const promiseList: Promise<{
name: InternalNamePath;
errors: string[];
}>[] = [];
Object.values(fields).forEach(field => {
// Add field if not provide `nameList`
if (!provideNameList) {
namePathList.push(field.namePath.value);
}
// Skip if without rule
if (!field.rules?.value.length) {
return;
}
const fieldNamePath = field.namePath.value;
// Add field validate rule in to promise list
if (!provideNameList || containsNamePath(namePathList, fieldNamePath)) {
const promise = field.validateRules({
validateMessages: validateMessages.value,
...options,
});
// Wrap promise with field
promiseList.push(
promise
.then<any, RuleError>(() => ({ name: fieldNamePath, errors: [], warnings: [] }))
.catch((ruleErrors: RuleError[]) => {
const mergedErrors: string[] = [];
const mergedWarnings: string[] = [];
ruleErrors.forEach(({ rule: { warningOnly }, errors }) => {
if (warningOnly) {
mergedWarnings.push(...errors);
} else {
mergedErrors.push(...errors);
}
});
if (mergedErrors.length) {
return Promise.reject({
name: fieldNamePath,
errors: mergedErrors,
warnings: mergedWarnings,
});
}
return {
name: fieldNamePath,
errors: mergedErrors,
warnings: mergedWarnings,
};
}),
);
}
});
const summaryPromise = allPromiseFinish(promiseList);
lastValidatePromise.value = summaryPromise;
const returnPromise = summaryPromise
.then(() => {
if (lastValidatePromise.value === summaryPromise) {
return Promise.resolve(getFieldsValue(namePathList));
}
return Promise.reject([]);
})
.catch(results => {
const errorList = results.filter(result => result && result.errors.length);
return Promise.reject({
values: getFieldsValue(namePathList),
errorFields: errorList,
outOfDate: lastValidatePromise.value !== summaryPromise,
});
});
// Do not throw in console
returnPromise.catch(e => e);
return returnPromise;
};
const validateField = (...args: any[]) => {
return validateFields(...args);
};
const handleSubmit = (e: Event) => {
e.preventDefault();
e.stopPropagation();
emit('submit', e);
if (props.model) {
const res = validateFields();
res
.then(values => {
emit('finish', values);
})
.catch(errors => {
handleFinishFailed(errors);
});
}
};
expose({
resetFields,
clearValidate,
validateFields,
getFieldsValue,
validate,
scrollToField,
} as FormExpose);
useProvideForm({
model: computed(() => props.model),
name: computed(() => props.name),
labelAlign: computed(() => props.labelAlign),
labelCol: computed(() => props.labelCol),
labelWrap: computed(() => props.labelWrap),
wrapperCol: computed(() => props.wrapperCol),
vertical: computed(() => props.layout === 'vertical'),
colon: mergedColon,
requiredMark: mergedRequiredMark,
validateTrigger: computed(() => props.validateTrigger),
rules: computed(() => props.rules),
addField,
removeField,
onValidate: (name, status, errors) => {
emit('validate', name, status, errors);
},
validateMessages,
});
watch(
() => props.rules,
() => {
if (props.validateOnRuleChange) {
validateFields();
}
},
);
return () => {
return (
<form {...attrs} onSubmit={handleSubmit} class={[formClassName.value, attrs.class]}>
{slots.default?.()}
</form>
);
};
},
});
export default Form as typeof Form & {
readonly Item: typeof FormItem;
readonly useForm: typeof useForm;
};