@carbonid1/design-system 5.15.1 → 5.15.2
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/dist/Select/Select.js +13 -2
- package/package.json +1 -1
package/dist/Select/Select.js
CHANGED
|
@@ -21,7 +21,9 @@ export const Select = ({ value, onChange, options, groups, placeholder, id, clas
|
|
|
21
21
|
return { items: flat, groupStartIndices: starts };
|
|
22
22
|
}, [options, groups]);
|
|
23
23
|
const selectedOption = items.find(o => o.value === value);
|
|
24
|
+
const selectedIndex = items.findIndex(o => o.value === value);
|
|
24
25
|
const displayText = selectedOption?.label ?? placeholder ?? value;
|
|
26
|
+
const initialHighlightedIndex = selectedIndex >= 0 ? selectedIndex : 0;
|
|
25
27
|
// Click outside to close
|
|
26
28
|
useEffect(() => {
|
|
27
29
|
const handleClickOutside = (e) => {
|
|
@@ -47,10 +49,13 @@ export const Select = ({ value, onChange, options, groups, placeholder, id, clas
|
|
|
47
49
|
if (e.key === 'ArrowDown' || e.key === 'ArrowUp' || e.key === 'Enter' || e.key === ' ') {
|
|
48
50
|
e.preventDefault();
|
|
49
51
|
changeOpen(true);
|
|
50
|
-
setHighlightedIndex(
|
|
52
|
+
setHighlightedIndex(initialHighlightedIndex);
|
|
51
53
|
}
|
|
52
54
|
return;
|
|
53
55
|
}
|
|
56
|
+
if (items.length === 0) {
|
|
57
|
+
return;
|
|
58
|
+
}
|
|
54
59
|
if (e.key === 'ArrowDown') {
|
|
55
60
|
e.preventDefault();
|
|
56
61
|
setHighlightedIndex(prev => (prev + 1) % items.length);
|
|
@@ -73,7 +78,13 @@ export const Select = ({ value, onChange, options, groups, placeholder, id, clas
|
|
|
73
78
|
const selected = itemValue === value ? 'font-medium' : '';
|
|
74
79
|
return `${base} ${highlight} ${selected}`;
|
|
75
80
|
};
|
|
76
|
-
return (_jsxs("div", { className: "relative", ref: containerRef, onKeyDown: handleKeyDown, children: [_jsxs("button", { type: "button", id: id, className: `flex items-center justify-between gap-2 ${className ?? ''}`, "aria-expanded": open, "aria-haspopup": "listbox", "aria-label": ariaLabel, onClick: () =>
|
|
81
|
+
return (_jsxs("div", { className: "relative", ref: containerRef, onKeyDown: handleKeyDown, children: [_jsxs("button", { type: "button", id: id, className: `flex items-center justify-between gap-2 ${className ?? ''}`, "aria-expanded": open, "aria-haspopup": "listbox", "aria-label": ariaLabel, onClick: () => {
|
|
82
|
+
const nextOpen = !open;
|
|
83
|
+
changeOpen(nextOpen);
|
|
84
|
+
if (nextOpen) {
|
|
85
|
+
setHighlightedIndex(initialHighlightedIndex);
|
|
86
|
+
}
|
|
87
|
+
}, children: [_jsx("span", { className: "truncate", children: displayText }), _jsx(ChevronDown, { className: `size-4 shrink-0 transition-transform ${open ? 'rotate-180' : ''}` })] }), open && (_jsx("ul", { role: "listbox", className: `border-border bg-background absolute z-50 mt-1 max-h-60 w-max min-w-full overflow-y-auto rounded-lg border shadow-lg ${menuClassName ?? ''}`, children: items.map((item, index) => {
|
|
77
88
|
const groupHeader = groupStartIndices.find(g => g.index === index);
|
|
78
89
|
return (_jsxs("li", { role: "presentation", children: [groupHeader && (_jsx("div", { className: "text-muted-foreground px-3 pt-2 pb-1 text-xs font-medium uppercase", children: groupHeader.label })), _jsx("div", { role: "option", "aria-selected": value === item.value, className: itemClassName(index, item.value), onClick: () => selectItem(item.value), onMouseEnter: () => setHighlightedIndex(index), children: renderOption
|
|
79
90
|
? renderOption(item, {
|