FormBuilder增加默认提交重置按钮

master
LiuJiaNan 2025-10-23 18:01:20 +08:00
parent af3f3702ae
commit 91ea1c4d14
2 changed files with 51 additions and 4 deletions

View File

@ -1,6 +1,6 @@
import type { FormInstance, FormProps } from "antd/es/form";
import type { Gutter } from "antd/es/grid/row";
import type { FC } from "react";
import type { FC, ReactNode } from "react";
import type { FormOption, FormValues } from "./FormItemsRenderer";
/**
@ -16,11 +16,23 @@ export interface FormBuilderProps extends Omit<FormProps, "form"> {
/** 占据栅格列数,默认 12 */
span?: number | string;
/** 表单实例(通过 Form.useForm() 创建) */
form: FormInstance;
form?: FormInstance;
/** 自动生成必填规则,默认 true */
useAutoGenerateRequired?: boolean;
/** 表单提交回调 */
onFinish?: (values: any) => void;
onFinish?: (values: FormValues) => void;
/** 是否显示操作按钮区域,默认 true */
showActionButtons?: boolean;
/** 提交按钮文字,默认为"提交" */
submitButtonText?: string;
/** 重置按钮文字,默认为"重置" */
resetButtonText?: string;
/** 是否显示提交按钮,默认 true */
showSubmitButton?: boolean;
/** 是否显示重置按钮,默认 true */
showResetButton?: boolean;
/** 自定义操作按钮组 */
customActionButtons?: ReactNode;
}
/**

View File

@ -1,4 +1,4 @@
import { Form, Row } from "antd";
import { Button, Col, Form, Row, Space } from "antd";
import FormItemsRenderer from "./FormItemsRenderer";
/**
@ -13,9 +13,23 @@ const FormBuilder = (props) => {
labelCol = { span: 4 },
onFinish,
useAutoGenerateRequired = true,
showActionButtons = true,
submitButtonText = "提交",
resetButtonText = "重置",
showSubmitButton = true,
showResetButton = true,
customActionButtons,
form: externalForm,
...restProps
} = props;
const [internalForm] = Form.useForm();
const form = externalForm || internalForm;
const handleReset = () => {
form.resetFields();
};
return (
<Form
labelCol={labelCol}
@ -23,6 +37,7 @@ const FormBuilder = (props) => {
wrapperCol={{ span: 24 - labelCol.span }}
onFinish={onFinish}
initialValues={values}
form={form}
{...restProps}
>
<Row gutter={gutter}>
@ -32,6 +47,26 @@ const FormBuilder = (props) => {
useAutoGenerateRequired={useAutoGenerateRequired}
/>
</Row>
{showActionButtons && (
<Row gutter={gutter} style={{ marginTop: 24 }}>
<Col span={24} style={{ textAlign: "center" }}>
{customActionButtons || (
<Space>
{showSubmitButton && (
<Button type="primary" htmlType="submit">
{submitButtonText}
</Button>
)}
{showResetButton && (
<Button onClick={handleReset} style={{ marginRight: 8 }}>
{resetButtonText}
</Button>
)}
</Space>
)}
</Col>
</Row>
)}
</Form>
);
};