|
| 1 | +<cn> |
| 2 | +#### 注册新用户 |
| 3 | +用户填写必须的信息以注册新用户。 |
| 4 | +</cn> |
| 5 | + |
| 6 | +<us> |
| 7 | +#### Registration |
| 8 | +Fill in this form to create a new account for you. |
| 9 | +</us> |
| 10 | + |
| 11 | +```html |
| 12 | +<script> |
| 13 | +import { Form } from 'vue-antd-ui' |
| 14 | +
|
| 15 | +const residences = [{ |
| 16 | + value: 'zhejiang', |
| 17 | + label: 'Zhejiang', |
| 18 | + children: [{ |
| 19 | + value: 'hangzhou', |
| 20 | + label: 'Hangzhou', |
| 21 | + children: [{ |
| 22 | + value: 'xihu', |
| 23 | + label: 'West Lake', |
| 24 | + }], |
| 25 | + }], |
| 26 | +}, { |
| 27 | + value: 'jiangsu', |
| 28 | + label: 'Jiangsu', |
| 29 | + children: [{ |
| 30 | + value: 'nanjing', |
| 31 | + label: 'Nanjing', |
| 32 | + children: [{ |
| 33 | + value: 'zhonghuamen', |
| 34 | + label: 'Zhong Hua Men', |
| 35 | + }], |
| 36 | + }], |
| 37 | +}] |
| 38 | +
|
| 39 | +const RegistrationForm = { |
| 40 | + data () { |
| 41 | + return { |
| 42 | + confirmDirty: false, |
| 43 | + autoCompleteResult: [], |
| 44 | + } |
| 45 | + }, |
| 46 | + methods: { |
| 47 | + handleSubmit (e) { |
| 48 | + e.preventDefault() |
| 49 | + this.form.validateFieldsAndScroll((err, values) => { |
| 50 | + if (!err) { |
| 51 | + console.log('Received values of form: ', values) |
| 52 | + } |
| 53 | + }) |
| 54 | + }, |
| 55 | + handleConfirmBlur (e) { |
| 56 | + const value = e.target.value |
| 57 | + this.confirmDirty = this.confirmDirty || !!value |
| 58 | + }, |
| 59 | + compareToFirstPassword (rule, value, callback) { |
| 60 | + const form = this.form |
| 61 | + if (value && value !== form.getFieldValue('password')) { |
| 62 | + callback('Two passwords that you enter is inconsistent!') |
| 63 | + } else { |
| 64 | + callback() |
| 65 | + } |
| 66 | + }, |
| 67 | + validateToNextPassword (rule, value, callback) { |
| 68 | + const form = this.form |
| 69 | + if (value && this.confirmDirty) { |
| 70 | + form.validateFields(['confirm'], { force: true }) |
| 71 | + } |
| 72 | + callback() |
| 73 | + }, |
| 74 | + handleWebsiteChange (value) { |
| 75 | + let autoCompleteResult |
| 76 | + if (!value) { |
| 77 | + autoCompleteResult = [] |
| 78 | + } else { |
| 79 | + autoCompleteResult = ['.com', '.org', '.net'].map(domain => `${value}${domain}`) |
| 80 | + } |
| 81 | + this.autoCompleteResult = autoCompleteResult |
| 82 | + }, |
| 83 | + }, |
| 84 | +
|
| 85 | + render () { |
| 86 | + const { getFieldDecorator } = this.form |
| 87 | + const { autoCompleteResult } = this |
| 88 | +
|
| 89 | + const formItemLayout = { |
| 90 | + labelCol: { |
| 91 | + xs: { span: 24 }, |
| 92 | + sm: { span: 8 }, |
| 93 | + }, |
| 94 | + wrapperCol: { |
| 95 | + xs: { span: 24 }, |
| 96 | + sm: { span: 16 }, |
| 97 | + }, |
| 98 | + } |
| 99 | + const tailFormItemLayout = { |
| 100 | + wrapperCol: { |
| 101 | + xs: { |
| 102 | + span: 24, |
| 103 | + offset: 0, |
| 104 | + }, |
| 105 | + sm: { |
| 106 | + span: 16, |
| 107 | + offset: 8, |
| 108 | + }, |
| 109 | + }, |
| 110 | + } |
| 111 | + const prefixSelector = getFieldDecorator('prefix', { |
| 112 | + initialValue: '86', |
| 113 | + })( |
| 114 | + <a-select style={{ width: '70px' }}> |
| 115 | + <a-select-option value='86'>+86</a-select-option> |
| 116 | + <a-select-option value='87'>+87</a-select-option> |
| 117 | + </a-select> |
| 118 | + ) |
| 119 | +
|
| 120 | + const websiteOptions = autoCompleteResult.map(website => ( |
| 121 | + <a-select-option key={website}>{website}</a-select-option> |
| 122 | + )) |
| 123 | +
|
| 124 | + return ( |
| 125 | + <a-form onSubmit={this.handleSubmit}> |
| 126 | + <a-form-item |
| 127 | + {...{ props: formItemLayout }} |
| 128 | + label='E-mail' |
| 129 | + > |
| 130 | + {getFieldDecorator('email', { |
| 131 | + rules: [{ |
| 132 | + type: 'email', message: 'The input is not valid E-mail!', |
| 133 | + }, { |
| 134 | + required: true, message: 'Please input your E-mail!', |
| 135 | + }], |
| 136 | + })( |
| 137 | + <a-input /> |
| 138 | + )} |
| 139 | + </a-form-item> |
| 140 | + <a-form-item |
| 141 | + {...{ props: formItemLayout }} |
| 142 | + label='Password' |
| 143 | + > |
| 144 | + {getFieldDecorator('password', { |
| 145 | + rules: [{ |
| 146 | + required: true, message: 'Please input your password!', |
| 147 | + }, { |
| 148 | + validator: this.validateToNextPassword, |
| 149 | + }], |
| 150 | + })( |
| 151 | + <a-input type='password' /> |
| 152 | + )} |
| 153 | + </a-form-item> |
| 154 | + <a-form-item |
| 155 | + {...{ props: formItemLayout }} |
| 156 | + label='Confirm Password' |
| 157 | + > |
| 158 | + {getFieldDecorator('confirm', { |
| 159 | + rules: [{ |
| 160 | + required: true, message: 'Please confirm your password!', |
| 161 | + }, { |
| 162 | + validator: this.compareToFirstPassword, |
| 163 | + }], |
| 164 | + })( |
| 165 | + <a-input type='password' onBlur={this.handleConfirmBlur} /> |
| 166 | + )} |
| 167 | + </a-form-item> |
| 168 | + <a-form-item |
| 169 | + {...{ props: formItemLayout }} |
| 170 | + label={( |
| 171 | + <span> |
| 172 | + Nickname |
| 173 | + <a-tooltip title='What do you want others to call you?'> |
| 174 | + <a-icon type='question-circle-o' /> |
| 175 | + </a-tooltip> |
| 176 | + </span> |
| 177 | + )} |
| 178 | + > |
| 179 | + {getFieldDecorator('nickname', { |
| 180 | + rules: [{ required: true, message: 'Please input your nickname!', whitespace: true }], |
| 181 | + })( |
| 182 | + <a-input /> |
| 183 | + )} |
| 184 | + </a-form-item> |
| 185 | + <a-form-item |
| 186 | + {...{ props: formItemLayout }} |
| 187 | + label='Habitual Residence' |
| 188 | + > |
| 189 | + {getFieldDecorator('residence', { |
| 190 | + initialValue: ['zhejiang', 'hangzhou', 'xihu'], |
| 191 | + rules: [{ type: 'array', required: true, message: 'Please select your habitual residence!' }], |
| 192 | + })( |
| 193 | + <a-cascader options={residences} /> |
| 194 | + )} |
| 195 | + </a-form-item> |
| 196 | + <a-form-item |
| 197 | + {...{ props: formItemLayout }} |
| 198 | + label='Phone Number' |
| 199 | + > |
| 200 | + {getFieldDecorator('phone', { |
| 201 | + rules: [{ required: true, message: 'Please input your phone number!' }], |
| 202 | + })( |
| 203 | + <a-input addonBefore={prefixSelector} style={{ width: '100%' }} /> |
| 204 | + )} |
| 205 | + </a-form-item> |
| 206 | + <a-form-item |
| 207 | + {...{ props: formItemLayout }} |
| 208 | + label='Website' |
| 209 | + > |
| 210 | + {getFieldDecorator('website', { |
| 211 | + rules: [{ required: true, message: 'Please input website!' }], |
| 212 | + })( |
| 213 | + <a-auto-complete |
| 214 | + dataSource={websiteOptions} |
| 215 | + onChange={this.handleWebsiteChange} |
| 216 | + placeholder='website' |
| 217 | + > |
| 218 | + <a-input /> |
| 219 | + </a-auto-complete> |
| 220 | + )} |
| 221 | + </a-form-item> |
| 222 | + <a-form-item |
| 223 | + {...{ props: formItemLayout }} |
| 224 | + label='Captcha' |
| 225 | + extra='We must make sure that your are a human.' |
| 226 | + > |
| 227 | + <a-row gutter={8}> |
| 228 | + <a-col span={12}> |
| 229 | + {getFieldDecorator('captcha', { |
| 230 | + rules: [{ required: true, message: 'Please input the captcha you got!' }], |
| 231 | + })( |
| 232 | + <a-input /> |
| 233 | + )} |
| 234 | + </a-col> |
| 235 | + <a-col span={12}> |
| 236 | + <a-button>Get captcha</a-button> |
| 237 | + </a-col> |
| 238 | + </a-row> |
| 239 | + </a-form-item> |
| 240 | + <a-form-item {...{ props: tailFormItemLayout }}> |
| 241 | + {getFieldDecorator('agreement', { |
| 242 | + valuePropName: 'checked', |
| 243 | + })( |
| 244 | + <a-checkbox>I have read the <a href=''>agreement</a></a-checkbox> |
| 245 | + )} |
| 246 | + </a-form-item> |
| 247 | + <a-form-item {...{ props: tailFormItemLayout }}> |
| 248 | + <a-button type='primary' htmlType='submit'>Register</a-button> |
| 249 | + </a-form-item> |
| 250 | + </a-form> |
| 251 | + ) |
| 252 | + }, |
| 253 | +} |
| 254 | +
|
| 255 | +export default Form.create()(RegistrationForm) |
| 256 | +</script> |
| 257 | +``` |
| 258 | + |
| 259 | + |
| 260 | + |
0 commit comments