forked from alibaba/ice
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCreateActivityForm.jsx
More file actions
210 lines (197 loc) · 6.02 KB
/
CreateActivityForm.jsx
File metadata and controls
210 lines (197 loc) · 6.02 KB
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
import React, { Component } from 'react';
import IceContainer from '@icedesign/container';
import {
FormBinderWrapper as IceFormBinderWrapper,
FormBinder as IceFormBinder,
FormError as IceFormError,
} from '@icedesign/form-binder';
import {
Form,
Input,
Button,
Checkbox,
Select,
DatePicker,
Switch,
Radio,
} from '@icedesign/base';
import './CreateActivityForm.scss';
// Form 和 FormItem 用于描述表单 UI 结构
// FormBinder 用于获取表单组件的数据,通过标准受控 API value 和 onChange 来双向操作数据
// Inpput 等为具体表单组件
const FormItem = Form.Item;
const formItemLayout = {
labelCol: {
span: 4,
},
wrapperCol: {
span: 14,
},
};
const CheckboxGroup = Checkbox.Group;
const RadioGroup = Radio.Group;
const { RangePicker } = DatePicker;
// Switch 组件的选中等 props 是 checked 不符合表单规范的 value 在此做转换
const SwitchForForm = (props) => {
const checked = props.checked === undefined ? props.value : props.checked;
return (
<Switch
{...props}
checked={checked}
onChange={(currentChecked) => {
if (props.onChange) props.onChange(currentChecked);
}}
/>
);
};
export default class CreateActivityForm extends Component {
static displayName = 'CreateActivityForm';
static defaultProps = {};
constructor(props) {
super(props);
this.state = {
value: {
name: '',
area: 'location1',
time: [],
delivery: false,
type: ['地推活动'],
resource: '线下场地免费',
extra: '',
},
};
}
onFormChange = (value) => {
this.setState({
value,
});
};
reset = () => {
this.setState({
value: {
name: '',
area: 'location1',
time: [],
delivery: false,
type: ['地推活动'],
resource: '线下场地免费',
extra: '',
},
});
};
submit = () => {
this.formRef.validateAll((error, value) => {
console.log('error', error, 'value', value);
if (error) {
// 处理表单报错
}
// 提交当前填写的数据
});
};
render() {
return (
<div className="create-activity-form">
<IceContainer title="活动发布" style={styles.container}>
<IceFormBinderWrapper
ref={(formRef) => {
this.formRef = formRef;
}}
value={this.state.value}
onChange={this.onFormChange}
>
<Form direction="ver">
<FormItem label="活动名称:" required {...formItemLayout}>
<IceFormBinder name="name" required message="活动名称必须填写">
<Input />
</IceFormBinder>
<IceFormError name="name" />
</FormItem>
<FormItem label="活动区域:" {...formItemLayout}>
<IceFormBinder name="area">
<Select
className="next-form-text-align"
dataSource={[
{ label: '区域一', value: 'location1' },
{ label: '区域二', value: 'location2' },
]}
/>
</IceFormBinder>
</FormItem>
<FormItem label="活动时间:" {...formItemLayout}>
<IceFormBinder
name="time"
type="array"
// 使用 RangePicker 组件输出的第二个参数字符串格式的日期
valueFormatter={(date, dateStr) => dateStr}
>
<RangePicker showTime />
</IceFormBinder>
</FormItem>
<FormItem label="即时配送:" {...formItemLayout}>
<IceFormBinder name="delivery">
<SwitchForForm />
</IceFormBinder>
</FormItem>
<FormItem label="活动性质:" {...formItemLayout}>
<IceFormBinder
name="type"
len={2}
type="array"
message="活动性质必须选择两个"
>
<CheckboxGroup
className="next-form-text-align"
dataSource={[
{ label: '美食线上活动', value: '美食线上活动' },
{ label: '地推活动', value: '地推活动' },
{ label: '线下主题活动', value: '线下主题活动' },
{ label: '单纯品牌曝光', value: '单纯品牌曝光' },
]}
/>
</IceFormBinder>
<div>
<IceFormError name="type" />
</div>
</FormItem>
<FormItem label="特殊资源:" {...formItemLayout}>
<IceFormBinder name="resource">
<RadioGroup
className="next-form-text-align"
dataSource={[
{ label: '线上品牌商赞助', value: '线上品牌商赞助' },
{ label: '线下场地免费', value: '线下场地免费' },
]}
/>
</IceFormBinder>
</FormItem>
<FormItem label="活动形式:" {...formItemLayout}>
<IceFormBinder name="extra">
<Input multiple />
</IceFormBinder>
</FormItem>
<FormItem label=" " {...formItemLayout}>
<Button type="primary" onClick={this.submit}>
立即创建
</Button>
<Button style={styles.resetBtn} onClick={this.reset}>
重置
</Button>
</FormItem>
</Form>
</IceFormBinderWrapper>
</IceContainer>
</div>
);
}
}
const styles = {
container: {
paddingBottom: 0,
},
label: {
textAlign: 'right',
},
resetBtn: {
marginLeft: 20,
},
};