Skip to content

Commit c6fc575

Browse files
committed
fix: 消除modal-form告警
1 parent 98b5c79 commit c6fc575

File tree

4 files changed

+99
-3
lines changed

4 files changed

+99
-3
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import { Component } from 'react';
2+
import { Button, ModalForm } from 'react-admin-kit';
3+
4+
export default class ModalFormInitialValueCase extends Component<any, any> {
5+
constructor(props) {
6+
super(props);
7+
8+
this.state = {
9+
open: false,
10+
formData: {},
11+
};
12+
}
13+
14+
render() {
15+
return (
16+
<div>
17+
<Button
18+
onClick={() =>
19+
this.setState({
20+
open: true,
21+
formData: { name: 'Jack2', phone: 123 },
22+
})
23+
}
24+
>
25+
打开
26+
</Button>
27+
<ModalForm
28+
onCancel={() => this.setState({ open: false })}
29+
open={this.state.open}
30+
formProps={{
31+
initialValues: this.state.formData,
32+
}}
33+
columns={[
34+
{
35+
title: 'name',
36+
dataIndex: 'name',
37+
initialValue: 'Hello',
38+
},
39+
{
40+
title: 'phone',
41+
dataIndex: 'phone',
42+
},
43+
]}
44+
/>
45+
</div>
46+
);
47+
}
48+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import { useRef, useState } from 'react';
2+
import { Button, ModalForm } from 'react-admin-kit';
3+
4+
export default function () {
5+
const [open, setOpen] = useState(false);
6+
const formDataRef = useRef<any>({});
7+
return (
8+
<div>
9+
<Button
10+
onClick={() => {
11+
formDataRef.current = { name: 'Jack2', phone: 123 };
12+
setOpen(true);
13+
}}
14+
>
15+
打开
16+
</Button>
17+
<ModalForm
18+
open={open}
19+
onCancel={() => setOpen(false)}
20+
formProps={{
21+
initialValues: formDataRef.current,
22+
}}
23+
columns={[
24+
{
25+
title: 'name',
26+
dataIndex: 'name',
27+
initialValue: 'Hello',
28+
},
29+
{
30+
title: 'phone',
31+
dataIndex: 'phone',
32+
},
33+
]}
34+
/>
35+
</div>
36+
);
37+
}

docs/case/modal-form.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,12 @@ columns 上有 initialValue. Form 上也有 initialValues. 这两者会合并处
2929

3030
期望1: `{name: 'Jack2', phone: 123}`
3131

32-
<code src="./modal-form-demo/modal-form-initial-value" ></code>
32+
<code src="./modal-form-demo/modal-form-initial-value" ></code>
33+
34+
案例2. 采用 open 受控模式, 并且用于 Class 组件.
35+
36+
<code src="./modal-form-demo/modal-form-initial-value-control-class" ></code>
37+
38+
案例3. 采用 open 受控模式, 用于 Function 组件.
39+
40+
<code src="./modal-form-demo/modal-form-initial-value-control" ></code>

src/ModalForm/index.tsx

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,10 @@ class ModalForm extends Component<
206206
const { formData } = this.state;
207207
const { open } = this.props;
208208

209+
// 合并初始值. openModal 所携带的初始值优先级更大.
210+
const propsInitialValues = this.props.formProps?.initialValues || {};
211+
const initialValues = { ...propsInitialValues, ...formData };
212+
209213
const $cols = normalizeTree(
210214
this.props.columns,
211215
(item) => {
@@ -215,9 +219,8 @@ class ModalForm extends Component<
215219
// columns 上和 SchemaForm 组件的 initialValues 上不能有相同的字段, 否则会有告警.
216220
// Form already set 'initialValues' with path 'name'. Field can not overwrite it
217221
if (
218-
!open &&
219222
typeof item.dataIndex === 'string' &&
220-
formData.hasOwnProperty(item.dataIndex)
223+
initialValues.hasOwnProperty(item.dataIndex)
221224
) {
222225
return rest;
223226
} else {

0 commit comments

Comments
 (0)