/
demo11.js
146 lines (132 loc) · 3.27 KB
/
demo11.js
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
import React from 'react'
import { Form, Input, Radio, Field } from '@alicloud/console-components'
const FormItem = Form.Item
const RadioGroup = Radio.Group
const formItemLayout = {
labelCol: {
span: 6,
},
wrapperCol: {
span: 14,
},
}
const Demo11 = () => {
const myField = Field.useField()
const userExists = (rule, value) => {
return new Promise((resolve, reject) => {
if (!value) {
resolve()
} else {
setTimeout(() => {
if (value === 'frank') {
reject([new Error('Sorry, this username is already occupied.')])
} else {
resolve()
}
}, 500)
}
})
}
const checkPass = (rule, value, callback) => {
const { validate } = myField
if (value) {
validate(['rePasswd'])
}
callback()
}
const checkPass2 = (rule, value, callback) => {
const { getValue } = myField
if (value && value !== getValue('passwd')) {
callback('Inconsistent password input twice!')
} else {
callback()
}
}
const { getState, getError } = myField
return (
<Form {...formItemLayout} field={myField}>
<FormItem
label="Account:"
hasFeedback
validator={userExists}
help={
getState('username') === 'loading'
? 'Checking ...'
: getError('username')
}
>
<Input placeholder="Input frank" name="username" />
</FormItem>
<FormItem
label="Email:"
hasFeedback
required
requiredTrigger="onBlur"
format="email"
>
<Input placeholder="Both trigget onBlur and onChange" name="email" />
</FormItem>
<FormItem
label="Password:"
hasFeedback
required
requiredMessage="Please enter password"
validator={checkPass}
>
<Input htmlType="password" name="passwd" />
</FormItem>
<FormItem
label="Check your password:"
hasFeedback
required
requiredMessage="Enter your password again"
validator={checkPass2}
>
<Input
htmlType="password"
placeholder="Enter the same password twice"
name="rePasswd"
/>
</FormItem>
<FormItem
label="Gender:"
hasFeedback
required
requiredMessage="Please select your gender"
>
<RadioGroup name="sex">
<Radio value="male">Male</Radio>
<Radio value="female">Female</Radio>
</RadioGroup>
</FormItem>
<FormItem
label="Remarks:"
required
requiredMessage="Really do not intend to write anything?"
>
<Input.TextArea
maxLength={20}
hasLimitHint
placeholder="Everything is ok!"
name="textarea"
/>
</FormItem>
<FormItem wrapperCol={{ offset: 6 }}>
<Form.Submit
validate
type="primary"
onClick={(v, e) => console.log(v, e)}
style={{ marginRight: 10 }}
>
Submit
</Form.Submit>
<Form.Reset>Reset</Form.Reset>
</FormItem>
</Form>
)
}
export default Demo11
export const demoMeta = {
zhName: `复杂功能(Field)`,
zhDesc: `配合 \`Field\` 可以实现较复杂功能`,
}