hi-tm 0.3.8 → 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,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, 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,50 +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
|
-
function CreatableSelect({ options = [], value, onChange, onClick,
|
|
89
|
-
|
|
90
|
-
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)(() => ({
|
|
91
92
|
...defaultStyles,
|
|
92
93
|
...tailwindStyle,
|
|
93
|
-
};
|
|
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]);
|
|
94
111
|
const [isOpen, setIsOpen] = (0, react_1.useState)(false);
|
|
95
112
|
const [inputValue, setInputValue] = (0, react_1.useState)('');
|
|
96
113
|
const [filteredOptions, setFilteredOptions] = (0, react_1.useState)(options ?? []);
|
|
97
114
|
const [highlightedIndex, setHighlightedIndex] = (0, react_1.useState)(0);
|
|
98
115
|
const [internalLoading, setInternalLoading] = (0, react_1.useState)(false);
|
|
99
116
|
const [hasSearched, setHasSearched] = (0, react_1.useState)(false);
|
|
100
|
-
const [selectedValues, setSelectedValues] = (0, react_1.useState)(Array.isArray(value) && isMultiSelect
|
|
101
|
-
? value
|
|
102
|
-
: value
|
|
103
|
-
? [value]
|
|
104
|
-
: []);
|
|
117
|
+
const [selectedValues, setSelectedValues] = (0, react_1.useState)(Array.isArray(value) && isMultiSelect ? value : value ? [value] : []);
|
|
105
118
|
const containerRef = (0, react_1.useRef)(null);
|
|
106
119
|
const inputRef = (0, react_1.useRef)(null);
|
|
120
|
+
// Sync options into filtered list when not searching
|
|
107
121
|
(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
122
|
if (!hasSearched) {
|
|
111
123
|
setFilteredOptions(options ?? []);
|
|
112
124
|
}
|
|
113
125
|
}, [options, hasSearched]);
|
|
126
|
+
// Reset filtered list when input cleared
|
|
114
127
|
(0, react_1.useEffect)(() => {
|
|
115
128
|
if (inputValue === '') {
|
|
116
|
-
console.log('Input value is empty, resetting filtered options'); // Debugging log
|
|
117
129
|
setFilteredOptions(options ?? []);
|
|
118
130
|
setHasSearched(false);
|
|
119
131
|
}
|
|
120
132
|
}, [options, inputValue]);
|
|
133
|
+
// Close on outside click
|
|
121
134
|
(0, react_1.useEffect)(() => {
|
|
122
135
|
const handler = (e) => {
|
|
123
136
|
if (containerRef.current && !containerRef.current.contains(e.target)) {
|
|
@@ -129,47 +142,37 @@ onCreateOption, placeholder = 'Select...', isLoading = false, isDisabled = false
|
|
|
129
142
|
document.removeEventListener('click', handler);
|
|
130
143
|
};
|
|
131
144
|
}, []);
|
|
132
|
-
const handleInputChange = async (e) => {
|
|
133
|
-
const
|
|
134
|
-
setInputValue(
|
|
145
|
+
const handleInputChange = (0, react_1.useCallback)(async (e) => {
|
|
146
|
+
const next = e.target.value;
|
|
147
|
+
setInputValue(next);
|
|
135
148
|
setIsOpen(true);
|
|
136
|
-
|
|
137
|
-
if (onChange) {
|
|
149
|
+
if (onChange)
|
|
138
150
|
onChange(e);
|
|
139
|
-
|
|
140
|
-
if (fetchOptions && value.trim() !== '') {
|
|
151
|
+
if (fetchOptions && next.trim() !== '') {
|
|
141
152
|
setInternalLoading(true);
|
|
142
153
|
setHasSearched(true);
|
|
143
154
|
try {
|
|
144
|
-
const newOptions = await fetchOptions(
|
|
145
|
-
setFilteredOptions(newOptions);
|
|
155
|
+
const newOptions = await fetchOptions(next);
|
|
156
|
+
setFilteredOptions(newOptions ?? []);
|
|
146
157
|
}
|
|
147
|
-
catch
|
|
148
|
-
console.error('Error fetching options:', error);
|
|
158
|
+
catch {
|
|
149
159
|
}
|
|
150
160
|
finally {
|
|
151
161
|
setInternalLoading(false);
|
|
152
162
|
}
|
|
153
163
|
}
|
|
154
|
-
else if (
|
|
164
|
+
else if (next.trim() === '') {
|
|
155
165
|
setFilteredOptions(options ?? []);
|
|
156
166
|
setHasSearched(false);
|
|
157
167
|
}
|
|
158
168
|
else {
|
|
159
|
-
|
|
160
|
-
const
|
|
161
|
-
const filtered = (options ?? []).filter((option) => {
|
|
162
|
-
// Check for label, content, or value properties
|
|
163
|
-
const optionLabel = String(option.label || option.content || '');
|
|
164
|
-
return optionLabel.toLowerCase().includes(searchText.toLowerCase());
|
|
165
|
-
});
|
|
166
|
-
console.log('Search text:', searchText);
|
|
167
|
-
console.log('Filtered options:', filtered);
|
|
169
|
+
const searchText = String(next).trim().toLowerCase();
|
|
170
|
+
const filtered = (options ?? []).filter((option) => deriveLabel(option).toLowerCase().includes(searchText));
|
|
168
171
|
setFilteredOptions(filtered);
|
|
169
172
|
setHasSearched(true);
|
|
170
173
|
}
|
|
171
|
-
};
|
|
172
|
-
const handleKeyDown = (e) => {
|
|
174
|
+
}, [onChange, fetchOptions, options, deriveLabel]);
|
|
175
|
+
const handleKeyDown = (0, react_1.useCallback)((e) => {
|
|
173
176
|
if (isDisabled)
|
|
174
177
|
return;
|
|
175
178
|
switch (e.key) {
|
|
@@ -181,7 +184,7 @@ onCreateOption, placeholder = 'Select...', isLoading = false, isDisabled = false
|
|
|
181
184
|
}
|
|
182
185
|
else if (isCreatable &&
|
|
183
186
|
inputValue &&
|
|
184
|
-
!filteredOptions.some((o) => o
|
|
187
|
+
!filteredOptions.some((o) => deriveLabel(o) === inputValue)) {
|
|
185
188
|
handleCreateOption();
|
|
186
189
|
}
|
|
187
190
|
setIsOpen(false);
|
|
@@ -212,87 +215,86 @@ onCreateOption, placeholder = 'Select...', isLoading = false, isDisabled = false
|
|
|
212
215
|
}
|
|
213
216
|
break;
|
|
214
217
|
}
|
|
215
|
-
};
|
|
216
|
-
const
|
|
217
|
-
|
|
218
|
+
}, [isDisabled, isOpen, filteredOptions, highlightedIndex, isCreatable, inputValue, deriveLabel]);
|
|
219
|
+
const emitSelection = (0, react_1.useCallback)((next) => {
|
|
220
|
+
// Backward-compatible callback
|
|
221
|
+
if (onClick) {
|
|
222
|
+
onClick(isMultiSelect ? next ?? [] : next ? next[0] : null);
|
|
223
|
+
}
|
|
224
|
+
if (onValueChange) {
|
|
225
|
+
onValueChange(isMultiSelect ? next ?? [] : next ? next[0] : null);
|
|
226
|
+
}
|
|
227
|
+
}, [onClick, onValueChange, isMultiSelect]);
|
|
228
|
+
const handleCreateOption = (0, react_1.useCallback)(async () => {
|
|
229
|
+
if (!inputValue.trim() || !isCreatable)
|
|
218
230
|
return;
|
|
219
231
|
setInternalLoading(true);
|
|
220
232
|
try {
|
|
221
|
-
await onCreateOption(inputValue);
|
|
222
|
-
// Create value without toLowerCase
|
|
233
|
+
const created = onCreateOption ? await onCreateOption(inputValue) : undefined;
|
|
223
234
|
const safeInputValue = String(inputValue);
|
|
224
|
-
const
|
|
235
|
+
const fallback = {
|
|
225
236
|
label: safeInputValue,
|
|
226
|
-
content: safeInputValue,
|
|
237
|
+
content: safeInputValue,
|
|
227
238
|
value: safeInputValue.replace(/\s+/g, '-'),
|
|
228
239
|
};
|
|
229
|
-
|
|
240
|
+
const optionToAdd = created ?? fallback;
|
|
241
|
+
selectOption(optionToAdd);
|
|
230
242
|
setInputValue('');
|
|
231
243
|
setHasSearched(false);
|
|
232
244
|
}
|
|
233
|
-
catch
|
|
234
|
-
console.error('Error creating option:', error);
|
|
245
|
+
catch {
|
|
235
246
|
}
|
|
236
247
|
finally {
|
|
237
248
|
setInternalLoading(false);
|
|
238
249
|
}
|
|
239
|
-
};
|
|
240
|
-
const toggleDropdown = () => {
|
|
250
|
+
}, [inputValue, isCreatable, onCreateOption]);
|
|
251
|
+
const toggleDropdown = (0, react_1.useCallback)(() => {
|
|
241
252
|
if (!isDisabled) {
|
|
242
253
|
setIsOpen((prev) => !prev);
|
|
243
254
|
if (!isOpen && inputRef.current) {
|
|
244
255
|
inputRef.current.focus();
|
|
245
256
|
}
|
|
246
257
|
}
|
|
247
|
-
};
|
|
248
|
-
const clearValue = (e, option) => {
|
|
258
|
+
}, [isDisabled, isOpen]);
|
|
259
|
+
const clearValue = (0, react_1.useCallback)((e, option) => {
|
|
249
260
|
e.stopPropagation();
|
|
250
|
-
const
|
|
251
|
-
setSelectedValues(
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
}
|
|
256
|
-
};
|
|
257
|
-
const selectOption = (option) => {
|
|
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) => {
|
|
258
266
|
if (isMultiSelect) {
|
|
259
|
-
const
|
|
260
|
-
if (!
|
|
261
|
-
|
|
262
|
-
}
|
|
263
|
-
setSelectedValues(newValues);
|
|
264
|
-
// Use onClick instead of onChange for selection updates
|
|
265
|
-
if (onClick) {
|
|
266
|
-
onClick(newValues);
|
|
267
|
+
const next = [...selectedValues];
|
|
268
|
+
if (!next.some((val) => deriveValue(val) === deriveValue(option))) {
|
|
269
|
+
next.push(option);
|
|
267
270
|
}
|
|
271
|
+
setSelectedValues(next);
|
|
272
|
+
emitSelection(next);
|
|
268
273
|
}
|
|
269
274
|
else {
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
onClick(option);
|
|
274
|
-
}
|
|
275
|
+
const next = [option];
|
|
276
|
+
setSelectedValues(next);
|
|
277
|
+
emitSelection(next);
|
|
275
278
|
}
|
|
276
279
|
setInputValue('');
|
|
277
280
|
setIsOpen(false);
|
|
278
281
|
setHasSearched(false);
|
|
279
|
-
};
|
|
282
|
+
}, [isMultiSelect, selectedValues, deriveValue, emitSelection]);
|
|
280
283
|
const showLoading = isLoading || internalLoading;
|
|
281
|
-
const handleAddIconClick = (e) => {
|
|
284
|
+
const handleAddIconClick = (0, react_1.useCallback)((e) => {
|
|
282
285
|
e.stopPropagation();
|
|
283
286
|
if (onAddClick) {
|
|
284
|
-
// Pass the required argument to onAddClick
|
|
285
287
|
onAddClick(true);
|
|
286
288
|
}
|
|
287
|
-
};
|
|
289
|
+
}, [onAddClick]);
|
|
288
290
|
return (react_1.default.createElement("div", { ref: containerRef, className: `relative w-full ${className} ${styles.container}`, onKeyDown: handleKeyDown },
|
|
289
291
|
label && react_1.default.createElement("label", { className: "block text-sm font-medium text-gray-700 mb-1" }, label),
|
|
290
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 },
|
|
291
293
|
react_1.default.createElement("div", { className: "flex-grow flex items-center overflow-x-auto no-scrollbar" },
|
|
292
294
|
react_1.default.createElement("div", { className: "flex items-center gap-2 flex-nowrap" },
|
|
293
295
|
selectedValues.length > 0 &&
|
|
294
|
-
selectedValues.map((val) => (react_1.default.createElement("div", { key: val
|
|
295
|
-
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)),
|
|
296
298
|
react_1.default.createElement("button", { onClick: (e) => clearValue(e, val), className: styles.clearButton }, renderIcon(iconSrc.clear, styles.icon))))),
|
|
297
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() }))),
|
|
298
300
|
react_1.default.createElement("div", { className: "flex-shrink-0 flex items-center gap-1 ml-2" },
|
|
@@ -305,24 +307,23 @@ onCreateOption, placeholder = 'Select...', isLoading = false, isDisabled = false
|
|
|
305
307
|
isOpen && (react_1.default.createElement("div", { className: styles.dropdown }, showLoading ? (react_1.default.createElement("div", { className: styles.loadingState },
|
|
306
308
|
renderIcon(iconSrc.loading, `animate-spin mr-2 ${styles.icon}`),
|
|
307
309
|
loadingMessage)) : (react_1.default.createElement(react_1.default.Fragment, null,
|
|
308
|
-
filteredOptions.length > 0 && (react_1.default.createElement(react_1.default.Fragment, null, filteredOptions.map((option, index) => (react_1.default.createElement("div", { key: option
|
|
309
|
-
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)) &&
|
|
310
312
|
renderIcon(iconSrc.checkmark, 'w-4 h-4 mr-2 text-blue-500'),
|
|
311
|
-
react_1.default.createElement("span", { className: selectedValues.some((val) => val
|
|
313
|
+
react_1.default.createElement("span", { className: selectedValues.some((val) => deriveValue(val) === deriveValue(option))
|
|
312
314
|
? styles.selectedOption
|
|
313
|
-
: '' }, option
|
|
315
|
+
: '' }, deriveLabel(option) || deriveValue(option))))))),
|
|
314
316
|
filteredOptions.length === 0 && !inputValue && (react_1.default.createElement("div", { className: styles.noOptions }, noOptionsMessage)),
|
|
315
317
|
filteredOptions.length === 0 &&
|
|
316
318
|
inputValue &&
|
|
317
319
|
hasSearched &&
|
|
318
|
-
renderNoResults(noResultsFound, styles
|
|
320
|
+
renderNoResults(noResultsFound, styles.noResults ?? '', iconSrc.create),
|
|
319
321
|
isCreatable &&
|
|
320
322
|
inputValue &&
|
|
321
|
-
!options?.some((o) => (o
|
|
323
|
+
!options?.some((o) => deriveLabel(o) === inputValue) && (react_1.default.createElement("div", { className: styles.createOption, onClick: handleCreateOption },
|
|
322
324
|
renderIcon(iconSrc.create, styles.icon),
|
|
323
325
|
react_1.default.createElement("span", null,
|
|
324
326
|
createOptionMessage,
|
|
325
|
-
":
|
|
326
|
-
inputValue
|
|
327
|
-
"\"")))))))));
|
|
327
|
+
": ",
|
|
328
|
+
inputValue)))))))));
|
|
328
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,12 +46,13 @@ export type NoResultsType = ReactNode | string | {
|
|
|
39
46
|
icon?: IconType;
|
|
40
47
|
text: string;
|
|
41
48
|
};
|
|
42
|
-
export type CreatableSelectProps = {
|
|
43
|
-
options:
|
|
44
|
-
value:
|
|
49
|
+
export type CreatableSelectProps<T = SelectOption> = {
|
|
50
|
+
options: T[] | null;
|
|
51
|
+
value: T | T[] | null;
|
|
45
52
|
onChange?: (e: React.ChangeEvent<HTMLInputElement>) => void;
|
|
46
|
-
onClick?: (value:
|
|
47
|
-
|
|
53
|
+
onClick?: (value: T | T[] | null) => void;
|
|
54
|
+
onValueChange?: (value: T | T[] | null) => void;
|
|
55
|
+
onCreateOption?: (inputValue: string) => Promise<T | void> | T | void;
|
|
48
56
|
placeholder?: string;
|
|
49
57
|
isLoading?: boolean;
|
|
50
58
|
isDisabled?: boolean;
|
|
@@ -58,7 +66,9 @@ export type CreatableSelectProps = {
|
|
|
58
66
|
tailwindStyle?: TailwindStyleProps;
|
|
59
67
|
label?: string;
|
|
60
68
|
iconSrc: IconSourceProps;
|
|
61
|
-
fetchOptions?: (inputValue: string) => Promise<
|
|
69
|
+
fetchOptions?: (inputValue: string) => Promise<T[]>;
|
|
62
70
|
onAddClick: (e: boolean) => void;
|
|
63
71
|
isMultiSelect?: boolean;
|
|
72
|
+
getOptionLabel?: OptionLabelGetter<T>;
|
|
73
|
+
getOptionValue?: OptionValueGetter<T>;
|
|
64
74
|
};
|