hi-tm 0.3.9 → 0.4.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.
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
import React from 'react';
|
|
2
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,
|
|
3
|
+
export default function CreatableSelect<T = SelectOption>({ options, value, onChange, onClick, onValueChange, onCreateOption, placeholder, isLoading, isDisabled, isCreatable, isMultiSelect, noOptionsMessage, noResultsFound, loadingMessage, createOptionMessage, className, tailwindStyle, iconSrc, fetchOptions, label, onAddClick, getOptionLabel, getOptionValue, }: CreatableSelectProps<T>): React.JSX.Element;
|
|
@@ -58,7 +58,8 @@ const renderIcon = (icon, className = '') => {
|
|
|
58
58
|
return react_1.default.createElement("img", { src: icon || '/placeholder.svg', alt: "", className: className });
|
|
59
59
|
}
|
|
60
60
|
else if (typeof icon === 'object' && 'src' in icon) {
|
|
61
|
-
|
|
61
|
+
const img = icon;
|
|
62
|
+
return react_1.default.createElement("img", { src: img.src || '/placeholder.svg', alt: img.alt || '', className: className });
|
|
62
63
|
}
|
|
63
64
|
else {
|
|
64
65
|
return icon;
|
|
@@ -74,67 +75,62 @@ const renderNoResults = (noResults, className, searchIcon) => {
|
|
|
74
75
|
if (typeof noResults === 'string') {
|
|
75
76
|
return react_1.default.createElement("div", { className: className }, noResults);
|
|
76
77
|
}
|
|
77
|
-
else if (typeof noResults === 'object' && 'text' in noResults) {
|
|
78
|
+
else if (typeof noResults === 'object' && noResults && 'text' in noResults) {
|
|
79
|
+
const withText = noResults;
|
|
78
80
|
return (react_1.default.createElement("div", { className: className },
|
|
79
|
-
|
|
80
|
-
? renderIcon(
|
|
81
|
+
withText.icon
|
|
82
|
+
? renderIcon(withText.icon, 'w-6 h-6 mb-1')
|
|
81
83
|
: renderIcon(searchIcon, 'w-6 h-6 mb-1'),
|
|
82
|
-
react_1.default.createElement("span", null,
|
|
84
|
+
react_1.default.createElement("span", null, withText.text)));
|
|
83
85
|
}
|
|
84
86
|
else {
|
|
85
87
|
return noResults;
|
|
86
88
|
}
|
|
87
89
|
};
|
|
88
|
-
|
|
89
|
-
|
|
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, }) {
|
|
92
|
-
const styles = {
|
|
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
|
+
const styles = (0, react_1.useMemo)(() => ({
|
|
93
92
|
...defaultStyles,
|
|
94
93
|
...tailwindStyle,
|
|
95
|
-
};
|
|
96
|
-
//
|
|
97
|
-
const
|
|
98
|
-
if (
|
|
99
|
-
return
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
return
|
|
107
|
-
|
|
108
|
-
|
|
94
|
+
}), [tailwindStyle]);
|
|
95
|
+
// Derivers for label/value to support arbitrary option shapes
|
|
96
|
+
const deriveLabel = (0, react_1.useCallback)((option) => {
|
|
97
|
+
if (getOptionLabel)
|
|
98
|
+
return getOptionLabel(option);
|
|
99
|
+
const anyOption = option;
|
|
100
|
+
const label = anyOption.label ?? anyOption.content ?? anyOption.value;
|
|
101
|
+
return String(label ?? '');
|
|
102
|
+
}, [getOptionLabel]);
|
|
103
|
+
const deriveValue = (0, react_1.useCallback)((option) => {
|
|
104
|
+
if (getOptionValue)
|
|
105
|
+
return getOptionValue(option);
|
|
106
|
+
const anyOption = option;
|
|
107
|
+
const base = anyOption.value ?? anyOption.label ?? anyOption.content;
|
|
108
|
+
const str = String(base ?? deriveLabel(option));
|
|
109
|
+
return str;
|
|
110
|
+
}, [getOptionValue, deriveLabel]);
|
|
109
111
|
const [isOpen, setIsOpen] = (0, react_1.useState)(false);
|
|
110
112
|
const [inputValue, setInputValue] = (0, react_1.useState)('');
|
|
111
|
-
const [filteredOptions, setFilteredOptions] = (0, react_1.useState)(
|
|
113
|
+
const [filteredOptions, setFilteredOptions] = (0, react_1.useState)(options ?? []);
|
|
112
114
|
const [highlightedIndex, setHighlightedIndex] = (0, react_1.useState)(0);
|
|
113
115
|
const [internalLoading, setInternalLoading] = (0, react_1.useState)(false);
|
|
114
116
|
const [hasSearched, setHasSearched] = (0, react_1.useState)(false);
|
|
115
|
-
|
|
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]);
|
|
117
|
+
const [selectedValues, setSelectedValues] = (0, react_1.useState)(Array.isArray(value) && isMultiSelect ? value : value ? [value] : []);
|
|
123
118
|
const containerRef = (0, react_1.useRef)(null);
|
|
124
119
|
const inputRef = (0, react_1.useRef)(null);
|
|
125
|
-
//
|
|
120
|
+
// Sync options into filtered list when not searching
|
|
126
121
|
(0, react_1.useEffect)(() => {
|
|
127
122
|
if (!hasSearched) {
|
|
128
|
-
setFilteredOptions(
|
|
123
|
+
setFilteredOptions(options ?? []);
|
|
129
124
|
}
|
|
130
|
-
}, [options, hasSearched
|
|
131
|
-
// Reset
|
|
125
|
+
}, [options, hasSearched]);
|
|
126
|
+
// Reset filtered list when input cleared
|
|
132
127
|
(0, react_1.useEffect)(() => {
|
|
133
128
|
if (inputValue === '') {
|
|
134
|
-
setFilteredOptions(
|
|
129
|
+
setFilteredOptions(options ?? []);
|
|
135
130
|
setHasSearched(false);
|
|
136
131
|
}
|
|
137
|
-
}, [options, inputValue
|
|
132
|
+
}, [options, inputValue]);
|
|
133
|
+
// Close on outside click
|
|
138
134
|
(0, react_1.useEffect)(() => {
|
|
139
135
|
const handler = (e) => {
|
|
140
136
|
if (containerRef.current && !containerRef.current.contains(e.target)) {
|
|
@@ -146,210 +142,159 @@ isCreatable = true, isMultiSelect = false, noOptionsMessage = 'No options availa
|
|
|
146
142
|
document.removeEventListener('click', handler);
|
|
147
143
|
};
|
|
148
144
|
}, []);
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
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;
|
|
170
|
-
}
|
|
171
|
-
if (fetchOptions && query.trim() !== '') {
|
|
145
|
+
const handleInputChange = (0, react_1.useCallback)(async (e) => {
|
|
146
|
+
const next = e.target.value;
|
|
147
|
+
setInputValue(next);
|
|
148
|
+
setIsOpen(true);
|
|
149
|
+
if (onChange)
|
|
150
|
+
onChange(e);
|
|
151
|
+
if (fetchOptions && next.trim() !== '') {
|
|
172
152
|
setInternalLoading(true);
|
|
173
153
|
setHasSearched(true);
|
|
174
154
|
try {
|
|
175
|
-
const newOptions = await fetchOptions(
|
|
155
|
+
const newOptions = await fetchOptions(next);
|
|
176
156
|
setFilteredOptions(newOptions ?? []);
|
|
177
157
|
}
|
|
178
|
-
catch
|
|
179
|
-
console.error('Error fetching options:', error);
|
|
158
|
+
catch {
|
|
180
159
|
}
|
|
181
160
|
finally {
|
|
182
161
|
setInternalLoading(false);
|
|
183
162
|
}
|
|
184
|
-
return;
|
|
185
163
|
}
|
|
186
|
-
if (
|
|
187
|
-
setFilteredOptions(
|
|
164
|
+
else if (next.trim() === '') {
|
|
165
|
+
setFilteredOptions(options ?? []);
|
|
188
166
|
setHasSearched(false);
|
|
167
|
+
}
|
|
168
|
+
else {
|
|
169
|
+
const searchText = String(next).trim().toLowerCase();
|
|
170
|
+
const filtered = (options ?? []).filter((option) => deriveLabel(option).toLowerCase().includes(searchText));
|
|
171
|
+
setFilteredOptions(filtered);
|
|
172
|
+
setHasSearched(true);
|
|
173
|
+
}
|
|
174
|
+
}, [onChange, fetchOptions, options, deriveLabel]);
|
|
175
|
+
const handleKeyDown = (0, react_1.useCallback)((e) => {
|
|
176
|
+
if (isDisabled)
|
|
189
177
|
return;
|
|
178
|
+
switch (e.key) {
|
|
179
|
+
case 'Enter':
|
|
180
|
+
e.preventDefault();
|
|
181
|
+
if (isOpen) {
|
|
182
|
+
if (filteredOptions.length > 0 && highlightedIndex < filteredOptions.length) {
|
|
183
|
+
selectOption(filteredOptions[highlightedIndex]);
|
|
184
|
+
}
|
|
185
|
+
else if (isCreatable &&
|
|
186
|
+
inputValue &&
|
|
187
|
+
!filteredOptions.some((o) => deriveLabel(o) === inputValue)) {
|
|
188
|
+
handleCreateOption();
|
|
189
|
+
}
|
|
190
|
+
setIsOpen(false);
|
|
191
|
+
}
|
|
192
|
+
else {
|
|
193
|
+
setIsOpen(true);
|
|
194
|
+
}
|
|
195
|
+
break;
|
|
196
|
+
case 'Escape':
|
|
197
|
+
setIsOpen(false);
|
|
198
|
+
break;
|
|
199
|
+
case 'ArrowUp':
|
|
200
|
+
e.preventDefault();
|
|
201
|
+
if (isOpen) {
|
|
202
|
+
setHighlightedIndex((prev) => (prev > 0 ? prev - 1 : filteredOptions.length - 1));
|
|
203
|
+
}
|
|
204
|
+
else {
|
|
205
|
+
setIsOpen(true);
|
|
206
|
+
}
|
|
207
|
+
break;
|
|
208
|
+
case 'ArrowDown':
|
|
209
|
+
e.preventDefault();
|
|
210
|
+
if (isOpen) {
|
|
211
|
+
setHighlightedIndex((prev) => (prev < filteredOptions.length - 1 ? prev + 1 : 0));
|
|
212
|
+
}
|
|
213
|
+
else {
|
|
214
|
+
setIsOpen(true);
|
|
215
|
+
}
|
|
216
|
+
break;
|
|
190
217
|
}
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
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
|
|
218
|
+
}, [isDisabled, isOpen, filteredOptions, highlightedIndex, isCreatable, inputValue, deriveLabel]);
|
|
219
|
+
const emitSelection = (0, react_1.useCallback)((next) => {
|
|
220
|
+
// Backward-compatible callback
|
|
212
221
|
if (onClick) {
|
|
213
|
-
|
|
214
|
-
onClick(isMultiSelect ? mapped : mapped[0] || null);
|
|
222
|
+
onClick(isMultiSelect ? next ?? [] : next ? next[0] : null);
|
|
215
223
|
}
|
|
216
|
-
// New typed emit with T
|
|
217
224
|
if (onValueChange) {
|
|
218
|
-
onValueChange(isMultiSelect ?
|
|
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);
|
|
226
|
-
};
|
|
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);
|
|
225
|
+
onValueChange(isMultiSelect ? next ?? [] : next ? next[0] : null);
|
|
238
226
|
}
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
const raw = (options ?? []).find((item) => toSelectOption(item).value === option.value);
|
|
242
|
-
const nextRaw = raw ?? option;
|
|
243
|
-
setSelectedRawValues([nextRaw]);
|
|
244
|
-
notifySelection([nextRaw]);
|
|
245
|
-
}
|
|
246
|
-
setInputValue('');
|
|
247
|
-
setIsOpen(false);
|
|
248
|
-
setHasSearched(false);
|
|
249
|
-
};
|
|
250
|
-
const handleCreateOption = async () => {
|
|
227
|
+
}, [onClick, onValueChange, isMultiSelect]);
|
|
228
|
+
const handleCreateOption = (0, react_1.useCallback)(async () => {
|
|
251
229
|
if (!inputValue.trim() || !isCreatable)
|
|
252
230
|
return;
|
|
253
231
|
setInternalLoading(true);
|
|
254
232
|
try {
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
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
|
-
}
|
|
233
|
+
const created = onCreateOption ? await onCreateOption(inputValue) : undefined;
|
|
234
|
+
const safeInputValue = String(inputValue);
|
|
235
|
+
const fallback = {
|
|
236
|
+
label: safeInputValue,
|
|
237
|
+
content: safeInputValue,
|
|
238
|
+
value: safeInputValue.replace(/\s+/g, '-'),
|
|
239
|
+
};
|
|
240
|
+
const optionToAdd = created ?? fallback;
|
|
241
|
+
selectOption(optionToAdd);
|
|
278
242
|
setInputValue('');
|
|
279
243
|
setHasSearched(false);
|
|
280
244
|
}
|
|
281
|
-
catch
|
|
282
|
-
console.error('Error creating option:', error);
|
|
245
|
+
catch {
|
|
283
246
|
}
|
|
284
247
|
finally {
|
|
285
248
|
setInternalLoading(false);
|
|
286
249
|
}
|
|
287
|
-
};
|
|
288
|
-
const toggleDropdown = () => {
|
|
250
|
+
}, [inputValue, isCreatable, onCreateOption]);
|
|
251
|
+
const toggleDropdown = (0, react_1.useCallback)(() => {
|
|
289
252
|
if (!isDisabled) {
|
|
290
253
|
setIsOpen((prev) => !prev);
|
|
291
254
|
if (!isOpen && inputRef.current) {
|
|
292
255
|
inputRef.current.focus();
|
|
293
256
|
}
|
|
294
257
|
}
|
|
295
|
-
};
|
|
258
|
+
}, [isDisabled, isOpen]);
|
|
259
|
+
const clearValue = (0, react_1.useCallback)((e, option) => {
|
|
260
|
+
e.stopPropagation();
|
|
261
|
+
const next = selectedValues.filter((val) => deriveValue(val) !== deriveValue(option));
|
|
262
|
+
setSelectedValues(next);
|
|
263
|
+
emitSelection(next);
|
|
264
|
+
}, [selectedValues, deriveValue, emitSelection]);
|
|
265
|
+
const selectOption = (0, react_1.useCallback)((option) => {
|
|
266
|
+
if (isMultiSelect) {
|
|
267
|
+
const next = [...selectedValues];
|
|
268
|
+
if (!next.some((val) => deriveValue(val) === deriveValue(option))) {
|
|
269
|
+
next.push(option);
|
|
270
|
+
}
|
|
271
|
+
setSelectedValues(next);
|
|
272
|
+
emitSelection(next);
|
|
273
|
+
}
|
|
274
|
+
else {
|
|
275
|
+
const next = [option];
|
|
276
|
+
setSelectedValues(next);
|
|
277
|
+
emitSelection(next);
|
|
278
|
+
}
|
|
279
|
+
setInputValue('');
|
|
280
|
+
setIsOpen(false);
|
|
281
|
+
setHasSearched(false);
|
|
282
|
+
}, [isMultiSelect, selectedValues, deriveValue, emitSelection]);
|
|
296
283
|
const showLoading = isLoading || internalLoading;
|
|
297
|
-
const handleAddIconClick = (e) => {
|
|
284
|
+
const handleAddIconClick = (0, react_1.useCallback)((e) => {
|
|
298
285
|
e.stopPropagation();
|
|
299
|
-
if (onAddClick)
|
|
286
|
+
if (onAddClick) {
|
|
300
287
|
onAddClick(true);
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
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
|
-
} },
|
|
288
|
+
}
|
|
289
|
+
}, [onAddClick]);
|
|
290
|
+
return (react_1.default.createElement("div", { ref: containerRef, className: `relative w-full ${className} ${styles.container}`, onKeyDown: handleKeyDown },
|
|
346
291
|
label && react_1.default.createElement("label", { className: "block text-sm font-medium text-gray-700 mb-1" }, label),
|
|
347
292
|
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 },
|
|
348
293
|
react_1.default.createElement("div", { className: "flex-grow flex items-center overflow-x-auto no-scrollbar" },
|
|
349
294
|
react_1.default.createElement("div", { className: "flex items-center gap-2 flex-nowrap" },
|
|
350
295
|
selectedValues.length > 0 &&
|
|
351
|
-
selectedValues.map((val) => (react_1.default.createElement("div", { key: val
|
|
352
|
-
react_1.default.createElement("span", { className: "truncate" }, val
|
|
296
|
+
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" },
|
|
297
|
+
react_1.default.createElement("span", { className: "truncate" }, deriveLabel(val)),
|
|
353
298
|
react_1.default.createElement("button", { onClick: (e) => clearValue(e, val), className: styles.clearButton }, renderIcon(iconSrc.clear, styles.icon))))),
|
|
354
299
|
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() }))),
|
|
355
300
|
react_1.default.createElement("div", { className: "flex-shrink-0 flex items-center gap-1 ml-2" },
|
|
@@ -362,26 +307,23 @@ isCreatable = true, isMultiSelect = false, noOptionsMessage = 'No options availa
|
|
|
362
307
|
isOpen && (react_1.default.createElement("div", { className: styles.dropdown }, showLoading ? (react_1.default.createElement("div", { className: styles.loadingState },
|
|
363
308
|
renderIcon(iconSrc.loading, `animate-spin mr-2 ${styles.icon}`),
|
|
364
309
|
loadingMessage)) : (react_1.default.createElement(react_1.default.Fragment, null,
|
|
365
|
-
filteredOptions.length > 0 && (react_1.default.createElement(react_1.default.Fragment, null, filteredOptions.map((option, index) => (react_1.default.createElement("div", { key: option
|
|
366
|
-
selectedValues.some((val) => val
|
|
310
|
+
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) },
|
|
311
|
+
selectedValues.some((val) => deriveValue(val) === deriveValue(option)) &&
|
|
367
312
|
renderIcon(iconSrc.checkmark, 'w-4 h-4 mr-2 text-blue-500'),
|
|
368
|
-
react_1.default.createElement("span", { className: selectedValues.some((val) => val
|
|
313
|
+
react_1.default.createElement("span", { className: selectedValues.some((val) => deriveValue(val) === deriveValue(option))
|
|
369
314
|
? styles.selectedOption
|
|
370
|
-
: '' }, option
|
|
315
|
+
: '' }, deriveLabel(option) || deriveValue(option))))))),
|
|
371
316
|
filteredOptions.length === 0 && !inputValue && (react_1.default.createElement("div", { className: styles.noOptions }, noOptionsMessage)),
|
|
372
317
|
filteredOptions.length === 0 &&
|
|
373
318
|
inputValue &&
|
|
374
319
|
hasSearched &&
|
|
375
|
-
renderNoResults(noResultsFound, styles
|
|
320
|
+
renderNoResults(noResultsFound, styles.noResults ?? '', iconSrc.create),
|
|
376
321
|
isCreatable &&
|
|
377
322
|
inputValue &&
|
|
378
|
-
!(
|
|
379
|
-
.map(toSelectOption)
|
|
380
|
-
.some((o) => (o.label || o.content) === inputValue) && (react_1.default.createElement("div", { className: styles.createOption, onClick: handleCreateOption },
|
|
323
|
+
!options?.some((o) => deriveLabel(o) === inputValue) && (react_1.default.createElement("div", { className: styles.createOption, onClick: handleCreateOption },
|
|
381
324
|
renderIcon(iconSrc.create, styles.icon),
|
|
382
325
|
react_1.default.createElement("span", null,
|
|
383
326
|
createOptionMessage,
|
|
384
|
-
":
|
|
385
|
-
inputValue
|
|
386
|
-
"\"")))))))));
|
|
327
|
+
": ",
|
|
328
|
+
inputValue)))))))));
|
|
387
329
|
}
|
|
@@ -30,6 +30,13 @@ export interface TailwindStyleProps extends React.CSSProperties {
|
|
|
30
30
|
separator?: string;
|
|
31
31
|
spinAnimation?: string;
|
|
32
32
|
}
|
|
33
|
+
export type BaseOption = {
|
|
34
|
+
label?: string;
|
|
35
|
+
value?: string;
|
|
36
|
+
content?: string;
|
|
37
|
+
} & Record<string, unknown>;
|
|
38
|
+
export type OptionLabelGetter<T> = (option: T) => string;
|
|
39
|
+
export type OptionValueGetter<T> = (option: T) => string;
|
|
33
40
|
export type SelectOption = {
|
|
34
41
|
label: string;
|
|
35
42
|
value: string;
|
|
@@ -39,19 +46,13 @@ export type NoResultsType = ReactNode | string | {
|
|
|
39
46
|
icon?: IconType;
|
|
40
47
|
text: string;
|
|
41
48
|
};
|
|
42
|
-
export type ToOptionMapper<T> = (item: T) => SelectOption;
|
|
43
|
-
export type CreateOptionMapper<T> = (inputValue: string) => Promise<T> | T;
|
|
44
49
|
export type CreatableSelectProps<T = SelectOption> = {
|
|
45
50
|
options: T[] | null;
|
|
46
51
|
value: T | T[] | null;
|
|
47
52
|
onChange?: (e: React.ChangeEvent<HTMLInputElement>) => void;
|
|
48
|
-
onClick?: (value:
|
|
53
|
+
onClick?: (value: T | T[] | null) => void;
|
|
49
54
|
onValueChange?: (value: T | T[] | null) => void;
|
|
50
|
-
|
|
51
|
-
fetchRawOptions?: (inputValue: string) => Promise<T[]>;
|
|
52
|
-
fetchOptions?: (inputValue: string) => Promise<SelectOption[]>;
|
|
53
|
-
mapCreateOption?: CreateOptionMapper<T>;
|
|
54
|
-
onCreateOption?: (inputValue: string) => Promise<void> | void;
|
|
55
|
+
onCreateOption?: (inputValue: string) => Promise<T | void> | T | void;
|
|
55
56
|
placeholder?: string;
|
|
56
57
|
isLoading?: boolean;
|
|
57
58
|
isDisabled?: boolean;
|
|
@@ -65,6 +66,9 @@ export type CreatableSelectProps<T = SelectOption> = {
|
|
|
65
66
|
tailwindStyle?: TailwindStyleProps;
|
|
66
67
|
label?: string;
|
|
67
68
|
iconSrc: IconSourceProps;
|
|
69
|
+
fetchOptions?: (inputValue: string) => Promise<T[]>;
|
|
68
70
|
onAddClick: (e: boolean) => void;
|
|
69
71
|
isMultiSelect?: boolean;
|
|
72
|
+
getOptionLabel?: OptionLabelGetter<T>;
|
|
73
|
+
getOptionValue?: OptionValueGetter<T>;
|
|
70
74
|
};
|