-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Copy pathBaseForm.tsx
831 lines (775 loc) · 23.9 KB
/
BaseForm.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
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
/* eslint-disable react-hooks/exhaustive-deps */
import { ProConfigProvider } from '@ant-design/pro-provider';
import type {
ProFieldProps,
ProFieldValueType,
ProFormInstanceType,
ProRequestData,
SearchTransformKeyFn,
} from '@ant-design/pro-utils';
import {
ProFormContext,
conversionMomentValue,
isDeepEqualReact,
nanoid,
runFunction,
transformKeySubmitValue,
useFetchData,
useMountMergeState,
usePrevious,
useRefFunction,
useStyle,
} from '@ant-design/pro-utils';
import { useUrlSearchParams } from '@umijs/use-params';
import type { FormInstance, FormItemProps, FormProps } from 'antd';
import { ConfigProvider, Form, Spin } from 'antd';
import type { NamePath } from 'antd/lib/form/interface';
import classNames from 'classnames';
import type dayjs from 'dayjs';
import omit from 'rc-util/lib/omit';
import get from 'rc-util/lib/utils/get';
import { default as namePathSet, default as set } from 'rc-util/lib/utils/set';
import { noteOnce } from 'rc-util/lib/warning';
import React, {
useContext,
useEffect,
useImperativeHandle,
useMemo,
useRef,
useState,
} from 'react';
import FieldContext from '../FieldContext';
import type { SubmitterProps } from '../components';
import { Submitter } from '../components';
import { FormListContext } from '../components/List';
import { GridContext, useGridHelpers } from '../helpers';
import type {
FieldProps,
ProFormGridConfig,
ProFormGroupProps,
} from '../typing';
import { EditOrReadOnlyContext } from './EditOrReadOnlyContext';
export type CommonFormProps<
T = Record<string, any>,
U = Record<string, any>,
> = {
/**
* @name 自定义提交的配置
*
* @example 不展示提交按钮和重置按钮
* submitter={false}
* @example 修改重置按钮的样式,并且隐藏提交按钮
* submitter={{resetButtonProps: { type: 'dashed'},submitButtonProps: { style: { display: 'none', }}}}
*
* @example 修改提交按钮和重置按钮的顺序
* submitter={{ render:(props,dom)=> [...dom]}}
*
* @example 修改提交和重置按钮文字
* submitter={{ searchConfig: { submitText: '提交2',resetText: '重置2'}}}
*/
submitter?:
| SubmitterProps<{
form?: FormInstance<any>;
}>
| false;
/**
* @name 表单结束后调用
* @description 支持异步操作,更加方便
*
* @example onFinish={async (values) => { await save(values); return true }}
*/
onFinish?: (formData: T) => Promise<boolean | void> | void;
/**
* @name 表单按钮的 loading 状态
*/
loading?: boolean;
/**
* @name 这是一个可选的属性(onLoadingChange),它接受一个名为loading的参数,类型为boolean,表示加载状态是否改变。
* 当loading状态发生变化时,将会调用一个函数,这个函数接受这个loading状态作为参数,并且没有返回值(void)。
*/
onLoadingChange?: (loading: boolean) => void;
/**
* @name 获取 ProFormInstance
*
* ProFormInstance 可以用来获取当前表单的一些信息
*
* @example 获取 name 的值 formRef.current.getFieldValue("name");
* @example 获取所有的表单值 formRef.current.getFieldsValue(true);
*
* - formRef.current.nativeElement => `2.29.1+`
*/
formRef?:
| React.MutableRefObject<ProFormRef<T> | undefined>
| React.RefObject<ProFormRef<T> | undefined>;
/**
* @name 同步结果到 url 中
* */
syncToUrl?: boolean | ((values: T, type: 'get' | 'set') => T);
/**
* @name 当 syncToUrl 为 true,在页面回显示时,以url上的参数为主,默认为false
*/
syncToUrlAsImportant?: boolean;
/**
* @name 额外的 url 参数 中
* */
extraUrlParams?: Record<string, any>;
/**
* 同步结果到 initialValues,默认为true如果为false,reset的时将会忽略从url上获取的数据
*
* @name 是否将 url 参数写入 initialValues
*/
syncToInitialValues?: boolean;
/**
* 如果为 false,会原样保存。
*
* @default true
* @param 要不要值中的 Null 和 undefined
*/
omitNil?: boolean;
/**
* 格式化 Date 的方式,默认转化为 string
*
* @example dateFormatter="string" : Moment -> YYYY-MM-DD
* @example dateFormatter="YYYY-MM-DD HH:mm:SS" Moment -> YYYY-MM-DD HH:mm:SS
* @example dateFormatter="HH:mm:SS" Moment -> HH:mm:SS
* @example dateFormatter="number" Moment -> timestamp
* @example dateFormatter=false Moment -> Moment
* @example dateFormatter={(value)=>value.format("YYYY-MM-DD")}
*/
dateFormatter?:
| (string & {})
| 'string'
| 'number'
| ((value: dayjs.Dayjs, valueType: string) => string | number)
| false;
/**
* @name 表单初始化成功,比如布局,label等计算完成
* @example (values)=>{ console.log(values) }
*/
onInit?: (values: T, form: ProFormInstance<any>) => void;
/**
* @name 发起网络请求的参数
*
* @example params={{productId: 1}}
* */
params?: U;
/**
* @name 发起网络请求的参数,返回值会覆盖给 initialValues
*
* @example async (params)=>{ return initialValues }
*/
request?: ProRequestData<T, U>;
/** 是否回车提交 */
isKeyPressSubmit?: boolean;
/** 用于控制form 是否相同的key,高阶用法 */
formKey?: string;
/**
* @name自动选中第一项
* @description 只对有input的类型有效
*/
autoFocusFirstInput?: boolean;
/**
* @name 是否只读模式,对所有表单项生效
* @description 优先低于表单项的 readonly
*/
readonly?: boolean;
} & ProFormGridConfig;
export type BaseFormProps<T = Record<string, any>, U = Record<string, any>> = {
contentRender?: (
items: React.ReactNode[],
submitter: React.ReactElement<SubmitterProps> | undefined,
form: FormInstance<any>,
) => React.ReactNode;
fieldProps?: FieldProps<unknown>;
proFieldProps?: ProFieldProps;
/** 表单初始化完成,form已经存在,可以进行赋值的操作了 */
onInit?: (values: T, form: ProFormInstance<any>) => void;
formItemProps?: FormItemProps;
groupProps?: ProFormGroupProps;
/** 是否回车提交 */
isKeyPressSubmit?: boolean;
/** Form 组件的类型,内部使用 */
formComponentType?: 'DrawerForm' | 'ModalForm' | 'QueryFilter';
} & Omit<FormProps, 'onFinish'> &
CommonFormProps<T, U>;
const genParams = (
syncUrl: BaseFormProps<any>['syncToUrl'],
params: Record<string, any>,
type: 'get' | 'set',
) => {
if (syncUrl === true) {
return params;
}
return runFunction(syncUrl, params, type);
};
type ProFormInstance<T = any> = FormInstance<T> & ProFormInstanceType<T>;
type ProFormRef<T = any> = ProFormInstanceType<T> & any;
/**
* It takes a name path and converts it to an array.
* @param {NamePath} name - The name of the form.
* @returns string[]
*
* a-> [a]
* [a] -> [a]
*/
const covertFormName = (name?: NamePath) => {
if (!name) return name;
if (Array.isArray(name)) return name;
return [name];
};
function BaseFormComponents<T = Record<string, any>, U = Record<string, any>>(
props: BaseFormProps<T, U> & {
loading: boolean;
onUrlSearchChange: (value: Record<string, string | number>) => void;
transformKey: (values: any, omit: boolean, parentKey?: NamePath) => any;
},
) {
const {
children,
contentRender,
submitter,
fieldProps,
formItemProps,
groupProps,
transformKey,
formRef: propsFormRef,
onInit,
form,
loading,
formComponentType,
extraUrlParams = {} as Record<string, any>,
syncToUrl,
onUrlSearchChange,
onReset,
omitNil = true,
isKeyPressSubmit,
autoFocusFirstInput = true,
grid,
rowProps,
colProps,
...rest
} = props;
/**
* 获取 form 实例
*/
const formInstance = Form.useFormInstance();
const { componentSize } = ConfigProvider?.useConfig?.() || {
componentSize: 'middle',
};
/** 同步 url 上的参数 */
const formRef = useRef<ProFormInstance<any>>((form || formInstance) as any);
/**
* 获取布局
*/
const { RowWrapper } = useGridHelpers({ grid, rowProps });
const getFormInstance = useRefFunction(() => formInstance);
const formatValues = useMemo(
() => ({
/**
* 获取被 ProForm 格式化后的所有数据
* @param allData boolean
* @returns T
*
* @example getFieldsFormatValue(true) ->返回所有数据,即使没有被 form 托管的
*/
getFieldsFormatValue: (allData?: true) => {
return transformKey(
getFormInstance()?.getFieldsValue(allData!),
omitNil,
);
},
/**
* 获取被 ProForm 格式化后的单个数据
* @param nameList (string|number)[]
* @returns T
*
* @example {a:{b:value}} -> getFieldFormatValue(['a', 'b']) -> value
*/
/** 获取格式化之后的单个数据 */
getFieldFormatValue: (paramsNameList: NamePath = []) => {
const nameList = covertFormName(paramsNameList);
if (!nameList) throw new Error('nameList is require');
const value = getFormInstance()?.getFieldValue(nameList!);
const obj = nameList ? set({}, nameList as string[], value) : value;
//transformKey会将keys重新和nameList拼接,所以这里要将nameList的首个元素弹出
const newNameList = [...nameList];
newNameList.shift();
return get(
transformKey(obj, omitNil, newNameList),
nameList as string[],
);
},
/**
* 获取被 ProForm 格式化后的单个数据, 包含他的 name
* @param nameList (string|number)[]
* @returns T
*
* @example {a:{b:value}} -> getFieldFormatValueObject(['a', 'b']) -> {a:{b:value}}
*/
/** 获取格式化之后的单个数据 */
getFieldFormatValueObject: (paramsNameList?: NamePath) => {
const nameList = covertFormName(paramsNameList);
const value = getFormInstance()?.getFieldValue(nameList!);
const obj = nameList ? set({}, nameList as string[], value) : value;
return transformKey(obj, omitNil, nameList);
},
/**
/**
*验字段后返回格式化之后的所有数据
* @param nameList (string|number)[]
* @returns T
*
* @example validateFieldsReturnFormatValue -> {a:{b:value}}
*/
validateFieldsReturnFormatValue: async (nameList?: NamePath[]) => {
if (!Array.isArray(nameList) && nameList)
throw new Error('nameList must be array');
const values = await getFormInstance()?.validateFields(nameList);
const transformedKey = transformKey(values, omitNil);
return transformedKey ? transformedKey : {};
},
}),
[omitNil, transformKey],
);
const items = useMemo(() => {
return React.Children.toArray(children as any).map((item, index) => {
if (index === 0 && React.isValidElement(item) && autoFocusFirstInput) {
return React.cloneElement(item, {
...item.props,
autoFocus: autoFocusFirstInput,
});
}
return item;
});
}, [autoFocusFirstInput, children]);
/** 计算 props 的对象 */
const submitterProps: SubmitterProps = useMemo(
() => (typeof submitter === 'boolean' || !submitter ? {} : submitter),
[submitter],
);
/** 渲染提交按钮与重置按钮 */
const submitterNode = useMemo(() => {
if (submitter === false) return undefined;
return (
<Submitter
key="submitter"
{...submitterProps}
onReset={() => {
const finalValues = transformKey(
formRef.current?.getFieldsValue(),
omitNil,
);
submitterProps?.onReset?.(finalValues);
onReset?.(finalValues);
// 如果 syncToUrl,清空一下数据
if (syncToUrl) {
// 把没有的值设置为未定义可以删掉 url 的参数
const params = Object.keys(
transformKey(formRef.current?.getFieldsValue(), false),
).reduce((pre, next) => {
return {
...pre,
[next]: finalValues[next] || undefined,
};
}, extraUrlParams);
/** 在同步到 url 上时对参数进行转化 */
onUrlSearchChange(genParams(syncToUrl, params || {}, 'set'));
}
}}
submitButtonProps={{
loading,
...submitterProps.submitButtonProps,
}}
/>
);
}, [
submitter,
submitterProps,
loading,
transformKey,
omitNil,
onReset,
syncToUrl,
extraUrlParams,
onUrlSearchChange,
]);
const content = useMemo(() => {
const wrapItems = grid ? <RowWrapper>{items}</RowWrapper> : items;
if (contentRender) {
return contentRender(wrapItems as any, submitterNode, formRef.current);
}
return wrapItems;
}, [grid, RowWrapper, items, contentRender, submitterNode]);
const preInitialValues = usePrevious(props.initialValues);
// 提示一个 initialValues ,问的人实在是太多了
useEffect(() => {
if (syncToUrl || !props.initialValues || !preInitialValues || rest.request)
return;
const isEqual = isDeepEqualReact(props.initialValues, preInitialValues);
noteOnce(
isEqual,
`initialValues 只在 form 初始化时生效,如果你需要异步加载推荐使用 request,或者 initialValues ? <Form/> : null `,
);
noteOnce(
isEqual,
`The initialValues only take effect when the form is initialized, if you need to load asynchronously recommended request, or the initialValues ? <Form/> : null `,
);
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [props.initialValues]);
// 初始化给一个默认的 form
useImperativeHandle(propsFormRef, () => {
return {
...formRef.current,
...formatValues,
};
}, [formatValues, formRef.current]);
useEffect(() => {
const finalValues = transformKey(
formRef.current?.getFieldsValue?.(true),
omitNil,
);
onInit?.(finalValues, {
...formRef.current,
...formatValues,
});
}, []);
return (
<ProFormContext.Provider
value={{
...formatValues,
formRef,
}}
>
<ConfigProvider componentSize={rest.size || componentSize}>
<GridContext.Provider value={{ grid, colProps }}>
{rest.component !== false && (
<input
type="text"
style={{
display: 'none',
}}
/>
)}
{content}
</GridContext.Provider>
</ConfigProvider>
</ProFormContext.Provider>
);
}
/** 自动的formKey 防止重复 */
let requestFormCacheId = 0;
function BaseForm<T = Record<string, any>, U = Record<string, any>>(
props: BaseFormProps<T, U>,
) {
const {
extraUrlParams = {} as Record<string, any>,
syncToUrl,
isKeyPressSubmit,
syncToUrlAsImportant = false,
syncToInitialValues = true,
children,
contentRender,
submitter,
fieldProps,
proFieldProps,
formItemProps,
groupProps,
dateFormatter = 'string',
formRef: propsFormRef,
onInit,
form,
formComponentType,
onReset,
grid,
rowProps,
colProps,
omitNil = true,
request,
params,
initialValues,
formKey = requestFormCacheId,
readonly,
onLoadingChange,
loading: propsLoading,
...propRest
} = props;
const formRef = useRef<ProFormRef<any>>({} as any);
const [loading, setLoading] = useMountMergeState<boolean>(false, {
onChange: onLoadingChange,
value: propsLoading,
});
const [urlSearch, setUrlSearch] = useUrlSearchParams(
{},
{ disabled: !syncToUrl },
);
const curFormKey = useRef<string>(nanoid());
useEffect(() => {
requestFormCacheId += 0;
}, []);
const [initialData] = useFetchData<T, U>({
request,
params,
proFieldKey: formKey,
});
const { getPrefixCls } = useContext(ConfigProvider.ConfigContext);
const prefixCls = getPrefixCls('pro-form');
// css
const { wrapSSR, hashId } = useStyle('ProForm', (token) => {
return {
[`.${prefixCls}`]: {
[`> div:not(${token.proComponentsCls}-form-light-filter)`]: {
'.pro-field': {
maxWidth: '100%',
'@media screen and (max-width: 575px)': {
// 减少了 form 的 padding
maxWidth: 'calc(93vw - 48px)',
},
// 适用于短数字,短文本或者选项
'&-xs': {
width: 104,
},
'&-s': {
width: 216,
},
// 适用于较短字段录入、如姓名、电话、ID 等。
'&-sm': {
width: 216,
},
'&-m': {
width: 328,
},
// 标准宽度,适用于大部分字段长度
'&-md': {
width: 328,
},
'&-l': {
width: 440,
},
// 适用于较长字段录入,如长网址、标签组、文件路径等。
'&-lg': {
width: 440,
},
// 适用于长文本录入,如长链接、描述、备注等,通常搭配自适应多行输入框或定高文本域使用。
'&-xl': {
width: 552,
},
},
},
},
};
});
// 如果为 false,不需要触发设置进去
const [urlParamsMergeInitialValues, setUrlParamsMergeInitialValues] =
useState(() => {
if (!syncToUrl) {
return {};
}
return genParams(syncToUrl, urlSearch, 'get');
});
/** 保存 transformKeyRef,用于对表单key transform */
const transformKeyRef = useRef<
Record<string, SearchTransformKeyFn | undefined>
>({});
const fieldsValueType = useRef<
Record<
string,
{
valueType: ProFieldValueType;
dateFormat: string;
}
>
>({});
/** 使用 callback 的类型 */
const transformKey = useRefFunction(
(values: any, paramsOmitNil: boolean, parentKey?: NamePath) => {
return transformKeySubmitValue(
conversionMomentValue(
values,
dateFormatter,
fieldsValueType.current,
paramsOmitNil,
parentKey,
),
transformKeyRef.current,
paramsOmitNil,
);
},
);
useEffect(() => {
if (syncToInitialValues) return;
setUrlParamsMergeInitialValues({});
}, [syncToInitialValues]);
const getGenParams = useRefFunction(() => {
return {
...urlSearch,
...extraUrlParams,
};
});
useEffect(() => {
if (!syncToUrl) return;
setUrlSearch(genParams(syncToUrl, getGenParams(), 'set'));
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [extraUrlParams, getGenParams, syncToUrl]);
const getPopupContainer = useMemo(() => {
if (typeof window === 'undefined') return undefined;
// 如果在 drawerForm 和 modalForm 里就渲染dom到父节点里
// modalForm 可能高度太小不适合
if (formComponentType && ['DrawerForm'].includes(formComponentType)) {
return (e: HTMLElement) => e.parentNode || document.body;
}
return undefined;
}, [formComponentType]);
const onFinish = useRefFunction(async () => {
// 没设置 onFinish 就不执行
if (!propRest.onFinish) return;
// 防止重复提交
if (loading) return;
try {
const finalValues = formRef?.current?.getFieldsFormatValue?.();
const response = propRest.onFinish(finalValues);
if (response instanceof Promise) {
setLoading(true);
}
await response;
if (syncToUrl) {
// 把没有的值设置为未定义可以删掉 url 的参数
const syncToUrlParams = Object.keys(
formRef?.current?.getFieldsFormatValue?.(undefined, false),
).reduce((pre, next) => {
return {
...pre,
[next]: finalValues[next] ?? undefined,
};
}, extraUrlParams);
// fix #3547: 当原先在url中存在的字段被删除时,应该将 params 中的该字段设置为 undefined,以便触发url同步删除
Object.keys(urlSearch).forEach((key) => {
if (
syncToUrlParams[key] !== false &&
syncToUrlParams[key] !== 0 &&
!syncToUrlParams[key]
) {
syncToUrlParams[key] = undefined;
}
});
/** 在同步到 url 上时对参数进行转化 */
setUrlSearch(genParams(syncToUrl, syncToUrlParams, 'set'));
}
setLoading(false);
} catch (error) {
console.log(error);
setLoading(false);
}
});
// 初始化给一个默认的 form
useImperativeHandle(propsFormRef, () => {
return formRef.current;
}, [!initialData]);
if (!initialData && props.request) {
return (
<div style={{ paddingTop: 50, paddingBottom: 50, textAlign: 'center' }}>
<Spin />
</div>
);
}
return wrapSSR(
<EditOrReadOnlyContext.Provider
value={{
mode: props.readonly ? 'read' : 'edit',
}}
>
<ProConfigProvider needDeps>
{/* // 增加国际化的能力,与 table 组件可以统一 */}
<FieldContext.Provider
value={{
formRef,
fieldProps,
proFieldProps,
formItemProps,
groupProps,
formComponentType,
getPopupContainer,
formKey: curFormKey.current,
setFieldValueType: (
name,
{ valueType = 'text', dateFormat, transform },
) => {
if (!Array.isArray(name)) return;
transformKeyRef.current = namePathSet(
transformKeyRef.current,
name,
transform,
);
fieldsValueType.current = namePathSet(
fieldsValueType.current,
name,
{
valueType,
dateFormat,
},
);
},
}}
>
<FormListContext.Provider value={{}}>
<Form
onKeyPress={(event) => {
if (!isKeyPressSubmit) return;
if (event.key === 'Enter') {
formRef.current?.submit();
}
}}
autoComplete="off"
form={form}
{...omit(propRest, [
'ref',
'labelWidth',
'autoFocusFirstInput',
] as any[])}
ref={(instance) => {
if (!formRef.current) return;
formRef.current.nativeElement = instance?.nativeElement;
}}
// 组合 urlSearch 和 initialValues
initialValues={
syncToUrlAsImportant
? {
...initialValues,
...initialData,
...urlParamsMergeInitialValues,
}
: {
...urlParamsMergeInitialValues,
...initialValues,
...initialData,
}
}
onValuesChange={(changedValues, values) => {
propRest?.onValuesChange?.(
transformKey(changedValues, !!omitNil),
transformKey(values, !!omitNil),
);
}}
className={classNames(props.className, prefixCls, hashId)}
onFinish={onFinish}
>
<BaseFormComponents<T, U>
transformKey={transformKey}
autoComplete="off"
loading={loading}
onUrlSearchChange={setUrlSearch}
{...props}
formRef={formRef}
initialValues={{
...initialValues,
...initialData,
}}
/>
</Form>
</FormListContext.Provider>
</FieldContext.Provider>
</ProConfigProvider>
</EditOrReadOnlyContext.Provider>,
);
}
export { BaseForm };
export type { FormInstance, FormItemProps, FormProps, ProFormInstance };