hi-tm 0.8.0 → 0.8.1
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.
|
@@ -50,9 +50,8 @@ const defaultStyles = {
|
|
|
50
50
|
noResults: 'py-4 px-3 text-gray-500 flex flex-col items-center justify-center gap-2',
|
|
51
51
|
clearButton: 'p-1 rounded-full hover:bg-gray-100',
|
|
52
52
|
icon: 'w-4 h-4 text-gray-400',
|
|
53
|
-
separator: 'h-4 border-l mx-2',
|
|
53
|
+
separator: 'h-4 border-l mx-2',
|
|
54
54
|
};
|
|
55
|
-
// Helper function to render different icon types
|
|
56
55
|
const renderIcon = (icon, className = '') => {
|
|
57
56
|
if (typeof icon === 'string') {
|
|
58
57
|
return react_1.default.createElement("img", { src: icon || '/placeholder.svg', alt: "", className: className });
|
|
@@ -65,7 +64,6 @@ const renderIcon = (icon, className = '') => {
|
|
|
65
64
|
return icon;
|
|
66
65
|
}
|
|
67
66
|
};
|
|
68
|
-
// Helper function to render the no results message
|
|
69
67
|
const renderNoResults = (noResults, className, searchIcon) => {
|
|
70
68
|
if (!noResults) {
|
|
71
69
|
return (react_1.default.createElement("div", { className: className },
|
|
@@ -88,7 +86,6 @@ const renderNoResults = (noResults, className, searchIcon) => {
|
|
|
88
86
|
}
|
|
89
87
|
};
|
|
90
88
|
function CreatableSelect({ options = [], value, onChange, onClick, onValueChange, onCreateOption, placeholder = 'جستوجو', isLoading = false, isDisabled = false, isCreatable = true, isMultiSelect = false, noOptionsMessage = 'مقدار پیشفرضی وجود ندارد', noResultsFound, loadingMessage = 'در حال بارگذاری...', createOptionMessage = 'ایجاد ', className = '', tailwindStyle = {}, iconSrc = {}, fetchOptions, label, onAddClick, getOptionLabel, getOptionValue, }) {
|
|
91
|
-
// Default icons
|
|
92
89
|
const defaultIcons = {
|
|
93
90
|
dropdownOpen: '▼',
|
|
94
91
|
dropdownClosed: '▶',
|
|
@@ -99,28 +96,24 @@ function CreatableSelect({ options = [], value, onChange, onClick, onValueChange
|
|
|
99
96
|
xmark: '✕',
|
|
100
97
|
hasAdd: '➕',
|
|
101
98
|
};
|
|
102
|
-
// Merge provided icons with defaults
|
|
103
99
|
const icons = { ...defaultIcons, ...iconSrc };
|
|
104
100
|
const styles = (0, react_1.useMemo)(() => ({
|
|
105
101
|
...defaultStyles,
|
|
106
102
|
...tailwindStyle,
|
|
107
103
|
}), [tailwindStyle]);
|
|
108
|
-
// Derivers for label/value to support arbitrary option shapes
|
|
109
104
|
const deriveLabel = (0, react_1.useCallback)((option) => {
|
|
110
105
|
if (getOptionLabel)
|
|
111
106
|
return getOptionLabel(option);
|
|
112
107
|
const anyOption = option;
|
|
113
108
|
const label = anyOption.label ?? anyOption.content ?? anyOption.value;
|
|
114
|
-
|
|
115
|
-
return result;
|
|
109
|
+
return String(label ?? '');
|
|
116
110
|
}, [getOptionLabel]);
|
|
117
111
|
const deriveValue = (0, react_1.useCallback)((option) => {
|
|
118
112
|
if (getOptionValue)
|
|
119
113
|
return getOptionValue(option);
|
|
120
114
|
const anyOption = option;
|
|
121
115
|
const base = anyOption.value ?? anyOption.label ?? anyOption.content;
|
|
122
|
-
|
|
123
|
-
return str;
|
|
116
|
+
return String(base ?? deriveLabel(option));
|
|
124
117
|
}, [getOptionValue, deriveLabel]);
|
|
125
118
|
const [isOpen, setIsOpen] = (0, react_1.useState)(false);
|
|
126
119
|
const [inputValue, setInputValue] = (0, react_1.useState)('');
|
|
@@ -131,34 +124,29 @@ function CreatableSelect({ options = [], value, onChange, onClick, onValueChange
|
|
|
131
124
|
const [selectedValues, setSelectedValues] = (0, react_1.useState)([]);
|
|
132
125
|
const containerRef = (0, react_1.useRef)(null);
|
|
133
126
|
const inputRef = (0, react_1.useRef)(null);
|
|
134
|
-
// Sync external value prop with internal selectedValues state
|
|
135
127
|
(0, react_1.useEffect)(() => {
|
|
136
128
|
if (value === undefined || value === null) {
|
|
137
129
|
setSelectedValues([]);
|
|
138
130
|
return;
|
|
139
131
|
}
|
|
140
132
|
const externalValues = Array.isArray(value) ? value : [value];
|
|
141
|
-
// Only update if values are actually different
|
|
142
133
|
const currentValuesString = JSON.stringify(selectedValues.map(deriveValue));
|
|
143
134
|
const externalValuesString = JSON.stringify(externalValues.map(deriveValue));
|
|
144
135
|
if (currentValuesString !== externalValuesString) {
|
|
145
136
|
setSelectedValues(externalValues);
|
|
146
137
|
}
|
|
147
138
|
}, [value, deriveValue]);
|
|
148
|
-
// Sync options into filtered list when not searching
|
|
149
139
|
(0, react_1.useEffect)(() => {
|
|
150
140
|
if (!hasSearched) {
|
|
151
141
|
setFilteredOptions(options ?? []);
|
|
152
142
|
}
|
|
153
143
|
}, [options, hasSearched]);
|
|
154
|
-
// Reset filtered list when input cleared
|
|
155
144
|
(0, react_1.useEffect)(() => {
|
|
156
145
|
if (inputValue === '') {
|
|
157
146
|
setFilteredOptions(options ?? []);
|
|
158
147
|
setHasSearched(false);
|
|
159
148
|
}
|
|
160
149
|
}, [options, inputValue]);
|
|
161
|
-
// Close on outside click
|
|
162
150
|
(0, react_1.useEffect)(() => {
|
|
163
151
|
const handler = (e) => {
|
|
164
152
|
if (containerRef.current && !containerRef.current.contains(e.target)) {
|
|
@@ -245,7 +233,6 @@ function CreatableSelect({ options = [], value, onChange, onClick, onValueChange
|
|
|
245
233
|
}
|
|
246
234
|
}, [isDisabled, isOpen, filteredOptions, highlightedIndex, isCreatable, inputValue, deriveLabel]);
|
|
247
235
|
const emitSelection = (0, react_1.useCallback)((next) => {
|
|
248
|
-
// Backward-compatible callback
|
|
249
236
|
if (onClick) {
|
|
250
237
|
onClick(isMultiSelect ? next ?? [] : next ? next[0] : null);
|
|
251
238
|
}
|
|
@@ -301,10 +288,8 @@ function CreatableSelect({ options = [], value, onChange, onClick, onValueChange
|
|
|
301
288
|
}
|
|
302
289
|
setSelectedValues(next);
|
|
303
290
|
emitSelection(next);
|
|
304
|
-
// In multi-select mode, keep the input open and clear input for next search
|
|
305
291
|
setInputValue('');
|
|
306
292
|
setHasSearched(false);
|
|
307
|
-
// Don't close dropdown in multi-select mode
|
|
308
293
|
}
|
|
309
294
|
else {
|
|
310
295
|
const next = [option];
|
|
@@ -323,32 +308,28 @@ function CreatableSelect({ options = [], value, onChange, onClick, onValueChange
|
|
|
323
308
|
onAddClick(true);
|
|
324
309
|
}
|
|
325
310
|
}, [onAddClick]);
|
|
326
|
-
const
|
|
311
|
+
const handleInputAreaClick = (0, react_1.useCallback)((e) => {
|
|
327
312
|
e.preventDefault();
|
|
328
313
|
if (!isDisabled) {
|
|
329
|
-
setIsOpen(
|
|
330
|
-
if (
|
|
314
|
+
setIsOpen(true);
|
|
315
|
+
if (inputRef.current) {
|
|
331
316
|
inputRef.current.focus();
|
|
332
317
|
}
|
|
333
318
|
}
|
|
334
|
-
}, [isDisabled
|
|
319
|
+
}, [isDisabled]);
|
|
335
320
|
return (react_1.default.createElement("div", { ref: containerRef, className: `relative w-full ${className} ${styles.container}`, onKeyDown: handleKeyDown },
|
|
336
321
|
label && react_1.default.createElement("label", { className: "block text-sm font-medium text-gray-700 mb-1" }, label),
|
|
337
322
|
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'}` },
|
|
338
|
-
react_1.default.createElement("div", { className: "flex-grow flex items-center min-w-0", onClick:
|
|
323
|
+
react_1.default.createElement("div", { className: "flex-grow flex items-center min-w-0", onClick: handleInputAreaClick }, selectedValues.length > 0 ? (react_1.default.createElement("div", { className: "flex items-center gap-2 w-full min-w-0" },
|
|
339
324
|
react_1.default.createElement("div", { className: "flex items-center gap-1 overflow-x-auto no-scrollbar" }, 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" },
|
|
340
|
-
react_1.default.createElement("span", { className: "truncate max-w-[
|
|
341
|
-
react_1.default.createElement("button", { type: "button", onClick: (e) =>
|
|
342
|
-
|
|
343
|
-
e.stopPropagation();
|
|
344
|
-
clearValue(e, val);
|
|
345
|
-
}, className: styles.clearButton }, renderIcon(icons.clear, styles.icon)))))),
|
|
346
|
-
react_1.default.createElement("input", { ref: inputRef, type: "text", className: `${styles.input} ${isDisabled ? 'bg-gray-100' : ''} min-w-[80px] flex-shrink-0`, placeholder: "", value: inputValue, onChange: handleInputChange, disabled: isDisabled, onClick: (e) => e.stopPropagation() }))) : (react_1.default.createElement("input", { ref: inputRef, type: "text", className: `${styles.input} ${isDisabled ? 'bg-gray-100' : ''}`, placeholder: placeholder, value: inputValue, onChange: handleInputChange, disabled: isDisabled, onClick: (e) => e.stopPropagation() }))),
|
|
325
|
+
react_1.default.createElement("span", { className: "truncate max-w-[100px] text-sm" }, deriveLabel(val)),
|
|
326
|
+
react_1.default.createElement("button", { type: "button", onClick: (e) => clearValue(e, val), className: styles.clearButton }, renderIcon(icons.clear, styles.icon)))))),
|
|
327
|
+
react_1.default.createElement("input", { ref: inputRef, type: "text", className: `${styles.input} ${isDisabled ? 'bg-gray-100' : ''} min-w-[60px] flex-shrink-0`, placeholder: "", value: inputValue, onChange: handleInputChange, disabled: isDisabled, onClick: (e) => e.stopPropagation() }))) : (react_1.default.createElement("input", { ref: inputRef, type: "text", className: `${styles.input} ${isDisabled ? 'bg-gray-100' : ''}`, placeholder: placeholder, value: inputValue, onChange: handleInputChange, disabled: isDisabled, onClick: (e) => e.stopPropagation() }))),
|
|
347
328
|
react_1.default.createElement("div", { className: "flex-shrink-0 flex items-center gap-1 ml-2" },
|
|
348
329
|
iconSrc.hasAdd && (react_1.default.createElement("button", { type: "button", onClick: handleAddIconClick, className: styles.clearButton }, renderIcon(iconSrc.hasAdd, styles.icon))),
|
|
349
330
|
styles.separator && react_1.default.createElement("div", { className: styles.separator }),
|
|
350
|
-
react_1.default.createElement("button", { type: "button", onClick: toggleDropdown }, isOpen
|
|
351
|
-
? renderIcon(iconSrc.dropdownOpen,
|
|
331
|
+
react_1.default.createElement("button", { type: "button", onClick: toggleDropdown, "aria-label": isOpen ? 'Close dropdown' : 'Open dropdown' }, isOpen
|
|
332
|
+
? renderIcon(iconSrc.dropdownOpen, styles.icon)
|
|
352
333
|
: renderIcon(iconSrc.dropdownClosed, styles.icon)))),
|
|
353
334
|
isOpen && (react_1.default.createElement("div", { className: styles.dropdown }, showLoading ? (react_1.default.createElement("div", { className: styles.loadingState },
|
|
354
335
|
renderIcon(iconSrc.loading, `animate-spin mr-2 ${styles.icon}`),
|