@smilodon/react 1.4.10 → 1.4.11
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/COMPLETE-GUIDE.md +1359 -0
- package/README.md +70 -1
- package/dist/index.cjs +112 -23
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +112 -23
- package/dist/index.js.map +1 -1
- package/dist/types/Select.d.ts +31 -1
- package/dist/types/Select.d.ts.map +1 -1
- package/dist/types/index.d.ts +1 -1
- package/dist/types/index.d.ts.map +1 -1
- package/package.json +8 -53
|
@@ -0,0 +1,1359 @@
|
|
|
1
|
+
# @smilodon/react - Complete Guide
|
|
2
|
+
|
|
3
|
+
**Production-ready, accessible Select component for React applications**
|
|
4
|
+
|
|
5
|
+
This guide provides comprehensive documentation for using Smilodon Select in React applications, covering all features, styling options, and advanced use cases.
|
|
6
|
+
|
|
7
|
+
---
|
|
8
|
+
|
|
9
|
+
## Table of Contents
|
|
10
|
+
|
|
11
|
+
1. [Installation & Setup](#installation--setup)
|
|
12
|
+
2. [Basic Usage](#basic-usage)
|
|
13
|
+
3. [Complete Props Reference](#complete-props-reference)
|
|
14
|
+
4. [Input Formats](#input-formats)
|
|
15
|
+
5. [Single Selection](#single-selection)
|
|
16
|
+
6. [Multi-Selection](#multi-selection)
|
|
17
|
+
7. [Searchable Select](#searchable-select)
|
|
18
|
+
8. [Grouped Options](#grouped-options)
|
|
19
|
+
9. [Disabled States](#disabled-states)
|
|
20
|
+
10. [Event Handling](#event-handling)
|
|
21
|
+
11. [Styling & Theming](#styling--theming)
|
|
22
|
+
12. [Custom Renderers](#custom-renderers)
|
|
23
|
+
13. [Performance Optimization](#performance-optimization)
|
|
24
|
+
14. [TypeScript Integration](#typescript-integration)
|
|
25
|
+
15. [Accessibility](#accessibility)
|
|
26
|
+
16. [Advanced Patterns](#advanced-patterns)
|
|
27
|
+
17. [Troubleshooting](#troubleshooting)
|
|
28
|
+
|
|
29
|
+
---
|
|
30
|
+
|
|
31
|
+
## Installation & Setup
|
|
32
|
+
|
|
33
|
+
### Installation
|
|
34
|
+
|
|
35
|
+
```bash
|
|
36
|
+
npm install @smilodon/react @smilodon/core
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
or with yarn:
|
|
40
|
+
|
|
41
|
+
```bash
|
|
42
|
+
yarn add @smilodon/react @smilodon/core
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
or with pnpm:
|
|
46
|
+
|
|
47
|
+
```bash
|
|
48
|
+
pnpm add @smilodon/react @smilodon/core
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
### Basic Import
|
|
52
|
+
|
|
53
|
+
```tsx
|
|
54
|
+
import { Select } from '@smilodon/react';
|
|
55
|
+
import type { SelectItem } from '@smilodon/core';
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
---
|
|
59
|
+
|
|
60
|
+
## Basic Usage
|
|
61
|
+
|
|
62
|
+
### Minimal Example
|
|
63
|
+
|
|
64
|
+
```tsx
|
|
65
|
+
import { Select } from '@smilodon/react';
|
|
66
|
+
import { useState } from 'react';
|
|
67
|
+
|
|
68
|
+
function App() {
|
|
69
|
+
const [value, setValue] = useState('');
|
|
70
|
+
|
|
71
|
+
return (
|
|
72
|
+
<Select
|
|
73
|
+
items={['Apple', 'Banana', 'Cherry']}
|
|
74
|
+
value={value}
|
|
75
|
+
onChange={(newValue) => setValue(newValue as string)}
|
|
76
|
+
/>
|
|
77
|
+
);
|
|
78
|
+
}
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
### Controlled Component
|
|
82
|
+
|
|
83
|
+
```tsx
|
|
84
|
+
import { Select } from '@smilodon/react';
|
|
85
|
+
import { useState } from 'react';
|
|
86
|
+
import type { SelectItem } from '@smilodon/core';
|
|
87
|
+
|
|
88
|
+
function ControlledSelect() {
|
|
89
|
+
const [value, setValue] = useState<string | number>('');
|
|
90
|
+
|
|
91
|
+
const items: SelectItem[] = [
|
|
92
|
+
{ value: 'apple', label: 'Apple' },
|
|
93
|
+
{ value: 'banana', label: 'Banana' },
|
|
94
|
+
{ value: 'cherry', label: 'Cherry' },
|
|
95
|
+
];
|
|
96
|
+
|
|
97
|
+
return (
|
|
98
|
+
<div>
|
|
99
|
+
<Select
|
|
100
|
+
items={items}
|
|
101
|
+
value={value}
|
|
102
|
+
onChange={(newValue) => setValue(newValue as string | number)}
|
|
103
|
+
placeholder="Select a fruit..."
|
|
104
|
+
/>
|
|
105
|
+
|
|
106
|
+
{value && <p>Selected: {value}</p>}
|
|
107
|
+
</div>
|
|
108
|
+
);
|
|
109
|
+
}
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
---
|
|
113
|
+
|
|
114
|
+
## Complete Props Reference
|
|
115
|
+
|
|
116
|
+
### All Available Props
|
|
117
|
+
|
|
118
|
+
```tsx
|
|
119
|
+
interface SelectProps {
|
|
120
|
+
// Required
|
|
121
|
+
items: SelectItem[] | string[] | number[];
|
|
122
|
+
|
|
123
|
+
// Value Management
|
|
124
|
+
value?: string | number | (string | number)[];
|
|
125
|
+
onChange?: (value: string | number | (string | number)[]) => void;
|
|
126
|
+
|
|
127
|
+
// Behavior
|
|
128
|
+
multiple?: boolean;
|
|
129
|
+
searchable?: boolean;
|
|
130
|
+
disabled?: boolean;
|
|
131
|
+
placeholder?: string;
|
|
132
|
+
|
|
133
|
+
// Display
|
|
134
|
+
maxHeight?: number;
|
|
135
|
+
estimatedItemHeight?: number;
|
|
136
|
+
|
|
137
|
+
// Styling
|
|
138
|
+
className?: string;
|
|
139
|
+
style?: React.CSSProperties;
|
|
140
|
+
|
|
141
|
+
// Advanced
|
|
142
|
+
virtualization?: boolean;
|
|
143
|
+
customRenderer?: (item: SelectItem, index: number) => React.ReactNode;
|
|
144
|
+
|
|
145
|
+
// Events
|
|
146
|
+
onOpen?: () => void;
|
|
147
|
+
onClose?: () => void;
|
|
148
|
+
onSearch?: (query: string) => void;
|
|
149
|
+
}
|
|
150
|
+
```
|
|
151
|
+
|
|
152
|
+
### Props Details
|
|
153
|
+
|
|
154
|
+
| Prop | Type | Default | Description |
|
|
155
|
+
|------|------|---------|-------------|
|
|
156
|
+
| `items` | `SelectItem[] \| string[] \| number[]` | **Required** | Array of items to display |
|
|
157
|
+
| `value` | `string \| number \| array` | `undefined` | Current selected value(s) |
|
|
158
|
+
| `onChange` | `(value) => void` | `undefined` | Called when selection changes |
|
|
159
|
+
| `multiple` | `boolean` | `false` | Enable multi-selection |
|
|
160
|
+
| `searchable` | `boolean` | `true` | Enable search functionality |
|
|
161
|
+
| `disabled` | `boolean` | `false` | Disable the select |
|
|
162
|
+
| `placeholder` | `string` | `'Select...'` | Placeholder text |
|
|
163
|
+
| `maxHeight` | `number` | `300` | Max dropdown height (px) |
|
|
164
|
+
| `estimatedItemHeight` | `number` | `48` | Estimated item height for virtualization |
|
|
165
|
+
| `className` | `string` | `undefined` | CSS class name |
|
|
166
|
+
| `style` | `React.CSSProperties` | `undefined` | Inline styles (CSS variables) |
|
|
167
|
+
| `virtualization` | `boolean` | `true` | Enable virtual scrolling |
|
|
168
|
+
| `customRenderer` | `function` | `undefined` | Custom option renderer |
|
|
169
|
+
| `onOpen` | `() => void` | `undefined` | Called when dropdown opens |
|
|
170
|
+
| `onClose` | `() => void` | `undefined` | Called when dropdown closes |
|
|
171
|
+
| `onSearch` | `(query) => void` | `undefined` | Called on search input |
|
|
172
|
+
|
|
173
|
+
---
|
|
174
|
+
|
|
175
|
+
## Input Formats
|
|
176
|
+
|
|
177
|
+
Smilodon accepts three input formats for maximum flexibility:
|
|
178
|
+
|
|
179
|
+
### 1. Object Array (SelectItem[])
|
|
180
|
+
|
|
181
|
+
```tsx
|
|
182
|
+
const items: SelectItem[] = [
|
|
183
|
+
{ value: 'us', label: 'United States' },
|
|
184
|
+
{ value: 'ca', label: 'Canada' },
|
|
185
|
+
{ value: 'mx', label: 'Mexico' },
|
|
186
|
+
];
|
|
187
|
+
|
|
188
|
+
<Select items={items} value={value} onChange={setValue} />
|
|
189
|
+
```
|
|
190
|
+
|
|
191
|
+
### 2. String Array (Auto-converted)
|
|
192
|
+
|
|
193
|
+
```tsx
|
|
194
|
+
const items = ['Apple', 'Banana', 'Cherry', 'Date'];
|
|
195
|
+
|
|
196
|
+
<Select items={items} value={value} onChange={setValue} />
|
|
197
|
+
```
|
|
198
|
+
|
|
199
|
+
Automatically converted to:
|
|
200
|
+
```tsx
|
|
201
|
+
[
|
|
202
|
+
{ value: 'Apple', label: 'Apple' },
|
|
203
|
+
{ value: 'Banana', label: 'Banana' },
|
|
204
|
+
{ value: 'Cherry', label: 'Cherry' },
|
|
205
|
+
{ value: 'Date', label: 'Date' },
|
|
206
|
+
]
|
|
207
|
+
```
|
|
208
|
+
|
|
209
|
+
### 3. Number Array (Auto-converted)
|
|
210
|
+
|
|
211
|
+
```tsx
|
|
212
|
+
const items = [1, 2, 3, 5, 8, 13, 21];
|
|
213
|
+
|
|
214
|
+
<Select items={items} value={value} onChange={setValue} />
|
|
215
|
+
```
|
|
216
|
+
|
|
217
|
+
Automatically converted to:
|
|
218
|
+
```tsx
|
|
219
|
+
[
|
|
220
|
+
{ value: 1, label: '1' },
|
|
221
|
+
{ value: 2, label: '2' },
|
|
222
|
+
{ value: 3, label: '3' },
|
|
223
|
+
// ...
|
|
224
|
+
]
|
|
225
|
+
```
|
|
226
|
+
|
|
227
|
+
---
|
|
228
|
+
|
|
229
|
+
## Single Selection
|
|
230
|
+
|
|
231
|
+
### Basic Single Select
|
|
232
|
+
|
|
233
|
+
```tsx
|
|
234
|
+
import { Select } from '@smilodon/react';
|
|
235
|
+
import { useState } from 'react';
|
|
236
|
+
|
|
237
|
+
function SingleSelect() {
|
|
238
|
+
const [country, setCountry] = useState('');
|
|
239
|
+
|
|
240
|
+
const countries = [
|
|
241
|
+
{ value: 'us', label: 'United States' },
|
|
242
|
+
{ value: 'ca', label: 'Canada' },
|
|
243
|
+
{ value: 'uk', label: 'United Kingdom' },
|
|
244
|
+
{ value: 'au', label: 'Australia' },
|
|
245
|
+
];
|
|
246
|
+
|
|
247
|
+
return (
|
|
248
|
+
<Select
|
|
249
|
+
items={countries}
|
|
250
|
+
value={country}
|
|
251
|
+
onChange={(val) => setCountry(val as string)}
|
|
252
|
+
placeholder="Select your country"
|
|
253
|
+
/>
|
|
254
|
+
);
|
|
255
|
+
}
|
|
256
|
+
```
|
|
257
|
+
|
|
258
|
+
### With Form Integration
|
|
259
|
+
|
|
260
|
+
```tsx
|
|
261
|
+
import { Select } from '@smilodon/react';
|
|
262
|
+
import { useState } from 'react';
|
|
263
|
+
|
|
264
|
+
function UserForm() {
|
|
265
|
+
const [formData, setFormData] = useState({
|
|
266
|
+
name: '',
|
|
267
|
+
country: '',
|
|
268
|
+
language: '',
|
|
269
|
+
});
|
|
270
|
+
|
|
271
|
+
const handleCountryChange = (value: string | number) => {
|
|
272
|
+
setFormData(prev => ({ ...prev, country: value as string }));
|
|
273
|
+
};
|
|
274
|
+
|
|
275
|
+
return (
|
|
276
|
+
<form onSubmit={(e) => {
|
|
277
|
+
e.preventDefault();
|
|
278
|
+
console.log('Form data:', formData);
|
|
279
|
+
}}>
|
|
280
|
+
<label>
|
|
281
|
+
Name:
|
|
282
|
+
<input
|
|
283
|
+
type="text"
|
|
284
|
+
value={formData.name}
|
|
285
|
+
onChange={(e) => setFormData(prev => ({ ...prev, name: e.target.value }))}
|
|
286
|
+
/>
|
|
287
|
+
</label>
|
|
288
|
+
|
|
289
|
+
<label>
|
|
290
|
+
Country:
|
|
291
|
+
<Select
|
|
292
|
+
items={['USA', 'Canada', 'Mexico', 'UK', 'Australia']}
|
|
293
|
+
value={formData.country}
|
|
294
|
+
onChange={handleCountryChange}
|
|
295
|
+
placeholder="Select country"
|
|
296
|
+
/>
|
|
297
|
+
</label>
|
|
298
|
+
|
|
299
|
+
<button type="submit">Submit</button>
|
|
300
|
+
</form>
|
|
301
|
+
);
|
|
302
|
+
}
|
|
303
|
+
```
|
|
304
|
+
|
|
305
|
+
---
|
|
306
|
+
|
|
307
|
+
## Multi-Selection
|
|
308
|
+
|
|
309
|
+
### Basic Multi-Select
|
|
310
|
+
|
|
311
|
+
```tsx
|
|
312
|
+
import { Select } from '@smilodon/react';
|
|
313
|
+
import { useState } from 'react';
|
|
314
|
+
|
|
315
|
+
function MultiSelect() {
|
|
316
|
+
const [languages, setLanguages] = useState<(string | number)[]>([]);
|
|
317
|
+
|
|
318
|
+
const items = [
|
|
319
|
+
{ value: 'js', label: 'JavaScript' },
|
|
320
|
+
{ value: 'ts', label: 'TypeScript' },
|
|
321
|
+
{ value: 'py', label: 'Python' },
|
|
322
|
+
{ value: 'rs', label: 'Rust' },
|
|
323
|
+
{ value: 'go', label: 'Go' },
|
|
324
|
+
];
|
|
325
|
+
|
|
326
|
+
return (
|
|
327
|
+
<div>
|
|
328
|
+
<Select
|
|
329
|
+
items={items}
|
|
330
|
+
value={languages}
|
|
331
|
+
onChange={(val) => setLanguages(val as (string | number)[])}
|
|
332
|
+
multiple
|
|
333
|
+
placeholder="Select programming languages"
|
|
334
|
+
/>
|
|
335
|
+
|
|
336
|
+
<div>
|
|
337
|
+
<strong>Selected ({languages.length}):</strong>
|
|
338
|
+
{languages.length > 0 ? (
|
|
339
|
+
<ul>
|
|
340
|
+
{languages.map(lang => (
|
|
341
|
+
<li key={lang}>{lang}</li>
|
|
342
|
+
))}
|
|
343
|
+
</ul>
|
|
344
|
+
) : (
|
|
345
|
+
<p>No languages selected</p>
|
|
346
|
+
)}
|
|
347
|
+
</div>
|
|
348
|
+
</div>
|
|
349
|
+
);
|
|
350
|
+
}
|
|
351
|
+
```
|
|
352
|
+
|
|
353
|
+
### Multi-Select with Limit
|
|
354
|
+
|
|
355
|
+
```tsx
|
|
356
|
+
import { Select } from '@smilodon/react';
|
|
357
|
+
import { useState } from 'react';
|
|
358
|
+
|
|
359
|
+
function LimitedMultiSelect() {
|
|
360
|
+
const [selected, setSelected] = useState<(string | number)[]>([]);
|
|
361
|
+
const maxSelections = 3;
|
|
362
|
+
|
|
363
|
+
const handleChange = (value: string | number | (string | number)[]) => {
|
|
364
|
+
const newValue = value as (string | number)[];
|
|
365
|
+
if (newValue.length <= maxSelections) {
|
|
366
|
+
setSelected(newValue);
|
|
367
|
+
}
|
|
368
|
+
};
|
|
369
|
+
|
|
370
|
+
return (
|
|
371
|
+
<div>
|
|
372
|
+
<Select
|
|
373
|
+
items={['Red', 'Blue', 'Green', 'Yellow', 'Purple', 'Orange']}
|
|
374
|
+
value={selected}
|
|
375
|
+
onChange={handleChange}
|
|
376
|
+
multiple
|
|
377
|
+
placeholder={`Select up to ${maxSelections} colors`}
|
|
378
|
+
/>
|
|
379
|
+
<p>{selected.length} / {maxSelections} selected</p>
|
|
380
|
+
</div>
|
|
381
|
+
);
|
|
382
|
+
}
|
|
383
|
+
```
|
|
384
|
+
|
|
385
|
+
---
|
|
386
|
+
|
|
387
|
+
## Searchable Select
|
|
388
|
+
|
|
389
|
+
### Basic Search
|
|
390
|
+
|
|
391
|
+
```tsx
|
|
392
|
+
import { Select } from '@smilodon/react';
|
|
393
|
+
import { useState } from 'react';
|
|
394
|
+
|
|
395
|
+
function SearchableSelect() {
|
|
396
|
+
const [value, setValue] = useState('');
|
|
397
|
+
|
|
398
|
+
// Large dataset
|
|
399
|
+
const countries = [
|
|
400
|
+
{ value: 'us', label: 'United States' },
|
|
401
|
+
{ value: 'ca', label: 'Canada' },
|
|
402
|
+
{ value: 'mx', label: 'Mexico' },
|
|
403
|
+
{ value: 'br', label: 'Brazil' },
|
|
404
|
+
{ value: 'ar', label: 'Argentina' },
|
|
405
|
+
// ... 200+ countries
|
|
406
|
+
];
|
|
407
|
+
|
|
408
|
+
return (
|
|
409
|
+
<Select
|
|
410
|
+
items={countries}
|
|
411
|
+
value={value}
|
|
412
|
+
onChange={(val) => setValue(val as string)}
|
|
413
|
+
searchable
|
|
414
|
+
placeholder="Search for a country..."
|
|
415
|
+
/>
|
|
416
|
+
);
|
|
417
|
+
}
|
|
418
|
+
```
|
|
419
|
+
|
|
420
|
+
### Search with Callback
|
|
421
|
+
|
|
422
|
+
```tsx
|
|
423
|
+
import { Select } from '@smilodon/react';
|
|
424
|
+
import { useState } from 'react';
|
|
425
|
+
|
|
426
|
+
function SearchWithCallback() {
|
|
427
|
+
const [value, setValue] = useState('');
|
|
428
|
+
const [searchQuery, setSearchQuery] = useState('');
|
|
429
|
+
|
|
430
|
+
const handleSearch = (query: string) => {
|
|
431
|
+
setSearchQuery(query);
|
|
432
|
+
console.log('Searching for:', query);
|
|
433
|
+
// Optionally trigger API call, analytics, etc.
|
|
434
|
+
};
|
|
435
|
+
|
|
436
|
+
return (
|
|
437
|
+
<div>
|
|
438
|
+
<Select
|
|
439
|
+
items={['Apple', 'Banana', 'Cherry', 'Date', 'Elderberry']}
|
|
440
|
+
value={value}
|
|
441
|
+
onChange={(val) => setValue(val as string)}
|
|
442
|
+
searchable
|
|
443
|
+
onSearch={handleSearch}
|
|
444
|
+
placeholder="Type to search..."
|
|
445
|
+
/>
|
|
446
|
+
{searchQuery && <p>Current search: {searchQuery}</p>}
|
|
447
|
+
</div>
|
|
448
|
+
);
|
|
449
|
+
}
|
|
450
|
+
```
|
|
451
|
+
|
|
452
|
+
---
|
|
453
|
+
|
|
454
|
+
## Grouped Options
|
|
455
|
+
|
|
456
|
+
### Basic Groups
|
|
457
|
+
|
|
458
|
+
```tsx
|
|
459
|
+
import { Select } from '@smilodon/react';
|
|
460
|
+
import { useState } from 'react';
|
|
461
|
+
import type { SelectItem } from '@smilodon/core';
|
|
462
|
+
|
|
463
|
+
function GroupedSelect() {
|
|
464
|
+
const [value, setValue] = useState('');
|
|
465
|
+
|
|
466
|
+
const items: SelectItem[] = [
|
|
467
|
+
// Fruits group
|
|
468
|
+
{ value: 'apple', label: 'Apple', group: 'Fruits' },
|
|
469
|
+
{ value: 'banana', label: 'Banana', group: 'Fruits' },
|
|
470
|
+
{ value: 'cherry', label: 'Cherry', group: 'Fruits' },
|
|
471
|
+
|
|
472
|
+
// Vegetables group
|
|
473
|
+
{ value: 'carrot', label: 'Carrot', group: 'Vegetables' },
|
|
474
|
+
{ value: 'broccoli', label: 'Broccoli', group: 'Vegetables' },
|
|
475
|
+
{ value: 'spinach', label: 'Spinach', group: 'Vegetables' },
|
|
476
|
+
];
|
|
477
|
+
|
|
478
|
+
return (
|
|
479
|
+
<Select
|
|
480
|
+
items={items}
|
|
481
|
+
value={value}
|
|
482
|
+
onChange={(val) => setValue(val as string)}
|
|
483
|
+
placeholder="Select food..."
|
|
484
|
+
/>
|
|
485
|
+
);
|
|
486
|
+
}
|
|
487
|
+
```
|
|
488
|
+
|
|
489
|
+
### Complex Grouped Structure
|
|
490
|
+
|
|
491
|
+
```tsx
|
|
492
|
+
import { Select } from '@smilodon/react';
|
|
493
|
+
import { useState } from 'react';
|
|
494
|
+
import type { SelectItem } from '@smilodon/core';
|
|
495
|
+
|
|
496
|
+
function ComplexGroupedSelect() {
|
|
497
|
+
const [value, setValue] = useState('');
|
|
498
|
+
|
|
499
|
+
const items: SelectItem[] = [
|
|
500
|
+
// Frontend
|
|
501
|
+
{ value: 'react', label: 'React', group: 'Frontend' },
|
|
502
|
+
{ value: 'vue', label: 'Vue.js', group: 'Frontend' },
|
|
503
|
+
{ value: 'svelte', label: 'Svelte', group: 'Frontend' },
|
|
504
|
+
|
|
505
|
+
// Backend
|
|
506
|
+
{ value: 'node', label: 'Node.js', group: 'Backend' },
|
|
507
|
+
{ value: 'django', label: 'Django', group: 'Backend' },
|
|
508
|
+
{ value: 'rails', label: 'Ruby on Rails', group: 'Backend' },
|
|
509
|
+
|
|
510
|
+
// Database
|
|
511
|
+
{ value: 'postgres', label: 'PostgreSQL', group: 'Database' },
|
|
512
|
+
{ value: 'mongo', label: 'MongoDB', group: 'Database' },
|
|
513
|
+
{ value: 'redis', label: 'Redis', group: 'Database' },
|
|
514
|
+
];
|
|
515
|
+
|
|
516
|
+
return (
|
|
517
|
+
<Select
|
|
518
|
+
items={items}
|
|
519
|
+
value={value}
|
|
520
|
+
onChange={(val) => setValue(val as string)}
|
|
521
|
+
searchable
|
|
522
|
+
placeholder="Select technology..."
|
|
523
|
+
/>
|
|
524
|
+
);
|
|
525
|
+
}
|
|
526
|
+
```
|
|
527
|
+
|
|
528
|
+
---
|
|
529
|
+
|
|
530
|
+
## Disabled States
|
|
531
|
+
|
|
532
|
+
### Disabled Select
|
|
533
|
+
|
|
534
|
+
```tsx
|
|
535
|
+
import { Select } from '@smilodon/react';
|
|
536
|
+
|
|
537
|
+
function DisabledSelect() {
|
|
538
|
+
return (
|
|
539
|
+
<Select
|
|
540
|
+
items={['Option 1', 'Option 2', 'Option 3']}
|
|
541
|
+
value=""
|
|
542
|
+
onChange={() => {}}
|
|
543
|
+
disabled
|
|
544
|
+
placeholder="This select is disabled"
|
|
545
|
+
/>
|
|
546
|
+
);
|
|
547
|
+
}
|
|
548
|
+
```
|
|
549
|
+
|
|
550
|
+
### Disabled Options
|
|
551
|
+
|
|
552
|
+
```tsx
|
|
553
|
+
import { Select } from '@smilodon/react';
|
|
554
|
+
import { useState } from 'react';
|
|
555
|
+
import type { SelectItem } from '@smilodon/core';
|
|
556
|
+
|
|
557
|
+
function DisabledOptions() {
|
|
558
|
+
const [value, setValue] = useState('');
|
|
559
|
+
|
|
560
|
+
const items: SelectItem[] = [
|
|
561
|
+
{ value: '1', label: 'Available Option 1' },
|
|
562
|
+
{ value: '2', label: 'Available Option 2' },
|
|
563
|
+
{ value: '3', label: 'Disabled Option', disabled: true },
|
|
564
|
+
{ value: '4', label: 'Available Option 3' },
|
|
565
|
+
{ value: '5', label: 'Disabled Option 2', disabled: true },
|
|
566
|
+
];
|
|
567
|
+
|
|
568
|
+
return (
|
|
569
|
+
<Select
|
|
570
|
+
items={items}
|
|
571
|
+
value={value}
|
|
572
|
+
onChange={(val) => setValue(val as string)}
|
|
573
|
+
placeholder="Some options are disabled"
|
|
574
|
+
/>
|
|
575
|
+
);
|
|
576
|
+
}
|
|
577
|
+
```
|
|
578
|
+
|
|
579
|
+
### Conditional Disabling
|
|
580
|
+
|
|
581
|
+
```tsx
|
|
582
|
+
import { Select } from '@smilodon/react';
|
|
583
|
+
import { useState } from 'react';
|
|
584
|
+
|
|
585
|
+
function ConditionalDisabled() {
|
|
586
|
+
const [isLoading, setIsLoading] = useState(false);
|
|
587
|
+
const [value, setValue] = useState('');
|
|
588
|
+
|
|
589
|
+
const handleChange = async (val: string | number | (string | number)[]) => {
|
|
590
|
+
setIsLoading(true);
|
|
591
|
+
// Simulate API call
|
|
592
|
+
await new Promise(resolve => setTimeout(resolve, 1000));
|
|
593
|
+
setValue(val as string);
|
|
594
|
+
setIsLoading(false);
|
|
595
|
+
};
|
|
596
|
+
|
|
597
|
+
return (
|
|
598
|
+
<div>
|
|
599
|
+
<Select
|
|
600
|
+
items={['Option 1', 'Option 2', 'Option 3']}
|
|
601
|
+
value={value}
|
|
602
|
+
onChange={handleChange}
|
|
603
|
+
disabled={isLoading}
|
|
604
|
+
placeholder={isLoading ? 'Loading...' : 'Select an option'}
|
|
605
|
+
/>
|
|
606
|
+
{isLoading && <p>Processing selection...</p>}
|
|
607
|
+
</div>
|
|
608
|
+
);
|
|
609
|
+
}
|
|
610
|
+
```
|
|
611
|
+
|
|
612
|
+
---
|
|
613
|
+
|
|
614
|
+
## Event Handling
|
|
615
|
+
|
|
616
|
+
### All Events
|
|
617
|
+
|
|
618
|
+
```tsx
|
|
619
|
+
import { Select } from '@smilodon/react';
|
|
620
|
+
import { useState } from 'react';
|
|
621
|
+
|
|
622
|
+
function EventHandling() {
|
|
623
|
+
const [value, setValue] = useState('');
|
|
624
|
+
const [isOpen, setIsOpen] = useState(false);
|
|
625
|
+
const [logs, setLogs] = useState<string[]>([]);
|
|
626
|
+
|
|
627
|
+
const addLog = (message: string) => {
|
|
628
|
+
setLogs(prev => [...prev, `[${new Date().toLocaleTimeString()}] ${message}`]);
|
|
629
|
+
};
|
|
630
|
+
|
|
631
|
+
return (
|
|
632
|
+
<div>
|
|
633
|
+
<Select
|
|
634
|
+
items={['Apple', 'Banana', 'Cherry']}
|
|
635
|
+
value={value}
|
|
636
|
+
onChange={(val) => {
|
|
637
|
+
setValue(val as string);
|
|
638
|
+
addLog(`Changed to: ${val}`);
|
|
639
|
+
}}
|
|
640
|
+
onOpen={() => {
|
|
641
|
+
setIsOpen(true);
|
|
642
|
+
addLog('Dropdown opened');
|
|
643
|
+
}}
|
|
644
|
+
onClose={() => {
|
|
645
|
+
setIsOpen(false);
|
|
646
|
+
addLog('Dropdown closed');
|
|
647
|
+
}}
|
|
648
|
+
onSearch={(query) => {
|
|
649
|
+
addLog(`Searching for: ${query}`);
|
|
650
|
+
}}
|
|
651
|
+
placeholder="Select a fruit"
|
|
652
|
+
/>
|
|
653
|
+
|
|
654
|
+
<div>
|
|
655
|
+
<p>Current value: {value || 'None'}</p>
|
|
656
|
+
<p>Dropdown is: {isOpen ? 'Open' : 'Closed'}</p>
|
|
657
|
+
</div>
|
|
658
|
+
|
|
659
|
+
<div>
|
|
660
|
+
<strong>Event Log:</strong>
|
|
661
|
+
<ul style={{ maxHeight: '200px', overflow: 'auto' }}>
|
|
662
|
+
{logs.map((log, i) => (
|
|
663
|
+
<li key={i}>{log}</li>
|
|
664
|
+
))}
|
|
665
|
+
</ul>
|
|
666
|
+
</div>
|
|
667
|
+
</div>
|
|
668
|
+
);
|
|
669
|
+
}
|
|
670
|
+
```
|
|
671
|
+
|
|
672
|
+
---
|
|
673
|
+
|
|
674
|
+
## Styling & Theming
|
|
675
|
+
|
|
676
|
+
### Inline Styles with CSS Variables
|
|
677
|
+
|
|
678
|
+
```tsx
|
|
679
|
+
import { Select } from '@smilodon/react';
|
|
680
|
+
import { useState } from 'react';
|
|
681
|
+
|
|
682
|
+
function StyledSelect() {
|
|
683
|
+
const [value, setValue] = useState('');
|
|
684
|
+
|
|
685
|
+
return (
|
|
686
|
+
<Select
|
|
687
|
+
items={['Red', 'Blue', 'Green']}
|
|
688
|
+
value={value}
|
|
689
|
+
onChange={(val) => setValue(val as string)}
|
|
690
|
+
style={{
|
|
691
|
+
'--select-input-border': '2px solid #3b82f6',
|
|
692
|
+
'--select-input-border-radius': '8px',
|
|
693
|
+
'--select-input-focus-border': '#2563eb',
|
|
694
|
+
'--select-option-hover-bg': '#dbeafe',
|
|
695
|
+
'--select-option-selected-bg': '#3b82f6',
|
|
696
|
+
'--select-option-selected-color': 'white',
|
|
697
|
+
'--select-badge-bg': '#3b82f6',
|
|
698
|
+
} as React.CSSProperties}
|
|
699
|
+
/>
|
|
700
|
+
);
|
|
701
|
+
}
|
|
702
|
+
```
|
|
703
|
+
|
|
704
|
+
### Complete CSS Variables Reference
|
|
705
|
+
|
|
706
|
+
All available CSS variables for styling:
|
|
707
|
+
|
|
708
|
+
```tsx
|
|
709
|
+
<Select
|
|
710
|
+
style={{
|
|
711
|
+
// Input Container
|
|
712
|
+
'--select-input-gap': '6px',
|
|
713
|
+
'--select-input-padding': '6px 52px 6px 8px',
|
|
714
|
+
'--select-input-min-height': '44px',
|
|
715
|
+
'--select-input-bg': 'white',
|
|
716
|
+
'--select-input-border': '1px solid #d1d5db',
|
|
717
|
+
'--select-input-border-radius': '6px',
|
|
718
|
+
'--select-input-focus-border': '#667eea',
|
|
719
|
+
'--select-input-focus-shadow': '0 0 0 3px rgba(102, 126, 234, 0.1)',
|
|
720
|
+
|
|
721
|
+
// Input Field
|
|
722
|
+
'--select-input-min-width': '120px',
|
|
723
|
+
'--select-input-field-padding': '4px',
|
|
724
|
+
'--select-input-font-size': '14px',
|
|
725
|
+
'--select-input-line-height': '1.5',
|
|
726
|
+
'--select-input-color': '#1f2937',
|
|
727
|
+
'--select-input-placeholder-color': '#9ca3af',
|
|
728
|
+
'--select-font-family': 'inherit',
|
|
729
|
+
|
|
730
|
+
// Dropdown Arrow
|
|
731
|
+
'--select-arrow-width': '40px',
|
|
732
|
+
'--select-arrow-size': '16px',
|
|
733
|
+
'--select-arrow-color': '#667eea',
|
|
734
|
+
'--select-arrow-hover-color': '#667eea',
|
|
735
|
+
'--select-arrow-hover-bg': 'rgba(102, 126, 234, 0.08)',
|
|
736
|
+
'--select-arrow-border-radius': '0 4px 4px 0',
|
|
737
|
+
|
|
738
|
+
// Separator Line
|
|
739
|
+
'--select-separator-position': '40px',
|
|
740
|
+
'--select-separator-width': '1px',
|
|
741
|
+
'--select-separator-height': '60%',
|
|
742
|
+
'--select-separator-gradient': 'linear-gradient(to bottom, transparent, #ccc, transparent)',
|
|
743
|
+
|
|
744
|
+
// Selection Badges (Multi-select)
|
|
745
|
+
'--select-badge-gap': '4px',
|
|
746
|
+
'--select-badge-padding': '4px 8px',
|
|
747
|
+
'--select-badge-margin': '2px',
|
|
748
|
+
'--select-badge-bg': '#667eea',
|
|
749
|
+
'--select-badge-color': 'white',
|
|
750
|
+
'--select-badge-border-radius': '4px',
|
|
751
|
+
'--select-badge-font-size': '13px',
|
|
752
|
+
|
|
753
|
+
// Badge Remove Button
|
|
754
|
+
'--select-badge-remove-size': '16px',
|
|
755
|
+
// '--select-badge-remove-bg': '#ffffff4d',
|
|
756
|
+
'--select-badge-remove-color': 'white',
|
|
757
|
+
'--select-badge-remove-font-size': '16px',
|
|
758
|
+
'--select-badge-remove-hover-bg': 'rgba(255, 255, 255, 0.5)',
|
|
759
|
+
|
|
760
|
+
// Dropdown
|
|
761
|
+
'--select-dropdown-margin-top': '4px',
|
|
762
|
+
'--select-dropdown-max-height': '300px',
|
|
763
|
+
'--select-dropdown-border-radius': '4px',
|
|
764
|
+
'--select-dropdown-bg': 'white',
|
|
765
|
+
'--select-dropdown-border': '1px solid #ccc',
|
|
766
|
+
'--select-dropdown-shadow': '0 4px 6px rgba(0, 0, 0, 0.1)',
|
|
767
|
+
|
|
768
|
+
// Options
|
|
769
|
+
'--select-options-bg': 'white',
|
|
770
|
+
'--select-option-padding': '8px 12px',
|
|
771
|
+
'--select-option-color': '#1f2937',
|
|
772
|
+
'--select-option-bg': 'white',
|
|
773
|
+
'--select-option-font-size': '14px',
|
|
774
|
+
'--select-option-hover-bg': '#f3f4f6',
|
|
775
|
+
'--select-option-selected-bg': '#e0e7ff',
|
|
776
|
+
'--select-option-selected-color': '#4338ca',
|
|
777
|
+
} as React.CSSProperties}
|
|
778
|
+
/>
|
|
779
|
+
```
|
|
780
|
+
|
|
781
|
+
### Theme Examples
|
|
782
|
+
|
|
783
|
+
#### Bootstrap Theme
|
|
784
|
+
|
|
785
|
+
```tsx
|
|
786
|
+
<Select
|
|
787
|
+
items={items}
|
|
788
|
+
value={value}
|
|
789
|
+
onChange={setValue}
|
|
790
|
+
style={{
|
|
791
|
+
'--select-input-border': '1px solid #ced4da',
|
|
792
|
+
'--select-input-border-radius': '0.375rem',
|
|
793
|
+
'--select-input-focus-border': '#86b7fe',
|
|
794
|
+
'--select-input-focus-shadow': '0 0 0 0.25rem rgba(13, 110, 253, 0.25)',
|
|
795
|
+
'--select-option-hover-bg': '#e9ecef',
|
|
796
|
+
'--select-option-selected-bg': '#0d6efd',
|
|
797
|
+
'--select-option-selected-color': 'white',
|
|
798
|
+
'--select-badge-bg': '#0d6efd',
|
|
799
|
+
} as React.CSSProperties}
|
|
800
|
+
/>
|
|
801
|
+
```
|
|
802
|
+
|
|
803
|
+
#### Material Design Theme
|
|
804
|
+
|
|
805
|
+
```tsx
|
|
806
|
+
<Select
|
|
807
|
+
items={items}
|
|
808
|
+
value={value}
|
|
809
|
+
onChange={setValue}
|
|
810
|
+
style={{
|
|
811
|
+
'--select-input-border-radius': '4px',
|
|
812
|
+
'--select-input-focus-border': '#1976d2',
|
|
813
|
+
'--select-dropdown-shadow': '0 2px 4px rgba(0,0,0,0.2), 0 4px 5px rgba(0,0,0,0.14)',
|
|
814
|
+
'--select-option-padding': '16px',
|
|
815
|
+
'--select-option-hover-bg': 'rgba(0, 0, 0, 0.04)',
|
|
816
|
+
'--select-option-selected-bg': '#e3f2fd',
|
|
817
|
+
'--select-option-selected-color': '#1976d2',
|
|
818
|
+
'--select-badge-bg': '#1976d2',
|
|
819
|
+
'--select-badge-border-radius': '16px',
|
|
820
|
+
} as React.CSSProperties}
|
|
821
|
+
/>
|
|
822
|
+
```
|
|
823
|
+
|
|
824
|
+
#### Dark Mode Theme
|
|
825
|
+
|
|
826
|
+
```tsx
|
|
827
|
+
<Select
|
|
828
|
+
items={items}
|
|
829
|
+
value={value}
|
|
830
|
+
onChange={setValue}
|
|
831
|
+
className="dark-mode"
|
|
832
|
+
style={{
|
|
833
|
+
'--select-input-bg': '#1f2937',
|
|
834
|
+
'--select-input-border': '1px solid #4b5563',
|
|
835
|
+
'--select-input-color': '#f9fafb',
|
|
836
|
+
'--select-dropdown-bg': '#1f2937',
|
|
837
|
+
'--select-options-bg': '#1f2937',
|
|
838
|
+
'--select-option-color': '#f9fafb',
|
|
839
|
+
'--select-option-bg': '#1f2937',
|
|
840
|
+
'--select-option-hover-bg': '#374151',
|
|
841
|
+
'--select-option-selected-bg': '#3730a3',
|
|
842
|
+
'--select-badge-bg': '#4f46e5',
|
|
843
|
+
} as React.CSSProperties}
|
|
844
|
+
/>
|
|
845
|
+
```
|
|
846
|
+
|
|
847
|
+
### External CSS
|
|
848
|
+
|
|
849
|
+
```css
|
|
850
|
+
/* styles.css */
|
|
851
|
+
.custom-select {
|
|
852
|
+
--select-input-border: 2px solid #10b981;
|
|
853
|
+
--select-input-border-radius: 12px;
|
|
854
|
+
--select-option-hover-bg: #d1fae5;
|
|
855
|
+
--select-option-selected-bg: #10b981;
|
|
856
|
+
--select-option-selected-color: white;
|
|
857
|
+
--select-badge-bg: #10b981;
|
|
858
|
+
--select-separator-gradient: linear-gradient(
|
|
859
|
+
to bottom,
|
|
860
|
+
transparent 0%,
|
|
861
|
+
#10b981 20%,
|
|
862
|
+
#10b981 80%,
|
|
863
|
+
transparent 100%
|
|
864
|
+
);
|
|
865
|
+
}
|
|
866
|
+
```
|
|
867
|
+
|
|
868
|
+
```tsx
|
|
869
|
+
import './styles.css';
|
|
870
|
+
|
|
871
|
+
function CustomStyledSelect() {
|
|
872
|
+
return (
|
|
873
|
+
<Select
|
|
874
|
+
className="custom-select"
|
|
875
|
+
items={['Option 1', 'Option 2', 'Option 3']}
|
|
876
|
+
value={value}
|
|
877
|
+
onChange={setValue}
|
|
878
|
+
/>
|
|
879
|
+
);
|
|
880
|
+
}
|
|
881
|
+
```
|
|
882
|
+
|
|
883
|
+
---
|
|
884
|
+
|
|
885
|
+
## Custom Renderers
|
|
886
|
+
|
|
887
|
+
### Custom Option Renderer
|
|
888
|
+
|
|
889
|
+
```tsx
|
|
890
|
+
import { Select } from '@smilodon/react';
|
|
891
|
+
import { useState } from 'react';
|
|
892
|
+
import type { SelectItem } from '@smilodon/core';
|
|
893
|
+
|
|
894
|
+
interface User {
|
|
895
|
+
value: string;
|
|
896
|
+
label: string;
|
|
897
|
+
email: string;
|
|
898
|
+
avatar: string;
|
|
899
|
+
}
|
|
900
|
+
|
|
901
|
+
function CustomRendererSelect() {
|
|
902
|
+
const [value, setValue] = useState('');
|
|
903
|
+
|
|
904
|
+
const users: User[] = [
|
|
905
|
+
{
|
|
906
|
+
value: '1',
|
|
907
|
+
label: 'John Doe',
|
|
908
|
+
email: 'john@example.com',
|
|
909
|
+
avatar: 'https://i.pravatar.cc/150?img=1'
|
|
910
|
+
},
|
|
911
|
+
{
|
|
912
|
+
value: '2',
|
|
913
|
+
label: 'Jane Smith',
|
|
914
|
+
email: 'jane@example.com',
|
|
915
|
+
avatar: 'https://i.pravatar.cc/150?img=2'
|
|
916
|
+
},
|
|
917
|
+
];
|
|
918
|
+
|
|
919
|
+
const customRenderer = (item: SelectItem, index: number) => {
|
|
920
|
+
const user = item as User;
|
|
921
|
+
return (
|
|
922
|
+
<div style={{ display: 'flex', alignItems: 'center', gap: '12px' }}>
|
|
923
|
+
<img
|
|
924
|
+
src={user.avatar}
|
|
925
|
+
alt={user.label}
|
|
926
|
+
style={{ width: '32px', height: '32px', borderRadius: '50%' }}
|
|
927
|
+
/>
|
|
928
|
+
<div>
|
|
929
|
+
<div style={{ fontWeight: '500' }}>{user.label}</div>
|
|
930
|
+
<div style={{ fontSize: '12px', color: '#6b7280' }}>{user.email}</div>
|
|
931
|
+
</div>
|
|
932
|
+
</div>
|
|
933
|
+
);
|
|
934
|
+
};
|
|
935
|
+
|
|
936
|
+
return (
|
|
937
|
+
<Select
|
|
938
|
+
items={users}
|
|
939
|
+
value={value}
|
|
940
|
+
onChange={(val) => setValue(val as string)}
|
|
941
|
+
customRenderer={customRenderer}
|
|
942
|
+
placeholder="Select a user"
|
|
943
|
+
estimatedItemHeight={60}
|
|
944
|
+
/>
|
|
945
|
+
);
|
|
946
|
+
}
|
|
947
|
+
```
|
|
948
|
+
|
|
949
|
+
---
|
|
950
|
+
|
|
951
|
+
## Performance Optimization
|
|
952
|
+
|
|
953
|
+
### Virtualization for Large Datasets
|
|
954
|
+
|
|
955
|
+
```tsx
|
|
956
|
+
import { Select } from '@smilodon/react';
|
|
957
|
+
import { useState, useMemo } from 'react';
|
|
958
|
+
|
|
959
|
+
function LargeDataset() {
|
|
960
|
+
const [value, setValue] = useState('');
|
|
961
|
+
|
|
962
|
+
// Generate 100,000 items
|
|
963
|
+
const items = useMemo(() =>
|
|
964
|
+
Array.from({ length: 100_000 }, (_, i) => ({
|
|
965
|
+
value: `item-${i}`,
|
|
966
|
+
label: `Item ${i + 1}`,
|
|
967
|
+
})),
|
|
968
|
+
[]
|
|
969
|
+
);
|
|
970
|
+
|
|
971
|
+
return (
|
|
972
|
+
<Select
|
|
973
|
+
items={items}
|
|
974
|
+
value={value}
|
|
975
|
+
onChange={(val) => setValue(val as string)}
|
|
976
|
+
virtualization={true}
|
|
977
|
+
estimatedItemHeight={48}
|
|
978
|
+
placeholder="Search 100k items..."
|
|
979
|
+
searchable
|
|
980
|
+
/>
|
|
981
|
+
);
|
|
982
|
+
}
|
|
983
|
+
```
|
|
984
|
+
|
|
985
|
+
### Memoization
|
|
986
|
+
|
|
987
|
+
```tsx
|
|
988
|
+
import { Select } from '@smilodon/react';
|
|
989
|
+
import { useState, useMemo, useCallback } from 'react';
|
|
990
|
+
|
|
991
|
+
function OptimizedSelect() {
|
|
992
|
+
const [value, setValue] = useState('');
|
|
993
|
+
|
|
994
|
+
// Memoize items
|
|
995
|
+
const items = useMemo(() => [
|
|
996
|
+
{ value: '1', label: 'Option 1' },
|
|
997
|
+
{ value: '2', label: 'Option 2' },
|
|
998
|
+
{ value: '3', label: 'Option 3' },
|
|
999
|
+
], []);
|
|
1000
|
+
|
|
1001
|
+
// Memoize handler
|
|
1002
|
+
const handleChange = useCallback((val: string | number | (string | number)[]) => {
|
|
1003
|
+
setValue(val as string);
|
|
1004
|
+
}, []);
|
|
1005
|
+
|
|
1006
|
+
return (
|
|
1007
|
+
<Select
|
|
1008
|
+
items={items}
|
|
1009
|
+
value={value}
|
|
1010
|
+
onChange={handleChange}
|
|
1011
|
+
/>
|
|
1012
|
+
);
|
|
1013
|
+
}
|
|
1014
|
+
```
|
|
1015
|
+
|
|
1016
|
+
---
|
|
1017
|
+
|
|
1018
|
+
## TypeScript Integration
|
|
1019
|
+
|
|
1020
|
+
### Full Type Safety
|
|
1021
|
+
|
|
1022
|
+
```tsx
|
|
1023
|
+
import { Select } from '@smilodon/react';
|
|
1024
|
+
import { useState } from 'react';
|
|
1025
|
+
import type { SelectItem } from '@smilodon/core';
|
|
1026
|
+
|
|
1027
|
+
// Define your data type
|
|
1028
|
+
interface Product extends SelectItem {
|
|
1029
|
+
value: string;
|
|
1030
|
+
label: string;
|
|
1031
|
+
price: number;
|
|
1032
|
+
category: string;
|
|
1033
|
+
}
|
|
1034
|
+
|
|
1035
|
+
function TypeSafeSelect() {
|
|
1036
|
+
const [selectedId, setSelectedId] = useState<string>('');
|
|
1037
|
+
|
|
1038
|
+
const products: Product[] = [
|
|
1039
|
+
{ value: 'p1', label: 'Laptop', price: 999, category: 'Electronics' },
|
|
1040
|
+
{ value: 'p2', label: 'Phone', price: 699, category: 'Electronics' },
|
|
1041
|
+
{ value: 'p3', label: 'Desk', price: 299, category: 'Furniture' },
|
|
1042
|
+
];
|
|
1043
|
+
|
|
1044
|
+
const handleChange = (value: string | number | (string | number)[]) => {
|
|
1045
|
+
setSelectedId(value as string);
|
|
1046
|
+
};
|
|
1047
|
+
|
|
1048
|
+
return (
|
|
1049
|
+
<Select
|
|
1050
|
+
items={products}
|
|
1051
|
+
value={selectedId}
|
|
1052
|
+
onChange={handleChange}
|
|
1053
|
+
placeholder="Select a product"
|
|
1054
|
+
/>
|
|
1055
|
+
);
|
|
1056
|
+
}
|
|
1057
|
+
```
|
|
1058
|
+
|
|
1059
|
+
### Generic Component
|
|
1060
|
+
|
|
1061
|
+
```tsx
|
|
1062
|
+
import { Select } from '@smilodon/react';
|
|
1063
|
+
import { useState } from 'react';
|
|
1064
|
+
import type { SelectItem } from '@smilodon/core';
|
|
1065
|
+
|
|
1066
|
+
interface GenericSelectProps<T extends SelectItem> {
|
|
1067
|
+
items: T[];
|
|
1068
|
+
onSelect: (item: T) => void;
|
|
1069
|
+
label: string;
|
|
1070
|
+
}
|
|
1071
|
+
|
|
1072
|
+
function GenericSelect<T extends SelectItem>({
|
|
1073
|
+
items,
|
|
1074
|
+
onSelect,
|
|
1075
|
+
label
|
|
1076
|
+
}: GenericSelectProps<T>) {
|
|
1077
|
+
const [value, setValue] = useState<string | number>('');
|
|
1078
|
+
|
|
1079
|
+
const handleChange = (val: string | number | (string | number)[]) => {
|
|
1080
|
+
setValue(val as string | number);
|
|
1081
|
+
const selected = items.find(item => item.value === val);
|
|
1082
|
+
if (selected) onSelect(selected);
|
|
1083
|
+
};
|
|
1084
|
+
|
|
1085
|
+
return (
|
|
1086
|
+
<div>
|
|
1087
|
+
<label>{label}</label>
|
|
1088
|
+
<Select
|
|
1089
|
+
items={items}
|
|
1090
|
+
value={value}
|
|
1091
|
+
onChange={handleChange}
|
|
1092
|
+
/>
|
|
1093
|
+
</div>
|
|
1094
|
+
);
|
|
1095
|
+
}
|
|
1096
|
+
```
|
|
1097
|
+
|
|
1098
|
+
---
|
|
1099
|
+
|
|
1100
|
+
## Accessibility
|
|
1101
|
+
|
|
1102
|
+
Smilodon is WCAG 2.1 AAA compliant with full keyboard support:
|
|
1103
|
+
|
|
1104
|
+
### Keyboard Navigation
|
|
1105
|
+
|
|
1106
|
+
- **Tab** - Focus the select
|
|
1107
|
+
- **Enter/Space** - Open dropdown
|
|
1108
|
+
- **↑/↓** - Navigate options
|
|
1109
|
+
- **Home/End** - Jump to first/last option
|
|
1110
|
+
- **Escape** - Close dropdown
|
|
1111
|
+
- **Type** - Search options
|
|
1112
|
+
|
|
1113
|
+
### ARIA Attributes
|
|
1114
|
+
|
|
1115
|
+
The component automatically includes:
|
|
1116
|
+
- `role="combobox"`
|
|
1117
|
+
- `aria-expanded`
|
|
1118
|
+
- `aria-haspopup`
|
|
1119
|
+
- `aria-label` / `aria-labelledby`
|
|
1120
|
+
- `aria-activedescendant`
|
|
1121
|
+
- `aria-multiselectable` (when `multiple={true}`)
|
|
1122
|
+
|
|
1123
|
+
### Screen Reader Support
|
|
1124
|
+
|
|
1125
|
+
```tsx
|
|
1126
|
+
function AccessibleSelect() {
|
|
1127
|
+
return (
|
|
1128
|
+
<div>
|
|
1129
|
+
<label id="country-label">Country</label>
|
|
1130
|
+
<Select
|
|
1131
|
+
items={['USA', 'Canada', 'Mexico']}
|
|
1132
|
+
value={value}
|
|
1133
|
+
onChange={setValue}
|
|
1134
|
+
placeholder="Select your country"
|
|
1135
|
+
// Component automatically uses the label via aria-labelledby
|
|
1136
|
+
/>
|
|
1137
|
+
</div>
|
|
1138
|
+
);
|
|
1139
|
+
}
|
|
1140
|
+
```
|
|
1141
|
+
|
|
1142
|
+
---
|
|
1143
|
+
|
|
1144
|
+
## Advanced Patterns
|
|
1145
|
+
|
|
1146
|
+
### Dependent Selects
|
|
1147
|
+
|
|
1148
|
+
```tsx
|
|
1149
|
+
import { Select } from '@smilodon/react';
|
|
1150
|
+
import { useState, useEffect } from 'react';
|
|
1151
|
+
|
|
1152
|
+
function DependentSelects() {
|
|
1153
|
+
const [country, setCountry] = useState('');
|
|
1154
|
+
const [state, setState] = useState('');
|
|
1155
|
+
const [states, setStates] = useState<string[]>([]);
|
|
1156
|
+
|
|
1157
|
+
const countries = ['USA', 'Canada', 'Mexico'];
|
|
1158
|
+
|
|
1159
|
+
useEffect(() => {
|
|
1160
|
+
// Reset state when country changes
|
|
1161
|
+
setState('');
|
|
1162
|
+
|
|
1163
|
+
// Load states for selected country
|
|
1164
|
+
if (country === 'USA') {
|
|
1165
|
+
setStates(['California', 'Texas', 'New York', 'Florida']);
|
|
1166
|
+
} else if (country === 'Canada') {
|
|
1167
|
+
setStates(['Ontario', 'Quebec', 'British Columbia']);
|
|
1168
|
+
} else if (country === 'Mexico') {
|
|
1169
|
+
setStates(['Mexico City', 'Jalisco', 'Nuevo León']);
|
|
1170
|
+
} else {
|
|
1171
|
+
setStates([]);
|
|
1172
|
+
}
|
|
1173
|
+
}, [country]);
|
|
1174
|
+
|
|
1175
|
+
return (
|
|
1176
|
+
<div>
|
|
1177
|
+
<Select
|
|
1178
|
+
items={countries}
|
|
1179
|
+
value={country}
|
|
1180
|
+
onChange={(val) => setCountry(val as string)}
|
|
1181
|
+
placeholder="Select country"
|
|
1182
|
+
/>
|
|
1183
|
+
|
|
1184
|
+
<Select
|
|
1185
|
+
items={states}
|
|
1186
|
+
value={state}
|
|
1187
|
+
onChange={(val) => setState(val as string)}
|
|
1188
|
+
disabled={!country}
|
|
1189
|
+
placeholder={country ? 'Select state' : 'Select country first'}
|
|
1190
|
+
/>
|
|
1191
|
+
</div>
|
|
1192
|
+
);
|
|
1193
|
+
}
|
|
1194
|
+
```
|
|
1195
|
+
|
|
1196
|
+
### Async Data Loading
|
|
1197
|
+
|
|
1198
|
+
```tsx
|
|
1199
|
+
import { Select } from '@smilodon/react';
|
|
1200
|
+
import { useState, useEffect } from 'react';
|
|
1201
|
+
import type { SelectItem } from '@smilodon/core';
|
|
1202
|
+
|
|
1203
|
+
function AsyncSelect() {
|
|
1204
|
+
const [items, setItems] = useState<SelectItem[]>([]);
|
|
1205
|
+
const [loading, setLoading] = useState(true);
|
|
1206
|
+
const [value, setValue] = useState('');
|
|
1207
|
+
|
|
1208
|
+
useEffect(() => {
|
|
1209
|
+
// Simulate API call
|
|
1210
|
+
const fetchData = async () => {
|
|
1211
|
+
setLoading(true);
|
|
1212
|
+
await new Promise(resolve => setTimeout(resolve, 1000));
|
|
1213
|
+
|
|
1214
|
+
setItems([
|
|
1215
|
+
{ value: '1', label: 'Option 1' },
|
|
1216
|
+
{ value: '2', label: 'Option 2' },
|
|
1217
|
+
{ value: '3', label: 'Option 3' },
|
|
1218
|
+
]);
|
|
1219
|
+
|
|
1220
|
+
setLoading(false);
|
|
1221
|
+
};
|
|
1222
|
+
|
|
1223
|
+
fetchData();
|
|
1224
|
+
}, []);
|
|
1225
|
+
|
|
1226
|
+
if (loading) {
|
|
1227
|
+
return <div>Loading options...</div>;
|
|
1228
|
+
}
|
|
1229
|
+
|
|
1230
|
+
return (
|
|
1231
|
+
<Select
|
|
1232
|
+
items={items}
|
|
1233
|
+
value={value}
|
|
1234
|
+
onChange={(val) => setValue(val as string)}
|
|
1235
|
+
placeholder="Select an option"
|
|
1236
|
+
/>
|
|
1237
|
+
);
|
|
1238
|
+
}
|
|
1239
|
+
```
|
|
1240
|
+
|
|
1241
|
+
### With React Hook Form
|
|
1242
|
+
|
|
1243
|
+
```tsx
|
|
1244
|
+
import { Select } from '@smilodon/react';
|
|
1245
|
+
import { useForm, Controller } from 'react-hook-form';
|
|
1246
|
+
|
|
1247
|
+
interface FormData {
|
|
1248
|
+
country: string;
|
|
1249
|
+
language: string;
|
|
1250
|
+
}
|
|
1251
|
+
|
|
1252
|
+
function FormWithSelect() {
|
|
1253
|
+
const { control, handleSubmit } = useForm<FormData>();
|
|
1254
|
+
|
|
1255
|
+
const onSubmit = (data: FormData) => {
|
|
1256
|
+
console.log('Form data:', data);
|
|
1257
|
+
};
|
|
1258
|
+
|
|
1259
|
+
return (
|
|
1260
|
+
<form onSubmit={handleSubmit(onSubmit)}>
|
|
1261
|
+
<Controller
|
|
1262
|
+
name="country"
|
|
1263
|
+
control={control}
|
|
1264
|
+
rules={{ required: 'Country is required' }}
|
|
1265
|
+
render={({ field, fieldState }) => (
|
|
1266
|
+
<div>
|
|
1267
|
+
<Select
|
|
1268
|
+
items={['USA', 'Canada', 'Mexico']}
|
|
1269
|
+
value={field.value}
|
|
1270
|
+
onChange={field.onChange}
|
|
1271
|
+
placeholder="Select country"
|
|
1272
|
+
/>
|
|
1273
|
+
{fieldState.error && (
|
|
1274
|
+
<span style={{ color: 'red' }}>{fieldState.error.message}</span>
|
|
1275
|
+
)}
|
|
1276
|
+
</div>
|
|
1277
|
+
)}
|
|
1278
|
+
/>
|
|
1279
|
+
|
|
1280
|
+
<button type="submit">Submit</button>
|
|
1281
|
+
</form>
|
|
1282
|
+
);
|
|
1283
|
+
}
|
|
1284
|
+
```
|
|
1285
|
+
|
|
1286
|
+
---
|
|
1287
|
+
|
|
1288
|
+
## Troubleshooting
|
|
1289
|
+
|
|
1290
|
+
### Common Issues
|
|
1291
|
+
|
|
1292
|
+
#### Issue: Value not updating
|
|
1293
|
+
|
|
1294
|
+
```tsx
|
|
1295
|
+
// ❌ Wrong - missing onChange
|
|
1296
|
+
<Select items={items} value={value} />
|
|
1297
|
+
|
|
1298
|
+
// ✅ Correct
|
|
1299
|
+
<Select items={items} value={value} onChange={setValue} />
|
|
1300
|
+
```
|
|
1301
|
+
|
|
1302
|
+
#### Issue: Multi-select not working
|
|
1303
|
+
|
|
1304
|
+
```tsx
|
|
1305
|
+
// ❌ Wrong - value should be array
|
|
1306
|
+
const [value, setValue] = useState('');
|
|
1307
|
+
|
|
1308
|
+
// ✅ Correct
|
|
1309
|
+
const [value, setValue] = useState<(string | number)[]>([]);
|
|
1310
|
+
|
|
1311
|
+
<Select items={items} value={value} onChange={setValue} multiple />
|
|
1312
|
+
```
|
|
1313
|
+
|
|
1314
|
+
#### Issue: Styles not applying
|
|
1315
|
+
|
|
1316
|
+
```tsx
|
|
1317
|
+
// ❌ Wrong - missing 'as React.CSSProperties'
|
|
1318
|
+
<Select style={{ '--select-input-border': '2px solid red' }} />
|
|
1319
|
+
|
|
1320
|
+
// ✅ Correct
|
|
1321
|
+
<Select style={{ '--select-input-border': '2px solid red' } as React.CSSProperties} />
|
|
1322
|
+
```
|
|
1323
|
+
|
|
1324
|
+
### Performance Issues
|
|
1325
|
+
|
|
1326
|
+
If you experience slow rendering with large datasets:
|
|
1327
|
+
|
|
1328
|
+
1. Enable virtualization (enabled by default)
|
|
1329
|
+
2. Use `useMemo` for items array
|
|
1330
|
+
3. Use `useCallback` for event handlers
|
|
1331
|
+
4. Increase `estimatedItemHeight` if items are taller than 48px
|
|
1332
|
+
|
|
1333
|
+
```tsx
|
|
1334
|
+
const items = useMemo(() => generateLargeDataset(), []);
|
|
1335
|
+
const handleChange = useCallback((val) => setValue(val), []);
|
|
1336
|
+
|
|
1337
|
+
<Select
|
|
1338
|
+
items={items}
|
|
1339
|
+
value={value}
|
|
1340
|
+
onChange={handleChange}
|
|
1341
|
+
virtualization={true}
|
|
1342
|
+
estimatedItemHeight={48}
|
|
1343
|
+
/>
|
|
1344
|
+
```
|
|
1345
|
+
|
|
1346
|
+
---
|
|
1347
|
+
|
|
1348
|
+
## Additional Resources
|
|
1349
|
+
|
|
1350
|
+
- **Core Documentation**: [packages/core/README.md](../core/README.md)
|
|
1351
|
+
- **API Reference**: [docs/API-REFERENCE.md](../../docs/API-REFERENCE.md)
|
|
1352
|
+
- **Live Examples**: [Playground](../../playground)
|
|
1353
|
+
- **GitHub Issues**: [Report a bug](https://github.com/navidrezadoost/smilodon/issues)
|
|
1354
|
+
|
|
1355
|
+
---
|
|
1356
|
+
|
|
1357
|
+
**Built with ❤️ by the Smilodon team**
|
|
1358
|
+
|
|
1359
|
+
*Last updated: February 9, 2026 - v1.3.6*
|