|
1 | | -/* eslint-disable no-console */ |
2 | | -import React from 'react'; |
3 | | -import Select, { Option } from 'rc-select'; |
4 | | -import '../../assets/index.less'; |
| 1 | +import Select from '@/Select'; |
| 2 | +import { useRef } from 'react'; |
5 | 3 |
|
6 | | -class Combobox extends React.Component { |
7 | | - state = { |
8 | | - disabled: false, |
9 | | - value: '', |
10 | | - options: [], |
| 4 | +const Demo: React.FC = () => { |
| 5 | + const ref = useRef<HTMLInputElement>(null); |
| 6 | + const onSelect = () => { |
| 7 | + ref.current!.blur(); |
11 | 8 | }; |
12 | | - |
13 | | - textareaRef = React.createRef<HTMLTextAreaElement>(); |
14 | | - |
15 | | - timeoutId: number; |
16 | | - |
17 | | - componentDidMount() { |
18 | | - console.log('Ref:', this.textareaRef); |
19 | | - } |
20 | | - |
21 | | - onChange = (value, option) => { |
22 | | - console.log('onChange', value, option); |
23 | | - this.setState({ |
24 | | - value, |
25 | | - }); |
26 | | - }; |
27 | | - |
28 | | - onKeyDown = (e) => { |
29 | | - const { value } = this.state; |
30 | | - if (e.keyCode === 13) { |
31 | | - console.log('onEnter', value); |
32 | | - } |
33 | | - }; |
34 | | - |
35 | | - onSelect = (v, option) => { |
36 | | - console.log('onSelect', v, option); |
37 | | - }; |
38 | | - |
39 | | - onSearch = (text: string) => { |
40 | | - console.log('onSearch:', text); |
| 9 | + const getInputElement = () => { |
| 10 | + return <input ref={ref} />; |
41 | 11 | }; |
42 | | - |
43 | | - onAsyncChange = (value) => { |
44 | | - window.clearTimeout(this.timeoutId); |
45 | | - |
46 | | - this.setState({ |
47 | | - options: [], |
48 | | - }); |
49 | | - |
50 | | - this.timeoutId = window.setTimeout(() => { |
51 | | - this.setState({ |
52 | | - options: [{ value }, { value: `${value}-${value}` }], |
53 | | - }); |
54 | | - }, 1000); |
55 | | - }; |
56 | | - |
57 | | - toggleDisabled = () => { |
58 | | - const { disabled } = this.state; |
59 | | - this.setState({ |
60 | | - disabled: !disabled, |
61 | | - }); |
62 | | - }; |
63 | | - |
64 | | - render() { |
65 | | - const { value, disabled } = this.state; |
66 | | - return ( |
67 | | - <div> |
68 | | - <h2>combobox</h2> |
69 | | - <p> |
70 | | - <button type="button" onClick={this.toggleDisabled}> |
71 | | - toggle disabled |
72 | | - </button> |
73 | | - <button |
74 | | - type="button" |
75 | | - onClick={() => { |
76 | | - this.setState({ value: '' }); |
77 | | - }} |
78 | | - > |
79 | | - reset |
80 | | - </button> |
81 | | - </p> |
82 | | - <Select |
83 | | - value={value} |
84 | | - mode="combobox" |
85 | | - onChange={this.onChange} |
86 | | - filterOption={(inputValue, option) => { |
87 | | - if (!inputValue) { |
88 | | - return true; |
89 | | - } |
90 | | - return (option.value as string).includes(inputValue); |
91 | | - }} |
92 | | - > |
93 | | - {['1', '2', '3'].map((i) => ( |
94 | | - <Option value={i} key={i}> |
95 | | - {i} |
96 | | - </Option> |
97 | | - ))} |
98 | | - </Select> |
99 | | - <div> |
100 | | - <Select |
101 | | - disabled={disabled} |
102 | | - style={{ width: 500 }} |
103 | | - onChange={this.onChange} |
104 | | - onSelect={this.onSelect} |
105 | | - onSearch={this.onSearch} |
106 | | - onInputKeyDown={this.onKeyDown} |
107 | | - notFoundContent="" |
108 | | - allowClear |
109 | | - placeholder="please input, max len: 10" |
110 | | - value={value} |
111 | | - maxLength={10} |
112 | | - mode="combobox" |
113 | | - backfill |
114 | | - onFocus={() => console.log('focus')} |
115 | | - onBlur={() => console.log('blur')} |
116 | | - > |
117 | | - <Option value="jack"> |
118 | | - <b style={{ color: 'red' }}>jack</b> |
119 | | - </Option> |
120 | | - <Option value="lucy">lucy</Option> |
121 | | - <Option value="disabled" disabled> |
122 | | - disabled |
123 | | - </Option> |
124 | | - <Option value="yiminghe">yiminghe</Option> |
125 | | - <Option value="竹林星光">竹林星光</Option> |
126 | | - </Select> |
127 | | - |
128 | | - <h3>Customize Input Element</h3> |
129 | | - <Select |
130 | | - mode="combobox" |
131 | | - style={{ width: 200 }} |
132 | | - getInputElement={() => ( |
133 | | - <textarea style={{ background: 'red' }} rows={3} ref={this.textareaRef} /> |
134 | | - )} |
135 | | - options={[{ value: 'light' }, { value: 'bamboo' }]} |
136 | | - allowClear |
137 | | - placeholder="2333" |
138 | | - /> |
139 | | - |
140 | | - <h3>Async Input Element</h3> |
141 | | - <Select |
142 | | - mode="combobox" |
143 | | - notFoundContent={null} |
144 | | - style={{ width: 200 }} |
145 | | - options={this.state.options} |
146 | | - onChange={this.onAsyncChange} |
147 | | - /> |
148 | | - </div> |
149 | | - </div> |
150 | | - ); |
151 | | - } |
152 | | -} |
153 | | - |
154 | | -export default Combobox; |
155 | | -/* eslint-enable */ |
| 12 | + return ( |
| 13 | + <Select |
| 14 | + options={[{ value: 'aa' }, { value: 'bb' }]} |
| 15 | + onSelect={onSelect} |
| 16 | + mode="combobox" |
| 17 | + getInputElement={getInputElement} |
| 18 | + /> |
| 19 | + ); |
| 20 | +}; |
| 21 | + |
| 22 | +export default Demo; |
0 commit comments