@smilodon/react 1.1.1 → 1.2.0
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/README.md +647 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -2,6 +2,24 @@
|
|
|
2
2
|
|
|
3
3
|
Production-ready, accessible Select component for React applications.
|
|
4
4
|
|
|
5
|
+
## 📖 Documentation
|
|
6
|
+
|
|
7
|
+
**For comprehensive documentation covering all features, styling options, and advanced patterns:**
|
|
8
|
+
|
|
9
|
+
👉 **[Complete React Guide](./COMPLETE-GUIDE.md)** 👈
|
|
10
|
+
|
|
11
|
+
The complete guide includes:
|
|
12
|
+
- ✅ All 60+ CSS variables for complete customization
|
|
13
|
+
- ✅ React-specific patterns (hooks, controlled components, refs)
|
|
14
|
+
- ✅ Complete API reference with TypeScript types
|
|
15
|
+
- ✅ React Hook Form integration examples
|
|
16
|
+
- ✅ Performance optimization with useMemo/useCallback
|
|
17
|
+
- ✅ Custom renderers and theme examples
|
|
18
|
+
- ✅ Advanced patterns (dependent selects, async loading)
|
|
19
|
+
- ✅ Troubleshooting and accessibility information
|
|
20
|
+
|
|
21
|
+
---
|
|
22
|
+
|
|
5
23
|
## Features
|
|
6
24
|
|
|
7
25
|
- ✅ **Simple API** - Clean, intuitive props that feel natural in React
|
|
@@ -301,6 +319,635 @@ function RefExample() {
|
|
|
301
319
|
}
|
|
302
320
|
```
|
|
303
321
|
|
|
322
|
+
---
|
|
323
|
+
|
|
324
|
+
## 🎯 Two Ways to Specify Options
|
|
325
|
+
|
|
326
|
+
Smilodon React provides **two powerful approaches** for defining select options, each optimized for different use cases:
|
|
327
|
+
|
|
328
|
+
### Method 1: Data-Driven (Object Arrays) 📊
|
|
329
|
+
|
|
330
|
+
**Use when**: You have structured data and want simple, declarative option rendering.
|
|
331
|
+
|
|
332
|
+
**Advantages**:
|
|
333
|
+
- ✅ Simple and declarative - React-friendly
|
|
334
|
+
- ✅ Auto-conversion from strings/numbers
|
|
335
|
+
- ✅ Perfect for basic dropdowns
|
|
336
|
+
- ✅ Works seamlessly with React state
|
|
337
|
+
- ✅ Extremely performant (millions of items)
|
|
338
|
+
- ✅ Built-in search and filtering
|
|
339
|
+
- ✅ Full TypeScript type safety
|
|
340
|
+
|
|
341
|
+
**Examples**:
|
|
342
|
+
|
|
343
|
+
```tsx
|
|
344
|
+
import { Select } from '@smilodon/react';
|
|
345
|
+
import { useState } from 'react';
|
|
346
|
+
|
|
347
|
+
// Example 1: Simple object array
|
|
348
|
+
function SimpleExample() {
|
|
349
|
+
const [value, setValue] = useState('');
|
|
350
|
+
|
|
351
|
+
const items = [
|
|
352
|
+
{ value: '1', label: 'Apple' },
|
|
353
|
+
{ value: '2', label: 'Banana' },
|
|
354
|
+
{ value: '3', label: 'Cherry' }
|
|
355
|
+
];
|
|
356
|
+
|
|
357
|
+
return (
|
|
358
|
+
<Select
|
|
359
|
+
items={items}
|
|
360
|
+
value={value}
|
|
361
|
+
onChange={(val) => setValue(val as string)}
|
|
362
|
+
placeholder="Select a fruit..."
|
|
363
|
+
/>
|
|
364
|
+
);
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
// Example 2: With metadata and disabled options
|
|
368
|
+
function MetadataExample() {
|
|
369
|
+
const [country, setCountry] = useState('');
|
|
370
|
+
|
|
371
|
+
const countries = [
|
|
372
|
+
{ value: 'us', label: 'United States', disabled: false },
|
|
373
|
+
{ value: 'ca', label: 'Canada', disabled: false },
|
|
374
|
+
{ value: 'mx', label: 'Mexico', disabled: true }
|
|
375
|
+
];
|
|
376
|
+
|
|
377
|
+
return (
|
|
378
|
+
<Select
|
|
379
|
+
items={countries}
|
|
380
|
+
value={country}
|
|
381
|
+
onChange={(val) => setCountry(val as string)}
|
|
382
|
+
placeholder="Select a country..."
|
|
383
|
+
/>
|
|
384
|
+
);
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
// Example 3: With grouping
|
|
388
|
+
function GroupedExample() {
|
|
389
|
+
const [food, setFood] = useState('');
|
|
390
|
+
|
|
391
|
+
const foods = [
|
|
392
|
+
{ value: 'apple', label: 'Apple', group: 'Fruits' },
|
|
393
|
+
{ value: 'banana', label: 'Banana', group: 'Fruits' },
|
|
394
|
+
{ value: 'carrot', label: 'Carrot', group: 'Vegetables' },
|
|
395
|
+
{ value: 'broccoli', label: 'Broccoli', group: 'Vegetables' }
|
|
396
|
+
];
|
|
397
|
+
|
|
398
|
+
return (
|
|
399
|
+
<Select
|
|
400
|
+
items={foods}
|
|
401
|
+
value={food}
|
|
402
|
+
onChange={(val) => setFood(val as string)}
|
|
403
|
+
placeholder="Select food..."
|
|
404
|
+
/>
|
|
405
|
+
);
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
// Example 4: Auto-conversion from strings
|
|
409
|
+
function StringArrayExample() {
|
|
410
|
+
const [color, setColor] = useState('');
|
|
411
|
+
|
|
412
|
+
const colors = ['Red', 'Green', 'Blue', 'Yellow'];
|
|
413
|
+
|
|
414
|
+
return (
|
|
415
|
+
<Select
|
|
416
|
+
items={colors}
|
|
417
|
+
value={color}
|
|
418
|
+
onChange={(val) => setColor(val as string)}
|
|
419
|
+
placeholder="Select a color..."
|
|
420
|
+
/>
|
|
421
|
+
);
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
// Example 5: Auto-conversion from numbers
|
|
425
|
+
function NumberArrayExample() {
|
|
426
|
+
const [size, setSize] = useState<number | string>('');
|
|
427
|
+
|
|
428
|
+
const sizes = [10, 20, 30, 40, 50];
|
|
429
|
+
|
|
430
|
+
return (
|
|
431
|
+
<Select
|
|
432
|
+
items={sizes}
|
|
433
|
+
value={size}
|
|
434
|
+
onChange={(val) => setSize(val as number)}
|
|
435
|
+
placeholder="Select size..."
|
|
436
|
+
/>
|
|
437
|
+
);
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
// Example 6: Large datasets with useMemo
|
|
441
|
+
function LargeDatasetExample() {
|
|
442
|
+
const [id, setId] = useState('');
|
|
443
|
+
|
|
444
|
+
const items = useMemo(
|
|
445
|
+
() =>
|
|
446
|
+
Array.from({ length: 100_000 }, (_, i) => ({
|
|
447
|
+
value: i.toString(),
|
|
448
|
+
label: `Item ${i + 1}`
|
|
449
|
+
})),
|
|
450
|
+
[]
|
|
451
|
+
);
|
|
452
|
+
|
|
453
|
+
return (
|
|
454
|
+
<Select
|
|
455
|
+
items={items}
|
|
456
|
+
value={id}
|
|
457
|
+
onChange={(val) => setId(val as string)}
|
|
458
|
+
virtualized // Enable virtual scrolling for performance
|
|
459
|
+
placeholder="Select from 100K items..."
|
|
460
|
+
/>
|
|
461
|
+
);
|
|
462
|
+
}
|
|
463
|
+
```
|
|
464
|
+
|
|
465
|
+
### Method 2: Component-Driven (Custom Renderers) 🎨
|
|
466
|
+
|
|
467
|
+
**Use when**: You need rich, interactive option content with custom React components.
|
|
468
|
+
|
|
469
|
+
**Advantages**:
|
|
470
|
+
- ✅ Full control over option rendering
|
|
471
|
+
- ✅ Render any React component
|
|
472
|
+
- ✅ Rich content (images, icons, badges, multi-line text)
|
|
473
|
+
- ✅ Custom styling with CSS-in-JS or Tailwind
|
|
474
|
+
- ✅ Interactive elements within options
|
|
475
|
+
- ✅ Conditional rendering based on item data
|
|
476
|
+
- ✅ Access to React hooks and context
|
|
477
|
+
- ✅ Perfect for complex UIs (user cards, product listings, etc.)
|
|
478
|
+
|
|
479
|
+
**How it works**: Provide a `customRenderer` function that returns `React.ReactNode` for each option.
|
|
480
|
+
|
|
481
|
+
**Examples**:
|
|
482
|
+
|
|
483
|
+
```tsx
|
|
484
|
+
import { Select, SelectItem } from '@smilodon/react';
|
|
485
|
+
import { useState } from 'react';
|
|
486
|
+
|
|
487
|
+
// Example 1: Simple custom template with icons
|
|
488
|
+
interface Language extends SelectItem {
|
|
489
|
+
icon: string;
|
|
490
|
+
description: string;
|
|
491
|
+
}
|
|
492
|
+
|
|
493
|
+
function LanguageSelect() {
|
|
494
|
+
const [lang, setLang] = useState('');
|
|
495
|
+
|
|
496
|
+
const languages: Language[] = [
|
|
497
|
+
{ value: 'js', label: 'JavaScript', icon: '🟨', description: 'Dynamic scripting language' },
|
|
498
|
+
{ value: 'py', label: 'Python', icon: '🐍', description: 'General-purpose programming' },
|
|
499
|
+
{ value: 'rs', label: 'Rust', icon: '🦀', description: 'Systems programming language' }
|
|
500
|
+
];
|
|
501
|
+
|
|
502
|
+
return (
|
|
503
|
+
<Select
|
|
504
|
+
items={languages}
|
|
505
|
+
value={lang}
|
|
506
|
+
onChange={(val) => setLang(val as string)}
|
|
507
|
+
customRenderer={(item: Language, index) => (
|
|
508
|
+
<div style={{ display: 'flex', alignItems: 'center', gap: 12 }}>
|
|
509
|
+
<span style={{ fontSize: 24 }}>{item.icon}</span>
|
|
510
|
+
<div>
|
|
511
|
+
<div style={{ fontWeight: 600 }}>{item.label}</div>
|
|
512
|
+
<div style={{ fontSize: 12, color: '#6b7280' }}>{item.description}</div>
|
|
513
|
+
</div>
|
|
514
|
+
</div>
|
|
515
|
+
)}
|
|
516
|
+
placeholder="Select a language..."
|
|
517
|
+
/>
|
|
518
|
+
);
|
|
519
|
+
}
|
|
520
|
+
|
|
521
|
+
// Example 2: User selection with avatars
|
|
522
|
+
interface User extends SelectItem {
|
|
523
|
+
email: string;
|
|
524
|
+
avatar: string;
|
|
525
|
+
role: 'Admin' | 'User' | 'Moderator';
|
|
526
|
+
}
|
|
527
|
+
|
|
528
|
+
function UserSelect() {
|
|
529
|
+
const [userId, setUserId] = useState('');
|
|
530
|
+
|
|
531
|
+
const users: User[] = [
|
|
532
|
+
{
|
|
533
|
+
value: '1',
|
|
534
|
+
label: 'John Doe',
|
|
535
|
+
email: 'john@example.com',
|
|
536
|
+
avatar: 'https://i.pravatar.cc/150?img=1',
|
|
537
|
+
role: 'Admin'
|
|
538
|
+
},
|
|
539
|
+
{
|
|
540
|
+
value: '2',
|
|
541
|
+
label: 'Jane Smith',
|
|
542
|
+
email: 'jane@example.com',
|
|
543
|
+
avatar: 'https://i.pravatar.cc/150?img=2',
|
|
544
|
+
role: 'User'
|
|
545
|
+
},
|
|
546
|
+
{
|
|
547
|
+
value: '3',
|
|
548
|
+
label: 'Bob Johnson',
|
|
549
|
+
email: 'bob@example.com',
|
|
550
|
+
avatar: 'https://i.pravatar.cc/150?img=3',
|
|
551
|
+
role: 'Moderator'
|
|
552
|
+
}
|
|
553
|
+
];
|
|
554
|
+
|
|
555
|
+
return (
|
|
556
|
+
<Select
|
|
557
|
+
items={users}
|
|
558
|
+
value={userId}
|
|
559
|
+
onChange={(val) => setUserId(val as string)}
|
|
560
|
+
customRenderer={(item: User) => (
|
|
561
|
+
<div style={{ display: 'flex', alignItems: 'center', gap: 12, padding: '4px 0' }}>
|
|
562
|
+
<img
|
|
563
|
+
src={item.avatar}
|
|
564
|
+
alt={item.label}
|
|
565
|
+
style={{
|
|
566
|
+
width: 40,
|
|
567
|
+
height: 40,
|
|
568
|
+
borderRadius: '50%',
|
|
569
|
+
objectFit: 'cover'
|
|
570
|
+
}}
|
|
571
|
+
/>
|
|
572
|
+
<div style={{ flex: 1 }}>
|
|
573
|
+
<div style={{ fontWeight: 600, color: '#1f2937' }}>{item.label}</div>
|
|
574
|
+
<div style={{ fontSize: 13, color: '#6b7280' }}>{item.email}</div>
|
|
575
|
+
</div>
|
|
576
|
+
<span
|
|
577
|
+
style={{
|
|
578
|
+
padding: '4px 8px',
|
|
579
|
+
background: item.role === 'Admin' ? '#dbeafe' : '#f3f4f6',
|
|
580
|
+
color: item.role === 'Admin' ? '#1e40af' : '#374151',
|
|
581
|
+
borderRadius: 12,
|
|
582
|
+
fontSize: 11,
|
|
583
|
+
fontWeight: 600
|
|
584
|
+
}}
|
|
585
|
+
>
|
|
586
|
+
{item.role}
|
|
587
|
+
</span>
|
|
588
|
+
</div>
|
|
589
|
+
)}
|
|
590
|
+
placeholder="Select a user..."
|
|
591
|
+
/>
|
|
592
|
+
);
|
|
593
|
+
}
|
|
594
|
+
|
|
595
|
+
// Example 3: Product selection with images and pricing
|
|
596
|
+
interface Product extends SelectItem {
|
|
597
|
+
price: number;
|
|
598
|
+
stock: number;
|
|
599
|
+
image: string;
|
|
600
|
+
badge?: string;
|
|
601
|
+
}
|
|
602
|
+
|
|
603
|
+
function ProductSelect() {
|
|
604
|
+
const [productId, setProductId] = useState('');
|
|
605
|
+
|
|
606
|
+
const products: Product[] = [
|
|
607
|
+
{
|
|
608
|
+
value: 'p1',
|
|
609
|
+
label: 'Premium Laptop',
|
|
610
|
+
price: 1299.99,
|
|
611
|
+
stock: 15,
|
|
612
|
+
image: 'https://via.placeholder.com/60',
|
|
613
|
+
badge: 'Best Seller'
|
|
614
|
+
},
|
|
615
|
+
{
|
|
616
|
+
value: 'p2',
|
|
617
|
+
label: 'Wireless Mouse',
|
|
618
|
+
price: 29.99,
|
|
619
|
+
stock: 150,
|
|
620
|
+
image: 'https://via.placeholder.com/60'
|
|
621
|
+
},
|
|
622
|
+
{
|
|
623
|
+
value: 'p3',
|
|
624
|
+
label: 'Mechanical Keyboard',
|
|
625
|
+
price: 89.99,
|
|
626
|
+
stock: 0,
|
|
627
|
+
image: 'https://via.placeholder.com/60',
|
|
628
|
+
badge: 'Out of Stock'
|
|
629
|
+
}
|
|
630
|
+
];
|
|
631
|
+
|
|
632
|
+
return (
|
|
633
|
+
<Select
|
|
634
|
+
items={products}
|
|
635
|
+
value={productId}
|
|
636
|
+
onChange={(val) => setProductId(val as string)}
|
|
637
|
+
customRenderer={(item: Product) => (
|
|
638
|
+
<div
|
|
639
|
+
style={{
|
|
640
|
+
display: 'flex',
|
|
641
|
+
alignItems: 'center',
|
|
642
|
+
gap: 12,
|
|
643
|
+
opacity: item.stock === 0 ? 0.5 : 1
|
|
644
|
+
}}
|
|
645
|
+
>
|
|
646
|
+
<img
|
|
647
|
+
src={item.image}
|
|
648
|
+
alt={item.label}
|
|
649
|
+
style={{
|
|
650
|
+
width: 60,
|
|
651
|
+
height: 60,
|
|
652
|
+
borderRadius: 8,
|
|
653
|
+
objectFit: 'cover',
|
|
654
|
+
border: '1px solid #e5e7eb'
|
|
655
|
+
}}
|
|
656
|
+
/>
|
|
657
|
+
<div style={{ flex: 1 }}>
|
|
658
|
+
<div style={{ display: 'flex', alignItems: 'center', gap: 8 }}>
|
|
659
|
+
<span style={{ fontWeight: 600, color: '#1f2937' }}>{item.label}</span>
|
|
660
|
+
{item.badge && (
|
|
661
|
+
<span
|
|
662
|
+
style={{
|
|
663
|
+
padding: '2px 6px',
|
|
664
|
+
background: item.badge === 'Best Seller' ? '#dcfce7' : '#fee2e2',
|
|
665
|
+
color: item.badge === 'Best Seller' ? '#166534' : '#991b1b',
|
|
666
|
+
borderRadius: 4,
|
|
667
|
+
fontSize: 10,
|
|
668
|
+
fontWeight: 600
|
|
669
|
+
}}
|
|
670
|
+
>
|
|
671
|
+
{item.badge}
|
|
672
|
+
</span>
|
|
673
|
+
)}
|
|
674
|
+
</div>
|
|
675
|
+
<div
|
|
676
|
+
style={{
|
|
677
|
+
marginTop: 4,
|
|
678
|
+
display: 'flex',
|
|
679
|
+
justifyContent: 'space-between',
|
|
680
|
+
alignItems: 'center'
|
|
681
|
+
}}
|
|
682
|
+
>
|
|
683
|
+
<span style={{ fontSize: 16, fontWeight: 700, color: '#059669' }}>
|
|
684
|
+
${item.price.toFixed(2)}
|
|
685
|
+
</span>
|
|
686
|
+
<span style={{ fontSize: 12, color: '#6b7280' }}>
|
|
687
|
+
{item.stock > 0 ? `${item.stock} in stock` : 'Out of stock'}
|
|
688
|
+
</span>
|
|
689
|
+
</div>
|
|
690
|
+
</div>
|
|
691
|
+
</div>
|
|
692
|
+
)}
|
|
693
|
+
placeholder="Select a product..."
|
|
694
|
+
/>
|
|
695
|
+
);
|
|
696
|
+
}
|
|
697
|
+
|
|
698
|
+
// Example 4: Status indicators with conditional styling
|
|
699
|
+
interface Task extends SelectItem {
|
|
700
|
+
status: 'completed' | 'in-progress' | 'pending';
|
|
701
|
+
priority: 'high' | 'medium' | 'low';
|
|
702
|
+
assignee: string;
|
|
703
|
+
}
|
|
704
|
+
|
|
705
|
+
function TaskSelect() {
|
|
706
|
+
const [taskId, setTaskId] = useState('');
|
|
707
|
+
|
|
708
|
+
const tasks: Task[] = [
|
|
709
|
+
{ value: 't1', label: 'Design Homepage', status: 'completed', priority: 'high', assignee: 'John' },
|
|
710
|
+
{ value: 't2', label: 'API Integration', status: 'in-progress', priority: 'high', assignee: 'Jane' },
|
|
711
|
+
{ value: 't3', label: 'Write Documentation', status: 'pending', priority: 'medium', assignee: 'Bob' },
|
|
712
|
+
{ value: 't4', label: 'Bug Fixes', status: 'in-progress', priority: 'low', assignee: 'Alice' }
|
|
713
|
+
];
|
|
714
|
+
|
|
715
|
+
const statusConfig = {
|
|
716
|
+
'completed': { bg: '#dcfce7', color: '#166534', icon: '✓' },
|
|
717
|
+
'in-progress': { bg: '#dbeafe', color: '#1e40af', icon: '⟳' },
|
|
718
|
+
'pending': { bg: '#fef3c7', color: '#92400e', icon: '○' }
|
|
719
|
+
};
|
|
720
|
+
|
|
721
|
+
const priorityColors = {
|
|
722
|
+
'high': '#ef4444',
|
|
723
|
+
'medium': '#f59e0b',
|
|
724
|
+
'low': '#10b981'
|
|
725
|
+
};
|
|
726
|
+
|
|
727
|
+
return (
|
|
728
|
+
<Select
|
|
729
|
+
items={tasks}
|
|
730
|
+
value={taskId}
|
|
731
|
+
onChange={(val) => setTaskId(val as string)}
|
|
732
|
+
customRenderer={(item: Task) => {
|
|
733
|
+
const status = statusConfig[item.status];
|
|
734
|
+
return (
|
|
735
|
+
<div style={{ display: 'flex', alignItems: 'center', gap: 10, padding: '4px 0' }}>
|
|
736
|
+
<div
|
|
737
|
+
style={{
|
|
738
|
+
width: 24,
|
|
739
|
+
height: 24,
|
|
740
|
+
borderRadius: '50%',
|
|
741
|
+
background: status.bg,
|
|
742
|
+
color: status.color,
|
|
743
|
+
display: 'flex',
|
|
744
|
+
alignItems: 'center',
|
|
745
|
+
justifyContent: 'center',
|
|
746
|
+
fontWeight: 'bold'
|
|
747
|
+
}}
|
|
748
|
+
>
|
|
749
|
+
{status.icon}
|
|
750
|
+
</div>
|
|
751
|
+
<div style={{ flex: 1 }}>
|
|
752
|
+
<div style={{ fontWeight: 600, color: '#1f2937' }}>{item.label}</div>
|
|
753
|
+
<div style={{ fontSize: 12, color: '#6b7280', marginTop: 2 }}>
|
|
754
|
+
Assigned to {item.assignee}
|
|
755
|
+
</div>
|
|
756
|
+
</div>
|
|
757
|
+
<div
|
|
758
|
+
style={{
|
|
759
|
+
width: 8,
|
|
760
|
+
height: 8,
|
|
761
|
+
borderRadius: '50%',
|
|
762
|
+
background: priorityColors[item.priority]
|
|
763
|
+
}}
|
|
764
|
+
title={`${item.priority} priority`}
|
|
765
|
+
/>
|
|
766
|
+
</div>
|
|
767
|
+
);
|
|
768
|
+
}}
|
|
769
|
+
placeholder="Select a task..."
|
|
770
|
+
/>
|
|
771
|
+
);
|
|
772
|
+
}
|
|
773
|
+
|
|
774
|
+
// Example 5: Using Tailwind CSS (if available)
|
|
775
|
+
interface Tag extends SelectItem {
|
|
776
|
+
color: string;
|
|
777
|
+
count: number;
|
|
778
|
+
}
|
|
779
|
+
|
|
780
|
+
function TailwindExample() {
|
|
781
|
+
const [tag, setTag] = useState('');
|
|
782
|
+
|
|
783
|
+
const tags: Tag[] = [
|
|
784
|
+
{ value: 'react', label: 'React', color: 'blue', count: 1250 },
|
|
785
|
+
{ value: 'vue', label: 'Vue', color: 'green', count: 850 },
|
|
786
|
+
{ value: 'angular', label: 'Angular', color: 'red', count: 420 }
|
|
787
|
+
];
|
|
788
|
+
|
|
789
|
+
return (
|
|
790
|
+
<Select
|
|
791
|
+
items={tags}
|
|
792
|
+
value={tag}
|
|
793
|
+
onChange={(val) => setTag(val as string)}
|
|
794
|
+
customRenderer={(item: Tag) => (
|
|
795
|
+
<div className="flex items-center justify-between p-2">
|
|
796
|
+
<div className="flex items-center gap-2">
|
|
797
|
+
<span className={`w-3 h-3 rounded-full bg-${item.color}-500`} />
|
|
798
|
+
<span className="font-semibold text-gray-900">{item.label}</span>
|
|
799
|
+
</div>
|
|
800
|
+
<span className="text-sm text-gray-500">{item.count} posts</span>
|
|
801
|
+
</div>
|
|
802
|
+
)}
|
|
803
|
+
placeholder="Select a tag..."
|
|
804
|
+
/>
|
|
805
|
+
);
|
|
806
|
+
}
|
|
807
|
+
|
|
808
|
+
// Example 6: With React components
|
|
809
|
+
import { Badge } from './Badge'; // Your custom component
|
|
810
|
+
|
|
811
|
+
function ComponentExample() {
|
|
812
|
+
const [value, setValue] = useState('');
|
|
813
|
+
|
|
814
|
+
const items = [
|
|
815
|
+
{ value: '1', label: 'Feature', type: 'success', count: 12 },
|
|
816
|
+
{ value: '2', label: 'Bug', type: 'error', count: 5 },
|
|
817
|
+
{ value: '3', label: 'Enhancement', type: 'info', count: 8 }
|
|
818
|
+
];
|
|
819
|
+
|
|
820
|
+
return (
|
|
821
|
+
<Select
|
|
822
|
+
items={items}
|
|
823
|
+
value={value}
|
|
824
|
+
onChange={(val) => setValue(val as string)}
|
|
825
|
+
customRenderer={(item: any) => (
|
|
826
|
+
<div className="flex items-center justify-between">
|
|
827
|
+
<span>{item.label}</span>
|
|
828
|
+
<Badge type={item.type}>{item.count}</Badge>
|
|
829
|
+
</div>
|
|
830
|
+
)}
|
|
831
|
+
/>
|
|
832
|
+
);
|
|
833
|
+
}
|
|
834
|
+
```
|
|
835
|
+
|
|
836
|
+
### Comparison: When to Use Each Method
|
|
837
|
+
|
|
838
|
+
| Feature | Method 1: Object Arrays | Method 2: Custom Renderers |
|
|
839
|
+
|---------|------------------------|---------------------------|
|
|
840
|
+
| **Setup Complexity** | ⭐ Simple | ⭐⭐ Moderate |
|
|
841
|
+
| **Rendering Speed** | ⭐⭐⭐ Fastest | ⭐⭐ Fast |
|
|
842
|
+
| **Visual Customization** | ⭐⭐ Limited | ⭐⭐⭐ Unlimited |
|
|
843
|
+
| **React Integration** | ⭐⭐⭐ Seamless | ⭐⭐⭐ Seamless |
|
|
844
|
+
| **Component Reuse** | ⭐ Limited | ⭐⭐⭐ Full |
|
|
845
|
+
| **TypeScript Support** | ⭐⭐⭐ Full | ⭐⭐⭐ Full |
|
|
846
|
+
| **Performance (1M items)** | ⭐⭐⭐ Excellent | ⭐⭐ Good |
|
|
847
|
+
| **Learning Curve** | ⭐ Easy | ⭐⭐ Medium |
|
|
848
|
+
|
|
849
|
+
**Best Practices**:
|
|
850
|
+
|
|
851
|
+
✅ **Use Method 1 (Object Arrays) when**:
|
|
852
|
+
- You need simple text-based options
|
|
853
|
+
- Performance is critical (millions of items)
|
|
854
|
+
- You want minimal code
|
|
855
|
+
- Built-in search/filter is sufficient
|
|
856
|
+
- Working with external APIs returning plain data
|
|
857
|
+
|
|
858
|
+
✅ **Use Method 2 (Custom Renderers) when**:
|
|
859
|
+
- You need images, icons, or badges
|
|
860
|
+
- Options require multiple lines of text
|
|
861
|
+
- Custom styling/layout is important
|
|
862
|
+
- Reusing existing React components
|
|
863
|
+
- Conditional rendering based on data
|
|
864
|
+
- Rich user experience is priority
|
|
865
|
+
- Using CSS-in-JS or Tailwind CSS
|
|
866
|
+
|
|
867
|
+
### Combining Both Methods
|
|
868
|
+
|
|
869
|
+
You can start with Method 1 and add Method 2 later as your UI evolves:
|
|
870
|
+
|
|
871
|
+
```tsx
|
|
872
|
+
import { Select } from '@smilodon/react';
|
|
873
|
+
import { useState } from 'react';
|
|
874
|
+
|
|
875
|
+
function EvolvingComponent() {
|
|
876
|
+
const [value, setValue] = useState('');
|
|
877
|
+
|
|
878
|
+
// Start simple
|
|
879
|
+
const items = ['Option 1', 'Option 2', 'Option 3'];
|
|
880
|
+
|
|
881
|
+
return (
|
|
882
|
+
<Select
|
|
883
|
+
items={items}
|
|
884
|
+
value={value}
|
|
885
|
+
onChange={(val) => setValue(val as string)}
|
|
886
|
+
// Later, add custom rendering without changing items
|
|
887
|
+
customRenderer={(item, index) => (
|
|
888
|
+
<div style={{ padding: 8, background: index % 2 ? '#f9fafb' : 'white' }}>
|
|
889
|
+
<strong>{item.label || item}</strong>
|
|
890
|
+
</div>
|
|
891
|
+
)}
|
|
892
|
+
/>
|
|
893
|
+
);
|
|
894
|
+
}
|
|
895
|
+
```
|
|
896
|
+
|
|
897
|
+
### Performance Tips
|
|
898
|
+
|
|
899
|
+
**For Method 1**:
|
|
900
|
+
- Use `useMemo` to memoize large item arrays
|
|
901
|
+
- Enable `virtualized` prop for 1000+ items
|
|
902
|
+
- Enable `infiniteScroll` for dynamic loading
|
|
903
|
+
|
|
904
|
+
**For Method 2**:
|
|
905
|
+
- Use `useCallback` to memoize renderer function
|
|
906
|
+
- Keep renderer pure (no side effects)
|
|
907
|
+
- Avoid heavy computations in renderer
|
|
908
|
+
- Use React.memo for complex nested components
|
|
909
|
+
|
|
910
|
+
```tsx
|
|
911
|
+
import { Select } from '@smilodon/react';
|
|
912
|
+
import { useState, useMemo, useCallback } from 'react';
|
|
913
|
+
|
|
914
|
+
function OptimizedExample() {
|
|
915
|
+
const [value, setValue] = useState('');
|
|
916
|
+
|
|
917
|
+
// Memoize items
|
|
918
|
+
const items = useMemo(
|
|
919
|
+
() =>
|
|
920
|
+
Array.from({ length: 10000 }, (_, i) => ({
|
|
921
|
+
value: i.toString(),
|
|
922
|
+
label: `Item ${i + 1}`,
|
|
923
|
+
description: `Description for item ${i + 1}`
|
|
924
|
+
})),
|
|
925
|
+
[]
|
|
926
|
+
);
|
|
927
|
+
|
|
928
|
+
// Memoize renderer
|
|
929
|
+
const renderer = useCallback((item: any, index: number) => (
|
|
930
|
+
<div>
|
|
931
|
+
<div style={{ fontWeight: 600 }}>{item.label}</div>
|
|
932
|
+
<div style={{ fontSize: 12, color: '#666' }}>{item.description}</div>
|
|
933
|
+
</div>
|
|
934
|
+
), []);
|
|
935
|
+
|
|
936
|
+
return (
|
|
937
|
+
<Select
|
|
938
|
+
items={items}
|
|
939
|
+
value={value}
|
|
940
|
+
onChange={(val) => setValue(val as string)}
|
|
941
|
+
customRenderer={renderer}
|
|
942
|
+
virtualized // Enable virtual scrolling
|
|
943
|
+
estimatedItemHeight={60}
|
|
944
|
+
/>
|
|
945
|
+
);
|
|
946
|
+
}
|
|
947
|
+
```
|
|
948
|
+
|
|
949
|
+
---
|
|
950
|
+
|
|
304
951
|
## API Reference
|
|
305
952
|
|
|
306
953
|
### Props
|
package/package.json
CHANGED