|
| 1 | +import React from 'react'; |
| 2 | +import { |
| 3 | + Row, |
| 4 | + Col, |
| 5 | + Input, |
| 6 | + Select, |
| 7 | + DatePicker, |
| 8 | + Button, |
| 9 | + TimePicker, |
| 10 | + Cascader, |
| 11 | + InputNumber, |
| 12 | +} from 'antd'; |
| 13 | +import { Icon, Form } from '../../src'; |
| 14 | + |
| 15 | +const { Option } = Select; |
| 16 | + |
| 17 | +const LAYOUT = { |
| 18 | + ROW: { gutter: 24 }, |
| 19 | + COL: { span: 8 }, |
| 20 | + NORMAL: { |
| 21 | + labelCol: { span: 3 }, |
| 22 | + wrapperCol: { span: 21 }, |
| 23 | + }, |
| 24 | + WIDTH: { |
| 25 | + labelCol: { span: 6 }, |
| 26 | + wrapperCol: { span: 18 }, |
| 27 | + }, |
| 28 | +}; |
| 29 | + |
| 30 | +const { MonthPicker, RangePicker } = DatePicker; |
| 31 | + |
| 32 | +const formItemLayout = { |
| 33 | + labelCol: { |
| 34 | + xs: { span: 24 }, |
| 35 | + sm: { span: 5 }, |
| 36 | + }, |
| 37 | + wrapperCol: { |
| 38 | + xs: { span: 24 }, |
| 39 | + sm: { span: 12 }, |
| 40 | + }, |
| 41 | +}; |
| 42 | + |
| 43 | +const config = { |
| 44 | + rules: [{ type: 'object', required: true, message: 'Please select time!' }], |
| 45 | +}; |
| 46 | +const rangeConfig = { |
| 47 | + rules: [{ type: 'array', required: true, message: 'Please select time!' }], |
| 48 | +}; |
| 49 | + |
| 50 | +class FormVertical extends React.Component<any> { |
| 51 | + onSubmit = async event => { |
| 52 | + event.preventDefault(); |
| 53 | + const { validateFields } = this.props.form; |
| 54 | + const values = await validateFields(); |
| 55 | + console.log(values); |
| 56 | + }; |
| 57 | + |
| 58 | + render() { |
| 59 | + const { getFieldDecorator } = this.props.form; |
| 60 | + return ( |
| 61 | + <div> |
| 62 | + <Form layout="vertical"> |
| 63 | + <Row {...LAYOUT.ROW}> |
| 64 | + <Col {...LAYOUT.COL}> |
| 65 | + <Form.Item label="input"> |
| 66 | + {getFieldDecorator('input')(<Input placeholder="请输入" />)} |
| 67 | + </Form.Item> |
| 68 | + </Col> |
| 69 | + <Col {...LAYOUT.COL}> |
| 70 | + <Form.Item label="select"> |
| 71 | + {getFieldDecorator('select')( |
| 72 | + <Select defaultValue="1"> |
| 73 | + <Option value="1">Option 1</Option> |
| 74 | + <Option value="2">Option 2</Option> |
| 75 | + <Option value="3">Option 3</Option> |
| 76 | + </Select>, |
| 77 | + )} |
| 78 | + </Form.Item> |
| 79 | + </Col> |
| 80 | + <Col {...LAYOUT.COL}> |
| 81 | + <Form.Item label="input2"> |
| 82 | + {getFieldDecorator('input2')(<Input placeholder="请输入" />)} |
| 83 | + </Form.Item> |
| 84 | + </Col> |
| 85 | + </Row> |
| 86 | + </Form> |
| 87 | + <div |
| 88 | + style={{ |
| 89 | + maxWidth: 800, |
| 90 | + }} |
| 91 | + > |
| 92 | + <Form layout="inline" onSubmit={this.onSubmit}> |
| 93 | + <Form.Item hasFeedback validateStatus="warning"> |
| 94 | + {getFieldDecorator('username', { |
| 95 | + rules: [{ required: true, message: 'Please input your username!' }], |
| 96 | + })( |
| 97 | + <Input |
| 98 | + prefix={<Icon type="user" style={{ color: 'rgba(0,0,0,.25)' }} />} |
| 99 | + placeholder="Username" |
| 100 | + />, |
| 101 | + )} |
| 102 | + </Form.Item> |
| 103 | + <Form.Item> |
| 104 | + {getFieldDecorator('password', { |
| 105 | + rules: [{ required: true, message: 'Please input your Password!' }], |
| 106 | + })( |
| 107 | + <Input |
| 108 | + prefix={<Icon type="lock" style={{ color: 'rgba(0,0,0,.25)' }} />} |
| 109 | + type="password" |
| 110 | + placeholder="Password" |
| 111 | + />, |
| 112 | + )} |
| 113 | + </Form.Item> |
| 114 | + <Form.Item> |
| 115 | + <Button type="primary" htmlType="submit"> |
| 116 | + Log in |
| 117 | + </Button> |
| 118 | + </Form.Item> |
| 119 | + </Form> |
| 120 | + <Form layout="horizontal"> |
| 121 | + <Form.Item label="Field A"> |
| 122 | + <Input placeholder="input placeholder" /> |
| 123 | + </Form.Item> |
| 124 | + <Form.Item label="Field B"> |
| 125 | + <Input placeholder="input placeholder" /> |
| 126 | + </Form.Item> |
| 127 | + </Form> |
| 128 | + <Form layout="vertical"> |
| 129 | + <Form.Item label="Field A"> |
| 130 | + <Input placeholder="input placeholder" /> |
| 131 | + </Form.Item> |
| 132 | + <Form.Item label="Field B"> |
| 133 | + <Input placeholder="input placeholder" /> |
| 134 | + </Form.Item> |
| 135 | + </Form> |
| 136 | + <Form layout="inline"> |
| 137 | + <Form.Item label="Field A"> |
| 138 | + <Input placeholder="input placeholder" /> |
| 139 | + </Form.Item> |
| 140 | + <Form.Item label="Field B"> |
| 141 | + <Input placeholder="input placeholder" /> |
| 142 | + </Form.Item> |
| 143 | + </Form> |
| 144 | + <Form {...formItemLayout}> |
| 145 | + <Form.Item |
| 146 | + label="Fail" |
| 147 | + validateStatus="error" |
| 148 | + help="Should be combination of numbers & alphabets" |
| 149 | + > |
| 150 | + <Input placeholder="unavailable choice" id="error" /> |
| 151 | + </Form.Item> |
| 152 | + |
| 153 | + <Form.Item label="Warning" validateStatus="warning"> |
| 154 | + <Input placeholder="Warning" id="warning" /> |
| 155 | + </Form.Item> |
| 156 | + |
| 157 | + <Form.Item |
| 158 | + label="Validating" |
| 159 | + hasFeedback |
| 160 | + validateStatus="validating" |
| 161 | + help="The information is being validated..." |
| 162 | + > |
| 163 | + <Input placeholder="I'm the content is being validated" id="validating" /> |
| 164 | + </Form.Item> |
| 165 | + |
| 166 | + <Form.Item label="Success" hasFeedback validateStatus="success"> |
| 167 | + <Input placeholder="I'm the content" id="success" /> |
| 168 | + </Form.Item> |
| 169 | + |
| 170 | + <Form.Item label="Warning" hasFeedback validateStatus="warning"> |
| 171 | + <Input placeholder="Warning" id="warning2" /> |
| 172 | + </Form.Item> |
| 173 | + |
| 174 | + <Form.Item |
| 175 | + label="Fail" |
| 176 | + hasFeedback |
| 177 | + validateStatus="error" |
| 178 | + help="Should be combination of numbers & alphabets" |
| 179 | + > |
| 180 | + <Input placeholder="unavailable choice" id="error2" /> |
| 181 | + </Form.Item> |
| 182 | + |
| 183 | + <Form.Item label="Success" hasFeedback validateStatus="success"> |
| 184 | + <DatePicker style={{ width: '100%' }} /> |
| 185 | + </Form.Item> |
| 186 | + |
| 187 | + <Form.Item label="Warning" hasFeedback validateStatus="warning"> |
| 188 | + <TimePicker style={{ width: '100%' }} /> |
| 189 | + </Form.Item> |
| 190 | + |
| 191 | + <Form.Item label="Error" hasFeedback validateStatus="error"> |
| 192 | + <Select defaultValue="1"> |
| 193 | + <Option value="1">Option 1</Option> |
| 194 | + <Option value="2">Option 2</Option> |
| 195 | + <Option value="3">Option 3</Option> |
| 196 | + </Select> |
| 197 | + </Form.Item> |
| 198 | + |
| 199 | + <Form.Item |
| 200 | + label="Validating" |
| 201 | + hasFeedback |
| 202 | + validateStatus="validating" |
| 203 | + help="The information is being validated..." |
| 204 | + > |
| 205 | + <Cascader defaultValue={['1']} options={[]} /> |
| 206 | + </Form.Item> |
| 207 | + |
| 208 | + <Form.Item label="inline" style={{ marginBottom: 0 }}> |
| 209 | + <Form.Item |
| 210 | + validateStatus="error" |
| 211 | + help="Please select the correct date" |
| 212 | + style={{ display: 'inline-block', width: 'calc(50% - 12px)' }} |
| 213 | + > |
| 214 | + <DatePicker /> |
| 215 | + </Form.Item> |
| 216 | + <span |
| 217 | + style={{ |
| 218 | + display: 'inline-block', |
| 219 | + width: '24px', |
| 220 | + textAlign: 'center', |
| 221 | + }} |
| 222 | + > |
| 223 | + - |
| 224 | + </span> |
| 225 | + <Form.Item style={{ display: 'inline-block', width: 'calc(50% - 12px)' }}> |
| 226 | + <DatePicker /> |
| 227 | + </Form.Item> |
| 228 | + </Form.Item> |
| 229 | + |
| 230 | + <Form.Item label="Success" hasFeedback validateStatus="success"> |
| 231 | + <InputNumber style={{ width: '100%' }} /> |
| 232 | + </Form.Item> |
| 233 | + |
| 234 | + <Form.Item label="Success" hasFeedback validateStatus="success"> |
| 235 | + <Input allowClear placeholder="with allowClear" /> |
| 236 | + </Form.Item> |
| 237 | + |
| 238 | + <Form.Item label="Warning" hasFeedback validateStatus="warning"> |
| 239 | + <Input.Password placeholder="with input password" /> |
| 240 | + </Form.Item> |
| 241 | + |
| 242 | + <Form.Item label="Error" hasFeedback validateStatus="error"> |
| 243 | + <Input.Password allowClear placeholder="with input password and allowClear" /> |
| 244 | + </Form.Item> |
| 245 | + <Form.Item name="date-picker" label="DatePicker" {...config}> |
| 246 | + <DatePicker /> |
| 247 | + </Form.Item> |
| 248 | + <Form.Item name="date-time-picker" label="DatePicker[showTime]" {...config}> |
| 249 | + <DatePicker showTime format="YYYY-MM-DD HH:mm:ss" /> |
| 250 | + </Form.Item> |
| 251 | + <Form.Item name="month-picker" label="MonthPicker" {...config}> |
| 252 | + <MonthPicker /> |
| 253 | + </Form.Item> |
| 254 | + <Form.Item name="range-picker" label="RangePicker" {...rangeConfig}> |
| 255 | + <RangePicker /> |
| 256 | + </Form.Item> |
| 257 | + <Form.Item name="range-time-picker" label="RangePicker[showTime]" {...rangeConfig}> |
| 258 | + <RangePicker showTime format="YYYY-MM-DD HH:mm:ss" /> |
| 259 | + </Form.Item> |
| 260 | + <Form.Item name="time-picker" label="TimePicker" {...config}> |
| 261 | + <TimePicker /> |
| 262 | + </Form.Item> |
| 263 | + <Form.Item |
| 264 | + wrapperCol={{ |
| 265 | + xs: { span: 24, offset: 0 }, |
| 266 | + sm: { span: 16, offset: 8 }, |
| 267 | + }} |
| 268 | + > |
| 269 | + <Button type="primary" htmlType="submit"> |
| 270 | + Submit |
| 271 | + </Button> |
| 272 | + </Form.Item> |
| 273 | + </Form> |
| 274 | + </div> |
| 275 | + </div> |
| 276 | + ); |
| 277 | + } |
| 278 | +} |
| 279 | +export default Form.create()(FormVertical); |
0 commit comments