hi-tm 0.6.6 → 0.6.8
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.
|
@@ -127,13 +127,47 @@ function CreatableSelect({ options = [], value, onChange, onClick, onValueChange
|
|
|
127
127
|
const [isOpen, setIsOpen] = (0, react_1.useState)(false);
|
|
128
128
|
const [inputValue, setInputValue] = (0, react_1.useState)('');
|
|
129
129
|
const [filteredOptions, setFilteredOptions] = (0, react_1.useState)(options ?? []);
|
|
130
|
-
const
|
|
130
|
+
const highlightedIndexRef = (0, react_1.useRef)(0);
|
|
131
131
|
const [internalLoading, setInternalLoading] = (0, react_1.useState)(false);
|
|
132
132
|
const [hasSearched, setHasSearched] = (0, react_1.useState)(false);
|
|
133
133
|
const [selectedValues, setSelectedValues] = (0, react_1.useState)([]);
|
|
134
134
|
const [isUserInteracting, setIsUserInteracting] = (0, react_1.useState)(false);
|
|
135
|
+
const [forceUpdate, setForceUpdate] = (0, react_1.useState)(0);
|
|
135
136
|
const containerRef = (0, react_1.useRef)(null);
|
|
136
137
|
const inputRef = (0, react_1.useRef)(null);
|
|
138
|
+
const scrollContainerRef = (0, react_1.useRef)(null);
|
|
139
|
+
const scrollPositionRef = (0, react_1.useRef)(0);
|
|
140
|
+
// Save scroll position before re-renders
|
|
141
|
+
const saveScrollPosition = (0, react_1.useCallback)(() => {
|
|
142
|
+
if (scrollContainerRef.current) {
|
|
143
|
+
scrollPositionRef.current = scrollContainerRef.current.scrollLeft;
|
|
144
|
+
}
|
|
145
|
+
}, []);
|
|
146
|
+
// Restore scroll position after re-renders
|
|
147
|
+
const restoreScrollPosition = (0, react_1.useCallback)(() => {
|
|
148
|
+
if (scrollContainerRef.current && scrollPositionRef.current > 0) {
|
|
149
|
+
// Only restore if the position has actually changed
|
|
150
|
+
if (scrollContainerRef.current.scrollLeft !== scrollPositionRef.current) {
|
|
151
|
+
scrollContainerRef.current.scrollLeft = scrollPositionRef.current;
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
}, []);
|
|
155
|
+
// Prevent scroll jumping by maintaining position during keyboard navigation
|
|
156
|
+
const handleKeyboardNavigation = (0, react_1.useCallback)((direction) => {
|
|
157
|
+
saveScrollPosition();
|
|
158
|
+
if (direction === 'up') {
|
|
159
|
+
highlightedIndexRef.current = Math.max(0, highlightedIndexRef.current - 1);
|
|
160
|
+
}
|
|
161
|
+
else {
|
|
162
|
+
highlightedIndexRef.current = Math.min(filteredOptions.length - 1, highlightedIndexRef.current + 1);
|
|
163
|
+
}
|
|
164
|
+
// Force re-render of dropdown options only
|
|
165
|
+
setForceUpdate((prev) => prev + 1);
|
|
166
|
+
// Restore scroll position after state update
|
|
167
|
+
requestAnimationFrame(() => {
|
|
168
|
+
restoreScrollPosition();
|
|
169
|
+
});
|
|
170
|
+
}, [filteredOptions.length, saveScrollPosition, restoreScrollPosition]);
|
|
137
171
|
// Sync external value prop with internal selectedValues state
|
|
138
172
|
(0, react_1.useEffect)(() => {
|
|
139
173
|
// Don't sync if user is actively interacting with the component
|
|
@@ -182,6 +216,13 @@ function CreatableSelect({ options = [], value, onChange, onClick, onValueChange
|
|
|
182
216
|
setFilteredOptions(options ?? []);
|
|
183
217
|
}
|
|
184
218
|
}, [options, hasSearched]);
|
|
219
|
+
// Restore scroll position after filteredOptions changes
|
|
220
|
+
(0, react_1.useEffect)(() => {
|
|
221
|
+
// Use requestAnimationFrame to ensure DOM has updated
|
|
222
|
+
requestAnimationFrame(() => {
|
|
223
|
+
restoreScrollPosition();
|
|
224
|
+
});
|
|
225
|
+
}, [filteredOptions, restoreScrollPosition]);
|
|
185
226
|
// Reset filtered list when input cleared
|
|
186
227
|
(0, react_1.useEffect)(() => {
|
|
187
228
|
if (inputValue === '') {
|
|
@@ -239,8 +280,9 @@ function CreatableSelect({ options = [], value, onChange, onClick, onValueChange
|
|
|
239
280
|
case 'Enter':
|
|
240
281
|
e.preventDefault();
|
|
241
282
|
if (isOpen) {
|
|
242
|
-
if (filteredOptions.length > 0 &&
|
|
243
|
-
|
|
283
|
+
if (filteredOptions.length > 0 &&
|
|
284
|
+
highlightedIndexRef.current < filteredOptions.length) {
|
|
285
|
+
selectOption(filteredOptions[highlightedIndexRef.current]);
|
|
244
286
|
}
|
|
245
287
|
else if (isCreatable &&
|
|
246
288
|
inputValue &&
|
|
@@ -259,7 +301,7 @@ function CreatableSelect({ options = [], value, onChange, onClick, onValueChange
|
|
|
259
301
|
case 'ArrowUp':
|
|
260
302
|
e.preventDefault();
|
|
261
303
|
if (isOpen) {
|
|
262
|
-
|
|
304
|
+
handleKeyboardNavigation('up');
|
|
263
305
|
}
|
|
264
306
|
else {
|
|
265
307
|
setIsOpen(true);
|
|
@@ -268,14 +310,22 @@ function CreatableSelect({ options = [], value, onChange, onClick, onValueChange
|
|
|
268
310
|
case 'ArrowDown':
|
|
269
311
|
e.preventDefault();
|
|
270
312
|
if (isOpen) {
|
|
271
|
-
|
|
313
|
+
handleKeyboardNavigation('down');
|
|
272
314
|
}
|
|
273
315
|
else {
|
|
274
316
|
setIsOpen(true);
|
|
275
317
|
}
|
|
276
318
|
break;
|
|
277
319
|
}
|
|
278
|
-
}, [
|
|
320
|
+
}, [
|
|
321
|
+
isDisabled,
|
|
322
|
+
isOpen,
|
|
323
|
+
filteredOptions,
|
|
324
|
+
inputValue,
|
|
325
|
+
deriveLabel,
|
|
326
|
+
handleKeyboardNavigation,
|
|
327
|
+
isCreatable,
|
|
328
|
+
]);
|
|
279
329
|
const emitSelection = (0, react_1.useCallback)((next) => {
|
|
280
330
|
// Backward-compatible callback
|
|
281
331
|
if (onClick) {
|
|
@@ -351,16 +401,30 @@ function CreatableSelect({ options = [], value, onChange, onClick, onValueChange
|
|
|
351
401
|
onAddClick(true);
|
|
352
402
|
}
|
|
353
403
|
}, [onAddClick]);
|
|
404
|
+
const scrollContainer = (0, react_1.useMemo)(() => (react_1.default.createElement("div", { ref: scrollContainerRef, className: "flex-grow flex items-center overflow-x-auto creatable-dropdown-scroll", key: "scroll-container-stable" },
|
|
405
|
+
react_1.default.createElement("div", { className: "flex items-center gap-2 flex-nowrap" },
|
|
406
|
+
selectedValues.length > 0 &&
|
|
407
|
+
selectedValues.map((val) => (react_1.default.createElement("div", { key: deriveValue(val), className: "flex-shrink-0 flex items-center bg-gray-200 rounded-md px-2 py-1" },
|
|
408
|
+
react_1.default.createElement("span", { className: "truncate" }, deriveLabel(val)),
|
|
409
|
+
react_1.default.createElement("button", { onClick: (e) => clearValue(e, val), className: styles.clearButton }, renderIcon(icons.clear, styles.icon))))),
|
|
410
|
+
react_1.default.createElement("input", { ref: inputRef, type: "text", className: `${styles.input} ${isDisabled ? 'bg-gray-100' : ''} min-w-[80px] flex-shrink-0`, placeholder: selectedValues.length > 0 ? '' : placeholder, value: inputValue, onChange: handleInputChange, disabled: isDisabled, onClick: (e) => e.stopPropagation() })))), [
|
|
411
|
+
selectedValues,
|
|
412
|
+
isDisabled,
|
|
413
|
+
inputValue,
|
|
414
|
+
inputRef,
|
|
415
|
+
clearValue,
|
|
416
|
+
deriveValue,
|
|
417
|
+
deriveLabel,
|
|
418
|
+
icons.clear,
|
|
419
|
+
styles.input,
|
|
420
|
+
styles.clearButton,
|
|
421
|
+
placeholder,
|
|
422
|
+
handleInputChange,
|
|
423
|
+
]);
|
|
354
424
|
return (react_1.default.createElement("div", { ref: containerRef, className: `relative w-full ${className} ${styles.container}`, onKeyDown: handleKeyDown },
|
|
355
425
|
label && react_1.default.createElement("label", { className: "block text-sm font-medium text-gray-700 mb-1" }, label),
|
|
356
426
|
react_1.default.createElement("div", { className: `${styles.trigger} ${isDisabled ? 'bg-gray-100 cursor-not-allowed' : ''} ${isOpen ? 'border-blue-500 ring-2 ring-blue-100' : 'border-gray-300'}`, onClick: toggleDropdown },
|
|
357
|
-
|
|
358
|
-
react_1.default.createElement("div", { className: "flex items-center gap-2 flex-nowrap" },
|
|
359
|
-
selectedValues.length > 0 &&
|
|
360
|
-
selectedValues.map((val) => (react_1.default.createElement("div", { key: deriveValue(val), className: "flex-shrink-0 flex items-center bg-gray-200 rounded-md px-2 py-1" },
|
|
361
|
-
react_1.default.createElement("span", { className: "truncate" }, deriveLabel(val)),
|
|
362
|
-
react_1.default.createElement("button", { onClick: (e) => clearValue(e, val), className: styles.clearButton }, renderIcon(icons.clear, styles.icon))))),
|
|
363
|
-
react_1.default.createElement("input", { ref: inputRef, type: "text", className: `${styles.input} ${isDisabled ? 'bg-gray-100' : ''} min-w-[80px] flex-shrink-0`, placeholder: selectedValues.length > 0 ? '' : placeholder, value: inputValue, onChange: handleInputChange, disabled: isDisabled, onClick: (e) => e.stopPropagation() }))),
|
|
427
|
+
scrollContainer,
|
|
364
428
|
react_1.default.createElement("div", { className: "flex-shrink-0 flex items-center gap-1 ml-2" },
|
|
365
429
|
iconSrc.hasAdd && (react_1.default.createElement("button", { onClick: handleAddIconClick, className: styles.clearButton }, renderIcon(iconSrc.hasAdd, styles.icon))),
|
|
366
430
|
styles.separator && react_1.default.createElement("div", { className: styles.separator }),
|
|
@@ -370,7 +434,9 @@ function CreatableSelect({ options = [], value, onChange, onClick, onValueChange
|
|
|
370
434
|
isOpen && (react_1.default.createElement("div", { className: styles.dropdown }, showLoading ? (react_1.default.createElement("div", { className: styles.loadingState },
|
|
371
435
|
renderIcon(iconSrc.loading, `animate-spin mr-2 ${styles.icon}`),
|
|
372
436
|
loadingMessage)) : (react_1.default.createElement(react_1.default.Fragment, null,
|
|
373
|
-
filteredOptions.length > 0 && (react_1.default.createElement(react_1.default.Fragment, null, filteredOptions.map((option, index) => (react_1.default.createElement("div", { key: deriveValue(option)
|
|
437
|
+
filteredOptions.length > 0 && (react_1.default.createElement(react_1.default.Fragment, null, filteredOptions.map((option, index) => (react_1.default.createElement("div", { key: `${deriveValue(option)}-${forceUpdate}`, className: `${styles.option} ${index === highlightedIndexRef.current
|
|
438
|
+
? styles.highlightedOption
|
|
439
|
+
: 'hover:bg-gray-50'}`, onClick: () => selectOption(option), onMouseEnter: () => (highlightedIndexRef.current = index) },
|
|
374
440
|
selectedValues.some((val) => deriveValue(val) === deriveValue(option)) &&
|
|
375
441
|
renderIcon(iconSrc.checkmark, 'w-4 h-4 mr-2 text-blue-500'),
|
|
376
442
|
react_1.default.createElement("span", { className: selectedValues.some((val) => deriveValue(val) === deriveValue(option))
|
package/dist/styles/globals.css
CHANGED
|
@@ -87,6 +87,21 @@ li.activeSidebar-subset::after {
|
|
|
87
87
|
.noDisplay-scrollbar::-webkit-scrollbar {
|
|
88
88
|
display: none; /* For Chrome, Safari, and Opera */
|
|
89
89
|
}
|
|
90
|
+
|
|
91
|
+
/* Custom scroll behavior for CreatableDropDown */
|
|
92
|
+
.creatable-dropdown-scroll {
|
|
93
|
+
scroll-behavior: smooth;
|
|
94
|
+
scrollbar-width: none;
|
|
95
|
+
-ms-overflow-style: none;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
.creatable-dropdown-scroll::-webkit-scrollbar {
|
|
99
|
+
display: none;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
.creatable-dropdown-scroll:focus-within {
|
|
103
|
+
scroll-behavior: auto;
|
|
104
|
+
}
|
|
90
105
|
.danger {
|
|
91
106
|
background-color: #ffffff;
|
|
92
107
|
color: #f54180;
|