hi-tm 0.5.8 → 0.6.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.
|
@@ -114,24 +114,32 @@ function CreatableSelect({ options = [], value, onChange, onClick, onValueChange
|
|
|
114
114
|
const [highlightedIndex, setHighlightedIndex] = (0, react_1.useState)(0);
|
|
115
115
|
const [internalLoading, setInternalLoading] = (0, react_1.useState)(false);
|
|
116
116
|
const [hasSearched, setHasSearched] = (0, react_1.useState)(false);
|
|
117
|
-
const [selectedValues, setSelectedValues] = (0, react_1.useState)(
|
|
117
|
+
const [selectedValues, setSelectedValues] = (0, react_1.useState)([]);
|
|
118
118
|
const containerRef = (0, react_1.useRef)(null);
|
|
119
119
|
const inputRef = (0, react_1.useRef)(null);
|
|
120
|
-
// Sync
|
|
120
|
+
// Sync external value prop with internal selectedValues state
|
|
121
121
|
(0, react_1.useEffect)(() => {
|
|
122
|
-
if (
|
|
123
|
-
|
|
122
|
+
if (value === null || value === undefined) {
|
|
123
|
+
setSelectedValues([]);
|
|
124
124
|
}
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
125
|
+
else if (Array.isArray(value)) {
|
|
126
|
+
if (isMultiSelect) {
|
|
127
|
+
setSelectedValues(value);
|
|
128
|
+
}
|
|
129
|
+
else {
|
|
130
|
+
setSelectedValues(value.length > 0 ? [value[0]] : []);
|
|
131
|
+
}
|
|
130
132
|
}
|
|
131
133
|
else {
|
|
132
|
-
setSelectedValues(
|
|
134
|
+
setSelectedValues([value]);
|
|
133
135
|
}
|
|
134
136
|
}, [value, isMultiSelect]);
|
|
137
|
+
// Sync options into filtered list when not searching
|
|
138
|
+
(0, react_1.useEffect)(() => {
|
|
139
|
+
if (!hasSearched) {
|
|
140
|
+
setFilteredOptions(options ?? []);
|
|
141
|
+
}
|
|
142
|
+
}, [options, hasSearched]);
|
|
135
143
|
// Reset filtered list when input cleared
|
|
136
144
|
(0, react_1.useEffect)(() => {
|
|
137
145
|
if (inputValue === '') {
|
|
@@ -180,7 +188,6 @@ function CreatableSelect({ options = [], value, onChange, onClick, onValueChange
|
|
|
180
188
|
setFilteredOptions(filtered);
|
|
181
189
|
setHasSearched(true);
|
|
182
190
|
}
|
|
183
|
-
// Don't clear selectedValues here - preserve them during search
|
|
184
191
|
}, [onChange, fetchOptions, options, deriveLabel]);
|
|
185
192
|
const handleKeyDown = (0, react_1.useCallback)((e) => {
|
|
186
193
|
if (isDisabled)
|
|
@@ -235,25 +242,6 @@ function CreatableSelect({ options = [], value, onChange, onClick, onValueChange
|
|
|
235
242
|
onValueChange(isMultiSelect ? next ?? [] : next ? next[0] : null);
|
|
236
243
|
}
|
|
237
244
|
}, [onClick, onValueChange, isMultiSelect]);
|
|
238
|
-
const selectOption = (0, react_1.useCallback)((option) => {
|
|
239
|
-
if (isMultiSelect) {
|
|
240
|
-
const next = [...selectedValues];
|
|
241
|
-
if (!next.some((val) => deriveValue(val) === deriveValue(option))) {
|
|
242
|
-
next.push(option);
|
|
243
|
-
}
|
|
244
|
-
setSelectedValues(next);
|
|
245
|
-
emitSelection(next);
|
|
246
|
-
}
|
|
247
|
-
else {
|
|
248
|
-
const next = [option];
|
|
249
|
-
setSelectedValues(next);
|
|
250
|
-
emitSelection(next);
|
|
251
|
-
}
|
|
252
|
-
// Only clear input and close dropdown when actually selecting an option
|
|
253
|
-
setInputValue('');
|
|
254
|
-
setIsOpen(false);
|
|
255
|
-
setHasSearched(false);
|
|
256
|
-
}, [isMultiSelect, selectedValues, deriveValue, emitSelection]);
|
|
257
245
|
const handleCreateOption = (0, react_1.useCallback)(async () => {
|
|
258
246
|
if (!inputValue.trim() || !isCreatable)
|
|
259
247
|
return;
|
|
@@ -268,7 +256,7 @@ function CreatableSelect({ options = [], value, onChange, onClick, onValueChange
|
|
|
268
256
|
};
|
|
269
257
|
const optionToAdd = created ?? fallback;
|
|
270
258
|
selectOption(optionToAdd);
|
|
271
|
-
|
|
259
|
+
setInputValue('');
|
|
272
260
|
setHasSearched(false);
|
|
273
261
|
}
|
|
274
262
|
catch {
|
|
@@ -276,7 +264,7 @@ function CreatableSelect({ options = [], value, onChange, onClick, onValueChange
|
|
|
276
264
|
finally {
|
|
277
265
|
setInternalLoading(false);
|
|
278
266
|
}
|
|
279
|
-
}, [inputValue, isCreatable, onCreateOption
|
|
267
|
+
}, [inputValue, isCreatable, onCreateOption]);
|
|
280
268
|
const toggleDropdown = (0, react_1.useCallback)(() => {
|
|
281
269
|
if (!isDisabled) {
|
|
282
270
|
setIsOpen((prev) => !prev);
|
|
@@ -291,6 +279,24 @@ function CreatableSelect({ options = [], value, onChange, onClick, onValueChange
|
|
|
291
279
|
setSelectedValues(next);
|
|
292
280
|
emitSelection(next);
|
|
293
281
|
}, [selectedValues, deriveValue, emitSelection]);
|
|
282
|
+
const selectOption = (0, react_1.useCallback)((option) => {
|
|
283
|
+
if (isMultiSelect) {
|
|
284
|
+
const next = [...selectedValues];
|
|
285
|
+
if (!next.some((val) => deriveValue(val) === deriveValue(option))) {
|
|
286
|
+
next.push(option);
|
|
287
|
+
}
|
|
288
|
+
setSelectedValues(next);
|
|
289
|
+
emitSelection(next);
|
|
290
|
+
}
|
|
291
|
+
else {
|
|
292
|
+
const next = [option];
|
|
293
|
+
setSelectedValues(next);
|
|
294
|
+
emitSelection(next);
|
|
295
|
+
}
|
|
296
|
+
setInputValue('');
|
|
297
|
+
setIsOpen(false);
|
|
298
|
+
setHasSearched(false);
|
|
299
|
+
}, [isMultiSelect, selectedValues, deriveValue, emitSelection]);
|
|
294
300
|
const showLoading = isLoading || internalLoading;
|
|
295
301
|
const handleAddIconClick = (0, react_1.useCallback)((e) => {
|
|
296
302
|
e.stopPropagation();
|