# antd UI库的使用
// index.js
import { PageHeaderWrapper } from '@ant-design/pro-layout';
import { connect, Dispatch } from 'umi';
import { Form, Card, Input, Radio, Select,Button } from 'antd';
const FormItem = Form.Item;
const Option = Select.Option;
interface BasicFormProps {
submitting: boolean;
dispatch: Dispatch<any>
}
const BasicForm: FC<BasicFormProps> = (props) => {
const { submitting } = props;
const [form] = Form.useForm();
const [showPublicUsers, setShowPublicUsers] = React.useState(false);
const formItemLayout = {
labelCol: {
xs: { span: 24 },
sm: { span: 7 },
},
wrapperCol: {
xs: { span: 24 },
sm: { span: 12 },
md: { span: 10 },
},
};
const submitFormLayout = {
wrapperCol: {
xs: { span: 24, offset: 0 },
sm: { span: 10, offset: 7 },
},
};
const onFinish = (values: { [key: string]: any }) => {
const { dispatch } = props;
dispatch({
type: 'formAndbasicForm/submitRegularForm',
payload: values,
});
};
const onFinishFailed = (errorInfo: any) => {
console.log('Failed:', errorInfo);
};
const onValuesChange = (changedValues: { [key: string]: any }) => {
const { publicType } = changedValues;
if (publicType) setShowPublicUsers(publicType === '2');
};
return <PageHeaderWrapper content={'头部内容'}>
<Card bordered={false}>
<Form
hideRequiredMark
style={{ marginTop: 8 }}
form={form}
name="basic"
initialValues={{ publicType: '1' }}
onFinish={onFinish}
onFinishFailed={onFinishFailed}
onValuesChange={onValuesChange}
>
<FormItem
{...formItemLayout}
label={'名字'}
name="title"
rules={[
{
required: true,
message: '请输入姓名',
},
]}
>
<Input placeholder={'请输入姓名'} />
</FormItem>
<FormItem
{...formItemLayout}
label={'测试'}
name="publicType"
>
<Input.Group compact>
<div>
<div>
<Form.Item
name={'publicType'}
noStyle
rules={[{ required: true, message: 'Province is required' }]}
>
<Radio.Group>
<Radio value="1">
public
</Radio>
<Radio value="2">
partially-public
</Radio>
<Radio value="3">
private
</Radio>
</Radio.Group>
</Form.Item>
</div>
<div>
<FormItem style={{ marginBottom: 0 }} name="publicUsers">
<Select
mode="multiple"
placeholder={'测试'}
style={{
margin: '8px 0',
display: showPublicUsers ? 'block' : 'none',
}}
>
<Option value="1">
Option1
</Option>
<Option value="2">
Option2
</Option>
<Option value="3">
Option3
</Option>
</Select>
</FormItem>
</div>
</div>
</Input.Group>
</FormItem>
<FormItem {...submitFormLayout} style={{ marginTop: 32 }}>
<Button type="primary" htmlType="submit" loading={submitting}>
提交
</Button>
<Button style={{ marginLeft: 8 }}>
保存
</Button>
</FormItem>
</Form>
</Card>
</PageHeaderWrapper>
}
export default connect(
({ loading }:{loading: { effects: { [key: string]: boolean } } }) => ({ submitting: loading.effects['formAndbasicForm/submitRegularForm'] }),
)(BasicForm)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
// model.ts
import { Effect } from 'umi';
import { message } from 'antd';
import {fakeSubmitForm} from './service';
export interface ModelType {
namespace: string;
state: {};
effects: {
submitRegularForm: Effect;
}
}
const Model: ModelType = {
namespace: 'formAndbasicForm',
state: {},
effects: {
*submitRegularForm({ payload }, { call }) {
yield call(fakeSubmitForm, payload);
message.success('提交成功!');
return false
}
}
}
export default Model
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
//serve.ts
import request from 'umi-request';
export async function fakeSubmitForm(params: any) {
return request('/api/forms', {
method: 'POST',
data: params,
});
}
1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11