-
Notifications
You must be signed in to change notification settings - Fork 196
/
Copy pathform.tsx
153 lines (136 loc) · 3.76 KB
/
form.tsx
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
147
148
149
150
151
152
153
import React, { Component } from 'react';
import Select from '@rc-component/select';
import Form, { useForm, Field } from 'rc-field-form';
import TreeSelect from '../src';
import '@rc-component/select/assets/index.less';
import '../assets/index.less';
import { gData } from './utils/dataUtil';
const { Option } = Select;
const regionStyle = {
border: '1px solid red',
marginTop: 10,
padding: 10,
};
const errorStyle = {
color: 'red',
marginTop: 10,
padding: 10,
};
class TreeSelectInput extends Component<{
onChange?: (value: string[]) => void;
style: React.CSSProperties;
}> {
onChange = (value, ...args) => {
console.log(value, ...args);
const { props } = this;
if (props.onChange) {
props.onChange(value);
}
};
render() {
return <TreeSelect {...this.props} onChange={this.onChange} />;
}
}
const Demo = () => {
const [form] = useForm();
const onFinish = values => {
console.log('Submit:', values);
};
const onReset = () => {
form.resetFields();
};
const tProps = {
multiple: true,
treeData: gData,
treeCheckable: true,
};
return (
<div style={{ margin: 20 }}>
<h2>validity</h2>
<Form
form={form}
onFinish={onFinish}
initialValues={{
'tree-select': ['0-0-0-value'],
'tree-select1': ['0-0-0-value'],
select: ['jack'],
}}
>
<div style={regionStyle}>
<div>
<p style={{ color: 'blue' }}>no onChange</p>
<Field
name="tree-select"
rules={[
{
required: true,
type: 'array',
message: 'tree-select 需要必填',
},
]}
>
{(control, { errors }) => (
<div>
<TreeSelect {...tProps} {...control} style={{ width: 300 }} />
<p style={errorStyle}>{errors.join(',')}</p>
</div>
)}
</Field>
</div>
</div>
<div style={regionStyle}>
<div>
<p style={{ color: 'blue' }}>custom onChange</p>
<Field
name="tree-select1"
rules={[
{
required: true,
type: 'array',
message: 'tree-select1 需要必填',
},
]}
>
{(control, { errors }) => (
<div>
<TreeSelectInput {...tProps} {...control} style={{ width: 300 }} />
<p style={errorStyle}>{errors.join(',')}</p>
</div>
)}
</Field>
</div>
</div>
<div style={regionStyle}>
<div>
<Field
name="select"
rules={[{ required: true, type: 'array', message: 'select 需要必填' }]}
>
{(control, { errors }) => (
<div>
<Select style={{ width: 200 }} {...control} allowClear mode="multiple">
<Option value="jack">jack</Option>
<Option value="lucy">lucy</Option>
<Option value="disabled" disabled>
disabled
</Option>
<Option value="yiminghe">yiminghe</Option>
</Select>
<p style={errorStyle}>{errors.join(',')}</p>
</div>
)}
</Field>
</div>
</div>
<div style={regionStyle}>
<button type="button" onClick={onReset}>
reset
</button>
<input type="submit" value="submit" />
</div>
</Form>
</div>
);
};
export default Demo;