Skip to content

Commit fe4be2a

Browse files
committed
fix: modal-form 初始值问题
1. openModal 与 initialValues 合并. 2. 去除告警
1 parent f41f539 commit fe4be2a

File tree

8 files changed

+115
-12
lines changed

8 files changed

+115
-12
lines changed

docs/case/antdForm.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ group:
66
title: 核心组件
77
order: 0
88
toc: content
9-
order: 1
9+
order: 5
1010
title: Antd Form
1111
debug: true
1212
---
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { useRef } from 'react';
2+
import { Button, ModalForm, ModalFormInnerRefType } from 'react-admin-kit';
3+
4+
export default function () {
5+
const innerRef = useRef<ModalFormInnerRefType>();
6+
return (
7+
<div>
8+
<Button
9+
onClick={() => innerRef.current?.openModal('edit', { name: 'Jack2' })}
10+
>
11+
打开
12+
</Button>
13+
<ModalForm
14+
formProps={{
15+
initialValues: { name: 'Jack', phone: 123 },
16+
}}
17+
innerRef={innerRef}
18+
columns={[
19+
{
20+
title: 'name',
21+
dataIndex: 'name',
22+
initialValue: 'Hello',
23+
},
24+
{
25+
title: 'phone',
26+
dataIndex: 'phone',
27+
},
28+
]}
29+
/>
30+
</div>
31+
);
32+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { SchemaForm } from 'react-admin-kit';
2+
import { BetaSchemaForm } from '@ant-design/pro-form';
3+
4+
export default function () {
5+
return (
6+
<SchemaForm
7+
initialValues={{
8+
user: 'Jack',
9+
}}
10+
columns={[
11+
{
12+
title: 'user',
13+
dataIndex: 'user',
14+
initialValue: 'Hello',
15+
},
16+
{
17+
title: 'phone',
18+
dataIndex: 'phone',
19+
initialValue: 'World',
20+
},
21+
]}
22+
/>
23+
);
24+
}

docs/case/modal-form.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
---
2+
nav:
3+
title: 测试用例
4+
order: 10
5+
group:
6+
title: 核心组件
7+
order: 0
8+
order: 20
9+
toc: content
10+
title: ModalForm
11+
debug: true
12+
---
13+
14+
15+
# SchemaForm
16+
17+
## 初始值问题
18+
columns 上有 initialValue. Form 上也有 initialValues. 这两者会合并处理吗?
19+
20+
首先我们来看看 pro-component 是如何处理的.
21+
22+
<code src="./modal-form-demo/pro-component-initial-value" ></code>
23+
24+
可以看出 Form 上的 initialValues 优先级更高. 并且他们是合并的关系. 同时控制台会有告警信息.
25+
26+
同理, ModalForm 也要遵循这个规则.
27+
28+
案例1. columns 有初始值 `{name: 'hello'}`, ModalForm 上有初始值 `{name: 'Jack', phone: 123}`, openModal 有初始值 `{name: 'Jack2'}`
29+
30+
期望1: `{name: 'Jack2', phone: 123}`
31+
32+
<code src="./modal-form-demo/modal-form-initial-value" ></code>

docs/case/proForm.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ nav:
55
group:
66
title: 核心组件
77
order: 0
8-
order: 1
8+
order: 10
99
toc: content
1010
title: ProForm
1111
debug: true

docs/case/proTable.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ group:
66
title: 核心组件
77
order: 0
88
toc: content
9-
order: 1
9+
order: 30
1010
title: ProTable
1111
debug: true
1212
---

src/ModalForm/demos/innerRef/index.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,10 @@ const InnerRefDemo = () => {
1414
return (
1515
<div>
1616
<div style={{ textAlign: 'start' }}>
17-
<Button type="primary" onClick={() => ref.current?.openModal()}>
17+
<Button
18+
type="primary"
19+
onClick={() => ref.current?.openModal('edit', { username: 'Jack' })}
20+
>
1821
打开弹窗
1922
</Button>
2023
</div>

src/ModalForm/index.tsx

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -95,15 +95,14 @@ class ModalForm extends Component<
9595
const innerRef = this.getInnerRef();
9696
innerRef.current.formType = formType;
9797

98-
if (initialData) {
99-
this.setState({ visible: true, formType, formData: initialData });
100-
return;
101-
}
98+
// 合并初始值. openModal 所携带的初始值优先级更大.
99+
const propsInitialValues = this.props.formProps?.initialValues || {};
100+
const initialValues = { ...propsInitialValues, ...initialData };
102101

103102
this.setState({
104103
visible: true,
105104
formType,
106-
formData: this.props.formProps?.initialValues || {},
105+
formData: initialValues,
107106
});
108107
};
109108

@@ -204,13 +203,26 @@ class ModalForm extends Component<
204203
};
205204

206205
getColumns = (): any => {
206+
const { formData } = this.state;
207+
const { open } = this.props;
208+
207209
const $cols = normalizeTree(
208210
this.props.columns,
209211
(item) => {
210212
/** 去掉 width 属性, 因为在表单中不需要 width */
211-
const { width, ...rest } = item;
212-
213-
return rest;
213+
const { width, initialValue, ...rest } = item;
214+
215+
// columns 上和 SchemaForm 组件的 initialValues 上不能有相同的字段, 否则会有告警.
216+
// Form already set 'initialValues' with path 'name'. Field can not overwrite it
217+
if (
218+
!open &&
219+
typeof item.dataIndex === 'string' &&
220+
formData.hasOwnProperty(item.dataIndex)
221+
) {
222+
return rest;
223+
} else {
224+
return { initialValue, ...rest };
225+
}
214226
},
215227
{ replace: true },
216228
);

0 commit comments

Comments
 (0)