forked from react-component/select
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathselect-content-option.tsx
More file actions
186 lines (177 loc) · 5.19 KB
/
select-content-option.tsx
File metadata and controls
186 lines (177 loc) · 5.19 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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
import React from 'react';
import Select, { Option } from '@rc-component/select';
import '../../assets/index.less';
const Demo: React.FC = () => {
const [value, setValue] = React.useState<string>('red');
const [dynamicOptions, setDynamicOptions] = React.useState<
{ value: string; label: string; style?: React.CSSProperties }[]
>([
{
value: 'custom-1',
label: 'Custom Option 1',
style: { color: '#1890ff' },
},
{
value: 'custom-2',
label: 'Custom Option 2',
style: { color: '#52c41a' },
},
]);
const handleSearch = (searchValue: string) => {
if (searchValue && !dynamicOptions.find((opt) => opt.value === searchValue)) {
setDynamicOptions([
...dynamicOptions,
{
value: searchValue,
label: searchValue,
style: { color: '#faad14' },
},
]);
}
};
return (
<div style={{ margin: 20 }}>
<h2>Option Style & ClassName for Selected Item</h2>
<p>
When an option has <code>style</code> or <code>className</code> props, they will be applied
to the selected item display.
</p>
<div style={{ marginBottom: 24 }}>
<h3>Basic Usage with Style</h3>
<Select
value={value}
style={{ width: 200 }}
onChange={(val) => setValue(val as string)}
options={[
{
value: 'red',
label: 'Red Color',
style: { color: 'red', fontWeight: 'bold' },
},
{
value: 'blue',
label: 'Blue Color',
style: { color: 'blue', fontWeight: 'bold' },
},
{
value: 'green',
label: 'Green Color',
style: { color: 'green', fontWeight: 'bold' },
},
{
value: 'normal',
label: 'Normal (no style)',
},
]}
/>
</div>
<div style={{ marginBottom: 24 }}>
<h3>With ClassName</h3>
<Select
defaultValue="styled"
style={{ width: 200 }}
options={[
{
value: 'styled',
label: 'Styled Option',
className: 'custom-option-class',
style: { background: '#e6f7ff', border: '1px solid #1890ff' },
},
{
value: 'normal',
label: 'Normal Option',
},
]}
/>
</div>
<div style={{ marginBottom: 24 }}>
<h3>With Title (Tooltip)</h3>
<Select
defaultValue="with-title"
style={{ width: 200 }}
options={[
{
value: 'with-title',
label: 'Hover me!',
title: 'This is a custom tooltip for this option',
style: { color: 'purple' },
},
{
value: 'without-title',
label: 'No Title',
style: { color: 'orange' },
},
]}
/>
</div>
<div style={{ marginBottom: 24 }}>
<h3>Using Option Children Syntax</h3>
<Select defaultValue="option1" style={{ width: 200 }}>
<Option
value="option1"
style={{ color: 'orange' }}
className="custom-className"
title="Custom title for option 1"
>
Option 1
</Option>
<Option value="option2" style={{ color: 'pink' }}>
Option 2
</Option>
<Option value="option3">Option 3 (no style)</Option>
</Select>
</div>
<div style={{ marginBottom: 24 }}>
<h3>Root Title Override</h3>
<Select
value="override"
title="This title overrides option title"
style={{ width: 200 }}
options={[
{
value: 'override',
label: 'Hover to see title',
title: 'Option title (will be overridden)',
style: { color: 'teal' },
},
]}
/>
<h3>Custom Input Element with getInputElement (combobox mode)</h3>
<div style={{ marginBottom: 24 }}>
<p>
Use <code>getInputElement</code> to customize the input element. This only works with{' '}
<code>mode="combobox"</code>. Type to add new options dynamically.
</p>
<Select
mode="combobox"
style={{ width: 300 }}
suffixIcon={null}
showSearch
onSearch={handleSearch}
classNames={{
prefix: 'custom-prefix',
suffix: 'custom-suffix',
}}
styles={{
prefix: { marginRight: 8 },
suffix: { marginLeft: 8 },
}}
getInputElement={() => (
<input
style={{
border: '2px solid #1890ff',
borderRadius: 4,
padding: '4px 8px',
outline: 'none',
}}
placeholder="Type to add new option"
/>
)}
options={dynamicOptions}
/>
</div>
</div>
</div>
);
};
export default Demo;