@smilodon/react 1.0.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 +384 -0
- package/dist/index.cjs +248 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.js +226 -0
- package/dist/index.js.map +1 -0
- package/package.json +72 -0
package/README.md
ADDED
|
@@ -0,0 +1,384 @@
|
|
|
1
|
+
# @smilodon/react
|
|
2
|
+
|
|
3
|
+
Production-ready, accessible Select component for React applications.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- ✅ **Simple API** - Clean, intuitive props that feel natural in React
|
|
8
|
+
- ✅ **Fully Typed** - Complete TypeScript support with detailed type definitions
|
|
9
|
+
- ✅ **Controlled & Uncontrolled** - Works both ways, your choice
|
|
10
|
+
- ✅ **Single & Multi-select** - One prop to switch modes
|
|
11
|
+
- ✅ **Searchable** - Built-in filtering with customizable behavior
|
|
12
|
+
- ✅ **Infinite Scroll** - Handle massive datasets efficiently
|
|
13
|
+
- ✅ **Virtual Scrolling** - Render only visible items for performance
|
|
14
|
+
- ✅ **Grouped Options** - Organize items into categories
|
|
15
|
+
- ✅ **Accessible** - WCAG 2.1 AAA compliant, full keyboard navigation
|
|
16
|
+
- ✅ **Customizable** - Custom renderers, styles, and behaviors
|
|
17
|
+
- ✅ **Tiny Bundle** - Optimized for production
|
|
18
|
+
|
|
19
|
+
## Installation
|
|
20
|
+
|
|
21
|
+
```bash
|
|
22
|
+
npm install @smilodon/react @smilodon/core
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
or
|
|
26
|
+
|
|
27
|
+
```bash
|
|
28
|
+
yarn add @smilodon/react @smilodon/core
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
or
|
|
32
|
+
|
|
33
|
+
```bash
|
|
34
|
+
pnpm add @smilodon/react @smilodon/core
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
## Quick Start
|
|
38
|
+
|
|
39
|
+
```tsx
|
|
40
|
+
import { Select } from '@smilodon/react';
|
|
41
|
+
import { useState } from 'react';
|
|
42
|
+
|
|
43
|
+
function MyApp() {
|
|
44
|
+
const [value, setValue] = useState('');
|
|
45
|
+
|
|
46
|
+
return (
|
|
47
|
+
<Select
|
|
48
|
+
items={[
|
|
49
|
+
{ value: 'apple', label: 'Apple' },
|
|
50
|
+
{ value: 'banana', label: 'Banana' },
|
|
51
|
+
{ value: 'cherry', label: 'Cherry' },
|
|
52
|
+
]}
|
|
53
|
+
value={value}
|
|
54
|
+
onChange={(newValue) => setValue(newValue as string)}
|
|
55
|
+
placeholder="Select a fruit..."
|
|
56
|
+
/>
|
|
57
|
+
);
|
|
58
|
+
}
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
## Examples
|
|
62
|
+
|
|
63
|
+
### Basic Single Select
|
|
64
|
+
|
|
65
|
+
```tsx
|
|
66
|
+
import { Select } from '@smilodon/react';
|
|
67
|
+
|
|
68
|
+
function BasicExample() {
|
|
69
|
+
const [value, setValue] = useState('');
|
|
70
|
+
|
|
71
|
+
return (
|
|
72
|
+
<Select
|
|
73
|
+
items={[
|
|
74
|
+
{ value: '1', label: 'Option 1' },
|
|
75
|
+
{ value: '2', label: 'Option 2' },
|
|
76
|
+
{ value: '3', label: 'Option 3' },
|
|
77
|
+
]}
|
|
78
|
+
value={value}
|
|
79
|
+
onChange={(val) => setValue(val as string)}
|
|
80
|
+
placeholder="Choose an option"
|
|
81
|
+
/>
|
|
82
|
+
);
|
|
83
|
+
}
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
### Multi-Select
|
|
87
|
+
|
|
88
|
+
```tsx
|
|
89
|
+
import { Select } from '@smilodon/react';
|
|
90
|
+
|
|
91
|
+
function MultiSelectExample() {
|
|
92
|
+
const [values, setValues] = useState<Array<string | number>>([]);
|
|
93
|
+
|
|
94
|
+
return (
|
|
95
|
+
<Select
|
|
96
|
+
items={[
|
|
97
|
+
{ value: 'react', label: 'React' },
|
|
98
|
+
{ value: 'vue', label: 'Vue' },
|
|
99
|
+
{ value: 'angular', label: 'Angular' },
|
|
100
|
+
{ value: 'svelte', label: 'Svelte' },
|
|
101
|
+
]}
|
|
102
|
+
value={values}
|
|
103
|
+
onChange={(val) => setValues(val as Array<string | number>)}
|
|
104
|
+
multiple
|
|
105
|
+
placeholder="Select frameworks..."
|
|
106
|
+
/>
|
|
107
|
+
);
|
|
108
|
+
}
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
### Searchable Select
|
|
112
|
+
|
|
113
|
+
```tsx
|
|
114
|
+
import { Select } from '@smilodon/react';
|
|
115
|
+
|
|
116
|
+
function SearchableExample() {
|
|
117
|
+
const [value, setValue] = useState('');
|
|
118
|
+
|
|
119
|
+
return (
|
|
120
|
+
<Select
|
|
121
|
+
items={countries}
|
|
122
|
+
value={value}
|
|
123
|
+
onChange={(val) => setValue(val as string)}
|
|
124
|
+
searchable
|
|
125
|
+
placeholder="Search countries..."
|
|
126
|
+
/>
|
|
127
|
+
);
|
|
128
|
+
}
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
### Grouped Options
|
|
132
|
+
|
|
133
|
+
```tsx
|
|
134
|
+
import { Select } from '@smilodon/react';
|
|
135
|
+
|
|
136
|
+
function GroupedExample() {
|
|
137
|
+
const [value, setValue] = useState('');
|
|
138
|
+
|
|
139
|
+
return (
|
|
140
|
+
<Select
|
|
141
|
+
groupedItems={[
|
|
142
|
+
{
|
|
143
|
+
label: 'Fruits',
|
|
144
|
+
options: [
|
|
145
|
+
{ value: 'apple', label: 'Apple' },
|
|
146
|
+
{ value: 'banana', label: 'Banana' },
|
|
147
|
+
],
|
|
148
|
+
},
|
|
149
|
+
{
|
|
150
|
+
label: 'Vegetables',
|
|
151
|
+
options: [
|
|
152
|
+
{ value: 'carrot', label: 'Carrot' },
|
|
153
|
+
{ value: 'tomato', label: 'Tomato' },
|
|
154
|
+
],
|
|
155
|
+
},
|
|
156
|
+
]}
|
|
157
|
+
value={value}
|
|
158
|
+
onChange={(val) => setValue(val as string)}
|
|
159
|
+
placeholder="Select food..."
|
|
160
|
+
/>
|
|
161
|
+
);
|
|
162
|
+
}
|
|
163
|
+
```
|
|
164
|
+
|
|
165
|
+
### Infinite Scroll
|
|
166
|
+
|
|
167
|
+
```tsx
|
|
168
|
+
import { Select } from '@smilodon/react';
|
|
169
|
+
|
|
170
|
+
function InfiniteScrollExample() {
|
|
171
|
+
const [items, setItems] = useState(initialItems);
|
|
172
|
+
const [value, setValue] = useState('');
|
|
173
|
+
|
|
174
|
+
const handleLoadMore = async (page: number) => {
|
|
175
|
+
const newItems = await fetchItems(page);
|
|
176
|
+
setItems([...items, ...newItems]);
|
|
177
|
+
};
|
|
178
|
+
|
|
179
|
+
return (
|
|
180
|
+
<Select
|
|
181
|
+
items={items}
|
|
182
|
+
value={value}
|
|
183
|
+
onChange={(val) => setValue(val as string)}
|
|
184
|
+
infiniteScroll
|
|
185
|
+
pageSize={20}
|
|
186
|
+
onLoadMore={handleLoadMore}
|
|
187
|
+
/>
|
|
188
|
+
);
|
|
189
|
+
}
|
|
190
|
+
```
|
|
191
|
+
|
|
192
|
+
### With Validation
|
|
193
|
+
|
|
194
|
+
```tsx
|
|
195
|
+
import { Select } from '@smilodon/react';
|
|
196
|
+
|
|
197
|
+
function ValidationExample() {
|
|
198
|
+
const [value, setValue] = useState('');
|
|
199
|
+
const [error, setError] = useState('');
|
|
200
|
+
|
|
201
|
+
const handleChange = (val: string | number) => {
|
|
202
|
+
setValue(val as string);
|
|
203
|
+
if (!val) {
|
|
204
|
+
setError('This field is required');
|
|
205
|
+
} else {
|
|
206
|
+
setError('');
|
|
207
|
+
}
|
|
208
|
+
};
|
|
209
|
+
|
|
210
|
+
return (
|
|
211
|
+
<Select
|
|
212
|
+
items={items}
|
|
213
|
+
value={value}
|
|
214
|
+
onChange={handleChange}
|
|
215
|
+
required
|
|
216
|
+
error={!!error}
|
|
217
|
+
errorMessage={error}
|
|
218
|
+
placeholder="Required field"
|
|
219
|
+
/>
|
|
220
|
+
);
|
|
221
|
+
}
|
|
222
|
+
```
|
|
223
|
+
|
|
224
|
+
### Using Ref for Imperative Actions
|
|
225
|
+
|
|
226
|
+
```tsx
|
|
227
|
+
import { Select, SelectHandle } from '@smilodon/react';
|
|
228
|
+
import { useRef } from 'react';
|
|
229
|
+
|
|
230
|
+
function RefExample() {
|
|
231
|
+
const selectRef = useRef<SelectHandle>(null);
|
|
232
|
+
const [value, setValue] = useState('');
|
|
233
|
+
|
|
234
|
+
const handleOpen = () => {
|
|
235
|
+
selectRef.current?.open();
|
|
236
|
+
};
|
|
237
|
+
|
|
238
|
+
const handleClear = () => {
|
|
239
|
+
selectRef.current?.clear();
|
|
240
|
+
};
|
|
241
|
+
|
|
242
|
+
return (
|
|
243
|
+
<div>
|
|
244
|
+
<Select
|
|
245
|
+
ref={selectRef}
|
|
246
|
+
items={items}
|
|
247
|
+
value={value}
|
|
248
|
+
onChange={(val) => setValue(val as string)}
|
|
249
|
+
/>
|
|
250
|
+
<button onClick={handleOpen}>Open Dropdown</button>
|
|
251
|
+
<button onClick={handleClear}>Clear Selection</button>
|
|
252
|
+
</div>
|
|
253
|
+
);
|
|
254
|
+
}
|
|
255
|
+
```
|
|
256
|
+
|
|
257
|
+
## API Reference
|
|
258
|
+
|
|
259
|
+
### Props
|
|
260
|
+
|
|
261
|
+
| Prop | Type | Default | Description |
|
|
262
|
+
|------|------|---------|-------------|
|
|
263
|
+
| `items` | `SelectItem[]` | `[]` | Array of items to display |
|
|
264
|
+
| `groupedItems` | `GroupedItem[]` | - | Grouped items (alternative to flat array) |
|
|
265
|
+
| `value` | `string \| number \| Array<string \| number>` | - | Current value (controlled) |
|
|
266
|
+
| `defaultValue` | `string \| number \| Array<string \| number>` | - | Default value (uncontrolled) |
|
|
267
|
+
| `multiple` | `boolean` | `false` | Enable multi-select mode |
|
|
268
|
+
| `searchable` | `boolean` | `false` | Enable search/filter |
|
|
269
|
+
| `placeholder` | `string` | `"Select an option..."` | Placeholder text |
|
|
270
|
+
| `disabled` | `boolean` | `false` | Disable the select |
|
|
271
|
+
| `required` | `boolean` | `false` | Mark as required field |
|
|
272
|
+
| `error` | `boolean` | `false` | Show error state |
|
|
273
|
+
| `errorMessage` | `string` | - | Error message to display |
|
|
274
|
+
| `infiniteScroll` | `boolean` | `false` | Enable infinite scroll |
|
|
275
|
+
| `pageSize` | `number` | `20` | Items per page (infinite scroll) |
|
|
276
|
+
| `virtualized` | `boolean` | `false` | Enable virtual scrolling |
|
|
277
|
+
| `estimatedItemHeight` | `number` | `48` | Item height for virtualization |
|
|
278
|
+
| `maxSelections` | `number` | - | Max selections (multi-select) |
|
|
279
|
+
| `placement` | `string` | `"bottom-start"` | Dropdown placement |
|
|
280
|
+
| `className` | `string` | - | Custom CSS class |
|
|
281
|
+
| `style` | `CSSProperties` | - | Inline styles |
|
|
282
|
+
| `onChange` | `(value, items) => void` | - | Selection change callback |
|
|
283
|
+
| `onSelect` | `(item, index) => void` | - | Item select callback |
|
|
284
|
+
| `onOpen` | `() => void` | - | Dropdown open callback |
|
|
285
|
+
| `onClose` | `() => void` | - | Dropdown close callback |
|
|
286
|
+
| `onSearch` | `(query, results, count) => void` | - | Search callback |
|
|
287
|
+
| `onLoadMore` | `(page) => void \| Promise<void>` | - | Load more callback |
|
|
288
|
+
| `creatable` | `boolean` | `false` | Allow creating new options |
|
|
289
|
+
| `onCreate` | `(label) => void` | - | Create option callback |
|
|
290
|
+
|
|
291
|
+
### Types
|
|
292
|
+
|
|
293
|
+
#### SelectItem
|
|
294
|
+
|
|
295
|
+
```typescript
|
|
296
|
+
interface SelectItem {
|
|
297
|
+
value: string | number;
|
|
298
|
+
label: string;
|
|
299
|
+
disabled?: boolean;
|
|
300
|
+
[key: string]: unknown; // Additional custom properties
|
|
301
|
+
}
|
|
302
|
+
```
|
|
303
|
+
|
|
304
|
+
#### SelectHandle
|
|
305
|
+
|
|
306
|
+
```typescript
|
|
307
|
+
interface SelectHandle {
|
|
308
|
+
focus: () => void;
|
|
309
|
+
open: () => void;
|
|
310
|
+
close: () => void;
|
|
311
|
+
getSelectedItems: () => SelectItem[];
|
|
312
|
+
getSelectedValues: () => Array<string | number>;
|
|
313
|
+
setItems: (items: SelectItem[]) => void;
|
|
314
|
+
setGroupedItems: (groups: GroupedItem[]) => void;
|
|
315
|
+
clear: () => void;
|
|
316
|
+
}
|
|
317
|
+
```
|
|
318
|
+
|
|
319
|
+
## Styling
|
|
320
|
+
|
|
321
|
+
The component uses Shadow DOM for style encapsulation. You can customize it using CSS custom properties:
|
|
322
|
+
|
|
323
|
+
```css
|
|
324
|
+
enhanced-select {
|
|
325
|
+
--select-border-color: #e2e8f0;
|
|
326
|
+
--select-border-focus-color: #3b82f6;
|
|
327
|
+
--select-bg: white;
|
|
328
|
+
--select-text-color: #1f2937;
|
|
329
|
+
--select-placeholder-color: #9ca3af;
|
|
330
|
+
--select-dropdown-bg: white;
|
|
331
|
+
--select-dropdown-border: #e2e8f0;
|
|
332
|
+
--select-dropdown-shadow: 0 10px 15px -3px rgba(0, 0, 0, 0.1);
|
|
333
|
+
--select-option-hover-bg: #f3f4f6;
|
|
334
|
+
--select-option-selected-bg: #eff6ff;
|
|
335
|
+
--select-option-selected-color: #3b82f6;
|
|
336
|
+
}
|
|
337
|
+
```
|
|
338
|
+
|
|
339
|
+
## Accessibility
|
|
340
|
+
|
|
341
|
+
This component is built with accessibility in mind:
|
|
342
|
+
|
|
343
|
+
- ✅ Full keyboard navigation (Arrow keys, Enter, Escape, Tab)
|
|
344
|
+
- ✅ Screen reader support with ARIA attributes
|
|
345
|
+
- ✅ Focus management
|
|
346
|
+
- ✅ High contrast mode support
|
|
347
|
+
- ✅ Reduced motion support
|
|
348
|
+
- ✅ 44px minimum touch targets (WCAG 2.5.5)
|
|
349
|
+
- ✅ WCAG 2.1 AAA compliant
|
|
350
|
+
|
|
351
|
+
## Performance
|
|
352
|
+
|
|
353
|
+
- Virtual scrolling for large datasets (1000+ items)
|
|
354
|
+
- Lazy loading with infinite scroll
|
|
355
|
+
- Optimized re-renders with React.memo internally
|
|
356
|
+
- Tree-shakeable exports
|
|
357
|
+
- Minimal bundle size impact
|
|
358
|
+
|
|
359
|
+
## Browser Support
|
|
360
|
+
|
|
361
|
+
- Chrome/Edge 90+
|
|
362
|
+
- Firefox 88+
|
|
363
|
+
- Safari 14+
|
|
364
|
+
- iOS Safari 14+
|
|
365
|
+
- Samsung Internet 14+
|
|
366
|
+
|
|
367
|
+
## TypeScript
|
|
368
|
+
|
|
369
|
+
Full TypeScript support with comprehensive type definitions included.
|
|
370
|
+
|
|
371
|
+
```tsx
|
|
372
|
+
import { Select, SelectProps, SelectHandle, SelectItem } from '@smilodon/react';
|
|
373
|
+
```
|
|
374
|
+
|
|
375
|
+
## License
|
|
376
|
+
|
|
377
|
+
MIT © Smilodon
|
|
378
|
+
|
|
379
|
+
## Related Packages
|
|
380
|
+
|
|
381
|
+
- `@smilodon/core` - Core web component
|
|
382
|
+
- `@smilodon/vue` - Vue 3 adapter
|
|
383
|
+
- `@smilodon/angular` - Angular adapter
|
|
384
|
+
- `@smilodon/svelte` - Svelte adapter
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,248 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __copyProps = (to, from, except, desc) => {
|
|
9
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
10
|
+
for (let key of __getOwnPropNames(from))
|
|
11
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
12
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
13
|
+
}
|
|
14
|
+
return to;
|
|
15
|
+
};
|
|
16
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
17
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
18
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
19
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
20
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
21
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
22
|
+
mod
|
|
23
|
+
));
|
|
24
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
25
|
+
const React = require("react");
|
|
26
|
+
const Select = React.forwardRef((props, ref) => {
|
|
27
|
+
const {
|
|
28
|
+
items = [],
|
|
29
|
+
groupedItems,
|
|
30
|
+
value,
|
|
31
|
+
defaultValue,
|
|
32
|
+
multiple = false,
|
|
33
|
+
searchable = false,
|
|
34
|
+
placeholder = "Select an option...",
|
|
35
|
+
disabled = false,
|
|
36
|
+
required = false,
|
|
37
|
+
error = false,
|
|
38
|
+
errorMessage,
|
|
39
|
+
infiniteScroll = false,
|
|
40
|
+
pageSize = 20,
|
|
41
|
+
virtualized = false,
|
|
42
|
+
estimatedItemHeight = 48,
|
|
43
|
+
maxSelections,
|
|
44
|
+
placement = "bottom-start",
|
|
45
|
+
className,
|
|
46
|
+
style,
|
|
47
|
+
renderItem,
|
|
48
|
+
renderValue,
|
|
49
|
+
onChange,
|
|
50
|
+
onSelect,
|
|
51
|
+
onOpen,
|
|
52
|
+
onClose,
|
|
53
|
+
onSearch,
|
|
54
|
+
onLoadMore,
|
|
55
|
+
loading = false,
|
|
56
|
+
creatable = false,
|
|
57
|
+
onCreate
|
|
58
|
+
} = props;
|
|
59
|
+
const elementRef = React.useRef(null);
|
|
60
|
+
const [isControlled] = React.useState(value !== void 0);
|
|
61
|
+
const [internalValue, setInternalValue] = React.useState(defaultValue);
|
|
62
|
+
React.useEffect(() => {
|
|
63
|
+
if (typeof window !== "undefined" && !customElements.get("enhanced-select")) {
|
|
64
|
+
import("@smilodon/core").then((module2) => {
|
|
65
|
+
if (!customElements.get("enhanced-select")) {
|
|
66
|
+
customElements.define("enhanced-select", module2.EnhancedSelect);
|
|
67
|
+
}
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
}, []);
|
|
71
|
+
React.useEffect(() => {
|
|
72
|
+
const element = elementRef.current;
|
|
73
|
+
if (!element) return;
|
|
74
|
+
if (groupedItems) {
|
|
75
|
+
element.setGroupedItems(groupedItems);
|
|
76
|
+
} else if (items.length > 0) {
|
|
77
|
+
element.setItems(items);
|
|
78
|
+
}
|
|
79
|
+
const config = {
|
|
80
|
+
searchable,
|
|
81
|
+
placeholder,
|
|
82
|
+
enabled: !disabled,
|
|
83
|
+
selection: {
|
|
84
|
+
mode: multiple ? "multi" : "single",
|
|
85
|
+
maxSelections
|
|
86
|
+
},
|
|
87
|
+
infiniteScroll: {
|
|
88
|
+
enabled: infiniteScroll,
|
|
89
|
+
pageSize
|
|
90
|
+
},
|
|
91
|
+
scrollToSelected: {
|
|
92
|
+
enabled: true
|
|
93
|
+
},
|
|
94
|
+
creatable
|
|
95
|
+
};
|
|
96
|
+
element.updateConfig(config);
|
|
97
|
+
const currentValue = isControlled ? value : internalValue;
|
|
98
|
+
if (currentValue !== void 0) {
|
|
99
|
+
const values = Array.isArray(currentValue) ? currentValue : [currentValue];
|
|
100
|
+
element.setSelectedValues(values);
|
|
101
|
+
}
|
|
102
|
+
if (error) {
|
|
103
|
+
element.setError(errorMessage || "Invalid selection");
|
|
104
|
+
} else {
|
|
105
|
+
element.clearError();
|
|
106
|
+
}
|
|
107
|
+
if (required) {
|
|
108
|
+
element.setRequired(true);
|
|
109
|
+
}
|
|
110
|
+
}, []);
|
|
111
|
+
React.useEffect(() => {
|
|
112
|
+
if (!elementRef.current) return;
|
|
113
|
+
if (groupedItems) {
|
|
114
|
+
elementRef.current.setGroupedItems(groupedItems);
|
|
115
|
+
} else if (items.length > 0) {
|
|
116
|
+
elementRef.current.setItems(items);
|
|
117
|
+
}
|
|
118
|
+
}, [items, groupedItems]);
|
|
119
|
+
React.useEffect(() => {
|
|
120
|
+
if (!elementRef.current || !isControlled) return;
|
|
121
|
+
if (value !== void 0) {
|
|
122
|
+
const values = Array.isArray(value) ? value : [value];
|
|
123
|
+
elementRef.current.setSelectedValues(values);
|
|
124
|
+
}
|
|
125
|
+
}, [value, isControlled]);
|
|
126
|
+
React.useEffect(() => {
|
|
127
|
+
if (!elementRef.current) return;
|
|
128
|
+
const config = {
|
|
129
|
+
searchable,
|
|
130
|
+
placeholder,
|
|
131
|
+
enabled: !disabled,
|
|
132
|
+
selection: {
|
|
133
|
+
mode: multiple ? "multi" : "single",
|
|
134
|
+
maxSelections
|
|
135
|
+
},
|
|
136
|
+
infiniteScroll: {
|
|
137
|
+
enabled: infiniteScroll,
|
|
138
|
+
pageSize
|
|
139
|
+
}
|
|
140
|
+
};
|
|
141
|
+
elementRef.current.updateConfig(config);
|
|
142
|
+
}, [searchable, placeholder, disabled, multiple, maxSelections, infiniteScroll, pageSize]);
|
|
143
|
+
React.useEffect(() => {
|
|
144
|
+
if (!elementRef.current) return;
|
|
145
|
+
if (error) {
|
|
146
|
+
elementRef.current.setError(errorMessage || "Invalid selection");
|
|
147
|
+
} else {
|
|
148
|
+
elementRef.current.clearError();
|
|
149
|
+
}
|
|
150
|
+
}, [error, errorMessage]);
|
|
151
|
+
React.useEffect(() => {
|
|
152
|
+
if (!elementRef.current) return;
|
|
153
|
+
elementRef.current.setRequired(required);
|
|
154
|
+
}, [required]);
|
|
155
|
+
React.useEffect(() => {
|
|
156
|
+
const element = elementRef.current;
|
|
157
|
+
if (!element) return;
|
|
158
|
+
const handleSelect = (e) => {
|
|
159
|
+
const { item, index } = e.detail;
|
|
160
|
+
onSelect == null ? void 0 : onSelect(item, index);
|
|
161
|
+
};
|
|
162
|
+
const handleChange = (e) => {
|
|
163
|
+
const { selectedItems, selectedValues } = e.detail;
|
|
164
|
+
const values = selectedValues;
|
|
165
|
+
if (!isControlled) {
|
|
166
|
+
setInternalValue(multiple ? values : values[0]);
|
|
167
|
+
}
|
|
168
|
+
if (onChange) {
|
|
169
|
+
const value2 = multiple ? values : values[0];
|
|
170
|
+
onChange(value2, selectedItems);
|
|
171
|
+
}
|
|
172
|
+
};
|
|
173
|
+
const handleOpen = (e) => {
|
|
174
|
+
onOpen == null ? void 0 : onOpen();
|
|
175
|
+
};
|
|
176
|
+
const handleClose = (e) => {
|
|
177
|
+
onClose == null ? void 0 : onClose();
|
|
178
|
+
};
|
|
179
|
+
const handleSearch = (e) => {
|
|
180
|
+
const { query, results, count } = e.detail;
|
|
181
|
+
onSearch == null ? void 0 : onSearch(query, results, count);
|
|
182
|
+
};
|
|
183
|
+
const handleLoadMore = (e) => {
|
|
184
|
+
const { page } = e.detail;
|
|
185
|
+
onLoadMore == null ? void 0 : onLoadMore(page);
|
|
186
|
+
};
|
|
187
|
+
element.addEventListener("select", handleSelect);
|
|
188
|
+
element.addEventListener("change", handleChange);
|
|
189
|
+
element.addEventListener("open", handleOpen);
|
|
190
|
+
element.addEventListener("close", handleClose);
|
|
191
|
+
element.addEventListener("search", handleSearch);
|
|
192
|
+
element.addEventListener("loadMore", handleLoadMore);
|
|
193
|
+
return () => {
|
|
194
|
+
element.removeEventListener("select", handleSelect);
|
|
195
|
+
element.removeEventListener("change", handleChange);
|
|
196
|
+
element.removeEventListener("open", handleOpen);
|
|
197
|
+
element.removeEventListener("close", handleClose);
|
|
198
|
+
element.removeEventListener("search", handleSearch);
|
|
199
|
+
element.removeEventListener("loadMore", handleLoadMore);
|
|
200
|
+
};
|
|
201
|
+
}, [onSelect, onChange, onOpen, onClose, onSearch, onLoadMore, isControlled, multiple]);
|
|
202
|
+
React.useImperativeHandle(ref, () => ({
|
|
203
|
+
focus: () => {
|
|
204
|
+
var _a;
|
|
205
|
+
(_a = elementRef.current) == null ? void 0 : _a.focus();
|
|
206
|
+
},
|
|
207
|
+
open: () => {
|
|
208
|
+
var _a;
|
|
209
|
+
(_a = elementRef.current) == null ? void 0 : _a.open();
|
|
210
|
+
},
|
|
211
|
+
close: () => {
|
|
212
|
+
var _a;
|
|
213
|
+
(_a = elementRef.current) == null ? void 0 : _a.close();
|
|
214
|
+
},
|
|
215
|
+
getSelectedItems: () => {
|
|
216
|
+
var _a;
|
|
217
|
+
return ((_a = elementRef.current) == null ? void 0 : _a.getSelectedItems()) || [];
|
|
218
|
+
},
|
|
219
|
+
getSelectedValues: () => {
|
|
220
|
+
var _a;
|
|
221
|
+
return ((_a = elementRef.current) == null ? void 0 : _a.getSelectedValues()) || [];
|
|
222
|
+
},
|
|
223
|
+
setItems: (items2) => {
|
|
224
|
+
var _a;
|
|
225
|
+
(_a = elementRef.current) == null ? void 0 : _a.setItems(items2);
|
|
226
|
+
},
|
|
227
|
+
setGroupedItems: (groups) => {
|
|
228
|
+
var _a;
|
|
229
|
+
(_a = elementRef.current) == null ? void 0 : _a.setGroupedItems(groups);
|
|
230
|
+
},
|
|
231
|
+
clear: () => {
|
|
232
|
+
var _a;
|
|
233
|
+
(_a = elementRef.current) == null ? void 0 : _a.setSelectedValues([]);
|
|
234
|
+
if (!isControlled) {
|
|
235
|
+
setInternalValue(multiple ? [] : void 0);
|
|
236
|
+
}
|
|
237
|
+
onChange == null ? void 0 : onChange(multiple ? [] : "", []);
|
|
238
|
+
}
|
|
239
|
+
}));
|
|
240
|
+
return React.createElement("enhanced-select", {
|
|
241
|
+
ref: elementRef,
|
|
242
|
+
className,
|
|
243
|
+
style
|
|
244
|
+
});
|
|
245
|
+
});
|
|
246
|
+
Select.displayName = "Select";
|
|
247
|
+
exports.Select = Select;
|
|
248
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.cjs","sources":["../src/Select.tsx"],"sourcesContent":["import React, { useEffect, useRef, useCallback, forwardRef, useImperativeHandle, useState } from 'react';\nimport type {\n SelectEventDetail,\n OpenEventDetail,\n CloseEventDetail,\n SearchEventDetail,\n ChangeEventDetail,\n LoadMoreEventDetail,\n GroupedItem,\n} from '@smilodon/core';\n\n/**\n * Item type for the select component\n */\nexport interface SelectItem {\n value: string | number;\n label: string;\n disabled?: boolean;\n [key: string]: unknown;\n}\n\n/**\n * Props for the Select component\n */\nexport interface SelectProps {\n /** Array of items to display in the dropdown */\n items?: SelectItem[];\n \n /** Grouped items (alternative to flat items array) */\n groupedItems?: GroupedItem[];\n \n /** Currently selected value(s) */\n value?: string | number | Array<string | number>;\n \n /** Default value(s) for uncontrolled mode */\n defaultValue?: string | number | Array<string | number>;\n \n /** Enable multi-select mode */\n multiple?: boolean;\n \n /** Enable search/filter functionality */\n searchable?: boolean;\n \n /** Placeholder text */\n placeholder?: string;\n \n /** Disable the select */\n disabled?: boolean;\n \n /** Required field */\n required?: boolean;\n \n /** Error state */\n error?: boolean;\n \n /** Error message to display */\n errorMessage?: string;\n \n /** Enable infinite scroll */\n infiniteScroll?: boolean;\n \n /** Page size for infinite scroll */\n pageSize?: number;\n \n /** Enable virtual scrolling for large lists */\n virtualized?: boolean;\n \n /** Estimated height of each item (for virtualization) */\n estimatedItemHeight?: number;\n \n /** Maximum number of selections (for multiple mode) */\n maxSelections?: number;\n \n /** Dropdown placement */\n placement?: 'top' | 'bottom' | 'top-start' | 'top-end' | 'bottom-start' | 'bottom-end';\n \n /** Custom CSS class name */\n className?: string;\n \n /** Inline styles */\n style?: React.CSSProperties;\n \n /** Custom item renderer */\n renderItem?: (item: SelectItem, index: number) => React.ReactNode;\n \n /** Custom selected value renderer */\n renderValue?: (selectedItems: SelectItem[]) => React.ReactNode;\n \n // Event Handlers\n /** Called when selection changes */\n onChange?: (value: string | number | Array<string | number>, items: SelectItem[]) => void;\n \n /** Called when an item is selected */\n onSelect?: (item: SelectItem, index: number) => void;\n \n /** Called when dropdown opens */\n onOpen?: () => void;\n \n /** Called when dropdown closes */\n onClose?: () => void;\n \n /** Called when search query changes */\n onSearch?: (query: string, results?: SelectItem[], count?: number) => void;\n \n /** Called when more items are needed (infinite scroll) */\n onLoadMore?: (page: number) => void | Promise<void>;\n \n /** Loading state for async operations */\n loading?: boolean;\n \n /** Enable creatable mode (allow creating new options) */\n creatable?: boolean;\n \n /** Called when a new option is created */\n onCreate?: (label: string) => void;\n}\n\n/**\n * Imperative handle for the Select component\n */\nexport interface SelectHandle {\n /** Focus the select input */\n focus: () => void;\n \n /** Open the dropdown */\n open: () => void;\n \n /** Close the dropdown */\n close: () => void;\n \n /** Get currently selected items */\n getSelectedItems: () => SelectItem[];\n \n /** Get currently selected values */\n getSelectedValues: () => Array<string | number>;\n \n /** Programmatically set items */\n setItems: (items: SelectItem[]) => void;\n \n /** Programmatically set grouped items */\n setGroupedItems: (groups: GroupedItem[]) => void;\n \n /** Clear the selection */\n clear: () => void;\n}\n\n/**\n * Smilodon Select Component for React\n * \n * A production-ready, accessible select component with advanced features:\n * - Single and multi-select modes\n * - Searchable with client or server-side filtering\n * - Infinite scroll and virtual scrolling for large datasets\n * - Grouped options\n * - Custom rendering\n * - Full keyboard navigation\n * - WCAG 2.1 AAA compliant\n * \n * @example\n * ```tsx\n * // Simple usage\n * <Select\n * items={[\n * { value: 'apple', label: 'Apple' },\n * { value: 'banana', label: 'Banana' }\n * ]}\n * value=\"apple\"\n * onChange={(value) => console.log(value)}\n * />\n * \n * // Multi-select with search\n * <Select\n * items={items}\n * multiple\n * searchable\n * value={selectedValues}\n * onChange={(values) => setSelectedValues(values)}\n * />\n * \n * // With grouped items\n * <Select\n * groupedItems={[\n * { label: 'Fruits', options: [...] },\n * { label: 'Vegetables', options: [...] }\n * ]}\n * onChange={(value) => console.log(value)}\n * />\n * ```\n */\nexport const Select = forwardRef<SelectHandle, SelectProps>((props, ref) => {\n const {\n items = [],\n groupedItems,\n value,\n defaultValue,\n multiple = false,\n searchable = false,\n placeholder = 'Select an option...',\n disabled = false,\n required = false,\n error = false,\n errorMessage,\n infiniteScroll = false,\n pageSize = 20,\n virtualized = false,\n estimatedItemHeight = 48,\n maxSelections,\n placement = 'bottom-start',\n className,\n style,\n renderItem,\n renderValue,\n onChange,\n onSelect,\n onOpen,\n onClose,\n onSearch,\n onLoadMore,\n loading = false,\n creatable = false,\n onCreate,\n } = props;\n\n const elementRef = useRef<any>(null);\n const [isControlled] = useState(value !== undefined);\n const [internalValue, setInternalValue] = useState(defaultValue);\n\n // Register custom element if not already registered\n useEffect(() => {\n if (typeof window !== 'undefined' && !customElements.get('enhanced-select')) {\n import('@smilodon/core').then((module) => {\n if (!customElements.get('enhanced-select')) {\n customElements.define('enhanced-select', module.EnhancedSelect);\n }\n });\n }\n }, []);\n\n // Initialize component\n useEffect(() => {\n const element = elementRef.current;\n if (!element) return;\n\n // Set initial items\n if (groupedItems) {\n element.setGroupedItems(groupedItems);\n } else if (items.length > 0) {\n element.setItems(items);\n }\n\n // Configure component\n const config = {\n searchable,\n placeholder,\n enabled: !disabled,\n selection: {\n mode: multiple ? 'multi' : 'single',\n maxSelections: maxSelections,\n },\n infiniteScroll: {\n enabled: infiniteScroll,\n pageSize: pageSize,\n },\n scrollToSelected: {\n enabled: true,\n },\n creatable: creatable,\n };\n\n element.updateConfig(config);\n\n // Set initial value\n const currentValue = isControlled ? value : internalValue;\n if (currentValue !== undefined) {\n const values = Array.isArray(currentValue) ? currentValue : [currentValue];\n element.setSelectedValues(values);\n }\n\n // Set error state\n if (error) {\n element.setError(errorMessage || 'Invalid selection');\n } else {\n element.clearError();\n }\n\n // Set required state\n if (required) {\n element.setRequired(true);\n }\n }, []);\n\n // Update items when they change\n useEffect(() => {\n if (!elementRef.current) return;\n \n if (groupedItems) {\n elementRef.current.setGroupedItems(groupedItems);\n } else if (items.length > 0) {\n elementRef.current.setItems(items);\n }\n }, [items, groupedItems]);\n\n // Update selected value when it changes (controlled mode)\n useEffect(() => {\n if (!elementRef.current || !isControlled) return;\n \n if (value !== undefined) {\n const values = Array.isArray(value) ? value : [value];\n elementRef.current.setSelectedValues(values);\n }\n }, [value, isControlled]);\n\n // Update config when props change\n useEffect(() => {\n if (!elementRef.current) return;\n\n const config = {\n searchable,\n placeholder,\n enabled: !disabled,\n selection: {\n mode: multiple ? 'multi' : 'single',\n maxSelections: maxSelections,\n },\n infiniteScroll: {\n enabled: infiniteScroll,\n pageSize: pageSize,\n },\n };\n\n elementRef.current.updateConfig(config);\n }, [searchable, placeholder, disabled, multiple, maxSelections, infiniteScroll, pageSize]);\n\n // Update error state\n useEffect(() => {\n if (!elementRef.current) return;\n\n if (error) {\n elementRef.current.setError(errorMessage || 'Invalid selection');\n } else {\n elementRef.current.clearError();\n }\n }, [error, errorMessage]);\n\n // Update required state\n useEffect(() => {\n if (!elementRef.current) return;\n elementRef.current.setRequired(required);\n }, [required]);\n\n // Event handlers\n useEffect(() => {\n const element = elementRef.current;\n if (!element) return;\n\n const handleSelect = (e: CustomEvent<SelectEventDetail>) => {\n const { item, index } = e.detail;\n onSelect?.(item as SelectItem, index);\n };\n\n const handleChange = (e: CustomEvent<ChangeEventDetail>) => {\n const { selectedItems, selectedValues } = e.detail;\n \n // Cast selectedValues to proper type\n const values = selectedValues as (string | number)[];\n \n // Update internal value in uncontrolled mode\n if (!isControlled) {\n setInternalValue(multiple ? values : values[0]);\n }\n\n // Call onChange callback\n if (onChange) {\n const value = multiple ? values : values[0];\n onChange(value, selectedItems as SelectItem[]);\n }\n };\n\n const handleOpen = (e: CustomEvent<OpenEventDetail>) => {\n onOpen?.();\n };\n\n const handleClose = (e: CustomEvent<CloseEventDetail>) => {\n onClose?.();\n };\n\n const handleSearch = (e: CustomEvent<SearchEventDetail>) => {\n const { query, results, count } = e.detail;\n onSearch?.(query, results as SelectItem[] | undefined, count);\n };\n\n const handleLoadMore = (e: CustomEvent<LoadMoreEventDetail>) => {\n const { page } = e.detail;\n onLoadMore?.(page);\n };\n\n element.addEventListener('select', handleSelect);\n element.addEventListener('change', handleChange);\n element.addEventListener('open', handleOpen);\n element.addEventListener('close', handleClose);\n element.addEventListener('search', handleSearch);\n element.addEventListener('loadMore', handleLoadMore);\n\n return () => {\n element.removeEventListener('select', handleSelect);\n element.removeEventListener('change', handleChange);\n element.removeEventListener('open', handleOpen);\n element.removeEventListener('close', handleClose);\n element.removeEventListener('search', handleSearch);\n element.removeEventListener('loadMore', handleLoadMore);\n };\n }, [onSelect, onChange, onOpen, onClose, onSearch, onLoadMore, isControlled, multiple]);\n\n // Expose imperative handle\n useImperativeHandle(ref, () => ({\n focus: () => {\n elementRef.current?.focus();\n },\n open: () => {\n elementRef.current?.open();\n },\n close: () => {\n elementRef.current?.close();\n },\n getSelectedItems: () => {\n return elementRef.current?.getSelectedItems() || [];\n },\n getSelectedValues: () => {\n return elementRef.current?.getSelectedValues() || [];\n },\n setItems: (items: SelectItem[]) => {\n elementRef.current?.setItems(items);\n },\n setGroupedItems: (groups: GroupedItem[]) => {\n elementRef.current?.setGroupedItems(groups);\n },\n clear: () => {\n elementRef.current?.setSelectedValues([]);\n if (!isControlled) {\n setInternalValue(multiple ? [] : undefined);\n }\n onChange?.(multiple ? [] : '', []);\n },\n }));\n\n return React.createElement('enhanced-select', {\n ref: elementRef,\n className,\n style,\n });\n});\n\nSelect.displayName = 'Select';\n"],"names":["forwardRef","useRef","useState","useEffect","module","value","useImperativeHandle","items"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;AA6LO,MAAM,SAASA,MAAAA,WAAsC,CAAC,OAAO,QAAQ;AAC1E,QAAM;AAAA,IACJ,QAAQ,CAAA;AAAA,IACR;AAAA,IACA;AAAA,IACA;AAAA,IACA,WAAW;AAAA,IACX,aAAa;AAAA,IACb,cAAc;AAAA,IACd,WAAW;AAAA,IACX,WAAW;AAAA,IACX,QAAQ;AAAA,IACR;AAAA,IACA,iBAAiB;AAAA,IACjB,WAAW;AAAA,IACX,cAAc;AAAA,IACd,sBAAsB;AAAA,IACtB;AAAA,IACA,YAAY;AAA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AAY;AAClD,cAAQ,oBAAoB,QAAQ,UAAU;AAC9C,cAAQ,oBAAoB,SAAS,WAAW;AAChD,cAAQ,oBAAoB,UAAU,YAAY;AAClD,cAAQ,oBAAoB,YAAY,cAAc;AAAA,IACxD;AAAA,EACF,GAAG,CAAC,UAAU,UAAU,QAAQ,SAAS,UAAU,YAAY,cAAc,QAAQ,CAAC;AAGtFC,QAAAA,oBAAoB,KAAK,OAAO;AAAA,IAC9B,OAAO,MAAM;;AACX,uBAAW,YAAX,mBAAoB;AAAA,IACtB;AAAA,IACA,MAAM,MAAM;;AACV,uBAAW,YAAX,mBAAoB;AAAA,IACtB;AAAA,IACA,OAAO,MAAM;;AACX,uBAAW,YAAX,mBAAoB;AAAA,IACtB;AAAA,IACA,kBAAkB,MAAM;;AACtB,eAAO,gBAAW,YAAX,mBAAoB,uBAAsB,CAAA;AAAA,IACnD;AAAA,IACA,mBAAmB,MAAM;;AACvB,eAAO,gBAAW,YAAX,mBAAoB,wBAAuB,CAAA;AAAA,IACpD;AAAA,IACA,UAAU,CAACC,WAAwB;;AACjC,uBAAW,YAAX,mBAAoB,SAASA;AAAAA,IAC/B;AAAA,IACA,iBAAiB,CAAC,WAA0B;;AAC1C,uBAAW,YAAX,mBAAoB,gBAAgB;AAAA,IACtC;AAAA,IACA,OAAO,MAAM;;AACX,uBAAW,YAAX,mBAAoB,kBAAkB;AACtC,UAAI,CAAC,cAAc;AACjB,yBAAiB,WAAW,CAAA,IAAK,MAAS;AAAA,MAC5C;AACA,2CAAW,WAAW,KAAK,IAAI,CAAA;AAAA,IACjC;AAAA,EAAA,EACA;AAEF,SAAO,MAAM,cAAc,mBAAmB;AAAA,IAC5C,KAAK;AAAA,IACL;AAAA,IACA;AAAA,EAAA,CACD;AACH,CAAC;AAED,OAAO,cAAc;;"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,226 @@
|
|
|
1
|
+
import React, { forwardRef, useRef, useState, useEffect, useImperativeHandle } from "react";
|
|
2
|
+
const Select = forwardRef((props, ref) => {
|
|
3
|
+
const {
|
|
4
|
+
items = [],
|
|
5
|
+
groupedItems,
|
|
6
|
+
value,
|
|
7
|
+
defaultValue,
|
|
8
|
+
multiple = false,
|
|
9
|
+
searchable = false,
|
|
10
|
+
placeholder = "Select an option...",
|
|
11
|
+
disabled = false,
|
|
12
|
+
required = false,
|
|
13
|
+
error = false,
|
|
14
|
+
errorMessage,
|
|
15
|
+
infiniteScroll = false,
|
|
16
|
+
pageSize = 20,
|
|
17
|
+
virtualized = false,
|
|
18
|
+
estimatedItemHeight = 48,
|
|
19
|
+
maxSelections,
|
|
20
|
+
placement = "bottom-start",
|
|
21
|
+
className,
|
|
22
|
+
style,
|
|
23
|
+
renderItem,
|
|
24
|
+
renderValue,
|
|
25
|
+
onChange,
|
|
26
|
+
onSelect,
|
|
27
|
+
onOpen,
|
|
28
|
+
onClose,
|
|
29
|
+
onSearch,
|
|
30
|
+
onLoadMore,
|
|
31
|
+
loading = false,
|
|
32
|
+
creatable = false,
|
|
33
|
+
onCreate
|
|
34
|
+
} = props;
|
|
35
|
+
const elementRef = useRef(null);
|
|
36
|
+
const [isControlled] = useState(value !== void 0);
|
|
37
|
+
const [internalValue, setInternalValue] = useState(defaultValue);
|
|
38
|
+
useEffect(() => {
|
|
39
|
+
if (typeof window !== "undefined" && !customElements.get("enhanced-select")) {
|
|
40
|
+
import("@smilodon/core").then((module) => {
|
|
41
|
+
if (!customElements.get("enhanced-select")) {
|
|
42
|
+
customElements.define("enhanced-select", module.EnhancedSelect);
|
|
43
|
+
}
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
}, []);
|
|
47
|
+
useEffect(() => {
|
|
48
|
+
const element = elementRef.current;
|
|
49
|
+
if (!element) return;
|
|
50
|
+
if (groupedItems) {
|
|
51
|
+
element.setGroupedItems(groupedItems);
|
|
52
|
+
} else if (items.length > 0) {
|
|
53
|
+
element.setItems(items);
|
|
54
|
+
}
|
|
55
|
+
const config = {
|
|
56
|
+
searchable,
|
|
57
|
+
placeholder,
|
|
58
|
+
enabled: !disabled,
|
|
59
|
+
selection: {
|
|
60
|
+
mode: multiple ? "multi" : "single",
|
|
61
|
+
maxSelections
|
|
62
|
+
},
|
|
63
|
+
infiniteScroll: {
|
|
64
|
+
enabled: infiniteScroll,
|
|
65
|
+
pageSize
|
|
66
|
+
},
|
|
67
|
+
scrollToSelected: {
|
|
68
|
+
enabled: true
|
|
69
|
+
},
|
|
70
|
+
creatable
|
|
71
|
+
};
|
|
72
|
+
element.updateConfig(config);
|
|
73
|
+
const currentValue = isControlled ? value : internalValue;
|
|
74
|
+
if (currentValue !== void 0) {
|
|
75
|
+
const values = Array.isArray(currentValue) ? currentValue : [currentValue];
|
|
76
|
+
element.setSelectedValues(values);
|
|
77
|
+
}
|
|
78
|
+
if (error) {
|
|
79
|
+
element.setError(errorMessage || "Invalid selection");
|
|
80
|
+
} else {
|
|
81
|
+
element.clearError();
|
|
82
|
+
}
|
|
83
|
+
if (required) {
|
|
84
|
+
element.setRequired(true);
|
|
85
|
+
}
|
|
86
|
+
}, []);
|
|
87
|
+
useEffect(() => {
|
|
88
|
+
if (!elementRef.current) return;
|
|
89
|
+
if (groupedItems) {
|
|
90
|
+
elementRef.current.setGroupedItems(groupedItems);
|
|
91
|
+
} else if (items.length > 0) {
|
|
92
|
+
elementRef.current.setItems(items);
|
|
93
|
+
}
|
|
94
|
+
}, [items, groupedItems]);
|
|
95
|
+
useEffect(() => {
|
|
96
|
+
if (!elementRef.current || !isControlled) return;
|
|
97
|
+
if (value !== void 0) {
|
|
98
|
+
const values = Array.isArray(value) ? value : [value];
|
|
99
|
+
elementRef.current.setSelectedValues(values);
|
|
100
|
+
}
|
|
101
|
+
}, [value, isControlled]);
|
|
102
|
+
useEffect(() => {
|
|
103
|
+
if (!elementRef.current) return;
|
|
104
|
+
const config = {
|
|
105
|
+
searchable,
|
|
106
|
+
placeholder,
|
|
107
|
+
enabled: !disabled,
|
|
108
|
+
selection: {
|
|
109
|
+
mode: multiple ? "multi" : "single",
|
|
110
|
+
maxSelections
|
|
111
|
+
},
|
|
112
|
+
infiniteScroll: {
|
|
113
|
+
enabled: infiniteScroll,
|
|
114
|
+
pageSize
|
|
115
|
+
}
|
|
116
|
+
};
|
|
117
|
+
elementRef.current.updateConfig(config);
|
|
118
|
+
}, [searchable, placeholder, disabled, multiple, maxSelections, infiniteScroll, pageSize]);
|
|
119
|
+
useEffect(() => {
|
|
120
|
+
if (!elementRef.current) return;
|
|
121
|
+
if (error) {
|
|
122
|
+
elementRef.current.setError(errorMessage || "Invalid selection");
|
|
123
|
+
} else {
|
|
124
|
+
elementRef.current.clearError();
|
|
125
|
+
}
|
|
126
|
+
}, [error, errorMessage]);
|
|
127
|
+
useEffect(() => {
|
|
128
|
+
if (!elementRef.current) return;
|
|
129
|
+
elementRef.current.setRequired(required);
|
|
130
|
+
}, [required]);
|
|
131
|
+
useEffect(() => {
|
|
132
|
+
const element = elementRef.current;
|
|
133
|
+
if (!element) return;
|
|
134
|
+
const handleSelect = (e) => {
|
|
135
|
+
const { item, index } = e.detail;
|
|
136
|
+
onSelect == null ? void 0 : onSelect(item, index);
|
|
137
|
+
};
|
|
138
|
+
const handleChange = (e) => {
|
|
139
|
+
const { selectedItems, selectedValues } = e.detail;
|
|
140
|
+
const values = selectedValues;
|
|
141
|
+
if (!isControlled) {
|
|
142
|
+
setInternalValue(multiple ? values : values[0]);
|
|
143
|
+
}
|
|
144
|
+
if (onChange) {
|
|
145
|
+
const value2 = multiple ? values : values[0];
|
|
146
|
+
onChange(value2, selectedItems);
|
|
147
|
+
}
|
|
148
|
+
};
|
|
149
|
+
const handleOpen = (e) => {
|
|
150
|
+
onOpen == null ? void 0 : onOpen();
|
|
151
|
+
};
|
|
152
|
+
const handleClose = (e) => {
|
|
153
|
+
onClose == null ? void 0 : onClose();
|
|
154
|
+
};
|
|
155
|
+
const handleSearch = (e) => {
|
|
156
|
+
const { query, results, count } = e.detail;
|
|
157
|
+
onSearch == null ? void 0 : onSearch(query, results, count);
|
|
158
|
+
};
|
|
159
|
+
const handleLoadMore = (e) => {
|
|
160
|
+
const { page } = e.detail;
|
|
161
|
+
onLoadMore == null ? void 0 : onLoadMore(page);
|
|
162
|
+
};
|
|
163
|
+
element.addEventListener("select", handleSelect);
|
|
164
|
+
element.addEventListener("change", handleChange);
|
|
165
|
+
element.addEventListener("open", handleOpen);
|
|
166
|
+
element.addEventListener("close", handleClose);
|
|
167
|
+
element.addEventListener("search", handleSearch);
|
|
168
|
+
element.addEventListener("loadMore", handleLoadMore);
|
|
169
|
+
return () => {
|
|
170
|
+
element.removeEventListener("select", handleSelect);
|
|
171
|
+
element.removeEventListener("change", handleChange);
|
|
172
|
+
element.removeEventListener("open", handleOpen);
|
|
173
|
+
element.removeEventListener("close", handleClose);
|
|
174
|
+
element.removeEventListener("search", handleSearch);
|
|
175
|
+
element.removeEventListener("loadMore", handleLoadMore);
|
|
176
|
+
};
|
|
177
|
+
}, [onSelect, onChange, onOpen, onClose, onSearch, onLoadMore, isControlled, multiple]);
|
|
178
|
+
useImperativeHandle(ref, () => ({
|
|
179
|
+
focus: () => {
|
|
180
|
+
var _a;
|
|
181
|
+
(_a = elementRef.current) == null ? void 0 : _a.focus();
|
|
182
|
+
},
|
|
183
|
+
open: () => {
|
|
184
|
+
var _a;
|
|
185
|
+
(_a = elementRef.current) == null ? void 0 : _a.open();
|
|
186
|
+
},
|
|
187
|
+
close: () => {
|
|
188
|
+
var _a;
|
|
189
|
+
(_a = elementRef.current) == null ? void 0 : _a.close();
|
|
190
|
+
},
|
|
191
|
+
getSelectedItems: () => {
|
|
192
|
+
var _a;
|
|
193
|
+
return ((_a = elementRef.current) == null ? void 0 : _a.getSelectedItems()) || [];
|
|
194
|
+
},
|
|
195
|
+
getSelectedValues: () => {
|
|
196
|
+
var _a;
|
|
197
|
+
return ((_a = elementRef.current) == null ? void 0 : _a.getSelectedValues()) || [];
|
|
198
|
+
},
|
|
199
|
+
setItems: (items2) => {
|
|
200
|
+
var _a;
|
|
201
|
+
(_a = elementRef.current) == null ? void 0 : _a.setItems(items2);
|
|
202
|
+
},
|
|
203
|
+
setGroupedItems: (groups) => {
|
|
204
|
+
var _a;
|
|
205
|
+
(_a = elementRef.current) == null ? void 0 : _a.setGroupedItems(groups);
|
|
206
|
+
},
|
|
207
|
+
clear: () => {
|
|
208
|
+
var _a;
|
|
209
|
+
(_a = elementRef.current) == null ? void 0 : _a.setSelectedValues([]);
|
|
210
|
+
if (!isControlled) {
|
|
211
|
+
setInternalValue(multiple ? [] : void 0);
|
|
212
|
+
}
|
|
213
|
+
onChange == null ? void 0 : onChange(multiple ? [] : "", []);
|
|
214
|
+
}
|
|
215
|
+
}));
|
|
216
|
+
return React.createElement("enhanced-select", {
|
|
217
|
+
ref: elementRef,
|
|
218
|
+
className,
|
|
219
|
+
style
|
|
220
|
+
});
|
|
221
|
+
});
|
|
222
|
+
Select.displayName = "Select";
|
|
223
|
+
export {
|
|
224
|
+
Select
|
|
225
|
+
};
|
|
226
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sources":["../src/Select.tsx"],"sourcesContent":["import React, { useEffect, useRef, useCallback, forwardRef, useImperativeHandle, useState } from 'react';\nimport type {\n SelectEventDetail,\n OpenEventDetail,\n CloseEventDetail,\n SearchEventDetail,\n ChangeEventDetail,\n LoadMoreEventDetail,\n GroupedItem,\n} from '@smilodon/core';\n\n/**\n * Item type for the select component\n */\nexport interface SelectItem {\n value: string | number;\n label: string;\n disabled?: boolean;\n [key: string]: unknown;\n}\n\n/**\n * Props for the Select component\n */\nexport interface SelectProps {\n /** Array of items to display in the dropdown */\n items?: SelectItem[];\n \n /** Grouped items (alternative to flat items array) */\n groupedItems?: GroupedItem[];\n \n /** Currently selected value(s) */\n value?: string | number | Array<string | number>;\n \n /** Default value(s) for uncontrolled mode */\n defaultValue?: string | number | Array<string | number>;\n \n /** Enable multi-select mode */\n multiple?: boolean;\n \n /** Enable search/filter functionality */\n searchable?: boolean;\n \n /** Placeholder text */\n placeholder?: string;\n \n /** Disable the select */\n disabled?: boolean;\n \n /** Required field */\n required?: boolean;\n \n /** Error state */\n error?: boolean;\n \n /** Error message to display */\n errorMessage?: string;\n \n /** Enable infinite scroll */\n infiniteScroll?: boolean;\n \n /** Page size for infinite scroll */\n pageSize?: number;\n \n /** Enable virtual scrolling for large lists */\n virtualized?: boolean;\n \n /** Estimated height of each item (for virtualization) */\n estimatedItemHeight?: number;\n \n /** Maximum number of selections (for multiple mode) */\n maxSelections?: number;\n \n /** Dropdown placement */\n placement?: 'top' | 'bottom' | 'top-start' | 'top-end' | 'bottom-start' | 'bottom-end';\n \n /** Custom CSS class name */\n className?: string;\n \n /** Inline styles */\n style?: React.CSSProperties;\n \n /** Custom item renderer */\n renderItem?: (item: SelectItem, index: number) => React.ReactNode;\n \n /** Custom selected value renderer */\n renderValue?: (selectedItems: SelectItem[]) => React.ReactNode;\n \n // Event Handlers\n /** Called when selection changes */\n onChange?: (value: string | number | Array<string | number>, items: SelectItem[]) => void;\n \n /** Called when an item is selected */\n onSelect?: (item: SelectItem, index: number) => void;\n \n /** Called when dropdown opens */\n onOpen?: () => void;\n \n /** Called when dropdown closes */\n onClose?: () => void;\n \n /** Called when search query changes */\n onSearch?: (query: string, results?: SelectItem[], count?: number) => void;\n \n /** Called when more items are needed (infinite scroll) */\n onLoadMore?: (page: number) => void | Promise<void>;\n \n /** Loading state for async operations */\n loading?: boolean;\n \n /** Enable creatable mode (allow creating new options) */\n creatable?: boolean;\n \n /** Called when a new option is created */\n onCreate?: (label: string) => void;\n}\n\n/**\n * Imperative handle for the Select component\n */\nexport interface SelectHandle {\n /** Focus the select input */\n focus: () => void;\n \n /** Open the dropdown */\n open: () => void;\n \n /** Close the dropdown */\n close: () => void;\n \n /** Get currently selected items */\n getSelectedItems: () => SelectItem[];\n \n /** Get currently selected values */\n getSelectedValues: () => Array<string | number>;\n \n /** Programmatically set items */\n setItems: (items: SelectItem[]) => void;\n \n /** Programmatically set grouped items */\n setGroupedItems: (groups: GroupedItem[]) => void;\n \n /** Clear the selection */\n clear: () => void;\n}\n\n/**\n * Smilodon Select Component for React\n * \n * A production-ready, accessible select component with advanced features:\n * - Single and multi-select modes\n * - Searchable with client or server-side filtering\n * - Infinite scroll and virtual scrolling for large datasets\n * - Grouped options\n * - Custom rendering\n * - Full keyboard navigation\n * - WCAG 2.1 AAA compliant\n * \n * @example\n * ```tsx\n * // Simple usage\n * <Select\n * items={[\n * { value: 'apple', label: 'Apple' },\n * { value: 'banana', label: 'Banana' }\n * ]}\n * value=\"apple\"\n * onChange={(value) => console.log(value)}\n * />\n * \n * // Multi-select with search\n * <Select\n * items={items}\n * multiple\n * searchable\n * value={selectedValues}\n * onChange={(values) => setSelectedValues(values)}\n * />\n * \n * // With grouped items\n * <Select\n * groupedItems={[\n * { label: 'Fruits', options: [...] },\n * { label: 'Vegetables', options: [...] }\n * ]}\n * onChange={(value) => console.log(value)}\n * />\n * ```\n */\nexport const Select = forwardRef<SelectHandle, SelectProps>((props, ref) => {\n const {\n items = [],\n groupedItems,\n value,\n defaultValue,\n multiple = false,\n searchable = false,\n placeholder = 'Select an option...',\n disabled = false,\n required = false,\n error = false,\n errorMessage,\n infiniteScroll = false,\n pageSize = 20,\n virtualized = false,\n estimatedItemHeight = 48,\n maxSelections,\n placement = 'bottom-start',\n className,\n style,\n renderItem,\n renderValue,\n onChange,\n onSelect,\n onOpen,\n onClose,\n onSearch,\n onLoadMore,\n loading = false,\n creatable = false,\n onCreate,\n } = props;\n\n const elementRef = useRef<any>(null);\n const [isControlled] = useState(value !== undefined);\n const [internalValue, setInternalValue] = useState(defaultValue);\n\n // Register custom element if not already registered\n useEffect(() => {\n if (typeof window !== 'undefined' && !customElements.get('enhanced-select')) {\n import('@smilodon/core').then((module) => {\n if (!customElements.get('enhanced-select')) {\n customElements.define('enhanced-select', module.EnhancedSelect);\n }\n });\n }\n }, []);\n\n // Initialize component\n useEffect(() => {\n const element = elementRef.current;\n if (!element) return;\n\n // Set initial items\n if (groupedItems) {\n element.setGroupedItems(groupedItems);\n } else if (items.length > 0) {\n element.setItems(items);\n }\n\n // Configure component\n const config = {\n searchable,\n placeholder,\n enabled: !disabled,\n selection: {\n mode: multiple ? 'multi' : 'single',\n maxSelections: maxSelections,\n },\n infiniteScroll: {\n enabled: infiniteScroll,\n pageSize: pageSize,\n },\n scrollToSelected: {\n enabled: true,\n },\n creatable: creatable,\n };\n\n element.updateConfig(config);\n\n // Set initial value\n const currentValue = isControlled ? value : internalValue;\n if (currentValue !== undefined) {\n const values = Array.isArray(currentValue) ? currentValue : [currentValue];\n element.setSelectedValues(values);\n }\n\n // Set error state\n if (error) {\n element.setError(errorMessage || 'Invalid selection');\n } else {\n element.clearError();\n }\n\n // Set required state\n if (required) {\n element.setRequired(true);\n }\n }, []);\n\n // Update items when they change\n useEffect(() => {\n if (!elementRef.current) return;\n \n if (groupedItems) {\n elementRef.current.setGroupedItems(groupedItems);\n } else if (items.length > 0) {\n elementRef.current.setItems(items);\n }\n }, [items, groupedItems]);\n\n // Update selected value when it changes (controlled mode)\n useEffect(() => {\n if (!elementRef.current || !isControlled) return;\n \n if (value !== undefined) {\n const values = Array.isArray(value) ? value : [value];\n elementRef.current.setSelectedValues(values);\n }\n }, [value, isControlled]);\n\n // Update config when props change\n useEffect(() => {\n if (!elementRef.current) return;\n\n const config = {\n searchable,\n placeholder,\n enabled: !disabled,\n selection: {\n mode: multiple ? 'multi' : 'single',\n maxSelections: maxSelections,\n },\n infiniteScroll: {\n enabled: infiniteScroll,\n pageSize: pageSize,\n },\n };\n\n elementRef.current.updateConfig(config);\n }, [searchable, placeholder, disabled, multiple, maxSelections, infiniteScroll, pageSize]);\n\n // Update error state\n useEffect(() => {\n if (!elementRef.current) return;\n\n if (error) {\n elementRef.current.setError(errorMessage || 'Invalid selection');\n } else {\n elementRef.current.clearError();\n }\n }, [error, errorMessage]);\n\n // Update required state\n useEffect(() => {\n if (!elementRef.current) return;\n elementRef.current.setRequired(required);\n }, [required]);\n\n // Event handlers\n useEffect(() => {\n const element = elementRef.current;\n if (!element) return;\n\n const handleSelect = (e: CustomEvent<SelectEventDetail>) => {\n const { item, index } = e.detail;\n onSelect?.(item as SelectItem, index);\n };\n\n const handleChange = (e: CustomEvent<ChangeEventDetail>) => {\n const { selectedItems, selectedValues } = e.detail;\n \n // Cast selectedValues to proper type\n const values = selectedValues as (string | number)[];\n \n // Update internal value in uncontrolled mode\n if (!isControlled) {\n setInternalValue(multiple ? values : values[0]);\n }\n\n // Call onChange callback\n if (onChange) {\n const value = multiple ? values : values[0];\n onChange(value, selectedItems as SelectItem[]);\n }\n };\n\n const handleOpen = (e: CustomEvent<OpenEventDetail>) => {\n onOpen?.();\n };\n\n const handleClose = (e: CustomEvent<CloseEventDetail>) => {\n onClose?.();\n };\n\n const handleSearch = (e: CustomEvent<SearchEventDetail>) => {\n const { query, results, count } = e.detail;\n onSearch?.(query, results as SelectItem[] | undefined, count);\n };\n\n const handleLoadMore = (e: CustomEvent<LoadMoreEventDetail>) => {\n const { page } = e.detail;\n onLoadMore?.(page);\n };\n\n element.addEventListener('select', handleSelect);\n element.addEventListener('change', handleChange);\n element.addEventListener('open', handleOpen);\n element.addEventListener('close', handleClose);\n element.addEventListener('search', handleSearch);\n element.addEventListener('loadMore', handleLoadMore);\n\n return () => {\n element.removeEventListener('select', handleSelect);\n element.removeEventListener('change', handleChange);\n element.removeEventListener('open', handleOpen);\n element.removeEventListener('close', handleClose);\n element.removeEventListener('search', handleSearch);\n element.removeEventListener('loadMore', handleLoadMore);\n };\n }, [onSelect, onChange, onOpen, onClose, onSearch, onLoadMore, isControlled, multiple]);\n\n // Expose imperative handle\n useImperativeHandle(ref, () => ({\n focus: () => {\n elementRef.current?.focus();\n },\n open: () => {\n elementRef.current?.open();\n },\n close: () => {\n elementRef.current?.close();\n },\n getSelectedItems: () => {\n return elementRef.current?.getSelectedItems() || [];\n },\n getSelectedValues: () => {\n return elementRef.current?.getSelectedValues() || [];\n },\n setItems: (items: SelectItem[]) => {\n elementRef.current?.setItems(items);\n },\n setGroupedItems: (groups: GroupedItem[]) => {\n elementRef.current?.setGroupedItems(groups);\n },\n clear: () => {\n elementRef.current?.setSelectedValues([]);\n if (!isControlled) {\n setInternalValue(multiple ? [] : undefined);\n }\n onChange?.(multiple ? [] : '', []);\n },\n }));\n\n return React.createElement('enhanced-select', {\n ref: elementRef,\n className,\n style,\n });\n});\n\nSelect.displayName = 'Select';\n"],"names":["value","items"],"mappings":";AA6LO,MAAM,SAAS,WAAsC,CAAC,OAAO,QAAQ;AAC1E,QAAM;AAAA,IACJ,QAAQ,CAAA;AAAA,IACR;AAAA,IACA;AAAA,IACA;AAAA,IACA,WAAW;AAAA,IACX,aAAa;AAAA,IACb,cAAc;AAAA,IACd,WAAW;AAAA,IACX,WAAW;AAAA,IACX,QAAQ;AAAA,IACR;AAAA,IACA,iBAAiB;AAAA,IACjB,WAAW;AAAA,IACX,cAAc;AAAA,IACd,sBAAsB;AAAA,IACtB;AAAA,IACA,YAAY;AAAA,IACZ;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA,UAAU;AAAA,IACV,YAAY;AAAA,IACZ;AAAA,EAAA,IACE;AAEJ,QAAM,aAAa,OAAY,IAAI;AACnC,QAAM,CAAC,YAAY,IAAI,SAAS,UAAU,MAAS;AACnD,QAAM,CAAC,eAAe,gBAAgB,IAAI,SAAS,YAAY;AAG/D,YAAU,MAAM;AACd,QAAI,OAAO,WAAW,eAAe,CAAC,eAAe,IAAI,iBAAiB,GAAG;AAC3E,aAAO,gBAAgB,EAAE,KAAK,CAAC,WAAW;AACxC,YAAI,CAAC,eAAe,IAAI,iBAAiB,GAAG;AAC1C,yBAAe,OAAO,mBAAmB,OAAO,cAAc;AAAA,QAChE;AAAA,MACF,CAAC;AAAA,IACH;AAAA,EACF,GA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AAwB;;AACjC,uBAAW,YAAX,mBAAoB,SAASA;AAAAA,IAC/B;AAAA,IACA,iBAAiB,CAAC,WAA0B;;AAC1C,uBAAW,YAAX,mBAAoB,gBAAgB;AAAA,IACtC;AAAA,IACA,OAAO,MAAM;;AACX,uBAAW,YAAX,mBAAoB,kBAAkB;AACtC,UAAI,CAAC,cAAc;AACjB,yBAAiB,WAAW,CAAA,IAAK,MAAS;AAAA,MAC5C;AACA,2CAAW,WAAW,KAAK,IAAI,CAAA;AAAA,IACjC;AAAA,EAAA,EACA;AAEF,SAAO,MAAM,cAAc,mBAAmB;AAAA,IAC5C,KAAK;AAAA,IACL;AAAA,IACA;AAAA,EAAA,CACD;AACH,CAAC;AAED,OAAO,cAAc;"}
|
package/package.json
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@smilodon/react",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "React adapter for @smilodon/core - Production-ready accessible select component",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./dist/index.cjs",
|
|
7
|
+
"module": "./dist/index.js",
|
|
8
|
+
"types": "./dist/types/index.d.ts",
|
|
9
|
+
"exports": {
|
|
10
|
+
".": {
|
|
11
|
+
"import": "./dist/index.js",
|
|
12
|
+
"require": "./dist/index.cjs",
|
|
13
|
+
"types": "./dist/types/index.d.ts"
|
|
14
|
+
}
|
|
15
|
+
},
|
|
16
|
+
"files": [
|
|
17
|
+
"dist",
|
|
18
|
+
"README.md"
|
|
19
|
+
],
|
|
20
|
+
"scripts": {
|
|
21
|
+
"build": "tsc --declaration --emitDeclarationOnly --outDir dist/types && vite build && node scripts/cleanup-types.js",
|
|
22
|
+
"size": "gzip -c dist/index.js | wc -c",
|
|
23
|
+
"prepublishOnly": "npm run build"
|
|
24
|
+
},
|
|
25
|
+
"peerDependencies": {
|
|
26
|
+
"@smilodon/core": "^1.0.0",
|
|
27
|
+
"react": ">=16.8.0"
|
|
28
|
+
},
|
|
29
|
+
"devDependencies": {
|
|
30
|
+
"@types/react": "^18.2.0",
|
|
31
|
+
"@vitejs/plugin-react": "^4.7.0",
|
|
32
|
+
"react": "^18.2.0",
|
|
33
|
+
"typescript": "^5.6.2",
|
|
34
|
+
"vite": "^5.4.21"
|
|
35
|
+
},
|
|
36
|
+
"keywords": [
|
|
37
|
+
"react",
|
|
38
|
+
"select",
|
|
39
|
+
"dropdown",
|
|
40
|
+
"combobox",
|
|
41
|
+
"autocomplete",
|
|
42
|
+
"searchable",
|
|
43
|
+
"multi-select",
|
|
44
|
+
"virtualization",
|
|
45
|
+
"infinite-scroll",
|
|
46
|
+
"accessible",
|
|
47
|
+
"wcag",
|
|
48
|
+
"aria",
|
|
49
|
+
"typescript",
|
|
50
|
+
"web-components"
|
|
51
|
+
],
|
|
52
|
+
"license": "MIT",
|
|
53
|
+
"author": "Navid Rezadoost <navidrezadoost07@gmail.com>",
|
|
54
|
+
"sideEffects": false,
|
|
55
|
+
"repository": {
|
|
56
|
+
"type": "git",
|
|
57
|
+
"url": "git+https://github.com/navidrezadoost/smilodon.git",
|
|
58
|
+
"directory": "packages/react"
|
|
59
|
+
},
|
|
60
|
+
"bugs": {
|
|
61
|
+
"url": "https://github.com/navidrezadoost/smilodon/issues"
|
|
62
|
+
},
|
|
63
|
+
"homepage": "https://github.com/navidrezadoost/smilodon#readme",
|
|
64
|
+
"engines": {
|
|
65
|
+
"node": ">=18",
|
|
66
|
+
"npm": ">=9"
|
|
67
|
+
},
|
|
68
|
+
"publishConfig": {
|
|
69
|
+
"access": "public",
|
|
70
|
+
"registry": "https://registry.npmjs.org/"
|
|
71
|
+
}
|
|
72
|
+
}
|