From 4b7bb143abdf939bb0a31cfeb4e401f153dde372 Mon Sep 17 00:00:00 2001 From: LiuJiaNan <15703339975@163.com> Date: Wed, 29 Oct 2025 09:38:36 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96FormBuilder=E5=92=8CTable?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- components/FormBuilder/FormBuilder.js | 1 + components/FormBuilder/FormItemsRenderer.d.ts | 2 ++ components/FormBuilder/FormItemsRenderer.js | 28 +++++++++++++++---- components/Search/index.js | 1 + components/Table/index.js | 3 +- components/Table/index.less | 3 ++ 6 files changed, 31 insertions(+), 7 deletions(-) create mode 100644 components/Table/index.less diff --git a/components/FormBuilder/FormBuilder.js b/components/FormBuilder/FormBuilder.js index cb62a08..5ce9005 100644 --- a/components/FormBuilder/FormBuilder.js +++ b/components/FormBuilder/FormBuilder.js @@ -47,6 +47,7 @@ const FormBuilder = (props) => { labelCol={labelCol} span={span} useAutoGenerateRequired={useAutoGenerateRequired} + initialValues={values} /> {showActionButtons && ( diff --git a/components/FormBuilder/FormItemsRenderer.d.ts b/components/FormBuilder/FormItemsRenderer.d.ts index 2db7432..af6ebdf 100644 --- a/components/FormBuilder/FormItemsRenderer.d.ts +++ b/components/FormBuilder/FormItemsRenderer.d.ts @@ -91,6 +91,8 @@ export interface FormItemsRendererProps { collapse?: boolean; /** 自动生成必填规则,默认 true */ useAutoGenerateRequired?: boolean; + /** 初始值,用于在表单未初始化时提供默认值 */ + initialValues?: FormValues; } /** diff --git a/components/FormBuilder/FormItemsRenderer.js b/components/FormBuilder/FormItemsRenderer.js index 8897ba6..6910ee9 100644 --- a/components/FormBuilder/FormItemsRenderer.js +++ b/components/FormBuilder/FormItemsRenderer.js @@ -26,20 +26,31 @@ const FormItemsRenderer = ({ span = 12, collapse = false, useAutoGenerateRequired = true, + initialValues, }) => { const form = Form.useFormInstance(); + // 获取表单值,优先使用 initialValues + const getFormValues = () => { + const formValues = form.getFieldsValue(); + // 如果表单值为空但有 initialValues,则使用 initialValues + if (Object.keys(formValues).length === 0 && initialValues) { + return initialValues; + } + return formValues; + }; + // 获取传给组件的属性 const getComponentProps = (option) => { return typeof option.componentProps === "function" - ? option.componentProps(form.getFieldsValue()) + ? option.componentProps(getFormValues()) : (option.componentProps || {}); }; // 获取传给formItem的属性 const getFormItemProps = (option) => { const formItemProps = typeof option.formItemProps === "function" - ? option.formItemProps(form.getFieldsValue()) + ? option.formItemProps(getFormValues()) : (option.formItemProps || {}); // 为日期组件添加特殊处理 @@ -90,7 +101,7 @@ const FormItemsRenderer = ({ // 支持动态计算 required const required = typeof option.required === "function" - ? option.required(form.getFieldsValue()) + ? option.required(getFormValues()) : (option.required ?? true); if (required) { @@ -290,10 +301,10 @@ const FormItemsRenderer = ({ shouldUpdate={option.shouldUpdate ?? option?.componentProps?.shouldUpdate} dependencies={option.dependencies || option?.componentProps?.dependencies} > - {(form) => { + {() => { // 支持动态计算 hidden const hidden = typeof option.hidden === "function" - ? option.hidden(form.getFieldsValue()) + ? option.hidden(getFormValues()) : (option.hidden ?? false); if (hidden) @@ -319,7 +330,12 @@ const FormItemsRenderer = ({ } // 普通表单项(静态配置) - if (option.hidden) + // 支持动态计算 hidden + const hidden = typeof option.hidden === "function" + ? option.hidden(getFormValues()) + : (option.hidden ?? false); + + if (hidden) return null; return ( diff --git a/components/Search/index.js b/components/Search/index.js index 5d96988..71f0b84 100644 --- a/components/Search/index.js +++ b/components/Search/index.js @@ -84,6 +84,7 @@ const Search = (props) => { span={6} collapse={collapse} useAutoGenerateRequired={false} + initialValues={values} /> diff --git a/components/Table/index.js b/components/Table/index.js index 28da486..d2c2a8d 100644 --- a/components/Table/index.js +++ b/components/Table/index.js @@ -1,12 +1,13 @@ import Table from "@cqsjjb/jjb-react-admin-component/Table"; import { getIndexColumn } from "../../utils/index"; +import "./index.less"; function TablePro(props) { const { columns = [], showIndex = true, useAlignCenter = true, - rowKey = 'id', + rowKey = "id", ...restProps } = props; diff --git a/components/Table/index.less b/components/Table/index.less new file mode 100644 index 0000000..dfd97ff --- /dev/null +++ b/components/Table/index.less @@ -0,0 +1,3 @@ +.@{ant-prefix}-table-cell-scrollbar { + width: 15px; +}