Skip to content

Commit aaabc2a

Browse files
authored
fix: fixed Modal.Form ts problems (#350)
* fix: fixed Modal.Form ts problems * feat: issuse ts name * feat: modifying the ts definition
1 parent 9db3a6e commit aaabc2a

File tree

5 files changed

+104
-26
lines changed

5 files changed

+104
-26
lines changed

src/modal/demos/advance.tsx

+18-7
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,21 @@ import React, { useState } from 'react';
22
import { Button, Form, Input, InputNumber, Table } from 'antd';
33
import { Modal } from 'dt-react-component';
44

5-
interface Data {
6-
key: string;
5+
interface FormValues {
76
name: string;
87
age: number;
98
address: string;
109
}
1110

12-
const data: Data[] = [
11+
interface TableData extends FormValues {
12+
key: string;
13+
}
14+
15+
interface CustomModalFormProps {
16+
customAttr: string;
17+
}
18+
19+
const data: TableData[] = [
1320
{
1421
key: '1',
1522
name: 'UED',
@@ -24,9 +31,12 @@ const data: Data[] = [
2431
},
2532
];
2633

27-
const ModalForm = Modal.Form((_props) => {
34+
const ModalForm = Modal.Form<CustomModalFormProps, FormValues>((props) => {
2835
return (
2936
<>
37+
<Form.Item label="我是自定义参数" name={'name'} initialValue={props.customAttr}>
38+
<Input disabled />
39+
</Form.Item>
3040
<Form.Item label="name" name={'name'}>
3141
<Input />
3242
</Form.Item>
@@ -43,7 +53,7 @@ const ModalForm = Modal.Form((_props) => {
4353
export default () => {
4454
const [visible, setVisible] = useState(false);
4555
const [index, setIndex] = useState<number>(0);
46-
const [dataSource, setDataSource] = useState<Data[]>(data);
56+
const [dataSource, setDataSource] = useState<TableData[]>(data);
4757

4858
const columns = [
4959
{
@@ -65,7 +75,7 @@ export default () => {
6575
title: '操作',
6676
dataIndex: 'operate',
6777
key: 'operate',
68-
render: (_: void, record: Data, index: number) => {
78+
render: (_: void, _record: TableData, index: number) => {
6979
return (
7080
<Button
7181
onClick={() => {
@@ -81,7 +91,7 @@ export default () => {
8191
},
8292
];
8393

84-
const onSubmit = (values: Data) => {
94+
const onSubmit = (values: FormValues) => {
8595
dataSource.splice(index, 0, { ...values, key: new Date() + '' });
8696
setDataSource([...dataSource]);
8797

@@ -98,6 +108,7 @@ export default () => {
98108
visible={visible}
99109
onCancel={changeVisible}
100110
onSubmit={onSubmit}
111+
customAttr={'customAttr'}
101112
/>
102113
</>
103114
);

src/modal/demos/basics.tsx

+18-4
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,24 @@ import React, { useState } from 'react';
22
import { Button, Form, Input } from 'antd';
33
import { Modal } from 'dt-react-component';
44

5-
const ModalForm = Modal.Form((_props) => {
5+
interface FormValues {
6+
username: string;
7+
}
8+
9+
interface CustomModalFormProps {
10+
customAttr: string;
11+
}
12+
13+
const ModalForm = Modal.Form<CustomModalFormProps, FormValues>((props) => {
614
return (
7-
<Form.Item label="username" name={'username'} rules={[{ max: 10 }]}>
8-
<Input />
9-
</Form.Item>
15+
<>
16+
<Form.Item label="我是自定义参数" name={'name'} initialValue={props.customAttr}>
17+
<Input disabled />
18+
</Form.Item>
19+
<Form.Item label="username" name={'username'} rules={[{ max: 10 }]}>
20+
<Input />
21+
</Form.Item>
22+
</>
1023
);
1124
});
1225

@@ -22,6 +35,7 @@ export default () => {
2235
onSubmit={(value) => {
2336
console.log(value);
2437
}}
38+
customAttr={'customAttr'}
2539
/>
2640
</>
2741
);

src/modal/demos/classComponent.tsx

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import React, { useState } from 'react';
2+
import { Button, Form, Input } from 'antd';
3+
import { Modal } from 'dt-react-component';
4+
import { IModalFormProps } from '../form';
5+
6+
interface FormValue {
7+
username: string;
8+
}
9+
10+
interface CustomModalFormProps {
11+
customAttr: string;
12+
}
13+
14+
type FormItemsProps = CustomModalFormProps & IModalFormProps<FormValue>;
15+
16+
class FormItems extends React.Component<FormItemsProps> {
17+
render(): React.ReactNode {
18+
return (
19+
<>
20+
<Form.Item
21+
label="我是自定义参数"
22+
name={'name'}
23+
initialValue={this.props.customAttr}
24+
>
25+
<Input disabled />
26+
</Form.Item>
27+
<Form.Item label="username" name={'username'} rules={[{ max: 10 }]}>
28+
<Input />
29+
</Form.Item>
30+
</>
31+
);
32+
}
33+
}
34+
35+
const ModalForm = Modal.Form<CustomModalFormProps, FormValue>(FormItems);
36+
37+
export default () => {
38+
const [visible, setVisible] = useState(false);
39+
return (
40+
<>
41+
<Button onClick={() => setVisible(true)}>click</Button>
42+
<ModalForm
43+
title="BasicModalForm"
44+
visible={visible}
45+
onCancel={() => setVisible((v) => !v)}
46+
onSubmit={(value) => {
47+
console.log(value);
48+
}}
49+
customAttr={'customAttr'}
50+
/>
51+
</>
52+
);
53+
};

src/modal/form.tsx

+13-14
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
11
import React, { ReactElement, useMemo } from 'react';
2-
import { Modal, FormProps, ModalFuncProps } from 'antd';
2+
import { Modal, FormProps, ModalProps } from 'antd';
33
import { FORM_PROPS, MODAL_PROPS } from '../utils/antdProps';
44
import Utils from '../utils';
55
import Form from '../form';
66

7-
export interface IModalFormProps<Values = any> extends FormProps, ModalFuncProps {
8-
/**
9-
* modal title
10-
* @param {string}
11-
*/
12-
title?: string;
7+
export interface IModalFormProps<Values = any>
8+
extends Omit<FormProps, 'title'>,
9+
Omit<ModalProps, 'children'> {
1310
/**
1411
* modal className
1512
* @param {string}
@@ -22,7 +19,6 @@ export interface IModalFormProps<Values = any> extends FormProps, ModalFuncProps
2219
* @returns
2320
*/
2421
onSubmit?: (values: Values) => void;
25-
[key: string]: any;
2622
}
2723

2824
const ModalForm = (props: IModalFormProps) => {
@@ -50,8 +46,8 @@ const ModalForm = (props: IModalFormProps) => {
5046
} catch (error) {}
5147
};
5248

53-
const onCancel = () => {
54-
props.onCancel?.();
49+
const onCancel = (e: React.MouseEvent<HTMLElement, MouseEvent>) => {
50+
props.onCancel?.(e);
5551
};
5652

5753
const afterClose = () => {
@@ -76,11 +72,14 @@ const ModalForm = (props: IModalFormProps) => {
7672
);
7773
};
7874

79-
const InternalForm = (FormComponent: React.ComponentType) => {
80-
return (props: IModalFormProps) => (
75+
function InternalForm<P = any, V = any>(
76+
FormComponent: React.ComponentType<IModalFormProps<V> & P>
77+
): React.FC<IModalFormProps<V> & P> {
78+
return (props: IModalFormProps<V> & P) => (
8179
<ModalForm {...props}>
82-
<FormComponent />
80+
<FormComponent {...(props as IModalFormProps<V> & P)} />
8381
</ModalForm>
8482
);
85-
};
83+
}
84+
8685
export default InternalForm;

src/modal/index.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ demo:
1717
所用使用方式同 `Ant Design` 的 Modal 组件,新增 `Modal.Form` 组件
1818

1919
<code src="./demos/basics.tsx" title="基础使用"></code>
20-
<code src="./demos/advance.tsx" title="进阶使用"></code>
20+
<code src="./demos/advance.tsx" title="接收函数组件"></code>
21+
<code src="./demos/classComponent.tsx" title="接收类组件"></code>
2122

2223
## API
2324

0 commit comments

Comments
 (0)