BasicForm.js 8.15 KB
Newer Older
1 2
import React, { PureComponent } from 'react';
import { connect } from 'dva';
3
import { formatMessage, FormattedMessage } from 'umi/locale';
afc163's avatar
afc163 committed
4
import {
jim's avatar
jim committed
5 6 7 8 9 10 11 12 13 14
  Form,
  Input,
  DatePicker,
  Select,
  Button,
  Card,
  InputNumber,
  Radio,
  Icon,
  Tooltip,
afc163's avatar
afc163 committed
15
} from 'antd';
16
import PageHeaderWrapper from '@/components/PageHeaderWrapper';
afc163's avatar
afc163 committed
17
import styles from './style.less';
18 19

const FormItem = Form.Item;
afc163's avatar
afc163 committed
20
const { Option } = Select;
21
const { RangePicker } = DatePicker;
afc163's avatar
afc163 committed
22
const { TextArea } = Input;
23

Andreas CederstrΓΆm's avatar
Andreas CederstrΓΆm committed
24 25
@connect(({ loading }) => ({
  submitting: loading.effects['form/submitRegularForm'],
26 27
}))
@Form.create()
afc163's avatar
afc163 committed
28
class BasicForms extends PureComponent {
jim's avatar
jim committed
29
  handleSubmit = e => {
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
30
    const { dispatch, form } = this.props;
31
    e.preventDefault();
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
32
    form.validateFieldsAndScroll((err, values) => {
33
      if (!err) {
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
34
        dispatch({
35 36 37 38 39
          type: 'form/submitRegularForm',
          payload: values,
        });
      }
    });
jim's avatar
jim committed
40
  };
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
41

42 43
  render() {
    const { submitting } = this.props;
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
44 45 46
    const {
      form: { getFieldDecorator, getFieldValue },
    } = this.props;
47 48 49 50

    const formItemLayout = {
      labelCol: {
        xs: { span: 24 },
afc163's avatar
afc163 committed
51
        sm: { span: 7 },
52 53 54 55 56 57 58 59 60 61 62
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 12 },
        md: { span: 10 },
      },
    };

    const submitFormLayout = {
      wrapperCol: {
        xs: { span: 24, offset: 0 },
afc163's avatar
afc163 committed
63
        sm: { span: 10, offset: 7 },
64 65 66 67
      },
    };

    return (
68
      <PageHeaderWrapper
69 70
        title={<FormattedMessage id="app.forms.basic.title" />}
        content={<FormattedMessage id="app.forms.basic.description" />}
jim's avatar
jim committed
71
      >
72
        <Card bordered={false}>
jim's avatar
jim committed
73
          <Form onSubmit={this.handleSubmit} hideRequiredMark style={{ marginTop: 8 }}>
74
            <FormItem {...formItemLayout} label={<FormattedMessage id="form.title.label" />}>
afc163's avatar
afc163 committed
75
              {getFieldDecorator('title', {
jim's avatar
jim committed
76 77 78
                rules: [
                  {
                    required: true,
79
                    message: formatMessage({ id: 'validation.title.required' }),
jim's avatar
jim committed
80 81
                  },
                ],
82
              })(<Input placeholder={formatMessage({ id: 'form.title.placeholder' })} />)}
83
            </FormItem>
84
            <FormItem {...formItemLayout} label={<FormattedMessage id="form.date.label" />}>
afc163's avatar
afc163 committed
85
              {getFieldDecorator('date', {
jim's avatar
jim committed
86 87 88
                rules: [
                  {
                    required: true,
89
                    message: formatMessage({ id: 'validation.date.required' }),
jim's avatar
jim committed
90 91
                  },
                ],
92 93 94 95 96 97 98 99 100
              })(
                <RangePicker
                  style={{ width: '100%' }}
                  placeholder={[
                    formatMessage({ id: 'form.date.placeholder.start' }),
                    formatMessage({ id: 'form.date.placeholder.end' }),
                  ]}
                />
              )}
101
            </FormItem>
102
            <FormItem {...formItemLayout} label={<FormattedMessage id="form.goal.label" />}>
afc163's avatar
afc163 committed
103
              {getFieldDecorator('goal', {
jim's avatar
jim committed
104 105 106
                rules: [
                  {
                    required: true,
107
                    message: formatMessage({ id: 'validation.goal.required' }),
jim's avatar
jim committed
108 109
                  },
                ],
110
              })(
jim's avatar
jim committed
111 112
                <TextArea
                  style={{ minHeight: 32 }}
113
                  placeholder={formatMessage({ id: 'form.goal.placeholder' })}
jim's avatar
jim committed
114 115
                  rows={4}
                />
116 117
              )}
            </FormItem>
118
            <FormItem {...formItemLayout} label={<FormattedMessage id="form.standard.label" />}>
afc163's avatar
afc163 committed
119
              {getFieldDecorator('standard', {
jim's avatar
jim committed
120 121 122
                rules: [
                  {
                    required: true,
123
                    message: formatMessage({ id: 'validation.standard.required' }),
jim's avatar
jim committed
124 125
                  },
                ],
126 127 128 129 130 131 132
              })(
                <TextArea
                  style={{ minHeight: 32 }}
                  placeholder={formatMessage({ id: 'form.standard.placeholder' })}
                  rows={4}
                />
              )}
133 134 135
            </FormItem>
            <FormItem
              {...formItemLayout}
afc163's avatar
afc163 committed
136 137
              label={
                <span>
138
                  <FormattedMessage id="form.client.label" />
afc163's avatar
afc163 committed
139
                  <em className={styles.optional}>
140 141
                    <FormattedMessage id="form.optional" />
                    <Tooltip title={<FormattedMessage id="form.client.label.tooltip" />}>
afc163's avatar
afc163 committed
142 143 144 145 146
                      <Icon type="info-circle-o" style={{ marginRight: 4 }} />
                    </Tooltip>
                  </em>
                </span>
              }
147
            >
afc163's avatar
afc163 committed
148
              {getFieldDecorator('client')(
149
                <Input placeholder={formatMessage({ id: 'form.client.placeholder' })} />
150 151 152 153
              )}
            </FormItem>
            <FormItem
              {...formItemLayout}
jim's avatar
jim committed
154 155
              label={
                <span>
156 157 158 159
                  <FormattedMessage id="form.invites.label" />
                  <em className={styles.optional}>
                    <FormattedMessage id="form.optional" />
                  </em>
jim's avatar
jim committed
160 161
                </span>
              }
162
            >
afc163's avatar
afc163 committed
163
              {getFieldDecorator('invites')(
164
                <Input placeholder={formatMessage({ id: 'form.invites.placeholder' })} />
165 166
              )}
            </FormItem>
afc163's avatar
afc163 committed
167 168
            <FormItem
              {...formItemLayout}
jim's avatar
jim committed
169 170
              label={
                <span>
171 172 173 174
                  <FormattedMessage id="form.weight.label" />
                  <em className={styles.optional}>
                    <FormattedMessage id="form.optional" />
                  </em>
jim's avatar
jim committed
175 176
                </span>
              }
afc163's avatar
afc163 committed
177
            >
178 179 180 181 182 183 184
              {getFieldDecorator('weight')(
                <InputNumber
                  placeholder={formatMessage({ id: 'form.weight.placeholder' })}
                  min={0}
                  max={100}
                />
              )}
afc163's avatar
afc163 committed
185
              <span className="ant-form-text">%</span>
afc163's avatar
afc163 committed
186
            </FormItem>
187 188 189 190 191
            <FormItem
              {...formItemLayout}
              label={<FormattedMessage id="form.public.label" />}
              help={<FormattedMessage id="form.public.label.help" />}
            >
afc163's avatar
afc163 committed
192 193 194 195
              <div>
                {getFieldDecorator('public', {
                  initialValue: '1',
                })(
afc163's avatar
afc163 committed
196
                  <Radio.Group>
197 198 199 200 201 202 203 204 205
                    <Radio value="1">
                      <FormattedMessage id="form.public.radio.public" />
                    </Radio>
                    <Radio value="2">
                      <FormattedMessage id="form.public.radio.partially-public" />
                    </Radio>
                    <Radio value="3">
                      <FormattedMessage id="form.public.radio.private" />
                    </Radio>
afc163's avatar
afc163 committed
206 207
                  </Radio.Group>
                )}
afc163's avatar
afc163 committed
208 209 210 211
                <FormItem style={{ marginBottom: 0 }}>
                  {getFieldDecorator('publicUsers')(
                    <Select
                      mode="multiple"
212
                      placeholder={formatMessage({ id: 'form.publicUsers.placeholder' })}
afc163's avatar
afc163 committed
213 214 215 216 217
                      style={{
                        margin: '8px 0',
                        display: getFieldValue('public') === '2' ? 'block' : 'none',
                      }}
                    >
218 219 220 221 222 223 224 225 226
                      <Option value="1">
                        <FormattedMessage id="form.publicUsers.option.A" />
                      </Option>
                      <Option value="2">
                        <FormattedMessage id="form.publicUsers.option.B" />
                      </Option>
                      <Option value="3">
                        <FormattedMessage id="form.publicUsers.option.C" />
                      </Option>
afc163's avatar
afc163 committed
227 228
                    </Select>
                  )}
229
                </FormItem>
afc163's avatar
afc163 committed
230 231
              </div>
            </FormItem>
afc163's avatar
afc163 committed
232
            <FormItem {...submitFormLayout} style={{ marginTop: 32 }}>
233
              <Button type="primary" htmlType="submit" loading={submitting}>
234 235 236 237
                <FormattedMessage id="form.submit" />
              </Button>
              <Button style={{ marginLeft: 8 }}>
                <FormattedMessage id="form.save" />
238 239 240 241
              </Button>
            </FormItem>
          </Form>
        </Card>
242
      </PageHeaderWrapper>
243 244 245
    );
  }
}
lijiehua's avatar
lijiehua committed
246 247

export default BasicForms;