hi-tm 0.6.0 → 0.6.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.
|
@@ -87,7 +87,20 @@ const renderNoResults = (noResults, className, searchIcon) => {
|
|
|
87
87
|
return noResults;
|
|
88
88
|
}
|
|
89
89
|
};
|
|
90
|
-
function CreatableSelect({ options = [], value, onChange, onClick, onValueChange, onCreateOption, placeholder = 'Select...', isLoading = false, isDisabled = false, isCreatable = true, isMultiSelect = false, noOptionsMessage = 'No options available', noResultsFound, loadingMessage = 'Loading...', createOptionMessage = 'Create option', className = '', tailwindStyle = {}, iconSrc, fetchOptions, label, onAddClick, getOptionLabel, getOptionValue, }) {
|
|
90
|
+
function CreatableSelect({ options = [], value, onChange, onClick, onValueChange, onCreateOption, placeholder = 'Select...', isLoading = false, isDisabled = false, isCreatable = true, isMultiSelect = false, noOptionsMessage = 'No options available', noResultsFound, loadingMessage = 'Loading...', createOptionMessage = 'Create option', className = '', tailwindStyle = {}, iconSrc = {}, fetchOptions, label, onAddClick, getOptionLabel, getOptionValue, }) {
|
|
91
|
+
// Default icons
|
|
92
|
+
const defaultIcons = {
|
|
93
|
+
dropdownOpen: '▼',
|
|
94
|
+
dropdownClosed: '▶',
|
|
95
|
+
checkmark: '✓',
|
|
96
|
+
loading: '⏳',
|
|
97
|
+
clear: '✕',
|
|
98
|
+
create: '➕',
|
|
99
|
+
xmark: '✕',
|
|
100
|
+
hasAdd: '➕',
|
|
101
|
+
};
|
|
102
|
+
// Merge provided icons with defaults
|
|
103
|
+
const icons = { ...defaultIcons, ...iconSrc };
|
|
91
104
|
const styles = (0, react_1.useMemo)(() => ({
|
|
92
105
|
...defaultStyles,
|
|
93
106
|
...tailwindStyle,
|
|
@@ -98,7 +111,8 @@ function CreatableSelect({ options = [], value, onChange, onClick, onValueChange
|
|
|
98
111
|
return getOptionLabel(option);
|
|
99
112
|
const anyOption = option;
|
|
100
113
|
const label = anyOption.label ?? anyOption.content ?? anyOption.value;
|
|
101
|
-
|
|
114
|
+
const result = String(label ?? '');
|
|
115
|
+
return result;
|
|
102
116
|
}, [getOptionLabel]);
|
|
103
117
|
const deriveValue = (0, react_1.useCallback)((option) => {
|
|
104
118
|
if (getOptionValue)
|
|
@@ -119,21 +133,35 @@ function CreatableSelect({ options = [], value, onChange, onClick, onValueChange
|
|
|
119
133
|
const inputRef = (0, react_1.useRef)(null);
|
|
120
134
|
// Sync external value prop with internal selectedValues state
|
|
121
135
|
(0, react_1.useEffect)(() => {
|
|
122
|
-
|
|
123
|
-
|
|
136
|
+
// Only sync if the external value is different from current selectedValues
|
|
137
|
+
const currentValues = selectedValues;
|
|
138
|
+
const externalValue = value;
|
|
139
|
+
if (externalValue === null || externalValue === undefined) {
|
|
140
|
+
if (currentValues.length > 0) {
|
|
141
|
+
setSelectedValues([]);
|
|
142
|
+
}
|
|
124
143
|
}
|
|
125
|
-
else if (Array.isArray(
|
|
144
|
+
else if (Array.isArray(externalValue)) {
|
|
126
145
|
if (isMultiSelect) {
|
|
127
|
-
|
|
146
|
+
const externalArray = externalValue;
|
|
147
|
+
if (JSON.stringify(externalArray) !== JSON.stringify(currentValues)) {
|
|
148
|
+
setSelectedValues(externalArray);
|
|
149
|
+
}
|
|
128
150
|
}
|
|
129
151
|
else {
|
|
130
|
-
|
|
152
|
+
const firstItem = externalValue.length > 0 ? [externalValue[0]] : [];
|
|
153
|
+
if (JSON.stringify(firstItem) !== JSON.stringify(currentValues)) {
|
|
154
|
+
setSelectedValues(firstItem);
|
|
155
|
+
}
|
|
131
156
|
}
|
|
132
157
|
}
|
|
133
158
|
else {
|
|
134
|
-
|
|
159
|
+
const singleItem = [externalValue];
|
|
160
|
+
if (JSON.stringify(singleItem) !== JSON.stringify(currentValues)) {
|
|
161
|
+
setSelectedValues(singleItem);
|
|
162
|
+
}
|
|
135
163
|
}
|
|
136
|
-
}, [value, isMultiSelect]);
|
|
164
|
+
}, [value, isMultiSelect, selectedValues]);
|
|
137
165
|
// Sync options into filtered list when not searching
|
|
138
166
|
(0, react_1.useEffect)(() => {
|
|
139
167
|
if (!hasSearched) {
|
|
@@ -287,15 +315,19 @@ function CreatableSelect({ options = [], value, onChange, onClick, onValueChange
|
|
|
287
315
|
}
|
|
288
316
|
setSelectedValues(next);
|
|
289
317
|
emitSelection(next);
|
|
318
|
+
// In multi-select mode, keep the input open and clear input for next search
|
|
319
|
+
setInputValue('');
|
|
320
|
+
setHasSearched(false);
|
|
321
|
+
// Don't close dropdown in multi-select mode
|
|
290
322
|
}
|
|
291
323
|
else {
|
|
292
324
|
const next = [option];
|
|
293
325
|
setSelectedValues(next);
|
|
294
326
|
emitSelection(next);
|
|
327
|
+
setInputValue('');
|
|
328
|
+
setIsOpen(false);
|
|
329
|
+
setHasSearched(false);
|
|
295
330
|
}
|
|
296
|
-
setInputValue('');
|
|
297
|
-
setIsOpen(false);
|
|
298
|
-
setHasSearched(false);
|
|
299
331
|
}, [isMultiSelect, selectedValues, deriveValue, emitSelection]);
|
|
300
332
|
const showLoading = isLoading || internalLoading;
|
|
301
333
|
const handleAddIconClick = (0, react_1.useCallback)((e) => {
|
|
@@ -312,21 +344,21 @@ function CreatableSelect({ options = [], value, onChange, onClick, onValueChange
|
|
|
312
344
|
selectedValues.length > 0 &&
|
|
313
345
|
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" },
|
|
314
346
|
react_1.default.createElement("span", { className: "truncate" }, deriveLabel(val)),
|
|
315
|
-
react_1.default.createElement("button", { onClick: (e) => clearValue(e, val), className: styles.clearButton }, renderIcon(
|
|
347
|
+
react_1.default.createElement("button", { onClick: (e) => clearValue(e, val), className: styles.clearButton }, renderIcon(icons.clear, styles.icon))))),
|
|
316
348
|
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() }))),
|
|
317
349
|
react_1.default.createElement("div", { className: "flex-shrink-0 flex items-center gap-1 ml-2" },
|
|
318
|
-
showLoading && renderIcon(
|
|
319
|
-
|
|
350
|
+
showLoading && renderIcon(icons.loading, `animate-spin ${styles.icon}`),
|
|
351
|
+
icons.hasAdd && onAddClick !== undefined && (react_1.default.createElement("button", { onClick: handleAddIconClick, className: styles.clearButton }, renderIcon(icons.hasAdd, styles.icon))),
|
|
320
352
|
styles.separator && react_1.default.createElement("div", { className: styles.separator }),
|
|
321
353
|
isOpen
|
|
322
|
-
? renderIcon(
|
|
323
|
-
: renderIcon(
|
|
354
|
+
? renderIcon(icons.dropdownOpen, `${styles.icon} ${styles.spinAnimation || ''}`)
|
|
355
|
+
: renderIcon(icons.dropdownClosed, styles.icon))),
|
|
324
356
|
isOpen && (react_1.default.createElement("div", { className: styles.dropdown }, showLoading ? (react_1.default.createElement("div", { className: styles.loadingState },
|
|
325
|
-
renderIcon(
|
|
357
|
+
renderIcon(icons.loading, `animate-spin mr-2 ${styles.icon}`),
|
|
326
358
|
loadingMessage)) : (react_1.default.createElement(react_1.default.Fragment, null,
|
|
327
359
|
filteredOptions.length > 0 && (react_1.default.createElement(react_1.default.Fragment, null, filteredOptions.map((option, index) => (react_1.default.createElement("div", { key: deriveValue(option), className: `${styles.option} ${index === highlightedIndex ? styles.highlightedOption : 'hover:bg-gray-50'}`, onClick: () => selectOption(option), onMouseEnter: () => setHighlightedIndex(index) },
|
|
328
360
|
selectedValues.some((val) => deriveValue(val) === deriveValue(option)) &&
|
|
329
|
-
renderIcon(
|
|
361
|
+
renderIcon(icons.checkmark, 'w-4 h-4 mr-2 text-blue-500'),
|
|
330
362
|
react_1.default.createElement("span", { className: selectedValues.some((val) => deriveValue(val) === deriveValue(option))
|
|
331
363
|
? styles.selectedOption
|
|
332
364
|
: '' }, deriveLabel(option) || deriveValue(option))))))),
|
|
@@ -334,11 +366,11 @@ function CreatableSelect({ options = [], value, onChange, onClick, onValueChange
|
|
|
334
366
|
filteredOptions.length === 0 &&
|
|
335
367
|
inputValue &&
|
|
336
368
|
hasSearched &&
|
|
337
|
-
renderNoResults(noResultsFound, styles.noResults ?? '',
|
|
369
|
+
renderNoResults(noResultsFound, styles.noResults ?? '', icons.create),
|
|
338
370
|
isCreatable &&
|
|
339
371
|
inputValue &&
|
|
340
372
|
!options?.some((o) => deriveLabel(o) === inputValue) && (react_1.default.createElement("div", { className: styles.createOption, onClick: handleCreateOption },
|
|
341
|
-
renderIcon(
|
|
373
|
+
renderIcon(icons.create, styles.icon),
|
|
342
374
|
react_1.default.createElement("span", null,
|
|
343
375
|
createOptionMessage,
|
|
344
376
|
": ",
|
|
@@ -65,9 +65,9 @@ export type CreatableSelectProps<T = SelectOption> = {
|
|
|
65
65
|
className?: string;
|
|
66
66
|
tailwindStyle?: TailwindStyleProps;
|
|
67
67
|
label?: string;
|
|
68
|
-
iconSrc
|
|
68
|
+
iconSrc?: IconSourceProps;
|
|
69
69
|
fetchOptions?: (inputValue: string) => Promise<T[]>;
|
|
70
|
-
onAddClick
|
|
70
|
+
onAddClick?: (e: boolean) => void;
|
|
71
71
|
isMultiSelect?: boolean;
|
|
72
72
|
getOptionLabel?: OptionLabelGetter<T>;
|
|
73
73
|
getOptionValue?: OptionValueGetter<T>;
|