hi-tm 0.3.8 → 0.3.9
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.
|
@@ -1,4 +1,3 @@
|
|
|
1
1
|
import React from 'react';
|
|
2
|
-
import type { CreatableSelectProps } from '../../types/DropDown/CreatableDropDown';
|
|
3
|
-
export default function CreatableSelect({ options, value, onChange, onClick,
|
|
4
|
-
onCreateOption, placeholder, isLoading, isDisabled, isClearable, isCreatable, isMultiSelect, noOptionsMessage, noResultsFound, loadingMessage, createOptionMessage, className, tailwindStyle, iconSrc, fetchOptions, label, onAddClick, }: CreatableSelectProps): React.JSX.Element;
|
|
2
|
+
import type { SelectOption, CreatableSelectProps } from '../../types/DropDown/CreatableDropDown';
|
|
3
|
+
export default function CreatableSelect<T = SelectOption>({ options, value, onChange, onClick, onCreateOption, placeholder, isLoading, isDisabled, isCreatable, isMultiSelect, noOptionsMessage, noResultsFound, loadingMessage, createOptionMessage, className, tailwindStyle, iconSrc, fetchOptions, fetchRawOptions, mapCreateOption, toOption, onValueChange, label, onAddClick, }: CreatableSelectProps<T>): React.JSX.Element;
|
|
@@ -85,39 +85,56 @@ const renderNoResults = (noResults, className, searchIcon) => {
|
|
|
85
85
|
return noResults;
|
|
86
86
|
}
|
|
87
87
|
};
|
|
88
|
-
|
|
89
|
-
|
|
88
|
+
// Generic component so consumers can pass any T and receive T back
|
|
89
|
+
function CreatableSelect({ options = [], value, onChange, onClick, onCreateOption, placeholder = 'Select...', isLoading = false, isDisabled = false,
|
|
90
|
+
// isClearable is currently unused; keep behavior but omit local param to avoid lint error
|
|
91
|
+
isCreatable = true, isMultiSelect = false, noOptionsMessage = 'No options available', noResultsFound, loadingMessage = 'Loading...', createOptionMessage = 'Create option', className = '', tailwindStyle = {}, iconSrc, fetchOptions, fetchRawOptions, mapCreateOption, toOption, onValueChange, label, onAddClick, }) {
|
|
90
92
|
const styles = {
|
|
91
93
|
...defaultStyles,
|
|
92
94
|
...tailwindStyle,
|
|
93
95
|
};
|
|
96
|
+
// Ensure we always have a mapper from T -> SelectOption
|
|
97
|
+
const toSelectOption = (0, react_1.useMemo)(() => {
|
|
98
|
+
if (toOption)
|
|
99
|
+
return toOption;
|
|
100
|
+
// Best-effort default: if item already looks like SelectOption use it; otherwise toString
|
|
101
|
+
return (item) => {
|
|
102
|
+
if (item && typeof item === 'object' && 'value' in item && 'label' in item) {
|
|
103
|
+
return item;
|
|
104
|
+
}
|
|
105
|
+
const text = String(item ?? '');
|
|
106
|
+
return { label: text, value: text, content: text };
|
|
107
|
+
};
|
|
108
|
+
}, [toOption]);
|
|
94
109
|
const [isOpen, setIsOpen] = (0, react_1.useState)(false);
|
|
95
110
|
const [inputValue, setInputValue] = (0, react_1.useState)('');
|
|
96
|
-
const [filteredOptions, setFilteredOptions] = (0, react_1.useState)(options ?? []);
|
|
111
|
+
const [filteredOptions, setFilteredOptions] = (0, react_1.useState)((options ?? []).map(toSelectOption));
|
|
97
112
|
const [highlightedIndex, setHighlightedIndex] = (0, react_1.useState)(0);
|
|
98
113
|
const [internalLoading, setInternalLoading] = (0, react_1.useState)(false);
|
|
99
114
|
const [hasSearched, setHasSearched] = (0, react_1.useState)(false);
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
115
|
+
// Store selected values in raw T form, but display via mapper
|
|
116
|
+
const initialSelected = (0, react_1.useMemo)(() => {
|
|
117
|
+
if (!value)
|
|
118
|
+
return [];
|
|
119
|
+
return Array.isArray(value) ? value : [value];
|
|
120
|
+
}, [value]);
|
|
121
|
+
const [selectedRawValues, setSelectedRawValues] = (0, react_1.useState)(initialSelected);
|
|
122
|
+
const selectedValues = (0, react_1.useMemo)(() => selectedRawValues.map(toSelectOption), [selectedRawValues, toSelectOption]);
|
|
105
123
|
const containerRef = (0, react_1.useRef)(null);
|
|
106
124
|
const inputRef = (0, react_1.useRef)(null);
|
|
125
|
+
// Keep filtered options in sync with external changes when not actively searching
|
|
107
126
|
(0, react_1.useEffect)(() => {
|
|
108
|
-
console.log('Options prop changed:', options); // Debugging log
|
|
109
|
-
// Only update filteredOptions when options change if we're not in the middle of a search
|
|
110
127
|
if (!hasSearched) {
|
|
111
|
-
setFilteredOptions(options ?? []);
|
|
128
|
+
setFilteredOptions((options ?? []).map(toSelectOption));
|
|
112
129
|
}
|
|
113
|
-
}, [options, hasSearched]);
|
|
130
|
+
}, [options, hasSearched, toSelectOption]);
|
|
131
|
+
// Reset search list when input cleared
|
|
114
132
|
(0, react_1.useEffect)(() => {
|
|
115
133
|
if (inputValue === '') {
|
|
116
|
-
|
|
117
|
-
setFilteredOptions(options ?? []);
|
|
134
|
+
setFilteredOptions((options ?? []).map(toSelectOption));
|
|
118
135
|
setHasSearched(false);
|
|
119
136
|
}
|
|
120
|
-
}, [options, inputValue]);
|
|
137
|
+
}, [options, inputValue, toSelectOption]);
|
|
121
138
|
(0, react_1.useEffect)(() => {
|
|
122
139
|
const handler = (e) => {
|
|
123
140
|
if (containerRef.current && !containerRef.current.contains(e.target)) {
|
|
@@ -129,20 +146,34 @@ onCreateOption, placeholder = 'Select...', isLoading = false, isDisabled = false
|
|
|
129
146
|
document.removeEventListener('click', handler);
|
|
130
147
|
};
|
|
131
148
|
}, []);
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
149
|
+
// Debounce input to limit fetch/filter work
|
|
150
|
+
const [debouncedQuery, setDebouncedQuery] = (0, react_1.useState)('');
|
|
151
|
+
(0, react_1.useEffect)(() => {
|
|
152
|
+
const id = setTimeout(() => setDebouncedQuery(inputValue), 200);
|
|
153
|
+
return () => clearTimeout(id);
|
|
154
|
+
}, [inputValue]);
|
|
155
|
+
const runSearch = (0, react_1.useCallback)(async (query) => {
|
|
156
|
+
if (fetchRawOptions && query.trim() !== '') {
|
|
157
|
+
setInternalLoading(true);
|
|
158
|
+
setHasSearched(true);
|
|
159
|
+
try {
|
|
160
|
+
const rawItems = await fetchRawOptions(query);
|
|
161
|
+
setFilteredOptions((rawItems ?? []).map(toSelectOption));
|
|
162
|
+
}
|
|
163
|
+
catch (error) {
|
|
164
|
+
console.error('Error fetching raw options:', error);
|
|
165
|
+
}
|
|
166
|
+
finally {
|
|
167
|
+
setInternalLoading(false);
|
|
168
|
+
}
|
|
169
|
+
return;
|
|
139
170
|
}
|
|
140
|
-
if (fetchOptions &&
|
|
171
|
+
if (fetchOptions && query.trim() !== '') {
|
|
141
172
|
setInternalLoading(true);
|
|
142
173
|
setHasSearched(true);
|
|
143
174
|
try {
|
|
144
|
-
const newOptions = await fetchOptions(
|
|
145
|
-
setFilteredOptions(newOptions);
|
|
175
|
+
const newOptions = await fetchOptions(query);
|
|
176
|
+
setFilteredOptions(newOptions ?? []);
|
|
146
177
|
}
|
|
147
178
|
catch (error) {
|
|
148
179
|
console.error('Error fetching options:', error);
|
|
@@ -150,83 +181,100 @@ onCreateOption, placeholder = 'Select...', isLoading = false, isDisabled = false
|
|
|
150
181
|
finally {
|
|
151
182
|
setInternalLoading(false);
|
|
152
183
|
}
|
|
184
|
+
return;
|
|
153
185
|
}
|
|
154
|
-
|
|
155
|
-
setFilteredOptions(options ?? []);
|
|
186
|
+
if (query.trim() === '') {
|
|
187
|
+
setFilteredOptions((options ?? []).map(toSelectOption));
|
|
156
188
|
setHasSearched(false);
|
|
189
|
+
return;
|
|
157
190
|
}
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
const
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
191
|
+
const searchText = String(query).trim().toLowerCase();
|
|
192
|
+
const baseOptions = (options ?? []).map(toSelectOption);
|
|
193
|
+
const filtered = baseOptions.filter((option) => {
|
|
194
|
+
const optionLabel = String(option.label || option.content || option.value || '');
|
|
195
|
+
return optionLabel.toLowerCase().includes(searchText);
|
|
196
|
+
});
|
|
197
|
+
setFilteredOptions(filtered);
|
|
198
|
+
setHasSearched(true);
|
|
199
|
+
}, [fetchRawOptions, fetchOptions, options, toSelectOption]);
|
|
200
|
+
(0, react_1.useEffect)(() => {
|
|
201
|
+
runSearch(debouncedQuery);
|
|
202
|
+
}, [debouncedQuery, runSearch]);
|
|
203
|
+
const handleInputChange = (e) => {
|
|
204
|
+
const next = e.target.value;
|
|
205
|
+
setInputValue(next);
|
|
206
|
+
setIsOpen(true);
|
|
207
|
+
if (onChange)
|
|
208
|
+
onChange(e);
|
|
209
|
+
};
|
|
210
|
+
const notifySelection = (0, react_1.useCallback)((newRawValues) => {
|
|
211
|
+
// Legacy emit with mapped options
|
|
212
|
+
if (onClick) {
|
|
213
|
+
const mapped = newRawValues.map(toSelectOption);
|
|
214
|
+
onClick(isMultiSelect ? mapped : mapped[0] || null);
|
|
170
215
|
}
|
|
216
|
+
// New typed emit with T
|
|
217
|
+
if (onValueChange) {
|
|
218
|
+
onValueChange(isMultiSelect ? newRawValues : newRawValues[0] ?? null);
|
|
219
|
+
}
|
|
220
|
+
}, [isMultiSelect, onClick, onValueChange, toSelectOption]);
|
|
221
|
+
const clearValue = (e, option) => {
|
|
222
|
+
e.stopPropagation();
|
|
223
|
+
const newRawValues = selectedRawValues.filter((val) => toSelectOption(val).value !== option.value);
|
|
224
|
+
setSelectedRawValues(newRawValues);
|
|
225
|
+
notifySelection(newRawValues);
|
|
171
226
|
};
|
|
172
|
-
const
|
|
173
|
-
if (
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
setIsOpen(true);
|
|
191
|
-
}
|
|
192
|
-
break;
|
|
193
|
-
case 'Escape':
|
|
194
|
-
setIsOpen(false);
|
|
195
|
-
break;
|
|
196
|
-
case 'ArrowUp':
|
|
197
|
-
e.preventDefault();
|
|
198
|
-
if (isOpen) {
|
|
199
|
-
setHighlightedIndex((prev) => (prev > 0 ? prev - 1 : filteredOptions.length - 1));
|
|
200
|
-
}
|
|
201
|
-
else {
|
|
202
|
-
setIsOpen(true);
|
|
203
|
-
}
|
|
204
|
-
break;
|
|
205
|
-
case 'ArrowDown':
|
|
206
|
-
e.preventDefault();
|
|
207
|
-
if (isOpen) {
|
|
208
|
-
setHighlightedIndex((prev) => (prev < filteredOptions.length - 1 ? prev + 1 : 0));
|
|
209
|
-
}
|
|
210
|
-
else {
|
|
211
|
-
setIsOpen(true);
|
|
212
|
-
}
|
|
213
|
-
break;
|
|
227
|
+
const selectOption = (option) => {
|
|
228
|
+
if (isMultiSelect) {
|
|
229
|
+
const newRawValues = [...selectedRawValues];
|
|
230
|
+
const already = newRawValues.some((val) => toSelectOption(val).value === option.value);
|
|
231
|
+
if (!already) {
|
|
232
|
+
// Try to find original raw item by value match; if not found, fallback to option payload
|
|
233
|
+
const raw = (options ?? []).find((item) => toSelectOption(item).value === option.value);
|
|
234
|
+
newRawValues.push(raw ?? option);
|
|
235
|
+
}
|
|
236
|
+
setSelectedRawValues(newRawValues);
|
|
237
|
+
notifySelection(newRawValues);
|
|
238
|
+
}
|
|
239
|
+
else {
|
|
240
|
+
// Try to map back to raw by value
|
|
241
|
+
const raw = (options ?? []).find((item) => toSelectOption(item).value === option.value);
|
|
242
|
+
const nextRaw = raw ?? option;
|
|
243
|
+
setSelectedRawValues([nextRaw]);
|
|
244
|
+
notifySelection([nextRaw]);
|
|
214
245
|
}
|
|
246
|
+
setInputValue('');
|
|
247
|
+
setIsOpen(false);
|
|
248
|
+
setHasSearched(false);
|
|
215
249
|
};
|
|
216
250
|
const handleCreateOption = async () => {
|
|
217
|
-
if (!inputValue.trim() || !isCreatable
|
|
251
|
+
if (!inputValue.trim() || !isCreatable)
|
|
218
252
|
return;
|
|
219
253
|
setInternalLoading(true);
|
|
220
254
|
try {
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
255
|
+
// Prefer typed creator
|
|
256
|
+
let createdRaw;
|
|
257
|
+
if (mapCreateOption) {
|
|
258
|
+
createdRaw = await Promise.resolve(mapCreateOption(inputValue));
|
|
259
|
+
}
|
|
260
|
+
if (!createdRaw) {
|
|
261
|
+
if (onCreateOption) {
|
|
262
|
+
await onCreateOption(inputValue);
|
|
263
|
+
}
|
|
264
|
+
// Fallback: fabricate SelectOption consistent with prior behavior
|
|
265
|
+
const text = String(inputValue);
|
|
266
|
+
const newOption = {
|
|
267
|
+
label: text,
|
|
268
|
+
content: text,
|
|
269
|
+
value: text.replace(/\s+/g, '-'),
|
|
270
|
+
};
|
|
271
|
+
selectOption(newOption);
|
|
272
|
+
}
|
|
273
|
+
else {
|
|
274
|
+
const nextValues = isMultiSelect ? [...selectedRawValues, createdRaw] : [createdRaw];
|
|
275
|
+
setSelectedRawValues(nextValues);
|
|
276
|
+
notifySelection(nextValues);
|
|
277
|
+
}
|
|
230
278
|
setInputValue('');
|
|
231
279
|
setHasSearched(false);
|
|
232
280
|
}
|
|
@@ -245,47 +293,56 @@ onCreateOption, placeholder = 'Select...', isLoading = false, isDisabled = false
|
|
|
245
293
|
}
|
|
246
294
|
}
|
|
247
295
|
};
|
|
248
|
-
const clearValue = (e, option) => {
|
|
249
|
-
e.stopPropagation();
|
|
250
|
-
const newValues = selectedValues.filter((val) => val.value !== option.value);
|
|
251
|
-
setSelectedValues(newValues);
|
|
252
|
-
// Use onClick instead of onChange for selection updates
|
|
253
|
-
if (onClick) {
|
|
254
|
-
onClick(isMultiSelect ? newValues : newValues[0] || null);
|
|
255
|
-
}
|
|
256
|
-
};
|
|
257
|
-
const selectOption = (option) => {
|
|
258
|
-
if (isMultiSelect) {
|
|
259
|
-
const newValues = [...selectedValues];
|
|
260
|
-
if (!newValues.some((val) => val.value === option.value)) {
|
|
261
|
-
newValues.push(option);
|
|
262
|
-
}
|
|
263
|
-
setSelectedValues(newValues);
|
|
264
|
-
// Use onClick instead of onChange for selection updates
|
|
265
|
-
if (onClick) {
|
|
266
|
-
onClick(newValues);
|
|
267
|
-
}
|
|
268
|
-
}
|
|
269
|
-
else {
|
|
270
|
-
setSelectedValues([option]);
|
|
271
|
-
// Use onClick instead of onChange for selection updates
|
|
272
|
-
if (onClick) {
|
|
273
|
-
onClick(option);
|
|
274
|
-
}
|
|
275
|
-
}
|
|
276
|
-
setInputValue('');
|
|
277
|
-
setIsOpen(false);
|
|
278
|
-
setHasSearched(false);
|
|
279
|
-
};
|
|
280
296
|
const showLoading = isLoading || internalLoading;
|
|
281
297
|
const handleAddIconClick = (e) => {
|
|
282
298
|
e.stopPropagation();
|
|
283
|
-
if (onAddClick)
|
|
284
|
-
// Pass the required argument to onAddClick
|
|
299
|
+
if (onAddClick)
|
|
285
300
|
onAddClick(true);
|
|
286
|
-
}
|
|
287
301
|
};
|
|
288
|
-
return (react_1.default.createElement("div", { ref: containerRef, className: `relative w-full ${className} ${styles.container}`, onKeyDown:
|
|
302
|
+
return (react_1.default.createElement("div", { ref: containerRef, className: `relative w-full ${className} ${styles.container}`, onKeyDown: (e) => {
|
|
303
|
+
if (isDisabled)
|
|
304
|
+
return;
|
|
305
|
+
switch (e.key) {
|
|
306
|
+
case 'Enter':
|
|
307
|
+
e.preventDefault();
|
|
308
|
+
if (isOpen) {
|
|
309
|
+
if (filteredOptions.length > 0 && highlightedIndex < filteredOptions.length) {
|
|
310
|
+
selectOption(filteredOptions[highlightedIndex]);
|
|
311
|
+
}
|
|
312
|
+
else if (isCreatable &&
|
|
313
|
+
inputValue &&
|
|
314
|
+
!filteredOptions.some((o) => o.label === inputValue)) {
|
|
315
|
+
handleCreateOption();
|
|
316
|
+
}
|
|
317
|
+
setIsOpen(false);
|
|
318
|
+
}
|
|
319
|
+
else {
|
|
320
|
+
setIsOpen(true);
|
|
321
|
+
}
|
|
322
|
+
break;
|
|
323
|
+
case 'Escape':
|
|
324
|
+
setIsOpen(false);
|
|
325
|
+
break;
|
|
326
|
+
case 'ArrowUp':
|
|
327
|
+
e.preventDefault();
|
|
328
|
+
if (isOpen) {
|
|
329
|
+
setHighlightedIndex((prev) => (prev > 0 ? prev - 1 : filteredOptions.length - 1));
|
|
330
|
+
}
|
|
331
|
+
else {
|
|
332
|
+
setIsOpen(true);
|
|
333
|
+
}
|
|
334
|
+
break;
|
|
335
|
+
case 'ArrowDown':
|
|
336
|
+
e.preventDefault();
|
|
337
|
+
if (isOpen) {
|
|
338
|
+
setHighlightedIndex((prev) => (prev < filteredOptions.length - 1 ? prev + 1 : 0));
|
|
339
|
+
}
|
|
340
|
+
else {
|
|
341
|
+
setIsOpen(true);
|
|
342
|
+
}
|
|
343
|
+
break;
|
|
344
|
+
}
|
|
345
|
+
} },
|
|
289
346
|
label && react_1.default.createElement("label", { className: "block text-sm font-medium text-gray-700 mb-1" }, label),
|
|
290
347
|
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 },
|
|
291
348
|
react_1.default.createElement("div", { className: "flex-grow flex items-center overflow-x-auto no-scrollbar" },
|
|
@@ -315,10 +372,12 @@ onCreateOption, placeholder = 'Select...', isLoading = false, isDisabled = false
|
|
|
315
372
|
filteredOptions.length === 0 &&
|
|
316
373
|
inputValue &&
|
|
317
374
|
hasSearched &&
|
|
318
|
-
renderNoResults(noResultsFound, styles?.noResults, iconSrc.create),
|
|
375
|
+
renderNoResults(noResultsFound, styles?.noResults ?? '', iconSrc.create),
|
|
319
376
|
isCreatable &&
|
|
320
377
|
inputValue &&
|
|
321
|
-
!options
|
|
378
|
+
!(options ?? [])
|
|
379
|
+
.map(toSelectOption)
|
|
380
|
+
.some((o) => (o.label || o.content) === inputValue) && (react_1.default.createElement("div", { className: styles.createOption, onClick: handleCreateOption },
|
|
322
381
|
renderIcon(iconSrc.create, styles.icon),
|
|
323
382
|
react_1.default.createElement("span", null,
|
|
324
383
|
createOptionMessage,
|
|
@@ -39,11 +39,18 @@ export type NoResultsType = ReactNode | string | {
|
|
|
39
39
|
icon?: IconType;
|
|
40
40
|
text: string;
|
|
41
41
|
};
|
|
42
|
-
export type
|
|
43
|
-
|
|
44
|
-
|
|
42
|
+
export type ToOptionMapper<T> = (item: T) => SelectOption;
|
|
43
|
+
export type CreateOptionMapper<T> = (inputValue: string) => Promise<T> | T;
|
|
44
|
+
export type CreatableSelectProps<T = SelectOption> = {
|
|
45
|
+
options: T[] | null;
|
|
46
|
+
value: T | T[] | null;
|
|
45
47
|
onChange?: (e: React.ChangeEvent<HTMLInputElement>) => void;
|
|
46
48
|
onClick?: (value: SelectOption | SelectOption[] | null) => void;
|
|
49
|
+
onValueChange?: (value: T | T[] | null) => void;
|
|
50
|
+
toOption?: ToOptionMapper<T>;
|
|
51
|
+
fetchRawOptions?: (inputValue: string) => Promise<T[]>;
|
|
52
|
+
fetchOptions?: (inputValue: string) => Promise<SelectOption[]>;
|
|
53
|
+
mapCreateOption?: CreateOptionMapper<T>;
|
|
47
54
|
onCreateOption?: (inputValue: string) => Promise<void> | void;
|
|
48
55
|
placeholder?: string;
|
|
49
56
|
isLoading?: boolean;
|
|
@@ -58,7 +65,6 @@ export type CreatableSelectProps = {
|
|
|
58
65
|
tailwindStyle?: TailwindStyleProps;
|
|
59
66
|
label?: string;
|
|
60
67
|
iconSrc: IconSourceProps;
|
|
61
|
-
fetchOptions?: (inputValue: string) => Promise<SelectOption[]>;
|
|
62
68
|
onAddClick: (e: boolean) => void;
|
|
63
69
|
isMultiSelect?: boolean;
|
|
64
70
|
};
|