zy-react-library/components/FormBuilder/FormBuilder.js

75 lines
1.9 KiB
JavaScript
Raw Normal View History

import { Button, Col, Form, Row, Space } from "antd";
2025-10-22 14:43:42 +08:00
import FormItemsRenderer from "./FormItemsRenderer";
/**
* 表单构建器组件
*/
const FormBuilder = (props) => {
const {
values,
options,
gutter = 24,
span = 12,
labelCol = { span: 4 },
useAutoGenerateRequired = true,
showActionButtons = true,
submitButtonText = "提交",
cancelButtonText = "取消",
showSubmitButton = true,
showCancelButton = true,
customActionButtons,
extraActionButtons,
2025-10-22 14:43:42 +08:00
...restProps
} = props;
const handleCancel = () => {
window.history.back();
};
2025-10-22 14:43:42 +08:00
return (
<Form
labelCol={labelCol}
scrollToFirstError
wrapperCol={{ span: 24 - labelCol.span }}
initialValues={values}
2025-10-28 11:28:18 +08:00
style={{ width: `calc(100% - ${gutter * 2}px)`, margin: `0 auto` }}
2025-10-22 14:43:42 +08:00
{...restProps}
>
<Row gutter={gutter}>
<FormItemsRenderer
options={options}
2025-10-28 12:30:57 +08:00
labelCol={labelCol}
2025-10-22 14:43:42 +08:00
span={span}
useAutoGenerateRequired={useAutoGenerateRequired}
2025-10-29 09:38:36 +08:00
initialValues={values}
2025-10-22 14:43:42 +08:00
/>
</Row>
{showActionButtons && (
<Row gutter={gutter} style={{ marginTop: 24 }}>
<Col span={24} style={{ textAlign: "center" }}>
{customActionButtons || (
<Space>
{showSubmitButton && (
<Button type="primary" htmlType="submit">
{submitButtonText}
</Button>
)}
{showCancelButton && (
<Button onClick={handleCancel} style={{ marginRight: 8 }}>
{cancelButtonText}
</Button>
)}
{extraActionButtons}
</Space>
)}
</Col>
</Row>
)}
2025-10-22 14:43:42 +08:00
</Form>
);
};
FormBuilder.displayName = "FormBuilder";
export default FormBuilder;