zy-react-library 1.0.2 → 1.0.4
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,6 +1,6 @@
|
|
|
1
1
|
import type { FormInstance, FormProps } from "antd/es/form";
|
|
2
2
|
import type { Gutter } from "antd/es/grid/row";
|
|
3
|
-
import type { FC } from "react";
|
|
3
|
+
import type { FC, ReactNode } from "react";
|
|
4
4
|
import type { FormOption, FormValues } from "./FormItemsRenderer";
|
|
5
5
|
|
|
6
6
|
/**
|
|
@@ -16,11 +16,23 @@ export interface FormBuilderProps extends Omit<FormProps, "form"> {
|
|
|
16
16
|
/** 占据栅格列数,默认 12 */
|
|
17
17
|
span?: number | string;
|
|
18
18
|
/** 表单实例(通过 Form.useForm() 创建) */
|
|
19
|
-
form
|
|
19
|
+
form?: FormInstance;
|
|
20
20
|
/** 自动生成必填规则,默认 true */
|
|
21
21
|
useAutoGenerateRequired?: boolean;
|
|
22
22
|
/** 表单提交回调 */
|
|
23
|
-
onFinish?: (values:
|
|
23
|
+
onFinish?: (values: FormValues) => void;
|
|
24
|
+
/** 是否显示操作按钮区域,默认 true */
|
|
25
|
+
showActionButtons?: boolean;
|
|
26
|
+
/** 提交按钮文字,默认为"提交" */
|
|
27
|
+
submitButtonText?: string;
|
|
28
|
+
/** 重置按钮文字,默认为"重置" */
|
|
29
|
+
resetButtonText?: string;
|
|
30
|
+
/** 是否显示提交按钮,默认 true */
|
|
31
|
+
showSubmitButton?: boolean;
|
|
32
|
+
/** 是否显示重置按钮,默认 true */
|
|
33
|
+
showResetButton?: boolean;
|
|
34
|
+
/** 自定义操作按钮组 */
|
|
35
|
+
customActionButtons?: ReactNode;
|
|
24
36
|
}
|
|
25
37
|
|
|
26
38
|
/**
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Form, Row } from "antd";
|
|
1
|
+
import { Button, Col, Form, Row, Space } from "antd";
|
|
2
2
|
import FormItemsRenderer from "./FormItemsRenderer";
|
|
3
3
|
|
|
4
4
|
/**
|
|
@@ -13,9 +13,23 @@ const FormBuilder = (props) => {
|
|
|
13
13
|
labelCol = { span: 4 },
|
|
14
14
|
onFinish,
|
|
15
15
|
useAutoGenerateRequired = true,
|
|
16
|
+
showActionButtons = true,
|
|
17
|
+
submitButtonText = "提交",
|
|
18
|
+
resetButtonText = "重置",
|
|
19
|
+
showSubmitButton = true,
|
|
20
|
+
showResetButton = true,
|
|
21
|
+
customActionButtons,
|
|
22
|
+
form: externalForm,
|
|
16
23
|
...restProps
|
|
17
24
|
} = props;
|
|
18
25
|
|
|
26
|
+
const [internalForm] = Form.useForm();
|
|
27
|
+
const form = externalForm || internalForm;
|
|
28
|
+
|
|
29
|
+
const handleReset = () => {
|
|
30
|
+
form.resetFields();
|
|
31
|
+
};
|
|
32
|
+
|
|
19
33
|
return (
|
|
20
34
|
<Form
|
|
21
35
|
labelCol={labelCol}
|
|
@@ -23,6 +37,7 @@ const FormBuilder = (props) => {
|
|
|
23
37
|
wrapperCol={{ span: 24 - labelCol.span }}
|
|
24
38
|
onFinish={onFinish}
|
|
25
39
|
initialValues={values}
|
|
40
|
+
form={form}
|
|
26
41
|
{...restProps}
|
|
27
42
|
>
|
|
28
43
|
<Row gutter={gutter}>
|
|
@@ -32,6 +47,26 @@ const FormBuilder = (props) => {
|
|
|
32
47
|
useAutoGenerateRequired={useAutoGenerateRequired}
|
|
33
48
|
/>
|
|
34
49
|
</Row>
|
|
50
|
+
{showActionButtons && (
|
|
51
|
+
<Row gutter={gutter} style={{ marginTop: 24 }}>
|
|
52
|
+
<Col span={24} style={{ textAlign: "center" }}>
|
|
53
|
+
{customActionButtons || (
|
|
54
|
+
<Space>
|
|
55
|
+
{showSubmitButton && (
|
|
56
|
+
<Button type="primary" htmlType="submit">
|
|
57
|
+
{submitButtonText}
|
|
58
|
+
</Button>
|
|
59
|
+
)}
|
|
60
|
+
{showResetButton && (
|
|
61
|
+
<Button onClick={handleReset} style={{ marginRight: 8 }}>
|
|
62
|
+
{resetButtonText}
|
|
63
|
+
</Button>
|
|
64
|
+
)}
|
|
65
|
+
</Space>
|
|
66
|
+
)}
|
|
67
|
+
</Col>
|
|
68
|
+
</Row>
|
|
69
|
+
)}
|
|
35
70
|
</Form>
|
|
36
71
|
);
|
|
37
72
|
};
|
|
@@ -90,7 +90,7 @@ const FormItemsRenderer = ({
|
|
|
90
90
|
// 支持动态计算 required
|
|
91
91
|
const required = typeof option.required === "function"
|
|
92
92
|
? option.required(form.getFieldsValue())
|
|
93
|
-
: (option.required
|
|
93
|
+
: (option.required ?? true);
|
|
94
94
|
|
|
95
95
|
if (required) {
|
|
96
96
|
const isBlurTrigger = !option.render || [
|
|
@@ -274,7 +274,7 @@ const FormItemsRenderer = ({
|
|
|
274
274
|
}
|
|
275
275
|
|
|
276
276
|
// 如果配置了 shouldUpdate 或 dependencies,使用 Form.Item 的联动机制
|
|
277
|
-
if (option.shouldUpdate
|
|
277
|
+
if ((option.shouldUpdate ?? option.dependencies) || (option?.componentProps?.shouldUpdate ?? option?.componentProps?.dependencies)) {
|
|
278
278
|
return (
|
|
279
279
|
option.customizeRender
|
|
280
280
|
? (renderFormControl(option))
|
|
@@ -282,14 +282,14 @@ const FormItemsRenderer = ({
|
|
|
282
282
|
<Col key={option.name || index} span={itemSpan} style={style}>
|
|
283
283
|
<Form.Item
|
|
284
284
|
noStyle
|
|
285
|
-
shouldUpdate={option.shouldUpdate
|
|
285
|
+
shouldUpdate={option.shouldUpdate ?? option?.componentProps?.shouldUpdate}
|
|
286
286
|
dependencies={option.dependencies || option?.componentProps?.dependencies}
|
|
287
287
|
>
|
|
288
288
|
{(form) => {
|
|
289
289
|
// 支持动态计算 hidden
|
|
290
290
|
const hidden = typeof option.hidden === "function"
|
|
291
291
|
? option.hidden(form.getFieldsValue())
|
|
292
|
-
: (option.hidden
|
|
292
|
+
: (option.hidden ?? false);
|
|
293
293
|
|
|
294
294
|
if (hidden)
|
|
295
295
|
return null;
|