优化FormItemsRenderer

master
LiuJiaNan 2025-11-05 14:42:39 +08:00
parent dad29a0763
commit d7052bfced
2 changed files with 11 additions and 4 deletions

View File

@ -41,6 +41,8 @@ export type FormValues = Record<string, any>;
* *
*/ */
export interface FormOption { export interface FormOption {
/** React 需要的 key如果传递了唯一的 name则不需要 */
key?: string;
/** 表单项字段名 */ /** 表单项字段名 */
name?: string | string[]; name?: string | string[];
/** 表单项标签 */ /** 表单项标签 */

View File

@ -133,6 +133,11 @@ const FormItemsRenderer = ({
return option.rules ? (Array.isArray(option.rules) ? option.rules : [option.rules]) : []; return option.rules ? (Array.isArray(option.rules) ? option.rules : [option.rules]) : [];
}; };
// 获取key
const getKey = (option) => {
return option.key || option.name
}
// 渲染表单控件 // 渲染表单控件
const renderFormControl = (option) => { const renderFormControl = (option) => {
const componentProps = getComponentProps(option); const componentProps = getComponentProps(option);
@ -278,7 +283,7 @@ const FormItemsRenderer = ({
// 如果是分割线 // 如果是分割线
if (option.render === FORM_ITEM_RENDER_ENUM.DIVIDER) { if (option.render === FORM_ITEM_RENDER_ENUM.DIVIDER) {
return ( return (
<Col key={option.name || index} span={itemSpan} style={style}> <Col key={getKey(option) || index} span={itemSpan} style={style}>
<Divider orientation="left">{option.label}</Divider> <Divider orientation="left">{option.label}</Divider>
</Col> </Col>
); );
@ -291,7 +296,7 @@ const FormItemsRenderer = ({
? (renderFormControl(option)) ? (renderFormControl(option))
: ( : (
<Form.Item <Form.Item
key={option.name || index} key={getKey(option) || index}
noStyle noStyle
preserve={false} preserve={false}
shouldUpdate={option.shouldUpdate ?? option?.componentProps?.shouldUpdate} shouldUpdate={option.shouldUpdate ?? option?.componentProps?.shouldUpdate}
@ -307,7 +312,7 @@ const FormItemsRenderer = ({
return null; return null;
return ( return (
<Col key={option.name || index} span={itemSpan} style={style}> <Col key={getKey(option) || index} span={itemSpan} style={style}>
<Form.Item <Form.Item
name={option.name} name={option.name}
label={renderLabel(option)} label={renderLabel(option)}
@ -337,7 +342,7 @@ const FormItemsRenderer = ({
return null; return null;
return ( return (
<Col key={option.name || index} span={itemSpan} style={style}> <Col key={getKey(option) || index} span={itemSpan} style={style}>
{ {
option.customizeRender option.customizeRender
? (renderFormControl(option)) ? (renderFormControl(option))