-
-
Notifications
You must be signed in to change notification settings - Fork 279
Expand file tree
/
Copy pathvalidate-perf.tsx
More file actions
142 lines (128 loc) · 4.12 KB
/
validate-perf.tsx
File metadata and controls
142 lines (128 loc) · 4.12 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
/* eslint-disable react/prop-types, @typescript-eslint/consistent-type-imports */
import React from 'react';
import Form, { Field, FormInstance } from 'rc-field-form';
import Input from './components/Input';
import LabelField from './components/LabelField';
import { Message, ValidateMessages } from '@/interface';
const myMessages: ValidateMessages = {
default: '${name} 看起来怪怪的……',
required: '你需要一个 ${displayName}',
types: {
number: '嗨,这个 ${name} 不是一个合格的 ${type}',
},
enum: '${name} 不在 ${enum} 中呢',
whitespace: '${name} 不可以是空的啦',
pattern: {
mismatch: '${name} 并不符合格式:${pattern}',
},
};
export default class Demo extends React.Component {
private form: FormInstance;
public setForm = (form: FormInstance) => {
this.form = form;
};
public onFinish = (values: { password: string }) => {
console.log('Finish:', values);
};
public onFinishFailed = errorInfo => {
console.log('Failed:', errorInfo);
};
public onPasswordError = ({ errors }: { errors: Message[] }) => {
console.log('🐞 Password Error:', errors);
};
public render() {
return (
<div>
<h3>High Perf Validate Form</h3>
<Form
ref={this.setForm}
style={{ padding: 16 }}
onFinish={this.onFinish}
onFinishFailed={this.onFinishFailed}
validateMessages={myMessages}
initialValues={{ remember: true }}
>
<LabelField
name="password"
messageVariables={{ displayName: '密码' }}
rules={[
{ required: true },
{
warningOnly: true,
validator: async (_, value: string = '') => {
if (value.length < 6) {
throw new Error('你的 ${displayName} 太短了……');
}
},
},
]}
onMetaChange={this.onPasswordError}
>
<Input placeholder="password" />
</LabelField>
<LabelField
initialValue="123"
name="password2"
dependencies={['password']}
messageVariables={{ displayName: '密码2' }}
rules={[
{ required: true },
({ getFieldValue }) => ({
async validator(_, value) {
if (getFieldValue('password') !== value) {
return Promise.reject('password2 is not same as password');
}
return Promise.resolve();
},
}),
]}
>
<Input placeholder="password 2" />
</LabelField>
<LabelField
name="field"
label="Full of rules"
messageVariables={{ displayName: '字段' }}
rules={[
{ required: true },
{ required: true, message: <h1>我是 ReactNode</h1> },
{ type: 'number' },
{ type: 'enum', enum: ['aaa', 'bbb'] },
{ type: 'date' },
{ whitespace: true },
{ pattern: /^\w{3}$/ },
]}
>
<Input />
</LabelField>
<div>
<Field name="remember" valuePropName="checked">
<input type="checkbox" />
</Field>
Remember Me
</div>
<Field shouldUpdate>
{(_, __, { getFieldsError, isFieldsTouched }) => {
const isAllTouched = isFieldsTouched(['password', 'password2'], true);
const hasErrors = !!getFieldsError().filter(({ errors }) => errors.length).length;
return (
<button type="submit" disabled={!isAllTouched || hasErrors}>
Submit
</button>
);
}}
</Field>
<button
type="button"
onClick={() => {
this.form.resetFields();
}}
>
Reset
</button>
<button type="reset">Reset Native</button>
</Form>
</div>
);
}
}