FormBuilder增加默认提交重置按钮
parent
af3f3702ae
commit
91ea1c4d14
|
|
@ -1,6 +1,6 @@
|
||||||
import type { FormInstance, FormProps } from "antd/es/form";
|
import type { FormInstance, FormProps } from "antd/es/form";
|
||||||
import type { Gutter } from "antd/es/grid/row";
|
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";
|
import type { FormOption, FormValues } from "./FormItemsRenderer";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -16,11 +16,23 @@ export interface FormBuilderProps extends Omit<FormProps, "form"> {
|
||||||
/** 占据栅格列数,默认 12 */
|
/** 占据栅格列数,默认 12 */
|
||||||
span?: number | string;
|
span?: number | string;
|
||||||
/** 表单实例(通过 Form.useForm() 创建) */
|
/** 表单实例(通过 Form.useForm() 创建) */
|
||||||
form: FormInstance;
|
form?: FormInstance;
|
||||||
/** 自动生成必填规则,默认 true */
|
/** 自动生成必填规则,默认 true */
|
||||||
useAutoGenerateRequired?: boolean;
|
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
import { Form, Row } from "antd";
|
import { Button, Col, Form, Row, Space } from "antd";
|
||||||
import FormItemsRenderer from "./FormItemsRenderer";
|
import FormItemsRenderer from "./FormItemsRenderer";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -13,9 +13,23 @@ const FormBuilder = (props) => {
|
||||||
labelCol = { span: 4 },
|
labelCol = { span: 4 },
|
||||||
onFinish,
|
onFinish,
|
||||||
useAutoGenerateRequired = true,
|
useAutoGenerateRequired = true,
|
||||||
|
showActionButtons = true,
|
||||||
|
submitButtonText = "提交",
|
||||||
|
resetButtonText = "重置",
|
||||||
|
showSubmitButton = true,
|
||||||
|
showResetButton = true,
|
||||||
|
customActionButtons,
|
||||||
|
form: externalForm,
|
||||||
...restProps
|
...restProps
|
||||||
} = props;
|
} = props;
|
||||||
|
|
||||||
|
const [internalForm] = Form.useForm();
|
||||||
|
const form = externalForm || internalForm;
|
||||||
|
|
||||||
|
const handleReset = () => {
|
||||||
|
form.resetFields();
|
||||||
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Form
|
<Form
|
||||||
labelCol={labelCol}
|
labelCol={labelCol}
|
||||||
|
|
@ -23,6 +37,7 @@ const FormBuilder = (props) => {
|
||||||
wrapperCol={{ span: 24 - labelCol.span }}
|
wrapperCol={{ span: 24 - labelCol.span }}
|
||||||
onFinish={onFinish}
|
onFinish={onFinish}
|
||||||
initialValues={values}
|
initialValues={values}
|
||||||
|
form={form}
|
||||||
{...restProps}
|
{...restProps}
|
||||||
>
|
>
|
||||||
<Row gutter={gutter}>
|
<Row gutter={gutter}>
|
||||||
|
|
@ -32,6 +47,26 @@ const FormBuilder = (props) => {
|
||||||
useAutoGenerateRequired={useAutoGenerateRequired}
|
useAutoGenerateRequired={useAutoGenerateRequired}
|
||||||
/>
|
/>
|
||||||
</Row>
|
</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>
|
</Form>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue