hi-tm 0.5.6 → 0.5.7
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.
- package/dist/components/DropDowns/CreatableDropDown.d.ts +2 -3
- package/dist/components/DropDowns/CreatableDropDown.js +101 -91
- package/dist/components/Inputs/InputMultiselect.d.ts +1 -17
- package/dist/components/Inputs/InputMultiselect.js +38 -36
- package/dist/types/DropDown/CreatableDropDown.d.ts +16 -6
- package/dist/types/Inputs/InputMultiselectType.d.ts +2 -1
- package/package.json +1 -1
|
@@ -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,71 @@ 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
|
+
// Sync selectedValues with the value prop
|
|
127
|
+
(0, react_1.useEffect)(() => {
|
|
128
|
+
if (isMultiSelect) {
|
|
129
|
+
setSelectedValues(Array.isArray(value) ? value : value ? [value] : []);
|
|
130
|
+
}
|
|
131
|
+
else {
|
|
132
|
+
setSelectedValues(value ? [value] : []);
|
|
133
|
+
}
|
|
134
|
+
}, [value, isMultiSelect]);
|
|
135
|
+
// Reset filtered list when input cleared
|
|
114
136
|
(0, react_1.useEffect)(() => {
|
|
115
137
|
if (inputValue === '') {
|
|
116
|
-
console.log('Input value is empty, resetting filtered options'); // Debugging log
|
|
117
138
|
setFilteredOptions(options ?? []);
|
|
118
139
|
setHasSearched(false);
|
|
119
140
|
}
|
|
120
141
|
}, [options, inputValue]);
|
|
142
|
+
// Close on outside click
|
|
121
143
|
(0, react_1.useEffect)(() => {
|
|
122
144
|
const handler = (e) => {
|
|
123
145
|
if (containerRef.current && !containerRef.current.contains(e.target)) {
|
|
@@ -129,47 +151,37 @@ onCreateOption, placeholder = 'Select...', isLoading = false, isDisabled = false
|
|
|
129
151
|
document.removeEventListener('click', handler);
|
|
130
152
|
};
|
|
131
153
|
}, []);
|
|
132
|
-
const handleInputChange = async (e) => {
|
|
133
|
-
const
|
|
134
|
-
setInputValue(
|
|
154
|
+
const handleInputChange = (0, react_1.useCallback)(async (e) => {
|
|
155
|
+
const next = e.target.value;
|
|
156
|
+
setInputValue(next);
|
|
135
157
|
setIsOpen(true);
|
|
136
|
-
|
|
137
|
-
if (onChange) {
|
|
158
|
+
if (onChange)
|
|
138
159
|
onChange(e);
|
|
139
|
-
|
|
140
|
-
if (fetchOptions && value.trim() !== '') {
|
|
160
|
+
if (fetchOptions && next.trim() !== '') {
|
|
141
161
|
setInternalLoading(true);
|
|
142
162
|
setHasSearched(true);
|
|
143
163
|
try {
|
|
144
|
-
const newOptions = await fetchOptions(
|
|
145
|
-
setFilteredOptions(newOptions);
|
|
164
|
+
const newOptions = await fetchOptions(next);
|
|
165
|
+
setFilteredOptions(newOptions ?? []);
|
|
146
166
|
}
|
|
147
|
-
catch
|
|
148
|
-
console.error('Error fetching options:', error);
|
|
167
|
+
catch {
|
|
149
168
|
}
|
|
150
169
|
finally {
|
|
151
170
|
setInternalLoading(false);
|
|
152
171
|
}
|
|
153
172
|
}
|
|
154
|
-
else if (
|
|
173
|
+
else if (next.trim() === '') {
|
|
155
174
|
setFilteredOptions(options ?? []);
|
|
156
175
|
setHasSearched(false);
|
|
157
176
|
}
|
|
158
177
|
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);
|
|
178
|
+
const searchText = String(next).trim().toLowerCase();
|
|
179
|
+
const filtered = (options ?? []).filter((option) => deriveLabel(option).toLowerCase().includes(searchText));
|
|
168
180
|
setFilteredOptions(filtered);
|
|
169
181
|
setHasSearched(true);
|
|
170
182
|
}
|
|
171
|
-
};
|
|
172
|
-
const handleKeyDown = (e) => {
|
|
183
|
+
}, [onChange, fetchOptions, options, deriveLabel]);
|
|
184
|
+
const handleKeyDown = (0, react_1.useCallback)((e) => {
|
|
173
185
|
if (isDisabled)
|
|
174
186
|
return;
|
|
175
187
|
switch (e.key) {
|
|
@@ -181,7 +193,7 @@ onCreateOption, placeholder = 'Select...', isLoading = false, isDisabled = false
|
|
|
181
193
|
}
|
|
182
194
|
else if (isCreatable &&
|
|
183
195
|
inputValue &&
|
|
184
|
-
!filteredOptions.some((o) => o
|
|
196
|
+
!filteredOptions.some((o) => deriveLabel(o) === inputValue)) {
|
|
185
197
|
handleCreateOption();
|
|
186
198
|
}
|
|
187
199
|
setIsOpen(false);
|
|
@@ -212,87 +224,86 @@ onCreateOption, placeholder = 'Select...', isLoading = false, isDisabled = false
|
|
|
212
224
|
}
|
|
213
225
|
break;
|
|
214
226
|
}
|
|
215
|
-
};
|
|
216
|
-
const
|
|
217
|
-
|
|
227
|
+
}, [isDisabled, isOpen, filteredOptions, highlightedIndex, isCreatable, inputValue, deriveLabel]);
|
|
228
|
+
const emitSelection = (0, react_1.useCallback)((next) => {
|
|
229
|
+
// Backward-compatible callback
|
|
230
|
+
if (onClick) {
|
|
231
|
+
onClick(isMultiSelect ? next ?? [] : next ? next[0] : null);
|
|
232
|
+
}
|
|
233
|
+
if (onValueChange) {
|
|
234
|
+
onValueChange(isMultiSelect ? next ?? [] : next ? next[0] : null);
|
|
235
|
+
}
|
|
236
|
+
}, [onClick, onValueChange, isMultiSelect]);
|
|
237
|
+
const handleCreateOption = (0, react_1.useCallback)(async () => {
|
|
238
|
+
if (!inputValue.trim() || !isCreatable)
|
|
218
239
|
return;
|
|
219
240
|
setInternalLoading(true);
|
|
220
241
|
try {
|
|
221
|
-
await onCreateOption(inputValue);
|
|
222
|
-
// Create value without toLowerCase
|
|
242
|
+
const created = onCreateOption ? await onCreateOption(inputValue) : undefined;
|
|
223
243
|
const safeInputValue = String(inputValue);
|
|
224
|
-
const
|
|
244
|
+
const fallback = {
|
|
225
245
|
label: safeInputValue,
|
|
226
|
-
content: safeInputValue,
|
|
246
|
+
content: safeInputValue,
|
|
227
247
|
value: safeInputValue.replace(/\s+/g, '-'),
|
|
228
248
|
};
|
|
229
|
-
|
|
249
|
+
const optionToAdd = created ?? fallback;
|
|
250
|
+
selectOption(optionToAdd);
|
|
230
251
|
setInputValue('');
|
|
231
252
|
setHasSearched(false);
|
|
232
253
|
}
|
|
233
|
-
catch
|
|
234
|
-
console.error('Error creating option:', error);
|
|
254
|
+
catch {
|
|
235
255
|
}
|
|
236
256
|
finally {
|
|
237
257
|
setInternalLoading(false);
|
|
238
258
|
}
|
|
239
|
-
};
|
|
240
|
-
const toggleDropdown = () => {
|
|
259
|
+
}, [inputValue, isCreatable, onCreateOption]);
|
|
260
|
+
const toggleDropdown = (0, react_1.useCallback)(() => {
|
|
241
261
|
if (!isDisabled) {
|
|
242
262
|
setIsOpen((prev) => !prev);
|
|
243
263
|
if (!isOpen && inputRef.current) {
|
|
244
264
|
inputRef.current.focus();
|
|
245
265
|
}
|
|
246
266
|
}
|
|
247
|
-
};
|
|
248
|
-
const clearValue = (e, option) => {
|
|
267
|
+
}, [isDisabled, isOpen]);
|
|
268
|
+
const clearValue = (0, react_1.useCallback)((e, option) => {
|
|
249
269
|
e.stopPropagation();
|
|
250
|
-
const
|
|
251
|
-
setSelectedValues(
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
}
|
|
256
|
-
};
|
|
257
|
-
const selectOption = (option) => {
|
|
270
|
+
const next = selectedValues.filter((val) => deriveValue(val) !== deriveValue(option));
|
|
271
|
+
setSelectedValues(next);
|
|
272
|
+
emitSelection(next);
|
|
273
|
+
}, [selectedValues, deriveValue, emitSelection]);
|
|
274
|
+
const selectOption = (0, react_1.useCallback)((option) => {
|
|
258
275
|
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);
|
|
276
|
+
const next = [...selectedValues];
|
|
277
|
+
if (!next.some((val) => deriveValue(val) === deriveValue(option))) {
|
|
278
|
+
next.push(option);
|
|
267
279
|
}
|
|
280
|
+
setSelectedValues(next);
|
|
281
|
+
emitSelection(next);
|
|
268
282
|
}
|
|
269
283
|
else {
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
onClick(option);
|
|
274
|
-
}
|
|
284
|
+
const next = [option];
|
|
285
|
+
setSelectedValues(next);
|
|
286
|
+
emitSelection(next);
|
|
275
287
|
}
|
|
276
288
|
setInputValue('');
|
|
277
289
|
setIsOpen(false);
|
|
278
290
|
setHasSearched(false);
|
|
279
|
-
};
|
|
291
|
+
}, [isMultiSelect, selectedValues, deriveValue, emitSelection]);
|
|
280
292
|
const showLoading = isLoading || internalLoading;
|
|
281
|
-
const handleAddIconClick = (e) => {
|
|
293
|
+
const handleAddIconClick = (0, react_1.useCallback)((e) => {
|
|
282
294
|
e.stopPropagation();
|
|
283
295
|
if (onAddClick) {
|
|
284
|
-
// Pass the required argument to onAddClick
|
|
285
296
|
onAddClick(true);
|
|
286
297
|
}
|
|
287
|
-
};
|
|
298
|
+
}, [onAddClick]);
|
|
288
299
|
return (react_1.default.createElement("div", { ref: containerRef, className: `relative w-full ${className} ${styles.container}`, onKeyDown: handleKeyDown },
|
|
289
300
|
label && react_1.default.createElement("label", { className: "block text-sm font-medium text-gray-700 mb-1" }, label),
|
|
290
301
|
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
302
|
react_1.default.createElement("div", { className: "flex-grow flex items-center overflow-x-auto no-scrollbar" },
|
|
292
303
|
react_1.default.createElement("div", { className: "flex items-center gap-2 flex-nowrap" },
|
|
293
304
|
selectedValues.length > 0 &&
|
|
294
|
-
selectedValues.map((val) => (react_1.default.createElement("div", { key: val
|
|
295
|
-
react_1.default.createElement("span", { className: "truncate" }, val
|
|
305
|
+
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" },
|
|
306
|
+
react_1.default.createElement("span", { className: "truncate" }, deriveLabel(val)),
|
|
296
307
|
react_1.default.createElement("button", { onClick: (e) => clearValue(e, val), className: styles.clearButton }, renderIcon(iconSrc.clear, styles.icon))))),
|
|
297
308
|
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
309
|
react_1.default.createElement("div", { className: "flex-shrink-0 flex items-center gap-1 ml-2" },
|
|
@@ -305,24 +316,23 @@ onCreateOption, placeholder = 'Select...', isLoading = false, isDisabled = false
|
|
|
305
316
|
isOpen && (react_1.default.createElement("div", { className: styles.dropdown }, showLoading ? (react_1.default.createElement("div", { className: styles.loadingState },
|
|
306
317
|
renderIcon(iconSrc.loading, `animate-spin mr-2 ${styles.icon}`),
|
|
307
318
|
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
|
|
319
|
+
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) },
|
|
320
|
+
selectedValues.some((val) => deriveValue(val) === deriveValue(option)) &&
|
|
310
321
|
renderIcon(iconSrc.checkmark, 'w-4 h-4 mr-2 text-blue-500'),
|
|
311
|
-
react_1.default.createElement("span", { className: selectedValues.some((val) => val
|
|
322
|
+
react_1.default.createElement("span", { className: selectedValues.some((val) => deriveValue(val) === deriveValue(option))
|
|
312
323
|
? styles.selectedOption
|
|
313
|
-
: '' }, option
|
|
324
|
+
: '' }, deriveLabel(option) || deriveValue(option))))))),
|
|
314
325
|
filteredOptions.length === 0 && !inputValue && (react_1.default.createElement("div", { className: styles.noOptions }, noOptionsMessage)),
|
|
315
326
|
filteredOptions.length === 0 &&
|
|
316
327
|
inputValue &&
|
|
317
328
|
hasSearched &&
|
|
318
|
-
renderNoResults(noResultsFound, styles
|
|
329
|
+
renderNoResults(noResultsFound, styles.noResults ?? '', iconSrc.create),
|
|
319
330
|
isCreatable &&
|
|
320
331
|
inputValue &&
|
|
321
|
-
!options?.some((o) => (o
|
|
332
|
+
!options?.some((o) => deriveLabel(o) === inputValue) && (react_1.default.createElement("div", { className: styles.createOption, onClick: handleCreateOption },
|
|
322
333
|
renderIcon(iconSrc.create, styles.icon),
|
|
323
334
|
react_1.default.createElement("span", null,
|
|
324
335
|
createOptionMessage,
|
|
325
|
-
":
|
|
326
|
-
inputValue
|
|
327
|
-
"\"")))))))));
|
|
336
|
+
": ",
|
|
337
|
+
inputValue)))))))));
|
|
328
338
|
}
|
|
@@ -1,20 +1,4 @@
|
|
|
1
1
|
import React from 'react';
|
|
2
2
|
import { InputMultiselectProps } from '../../types/Inputs/InputMultiselectType';
|
|
3
|
-
|
|
4
|
-
* @param {string} label - The label for the input field.
|
|
5
|
-
* @param {boolean} required - Indicates whether the input is required.
|
|
6
|
-
* @param {string | undefined} textError - The error message to display if validation fails.
|
|
7
|
-
* @param {string} placeholder - The placeholder text for the input field.
|
|
8
|
-
* @param {Function} onChangeHandler - Callback function triggered when the list of items changes.
|
|
9
|
-
* @param {boolean} autoComplete - Specifies whether autocomplete is enabled for the input field.
|
|
10
|
-
* @param {string[]} value - The initial list of selected items. => string[]
|
|
11
|
-
* @param {boolean} autoFocus - Determines whether the input should autofocus on render.
|
|
12
|
-
* @param {Function | undefined} onFocusHandler - Callback function triggered when the input gains focus.
|
|
13
|
-
* @param {Function | undefined} onKeyUpHandler - Callback function triggered when a key is released while the input is focused.
|
|
14
|
-
* @param {Function | undefined} handleKeyDown - Callback function triggered when a key is pressed while the input is focused.
|
|
15
|
-
* @param {Function | undefined} onBlurHandler - Callback function triggered when the input loses focus.
|
|
16
|
-
* @param {string} closeIconSRC - The source URL for the close icon image.
|
|
17
|
-
* @param {string} [width=w-full] - The width of the input container, with a default value of 'w-full'.
|
|
18
|
-
*/
|
|
19
|
-
declare const InputMultiselect: ({ label, required, textError, placeholder, onChangeHandler, autoComplete, value, autoFocus, onFocusHandler, onKeyUpHandler, handleKeyDown: propHandleKeyDown, onBlurHandler, closeIconSRC, style, }: InputMultiselectProps) => React.JSX.Element;
|
|
3
|
+
declare const InputMultiselect: ({ label, required, textError, placeholder, onChangeHandler, autoComplete, value, autoFocus, onFocusHandler, onKeyUpHandler, handleKeyDown: propHandleKeyDown, onBlurHandler, closeIconSRC, inputRef, style, }: InputMultiselectProps) => React.JSX.Element;
|
|
20
4
|
export default InputMultiselect;
|
|
@@ -39,52 +39,48 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
39
39
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
40
40
|
const react_1 = __importStar(require("react"));
|
|
41
41
|
const image_1 = __importDefault(require("next/image"));
|
|
42
|
-
|
|
43
|
-
* @param {string} label - The label for the input field.
|
|
44
|
-
* @param {boolean} required - Indicates whether the input is required.
|
|
45
|
-
* @param {string | undefined} textError - The error message to display if validation fails.
|
|
46
|
-
* @param {string} placeholder - The placeholder text for the input field.
|
|
47
|
-
* @param {Function} onChangeHandler - Callback function triggered when the list of items changes.
|
|
48
|
-
* @param {boolean} autoComplete - Specifies whether autocomplete is enabled for the input field.
|
|
49
|
-
* @param {string[]} value - The initial list of selected items. => string[]
|
|
50
|
-
* @param {boolean} autoFocus - Determines whether the input should autofocus on render.
|
|
51
|
-
* @param {Function | undefined} onFocusHandler - Callback function triggered when the input gains focus.
|
|
52
|
-
* @param {Function | undefined} onKeyUpHandler - Callback function triggered when a key is released while the input is focused.
|
|
53
|
-
* @param {Function | undefined} handleKeyDown - Callback function triggered when a key is pressed while the input is focused.
|
|
54
|
-
* @param {Function | undefined} onBlurHandler - Callback function triggered when the input loses focus.
|
|
55
|
-
* @param {string} closeIconSRC - The source URL for the close icon image.
|
|
56
|
-
* @param {string} [width=w-full] - The width of the input container, with a default value of 'w-full'.
|
|
57
|
-
*/
|
|
58
|
-
const InputMultiselect = ({ label, required, textError, placeholder, onChangeHandler, autoComplete, value, autoFocus, onFocusHandler, onKeyUpHandler, handleKeyDown: propHandleKeyDown, onBlurHandler, closeIconSRC, style = {
|
|
42
|
+
const InputMultiselect = ({ label, required, textError, placeholder, onChangeHandler, autoComplete, value, autoFocus, onFocusHandler, onKeyUpHandler, handleKeyDown: propHandleKeyDown, onBlurHandler, closeIconSRC, inputRef, style = {
|
|
59
43
|
margin: 'mt-[-6px]',
|
|
60
44
|
width: 'w-full',
|
|
61
45
|
}, }) => {
|
|
62
46
|
const [items, setItems] = (0, react_1.useState)([]);
|
|
63
47
|
const [currentValue, setCurrentValue] = (0, react_1.useState)('');
|
|
64
48
|
const [showError, setShowError] = (0, react_1.useState)(false);
|
|
65
|
-
const inputRef = (0, react_1.useRef)(null);
|
|
66
49
|
const isErrorVisible = () => {
|
|
67
|
-
|
|
50
|
+
// Show error if there's a textError OR if there's unconfirmed input
|
|
51
|
+
return (textError && textError.trim() !== '') || showError;
|
|
68
52
|
};
|
|
69
|
-
|
|
70
|
-
if (
|
|
71
|
-
|
|
53
|
+
const getErrorMessage = () => {
|
|
54
|
+
if (textError && textError.trim() !== '') {
|
|
55
|
+
return textError.trim();
|
|
56
|
+
}
|
|
57
|
+
if (showError && currentValue.trim() !== '') {
|
|
58
|
+
return 'برای ثبت بارکد کلید Enter را فشار دهید';
|
|
72
59
|
}
|
|
73
|
-
|
|
60
|
+
return '';
|
|
61
|
+
};
|
|
62
|
+
// Initialize items from value prop only once
|
|
74
63
|
(0, react_1.useEffect)(() => {
|
|
75
|
-
if (value && value.length > 0) {
|
|
64
|
+
if (value && value.length > 0 && items.length === 0) {
|
|
76
65
|
setItems(value);
|
|
77
66
|
}
|
|
78
|
-
}, [value]);
|
|
67
|
+
}, [value, items.length]);
|
|
79
68
|
const handleChange = (e) => {
|
|
80
69
|
setCurrentValue(e.target.value);
|
|
70
|
+
// Hide error when user starts typing
|
|
71
|
+
if (showError) {
|
|
72
|
+
setShowError(false);
|
|
73
|
+
}
|
|
81
74
|
};
|
|
82
75
|
const handleKeyDown = (e) => {
|
|
83
76
|
if (e.key === 'Enter' && currentValue.trim() !== '') {
|
|
84
77
|
e.preventDefault();
|
|
85
|
-
|
|
78
|
+
const newItems = [...items, currentValue.trim()];
|
|
79
|
+
setItems(newItems);
|
|
86
80
|
setCurrentValue('');
|
|
87
81
|
setShowError(false); // Reset error on Enter
|
|
82
|
+
// Call onChangeHandler directly here instead of in useEffect
|
|
83
|
+
onChangeHandler(newItems);
|
|
88
84
|
}
|
|
89
85
|
propHandleKeyDown?.(e);
|
|
90
86
|
};
|
|
@@ -94,23 +90,29 @@ const InputMultiselect = ({ label, required, textError, placeholder, onChangeHan
|
|
|
94
90
|
}
|
|
95
91
|
onBlurHandler?.(e);
|
|
96
92
|
};
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
93
|
+
// Remove the problematic useEffect that was causing the loop
|
|
94
|
+
// useEffect((): void => {
|
|
95
|
+
// onChangeHandler(items);
|
|
96
|
+
// }, [items]);
|
|
97
|
+
const handleRemoveItem = (0, react_1.useCallback)((index) => {
|
|
98
|
+
setItems((prevItems) => {
|
|
99
|
+
const newItems = prevItems.filter((_, i) => i !== index);
|
|
100
|
+
// Call onChangeHandler directly here
|
|
101
|
+
onChangeHandler(newItems);
|
|
102
|
+
return newItems;
|
|
103
|
+
});
|
|
104
|
+
}, [onChangeHandler]);
|
|
103
105
|
return (react_1.default.createElement("div", { className: `${style.width} ${style.margin} relative ` },
|
|
104
106
|
react_1.default.createElement("label", { htmlFor: label, className: "flex items-center gap-2 mx-3" },
|
|
105
107
|
react_1.default.createElement("p", { className: "text-sm text-text-color-primary font-medium cursor-pointer" }, label),
|
|
106
108
|
react_1.default.createElement("span", { className: `text-red-primary font-bold ${required ? 'block' : 'hidden'}` }, "*")),
|
|
107
109
|
react_1.default.createElement("div", { className: `w-full h-10 flex gap-1 items-center justify-between bg-inherit outline-none border rounded-lg text-sm px-2 ${isErrorVisible()
|
|
108
|
-
? 'border-
|
|
109
|
-
: 'border-
|
|
110
|
+
? 'border-red-primary'
|
|
111
|
+
: 'border-gray-tertiary focus:border-gray-quaternary'}` },
|
|
110
112
|
items.map((item, index) => (react_1.default.createElement("span", { key: index, className: "bg-gray-secondary text-text-color-primary px-2 py-1 rounded flex items-center justify-center gap-1" },
|
|
111
113
|
item,
|
|
112
114
|
react_1.default.createElement(image_1.default, { src: closeIconSRC, alt: "close", width: 22, height: 22, onClick: () => handleRemoveItem(index), className: "inline-block cursor-pointer" })))),
|
|
113
|
-
react_1.default.createElement("input", { ref: inputRef, type: "text", id: label, placeholder: items.length === 0 ? placeholder : '', onChange: handleChange, autoComplete: autoComplete ? 'on' : 'off', autoFocus: autoFocus, className: "outline-none bg-inherit flex-grow", required: required, onFocus: onFocusHandler, onKeyUp: onKeyUpHandler, onKeyDown: handleKeyDown, onBlur: handleBlur })),
|
|
114
|
-
|
|
115
|
+
react_1.default.createElement("input", { ref: inputRef, type: "text", value: currentValue, id: label, placeholder: items.length === 0 ? placeholder : '', onChange: handleChange, autoComplete: autoComplete ? 'on' : 'off', autoFocus: autoFocus, className: "outline-none bg-inherit flex-grow", required: required, onFocus: onFocusHandler, onKeyUp: onKeyUpHandler, onKeyDown: handleKeyDown, onBlur: handleBlur })),
|
|
116
|
+
isErrorVisible() && react_1.default.createElement("p", { className: "text-sm mt-1 text-red-primary" }, getErrorMessage())));
|
|
115
117
|
};
|
|
116
118
|
exports.default = InputMultiselect;
|
|
@@ -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
|
};
|
|
@@ -6,12 +6,13 @@ export interface InputMultiselectProps {
|
|
|
6
6
|
autoComplete: boolean;
|
|
7
7
|
autoFocus: boolean;
|
|
8
8
|
value: string[] | null;
|
|
9
|
-
onChangeHandler:
|
|
9
|
+
onChangeHandler: (value: string[]) => void;
|
|
10
10
|
onFocusHandler?: (event: React.FocusEvent) => void;
|
|
11
11
|
onKeyUpHandler?: (event: React.KeyboardEvent<HTMLInputElement>) => void;
|
|
12
12
|
handleKeyDown?: (event: React.KeyboardEvent<HTMLInputElement>) => void;
|
|
13
13
|
onBlurHandler?: (event: React.FocusEvent) => void;
|
|
14
14
|
closeIconSRC: string;
|
|
15
|
+
inputRef?: React.RefObject<HTMLInputElement>;
|
|
15
16
|
style?: {
|
|
16
17
|
width?: string;
|
|
17
18
|
margin?: string;
|