hi-tm 0.8.0 → 0.8.3
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,46 @@ 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);
|
|
107
|
+
// Handle primitive types directly
|
|
108
|
+
const optionType = typeof option;
|
|
109
|
+
if (option === null ||
|
|
110
|
+
optionType === 'string' ||
|
|
111
|
+
optionType === 'number' ||
|
|
112
|
+
optionType === 'boolean') {
|
|
113
|
+
return String(option ?? '');
|
|
114
|
+
}
|
|
115
|
+
// Handle objects with common fields
|
|
112
116
|
const anyOption = option;
|
|
113
|
-
const
|
|
114
|
-
|
|
115
|
-
|
|
117
|
+
const labelCandidate = anyOption.label ??
|
|
118
|
+
anyOption.content ??
|
|
119
|
+
anyOption.value ??
|
|
120
|
+
anyOption.name ??
|
|
121
|
+
anyOption.title;
|
|
122
|
+
return String(labelCandidate ?? '');
|
|
116
123
|
}, [getOptionLabel]);
|
|
117
124
|
const deriveValue = (0, react_1.useCallback)((option) => {
|
|
118
125
|
if (getOptionValue)
|
|
119
126
|
return getOptionValue(option);
|
|
127
|
+
// Handle primitive types directly
|
|
128
|
+
const optionType = typeof option;
|
|
129
|
+
if (option === null ||
|
|
130
|
+
optionType === 'string' ||
|
|
131
|
+
optionType === 'number' ||
|
|
132
|
+
optionType === 'boolean') {
|
|
133
|
+
return String(option ?? '');
|
|
134
|
+
}
|
|
135
|
+
// Handle objects with common fields
|
|
120
136
|
const anyOption = option;
|
|
121
|
-
const base = anyOption.value ?? anyOption.label ?? anyOption.content;
|
|
122
|
-
|
|
123
|
-
return str;
|
|
137
|
+
const base = anyOption.value ?? anyOption.label ?? anyOption.content ?? anyOption.id ?? anyOption.key;
|
|
138
|
+
return String(base ?? deriveLabel(option));
|
|
124
139
|
}, [getOptionValue, deriveLabel]);
|
|
125
140
|
const [isOpen, setIsOpen] = (0, react_1.useState)(false);
|
|
126
141
|
const [inputValue, setInputValue] = (0, react_1.useState)('');
|
|
@@ -131,34 +146,29 @@ function CreatableSelect({ options = [], value, onChange, onClick, onValueChange
|
|
|
131
146
|
const [selectedValues, setSelectedValues] = (0, react_1.useState)([]);
|
|
132
147
|
const containerRef = (0, react_1.useRef)(null);
|
|
133
148
|
const inputRef = (0, react_1.useRef)(null);
|
|
134
|
-
// Sync external value prop with internal selectedValues state
|
|
135
149
|
(0, react_1.useEffect)(() => {
|
|
136
150
|
if (value === undefined || value === null) {
|
|
137
151
|
setSelectedValues([]);
|
|
138
152
|
return;
|
|
139
153
|
}
|
|
140
154
|
const externalValues = Array.isArray(value) ? value : [value];
|
|
141
|
-
// Only update if values are actually different
|
|
142
155
|
const currentValuesString = JSON.stringify(selectedValues.map(deriveValue));
|
|
143
156
|
const externalValuesString = JSON.stringify(externalValues.map(deriveValue));
|
|
144
157
|
if (currentValuesString !== externalValuesString) {
|
|
145
158
|
setSelectedValues(externalValues);
|
|
146
159
|
}
|
|
147
160
|
}, [value, deriveValue]);
|
|
148
|
-
// Sync options into filtered list when not searching
|
|
149
161
|
(0, react_1.useEffect)(() => {
|
|
150
162
|
if (!hasSearched) {
|
|
151
163
|
setFilteredOptions(options ?? []);
|
|
152
164
|
}
|
|
153
165
|
}, [options, hasSearched]);
|
|
154
|
-
// Reset filtered list when input cleared
|
|
155
166
|
(0, react_1.useEffect)(() => {
|
|
156
167
|
if (inputValue === '') {
|
|
157
168
|
setFilteredOptions(options ?? []);
|
|
158
169
|
setHasSearched(false);
|
|
159
170
|
}
|
|
160
171
|
}, [options, inputValue]);
|
|
161
|
-
// Close on outside click
|
|
162
172
|
(0, react_1.useEffect)(() => {
|
|
163
173
|
const handler = (e) => {
|
|
164
174
|
if (containerRef.current && !containerRef.current.contains(e.target)) {
|
|
@@ -245,7 +255,6 @@ function CreatableSelect({ options = [], value, onChange, onClick, onValueChange
|
|
|
245
255
|
}
|
|
246
256
|
}, [isDisabled, isOpen, filteredOptions, highlightedIndex, isCreatable, inputValue, deriveLabel]);
|
|
247
257
|
const emitSelection = (0, react_1.useCallback)((next) => {
|
|
248
|
-
// Backward-compatible callback
|
|
249
258
|
if (onClick) {
|
|
250
259
|
onClick(isMultiSelect ? next ?? [] : next ? next[0] : null);
|
|
251
260
|
}
|
|
@@ -301,10 +310,8 @@ function CreatableSelect({ options = [], value, onChange, onClick, onValueChange
|
|
|
301
310
|
}
|
|
302
311
|
setSelectedValues(next);
|
|
303
312
|
emitSelection(next);
|
|
304
|
-
// In multi-select mode, keep the input open and clear input for next search
|
|
305
313
|
setInputValue('');
|
|
306
314
|
setHasSearched(false);
|
|
307
|
-
// Don't close dropdown in multi-select mode
|
|
308
315
|
}
|
|
309
316
|
else {
|
|
310
317
|
const next = [option];
|
|
@@ -323,32 +330,28 @@ function CreatableSelect({ options = [], value, onChange, onClick, onValueChange
|
|
|
323
330
|
onAddClick(true);
|
|
324
331
|
}
|
|
325
332
|
}, [onAddClick]);
|
|
326
|
-
const
|
|
333
|
+
const handleInputAreaClick = (0, react_1.useCallback)((e) => {
|
|
327
334
|
e.preventDefault();
|
|
328
335
|
if (!isDisabled) {
|
|
329
|
-
setIsOpen(
|
|
330
|
-
if (
|
|
336
|
+
setIsOpen(true);
|
|
337
|
+
if (inputRef.current) {
|
|
331
338
|
inputRef.current.focus();
|
|
332
339
|
}
|
|
333
340
|
}
|
|
334
|
-
}, [isDisabled
|
|
341
|
+
}, [isDisabled]);
|
|
335
342
|
return (react_1.default.createElement("div", { ref: containerRef, className: `relative w-full ${className} ${styles.container}`, onKeyDown: handleKeyDown },
|
|
336
343
|
label && react_1.default.createElement("label", { className: "block text-sm font-medium text-gray-700 mb-1" }, label),
|
|
337
344
|
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:
|
|
345
|
+
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
346
|
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() }))),
|
|
347
|
+
react_1.default.createElement("span", { className: "truncate max-w-[100px] text-sm" }, deriveLabel(val)),
|
|
348
|
+
react_1.default.createElement("button", { type: "button", onClick: (e) => clearValue(e, val), className: styles.clearButton }, renderIcon(icons.clear, styles.icon)))))),
|
|
349
|
+
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
350
|
react_1.default.createElement("div", { className: "flex-shrink-0 flex items-center gap-1 ml-2" },
|
|
348
351
|
iconSrc.hasAdd && (react_1.default.createElement("button", { type: "button", onClick: handleAddIconClick, className: styles.clearButton }, renderIcon(iconSrc.hasAdd, styles.icon))),
|
|
349
352
|
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,
|
|
353
|
+
react_1.default.createElement("button", { type: "button", onClick: toggleDropdown, "aria-label": isOpen ? 'Close dropdown' : 'Open dropdown' }, isOpen
|
|
354
|
+
? renderIcon(iconSrc.dropdownOpen, styles.icon)
|
|
352
355
|
: renderIcon(iconSrc.dropdownClosed, styles.icon)))),
|
|
353
356
|
isOpen && (react_1.default.createElement("div", { className: styles.dropdown }, showLoading ? (react_1.default.createElement("div", { className: styles.loadingState },
|
|
354
357
|
renderIcon(iconSrc.loading, `animate-spin mr-2 ${styles.icon}`),
|