|
1 | 1 | --- |
2 | | -title: Components Overview |
| 2 | +title: ReactSelection |
3 | 3 | order: 0 |
4 | 4 | --- |
5 | 5 |
|
6 | | -# Components |
| 6 | +# ReactSelection |
7 | 7 |
|
8 | | -This section is for documenting your components. |
| 8 | +A headless, type-safe selection component for React with slot-based architecture. |
9 | 9 |
|
10 | | -## Structure |
| 10 | +## When To Use |
11 | 11 |
|
12 | | -Organize your component documentation in this directory: |
| 12 | +- You need single or multiple selection without being tied to a specific UI framework |
| 13 | +- You want full control over the visual presentation of selection items |
| 14 | +- You need type-safe selection with TypeScript generics |
| 15 | +- You need configurable max selection limits with error handling |
13 | 16 |
|
14 | | -``` |
15 | | -docs/ |
16 | | -└── components/ |
17 | | - ├── index.md # This file |
18 | | - ├── button.md # Button component docs |
19 | | - ├── input.md # Input component docs |
20 | | - └── ... |
21 | | -``` |
| 17 | +## Main Exports |
22 | 18 |
|
23 | | -## Component Documentation Template |
| 19 | +| Export | Type | Description | |
| 20 | +| ------------------------ | --------- | ------------------------------------------ | |
| 21 | +| `ReactSelection` | Component | The main selection component | |
| 22 | +| `ReactSelectionProps` | Interface | Props type for the component | |
| 23 | +| `SelectionItemSlotProps` | Interface | Props passed to the item slot | |
| 24 | +| `Slot` | Type | Slot type (component, function, or object) | |
| 25 | +| `ErrorCode` | Enum | Error codes for selection errors | |
24 | 26 |
|
25 | | -```markdown |
26 | | ---- |
27 | | -title: ComponentName |
28 | | -order: 1 |
29 | | ---- |
| 27 | +## API |
30 | 28 |
|
31 | | -# ComponentName |
| 29 | +### ReactSelection Props |
32 | 30 |
|
33 | | -Brief description of the component. |
| 31 | +Extends `Omit<ReactListProps<T>, 'slots'>` plus: |
34 | 32 |
|
35 | | -## When To Use |
| 33 | +| Property | Description | Type | Default | |
| 34 | +| --------------- | -------------------------------------------------- | ----------------------------------------------------------- | ------------------------------ | |
| 35 | +| `data` | Array of data items (each must have `value` field) | `T[]` | - | |
| 36 | +| `keyExtractor` | Custom key for list items | `keyof T \| ((item: T, index: number) => string \| number)` | `item.value` | |
| 37 | +| `allowDeselect` | Allow deselecting in single selection mode | `boolean` | `false` | |
| 38 | +| `max` | Maximum selections allowed (multiple mode) | `number` | `1000` | |
| 39 | +| `multiple` | Enable multiple selection | `boolean` | `false` | |
| 40 | +| `value` | Current selected value | `any` | `null` (or `[]` when multiple) | |
| 41 | +| `onChange` | Callback when selection changes | `(value: any) => void` | - | |
| 42 | +| `onError` | Callback when an error occurs | `(error: { code: ErrorCode }) => void` | - | |
| 43 | +| `slots` | Slot configuration (see below) | `SelectionSlots<T>` | - | |
36 | 44 |
|
37 | | -- Use case 1 |
38 | | -- Use case 2 |
| 45 | +### SelectionItemSlotProps |
39 | 46 |
|
40 | | -## Examples |
| 47 | +Props received by the `slots.item` slot: |
41 | 48 |
|
42 | | -### Basic Usage |
| 49 | +| Property | Description | Type | |
| 50 | +| ---------- | ------------------------------------------- | ------------ | |
| 51 | +| `item` | The current data item | `T` | |
| 52 | +| `index` | Index in the data array | `number` | |
| 53 | +| `data` | The full data array | `T[]` | |
| 54 | +| `active` | Whether this item is currently selected | `boolean` | |
| 55 | +| `disabled` | Whether this item is disabled (max reached) | `boolean` | |
| 56 | +| `onClick` | Handler to select/deselect this item | `() => void` | |
43 | 57 |
|
44 | | -```tsx |
45 | | -import { ComponentName } from 'your-package'; |
| 58 | +### ErrorCode Enum |
| 59 | + |
| 60 | +| Value | Description | |
| 61 | +| ------------------ | ------------------------------------------------------- | |
| 62 | +| `MAX_LIMIT_EXCEED` | Selection count has exceeded the configured `max` value | |
| 63 | + |
| 64 | +### Slots Configuration |
46 | 65 |
|
47 | | -<ComponentName /> |
| 66 | +```typescript |
| 67 | +interface SelectionSlots<T> { |
| 68 | + item: Slot<SelectionItemSlotProps<T>>; // Required |
| 69 | + empty?: Slot<{ data: T[] }>; // Optional |
| 70 | +} |
48 | 71 | ``` |
49 | 72 |
|
50 | | -## API |
| 73 | +A `Slot` can be: |
| 74 | + |
| 75 | +1. **Component**: `MyComponent` |
| 76 | +2. **Render function**: `({ item, active, onClick }) => <button>...</button>` |
| 77 | +3. **Object with props**: `{ component: MyComponent, props: { className: 'item' } }` |
| 78 | + |
| 79 | +## Examples |
| 80 | + |
| 81 | +### Single Selection |
51 | 82 |
|
52 | | -| Property | Description | Type | Default | |
53 | | -|----------|-------------|------|---------| |
54 | | -| prop1 | Description | `string` | - | |
55 | | -| prop2 | Description | `number` | `0` | |
| 83 | +```tsx |
| 84 | +import { ReactSelection } from '@jswork/react-selection'; |
| 85 | + |
| 86 | +const items = [ |
| 87 | + { value: 'apple', label: 'Apple' }, |
| 88 | + { value: 'banana', label: 'Banana' }, |
| 89 | + { value: 'orange', label: 'Orange' }, |
| 90 | +]; |
| 91 | + |
| 92 | +function App() { |
| 93 | + const [selected, setSelected] = useState('apple'); |
| 94 | + |
| 95 | + return ( |
| 96 | + <ReactSelection |
| 97 | + data={items} |
| 98 | + value={selected} |
| 99 | + onChange={setSelected} |
| 100 | + slots={{ |
| 101 | + item: ({ item, active, onClick }) => ( |
| 102 | + <button |
| 103 | + className={active ? 'btn-primary' : 'btn-default'} |
| 104 | + onClick={onClick} |
| 105 | + > |
| 106 | + {item.label} |
| 107 | + </button> |
| 108 | + ), |
| 109 | + }} |
| 110 | + /> |
| 111 | + ); |
| 112 | +} |
56 | 113 | ``` |
57 | 114 |
|
58 | | -## Writing Component Docs |
| 115 | +### Multiple Selection with Max Limit |
59 | 116 |
|
60 | | -1. Create a markdown file for each component |
61 | | -2. Use frontmatter to set title and order |
62 | | -3. Include code examples with syntax highlighting |
63 | | -4. Document all props with a table |
| 117 | +```tsx |
| 118 | +<ReactSelection |
| 119 | + multiple |
| 120 | + max={3} |
| 121 | + data={items} |
| 122 | + value={selectedItems} |
| 123 | + onChange={setSelectedItems} |
| 124 | + onError={(err) => { |
| 125 | + if (err.code === ErrorCode.MAX_LIMIT_EXCEED) { |
| 126 | + console.warn('Maximum selections reached'); |
| 127 | + } |
| 128 | + }} |
| 129 | + slots={{ |
| 130 | + item: ({ item, active, disabled, onClick }) => ( |
| 131 | + <button disabled={disabled} onClick={onClick}> |
| 132 | + {active ? '✓ ' : ''} |
| 133 | + {item.label} |
| 134 | + </button> |
| 135 | + ), |
| 136 | + }} |
| 137 | +/> |
| 138 | +``` |
64 | 139 |
|
65 | | -## Next Steps |
| 140 | +### With Empty State |
66 | 141 |
|
67 | | -- Explore specific component documentation |
68 | | -- Check out the [API Reference](/api) |
| 142 | +```tsx |
| 143 | +<ReactSelection |
| 144 | + data={items} |
| 145 | + value={selected} |
| 146 | + onChange={setSelected} |
| 147 | + slots={{ |
| 148 | + item: ({ item, active, onClick }) => ( |
| 149 | + <div onClick={onClick} className={active ? 'active' : ''}> |
| 150 | + {item.label} |
| 151 | + </div> |
| 152 | + ), |
| 153 | + empty: () => <div>No items available</div>, |
| 154 | + }} |
| 155 | +/> |
| 156 | +``` |
0 commit comments