zy-react-library 1.0.77 → 1.0.79
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -1,105 +1,107 @@
|
|
|
1
|
-
import type { ColProps } from "antd/es/col";
|
|
2
|
-
import type { FormItemProps, Rule } from "antd/es/form";
|
|
3
|
-
import type { NamePath } from "rc-field-form/lib/interface";
|
|
4
|
-
import type { FC, ReactNode } from "react";
|
|
5
|
-
import type { FORM_ITEM_RENDER_ENUM } from "../../enum/formItemRender";
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
* 表单项渲染类型
|
|
9
|
-
*/
|
|
10
|
-
export type FormItemRenderType
|
|
11
|
-
= | (typeof FORM_ITEM_RENDER_ENUM)[keyof typeof FORM_ITEM_RENDER_ENUM]
|
|
12
|
-
| ReactNode;
|
|
13
|
-
|
|
14
|
-
/**
|
|
15
|
-
* 选项项数据类型
|
|
16
|
-
*/
|
|
17
|
-
export interface OptionItem {
|
|
18
|
-
/** ID字段 */
|
|
19
|
-
bianma?: any;
|
|
20
|
-
/** 名称字段 */
|
|
21
|
-
name?: string;
|
|
22
|
-
[key: string]: any;
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
/**
|
|
26
|
-
* 字段键配置
|
|
27
|
-
*/
|
|
28
|
-
export interface itemsFieldConfig {
|
|
29
|
-
/** 值字段的键名,默认为 'bianma' */
|
|
30
|
-
valueKey?: string;
|
|
31
|
-
/** 标签字段的键名,默认为 'name' */
|
|
32
|
-
labelKey?: string;
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
/**
|
|
36
|
-
* 表单值类型
|
|
37
|
-
*/
|
|
38
|
-
export type FormValues = Record<string, any>;
|
|
39
|
-
|
|
40
|
-
/**
|
|
41
|
-
* 表单配置项
|
|
42
|
-
*/
|
|
43
|
-
export interface FormOption {
|
|
44
|
-
/** React 需要的 key,如果传递了唯一的 name,则不需要 */
|
|
45
|
-
key?: string;
|
|
46
|
-
/** 表单项字段名 */
|
|
47
|
-
name?: string | string[];
|
|
48
|
-
/** 表单项标签 */
|
|
49
|
-
label?: ReactNode;
|
|
50
|
-
/** 渲染类型 */
|
|
51
|
-
render?: FormItemRenderType;
|
|
52
|
-
/** 占据栅格列数,默认 12 */
|
|
53
|
-
span?: number | string;
|
|
54
|
-
/** 是否必填,默认 true,支持函数动态计算 */
|
|
55
|
-
required?: boolean | ((formValues: FormValues) => boolean);
|
|
56
|
-
/** 验证规则 */
|
|
57
|
-
rules?: Rule | Rule[];
|
|
58
|
-
/** 占位符文本,默认会根据传入的 render 类型自动判断(请选择、请输入)和 label 组合 */
|
|
59
|
-
placeholder?: ReactNode;
|
|
60
|
-
/** 提示信息,传入将在 label 右侧生成图标展示 tooltip */
|
|
61
|
-
tip?: ReactNode;
|
|
62
|
-
/** 是否隐藏,默认 false,支持函数动态计算 */
|
|
63
|
-
hidden?: boolean | ((formValues: FormValues) => boolean);
|
|
64
|
-
/**
|
|
65
|
-
customizeRender?: boolean;
|
|
66
|
-
/** 选项数据(用于 select、radio、checkbox) */
|
|
67
|
-
items?: OptionItem[];
|
|
68
|
-
/** 字段键配置 */
|
|
69
|
-
itemsField?: itemsFieldConfig;
|
|
70
|
-
/** 传递给表单控件的属性,支持函数动态计算 */
|
|
71
|
-
componentProps?: Record<string, any> | ((formValues: FormValues) => Record<string, any>);
|
|
72
|
-
/** 传递给 Form.Item 的属性,支持函数动态计算 */
|
|
73
|
-
formItemProps?: FormItemProps | ((formValues: FormValues) => FormItemProps);
|
|
74
|
-
/** label 栅格配置,默认直接使用外层的 labelCol,如果 span 等于 24,是外层的 labelCol.span 一半 */
|
|
75
|
-
labelCol?: ColProps;
|
|
76
|
-
/** wrapper 栅格配置,默认为 24 - labelCol.span */
|
|
77
|
-
wrapperCol?: ColProps;
|
|
78
|
-
/** 是否应该更新(用于表单联动) */
|
|
79
|
-
shouldUpdate?: boolean | ((prevValues: FormValues, nextValues: FormValues, info: { source?: string }) => boolean);
|
|
80
|
-
/** 依赖字段(用于表单联动) */
|
|
81
|
-
dependencies?: NamePath[];
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
/**
|
|
91
|
-
|
|
92
|
-
/**
|
|
93
|
-
|
|
94
|
-
/**
|
|
95
|
-
|
|
96
|
-
/**
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
1
|
+
import type { ColProps } from "antd/es/col";
|
|
2
|
+
import type { FormItemProps, Rule } from "antd/es/form";
|
|
3
|
+
import type { NamePath } from "rc-field-form/lib/interface";
|
|
4
|
+
import type { FC, ReactNode } from "react";
|
|
5
|
+
import type { FORM_ITEM_RENDER_ENUM } from "../../enum/formItemRender";
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* 表单项渲染类型
|
|
9
|
+
*/
|
|
10
|
+
export type FormItemRenderType
|
|
11
|
+
= | (typeof FORM_ITEM_RENDER_ENUM)[keyof typeof FORM_ITEM_RENDER_ENUM]
|
|
12
|
+
| ReactNode;
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* 选项项数据类型
|
|
16
|
+
*/
|
|
17
|
+
export interface OptionItem {
|
|
18
|
+
/** ID字段 */
|
|
19
|
+
bianma?: any;
|
|
20
|
+
/** 名称字段 */
|
|
21
|
+
name?: string;
|
|
22
|
+
[key: string]: any;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* 字段键配置
|
|
27
|
+
*/
|
|
28
|
+
export interface itemsFieldConfig {
|
|
29
|
+
/** 值字段的键名,默认为 'bianma' */
|
|
30
|
+
valueKey?: string;
|
|
31
|
+
/** 标签字段的键名,默认为 'name' */
|
|
32
|
+
labelKey?: string;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* 表单值类型
|
|
37
|
+
*/
|
|
38
|
+
export type FormValues = Record<string, any>;
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* 表单配置项
|
|
42
|
+
*/
|
|
43
|
+
export interface FormOption {
|
|
44
|
+
/** React 需要的 key,如果传递了唯一的 name,则不需要 */
|
|
45
|
+
key?: string;
|
|
46
|
+
/** 表单项字段名 */
|
|
47
|
+
name?: string | string[];
|
|
48
|
+
/** 表单项标签 */
|
|
49
|
+
label?: ReactNode;
|
|
50
|
+
/** 渲染类型 */
|
|
51
|
+
render?: FormItemRenderType;
|
|
52
|
+
/** 占据栅格列数,默认 12 */
|
|
53
|
+
span?: number | string;
|
|
54
|
+
/** 是否必填,默认 true,支持函数动态计算 */
|
|
55
|
+
required?: boolean | ((formValues: FormValues) => boolean);
|
|
56
|
+
/** 验证规则 */
|
|
57
|
+
rules?: Rule | Rule[];
|
|
58
|
+
/** 占位符文本,默认会根据传入的 render 类型自动判断(请选择、请输入)和 label 组合 */
|
|
59
|
+
placeholder?: ReactNode;
|
|
60
|
+
/** 提示信息,传入将在 label 右侧生成图标展示 tooltip */
|
|
61
|
+
tip?: ReactNode;
|
|
62
|
+
/** 是否隐藏,默认 false,支持函数动态计算 */
|
|
63
|
+
hidden?: boolean | ((formValues: FormValues) => boolean);
|
|
64
|
+
/** 是否自定义渲染,完全交给外部控制渲染,默认 false */
|
|
65
|
+
customizeRender?: boolean;
|
|
66
|
+
/** 选项数据(用于 select、radio、checkbox) */
|
|
67
|
+
items?: OptionItem[];
|
|
68
|
+
/** 字段键配置 */
|
|
69
|
+
itemsField?: itemsFieldConfig;
|
|
70
|
+
/** 传递给表单控件的属性,支持函数动态计算 */
|
|
71
|
+
componentProps?: Record<string, any> | ((formValues: FormValues) => Record<string, any>);
|
|
72
|
+
/** 传递给 Form.Item 的属性,支持函数动态计算 */
|
|
73
|
+
formItemProps?: FormItemProps | ((formValues: FormValues) => FormItemProps);
|
|
74
|
+
/** label 栅格配置,默认直接使用外层的 labelCol,如果 span 等于 24,是外层的 labelCol.span 一半 */
|
|
75
|
+
labelCol?: ColProps;
|
|
76
|
+
/** wrapper 栅格配置,默认为 24 - labelCol.span */
|
|
77
|
+
wrapperCol?: ColProps;
|
|
78
|
+
/** 是否应该更新(用于表单联动) */
|
|
79
|
+
shouldUpdate?: boolean | ((prevValues: FormValues, nextValues: FormValues, info: { source?: string }) => boolean);
|
|
80
|
+
/** 依赖字段(用于表单联动) */
|
|
81
|
+
dependencies?: NamePath[];
|
|
82
|
+
/** 是否仅用于保存标签,不渲染到页面上,只在表单中保存数据,默认 false */
|
|
83
|
+
onlyForLabel?: boolean;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* FormItemsRenderer 组件属性
|
|
88
|
+
*/
|
|
89
|
+
export interface FormItemsRendererProps {
|
|
90
|
+
/** 表单配置项数组 */
|
|
91
|
+
options: FormOption[];
|
|
92
|
+
/** 默认栅格占据列数,默认 12 */
|
|
93
|
+
span?: number;
|
|
94
|
+
/** 是否折叠(仅显示前3项),默认 false */
|
|
95
|
+
collapse?: boolean;
|
|
96
|
+
/** 自动生成必填规则,默认 true */
|
|
97
|
+
useAutoGenerateRequired?: boolean;
|
|
98
|
+
/** 初始值,用于在表单未初始化时提供默认值 */
|
|
99
|
+
initialValues?: FormValues;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* 表单项渲染器组件
|
|
104
|
+
*/
|
|
105
|
+
declare const FormItemsRenderer: FC<FormItemsRendererProps>;
|
|
106
|
+
|
|
107
|
+
export default FormItemsRenderer;
|
|
@@ -135,8 +135,8 @@ const FormItemsRenderer = ({
|
|
|
135
135
|
|
|
136
136
|
// 获取key
|
|
137
137
|
const getKey = (option) => {
|
|
138
|
-
return option.key || option.name
|
|
139
|
-
}
|
|
138
|
+
return option.key || option.name;
|
|
139
|
+
};
|
|
140
140
|
|
|
141
141
|
// 渲染表单控件
|
|
142
142
|
const renderFormControl = (option) => {
|
|
@@ -280,6 +280,29 @@ const FormItemsRenderer = ({
|
|
|
280
280
|
// 使用 style 控制显示/隐藏
|
|
281
281
|
const style = collapse && index >= 3 ? { display: "none" } : undefined;
|
|
282
282
|
|
|
283
|
+
// 如果是 customizeRender 类型,完全交给外部控制渲染
|
|
284
|
+
if (option.customizeRender) {
|
|
285
|
+
return (
|
|
286
|
+
<Col key={getKey(option) || index} span={itemSpan} style={style}>
|
|
287
|
+
{option.render}
|
|
288
|
+
</Col>
|
|
289
|
+
);
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
// 如果是 onlyForLabel 类型,不渲染任何UI,只在表单中保存数据
|
|
293
|
+
if (option.onlyForLabel) {
|
|
294
|
+
return (
|
|
295
|
+
<Form.Item
|
|
296
|
+
key={getKey(option) || index}
|
|
297
|
+
name={option.name}
|
|
298
|
+
noStyle
|
|
299
|
+
preserve={false}
|
|
300
|
+
>
|
|
301
|
+
<input type="hidden" />
|
|
302
|
+
</Form.Item>
|
|
303
|
+
);
|
|
304
|
+
}
|
|
305
|
+
|
|
283
306
|
// 如果是分割线
|
|
284
307
|
if (option.render === FORM_ITEM_RENDER_ENUM.DIVIDER) {
|
|
285
308
|
return (
|
|
@@ -292,43 +315,39 @@ const FormItemsRenderer = ({
|
|
|
292
315
|
// 如果配置了 shouldUpdate 或 dependencies,使用 Form.Item 的联动机制
|
|
293
316
|
if ((option.shouldUpdate ?? option.dependencies) || (option?.componentProps?.shouldUpdate ?? option?.componentProps?.dependencies)) {
|
|
294
317
|
return (
|
|
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
|
-
</Form.Item>
|
|
331
|
-
)
|
|
318
|
+
<Form.Item
|
|
319
|
+
key={getKey(option) || index}
|
|
320
|
+
noStyle
|
|
321
|
+
preserve={false}
|
|
322
|
+
shouldUpdate={option.shouldUpdate ?? option?.componentProps?.shouldUpdate}
|
|
323
|
+
dependencies={option.dependencies || option?.componentProps?.dependencies}
|
|
324
|
+
>
|
|
325
|
+
{() => {
|
|
326
|
+
// 支持动态计算 hidden
|
|
327
|
+
const hidden = typeof option.hidden === "function"
|
|
328
|
+
? option.hidden(getFormValues())
|
|
329
|
+
: (option.hidden ?? false);
|
|
330
|
+
|
|
331
|
+
if (hidden)
|
|
332
|
+
return null;
|
|
333
|
+
|
|
334
|
+
return (
|
|
335
|
+
<Col key={getKey(option) || index} span={itemSpan} style={style}>
|
|
336
|
+
<Form.Item
|
|
337
|
+
name={option.name}
|
|
338
|
+
label={renderLabel(option)}
|
|
339
|
+
rules={getRules(option)}
|
|
340
|
+
labelCol={itemLabelCol}
|
|
341
|
+
wrapperCol={itemWrapperCol}
|
|
342
|
+
preserve={false}
|
|
343
|
+
{...getFormItemProps(option)}
|
|
344
|
+
>
|
|
345
|
+
{renderFormControl(option)}
|
|
346
|
+
</Form.Item>
|
|
347
|
+
</Col>
|
|
348
|
+
);
|
|
349
|
+
}}
|
|
350
|
+
</Form.Item>
|
|
332
351
|
);
|
|
333
352
|
}
|
|
334
353
|
|
|
@@ -343,23 +362,17 @@ const FormItemsRenderer = ({
|
|
|
343
362
|
|
|
344
363
|
return (
|
|
345
364
|
<Col key={getKey(option) || index} span={itemSpan} style={style}>
|
|
346
|
-
|
|
347
|
-
option.
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
{...getFormItemProps(option)}
|
|
358
|
-
>
|
|
359
|
-
{renderFormControl(option)}
|
|
360
|
-
</Form.Item>
|
|
361
|
-
)
|
|
362
|
-
}
|
|
365
|
+
<Form.Item
|
|
366
|
+
name={option.name}
|
|
367
|
+
label={renderLabel(option)}
|
|
368
|
+
rules={getRules(option)}
|
|
369
|
+
labelCol={itemLabelCol}
|
|
370
|
+
wrapperCol={itemWrapperCol}
|
|
371
|
+
preserve={false}
|
|
372
|
+
{...getFormItemProps(option)}
|
|
373
|
+
>
|
|
374
|
+
{renderFormControl(option)}
|
|
375
|
+
</Form.Item>
|
|
363
376
|
</Col>
|
|
364
377
|
);
|
|
365
378
|
})}
|