-
-
Notifications
You must be signed in to change notification settings - Fork 490
Expand file tree
/
Copy pathcombobox.tsx
More file actions
160 lines (143 loc) · 3.82 KB
/
combobox.tsx
File metadata and controls
160 lines (143 loc) · 3.82 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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/* eslint-disable no-console */
import React from 'react';
import Select, { Option } from '@rc-component/select';
import '../../assets/index.less';
class Combobox extends React.Component {
state = {
disabled: false,
value: '',
options: [],
};
textareaRef = React.createRef<HTMLTextAreaElement>();
timeoutId: number;
componentDidMount() {
console.log('Ref:', this.textareaRef);
}
onActive = (value) => {
console.log('onActive', value);
};
onChange = (value, option) => {
console.log('onChange', value, option);
this.setState({
value,
});
};
onKeyDown = (e) => {
const { value } = this.state;
if (e.keyCode === 13) {
console.log('onEnter', value);
}
};
onSelect = (v, option) => {
console.log('onSelect', v, option);
};
onSearch = (text: string) => {
console.log('onSearch:', text);
};
onAsyncChange = (value) => {
window.clearTimeout(this.timeoutId);
this.setState({
options: [],
});
this.timeoutId = window.setTimeout(() => {
this.setState({
options: [{ value }, { value: `${value}-${value}` }],
});
}, 1000);
};
toggleDisabled = () => {
const { disabled } = this.state;
this.setState({
disabled: !disabled,
});
};
render() {
const { value, disabled } = this.state;
return (
<div>
<h2>combobox</h2>
<p>
<button type="button" onClick={this.toggleDisabled}>
toggle disabled
</button>
<button
type="button"
onClick={() => {
this.setState({ value: '' });
}}
>
reset
</button>
</p>
<Select
value={value}
mode="combobox"
onChange={this.onChange}
onActive={this.onActive}
filterOption={(inputValue, option) => {
if (!inputValue) {
return true;
}
return (option.value as string).includes(inputValue);
}}
>
{['1', '2', '3'].map((i) => (
<Option value={i} key={i}>
{i}
</Option>
))}
</Select>
<div>
<Select
disabled={disabled}
style={{ width: 500 }}
onChange={this.onChange}
onSelect={this.onSelect}
onSearch={this.onSearch}
onInputKeyDown={this.onKeyDown}
notFoundContent=""
allowClear
placeholder="please input, max len: 10"
value={value}
maxLength={10}
mode="combobox"
backfill
onFocus={() => console.log('focus')}
onBlur={() => console.log('blur')}
>
<Option value="jack">
<b style={{ color: 'red' }}>jack</b>
</Option>
<Option value="lucy">lucy</Option>
<Option value="disabled" disabled>
disabled
</Option>
<Option value="yiminghe">yiminghe</Option>
<Option value="竹林星光">竹林星光</Option>
</Select>
<h3>Customize Input Element</h3>
<Select
mode="combobox"
style={{ width: 200 }}
getInputElement={() => (
<textarea style={{ background: 'red' }} rows={3} ref={this.textareaRef} />
)}
options={[{ value: 'light' }, { value: 'bamboo' }]}
allowClear
placeholder="2333"
/>
<h3>Async Input Element</h3>
<Select
mode="combobox"
notFoundContent={null}
style={{ width: 200 }}
options={this.state.options}
onChange={this.onAsyncChange}
/>
</div>
</div>
);
}
}
export default Combobox;
/* eslint-enable */