hi-tm 0.9.0 → 0.9.2
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.
|
@@ -41,7 +41,7 @@ const defaultStyles = {
|
|
|
41
41
|
trigger: 'flex items-center border rounded-md px-3 py-2 bg-white cursor-pointer',
|
|
42
42
|
input: 'w-full outline-none',
|
|
43
43
|
dropdown: 'absolute z-10 w-full mt-1 bg-white border border-gray-300 rounded-md shadow-lg max-h-60 overflow-y-auto',
|
|
44
|
-
option: 'py-2 px-3 cursor-pointer flex items-center',
|
|
44
|
+
option: 'py-2 px-3 cursor-pointer flex items-center gap-2',
|
|
45
45
|
selectedOption: 'font-medium',
|
|
46
46
|
highlightedOption: 'bg-blue-100',
|
|
47
47
|
createOption: 'flex items-center gap-2 py-2 px-3 cursor-pointer hover:bg-blue-50',
|
|
@@ -51,20 +51,33 @@ const defaultStyles = {
|
|
|
51
51
|
clearButton: 'p-1 rounded-full hover:bg-gray-100',
|
|
52
52
|
icon: 'w-4 h-4 text-gray-400',
|
|
53
53
|
separator: 'h-4 border-l mx-2',
|
|
54
|
-
selectedOptionIcon: 'w-4 h-4 mr-2 text-blue-500',
|
|
54
|
+
selectedOptionIcon: 'w-4 h-4 mr-2 text-blue-500 flex-shrink-0',
|
|
55
55
|
};
|
|
56
56
|
// Helper function to render different icon types
|
|
57
57
|
const renderIcon = (icon, className = '') => {
|
|
58
|
+
if (react_1.default.isValidElement(icon)) {
|
|
59
|
+
// Check if the element can accept className
|
|
60
|
+
if ('props' in icon && typeof icon.props === 'object' && icon.props !== null) {
|
|
61
|
+
// Create a new object with existing props plus className
|
|
62
|
+
const props = {
|
|
63
|
+
...icon.props,
|
|
64
|
+
className: `${icon.props.className || ''} ${className}`.trim(),
|
|
65
|
+
};
|
|
66
|
+
// Return a new element with the updated props
|
|
67
|
+
return react_1.default.cloneElement(icon, props);
|
|
68
|
+
}
|
|
69
|
+
// If we can't add className, just return the element as-is
|
|
70
|
+
return icon;
|
|
71
|
+
}
|
|
58
72
|
if (typeof icon === 'string') {
|
|
59
|
-
return react_1.default.createElement("
|
|
73
|
+
return react_1.default.createElement("span", { className: className }, icon);
|
|
60
74
|
}
|
|
61
|
-
|
|
75
|
+
if (typeof icon === 'object' && icon && 'src' in icon) {
|
|
62
76
|
const img = icon;
|
|
63
77
|
return react_1.default.createElement("img", { src: img.src || '/placeholder.svg', alt: img.alt || '', className: className });
|
|
64
78
|
}
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
}
|
|
79
|
+
// For any other case (like JSX.Element or null/undefined)
|
|
80
|
+
return icon;
|
|
68
81
|
};
|
|
69
82
|
// Helper function to render the no results message
|
|
70
83
|
const renderNoResults = (noResults, className, searchIcon) => {
|
|
@@ -76,7 +89,7 @@ const renderNoResults = (noResults, className, searchIcon) => {
|
|
|
76
89
|
if (typeof noResults === 'string') {
|
|
77
90
|
return react_1.default.createElement("div", { className: className }, noResults);
|
|
78
91
|
}
|
|
79
|
-
|
|
92
|
+
if (typeof noResults === 'object' && noResults && 'text' in noResults) {
|
|
80
93
|
const withText = noResults;
|
|
81
94
|
return (react_1.default.createElement("div", { className: className },
|
|
82
95
|
withText.icon
|
|
@@ -84,9 +97,7 @@ const renderNoResults = (noResults, className, searchIcon) => {
|
|
|
84
97
|
: renderIcon(searchIcon, 'w-6 h-6 mb-1'),
|
|
85
98
|
react_1.default.createElement("span", null, withText.text)));
|
|
86
99
|
}
|
|
87
|
-
|
|
88
|
-
return noResults;
|
|
89
|
-
}
|
|
100
|
+
return noResults;
|
|
90
101
|
};
|
|
91
102
|
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, }) {
|
|
92
103
|
// Default icons
|
|
@@ -102,26 +113,21 @@ function CreatableSelect({ options = [], value, onChange, onClick, onValueChange
|
|
|
102
113
|
};
|
|
103
114
|
// Merge provided icons with defaults
|
|
104
115
|
const icons = { ...defaultIcons, ...iconSrc };
|
|
105
|
-
const styles = (0, react_1.useMemo)(() => ({
|
|
106
|
-
...defaultStyles,
|
|
107
|
-
...tailwindStyle,
|
|
108
|
-
}), [tailwindStyle]);
|
|
116
|
+
const styles = (0, react_1.useMemo)(() => ({ ...defaultStyles, ...tailwindStyle }), [tailwindStyle]);
|
|
109
117
|
// Derivers for label/value to support arbitrary option shapes
|
|
110
118
|
const deriveLabel = (0, react_1.useCallback)((option) => {
|
|
111
119
|
if (getOptionLabel)
|
|
112
120
|
return getOptionLabel(option);
|
|
113
121
|
const anyOption = option;
|
|
114
122
|
const label = anyOption.label ?? anyOption.content ?? anyOption.value;
|
|
115
|
-
|
|
116
|
-
return result;
|
|
123
|
+
return String(label ?? '');
|
|
117
124
|
}, [getOptionLabel]);
|
|
118
125
|
const deriveValue = (0, react_1.useCallback)((option) => {
|
|
119
126
|
if (getOptionValue)
|
|
120
127
|
return getOptionValue(option);
|
|
121
128
|
const anyOption = option;
|
|
122
129
|
const base = anyOption.value ?? anyOption.label ?? anyOption.content;
|
|
123
|
-
|
|
124
|
-
return str;
|
|
130
|
+
return String(base ?? deriveLabel(option));
|
|
125
131
|
}, [getOptionValue, deriveLabel]);
|
|
126
132
|
const [isOpen, setIsOpen] = (0, react_1.useState)(false);
|
|
127
133
|
const [inputValue, setInputValue] = (0, react_1.useState)('');
|
|
@@ -132,13 +138,10 @@ function CreatableSelect({ options = [], value, onChange, onClick, onValueChange
|
|
|
132
138
|
const [selectedValues, setSelectedValues] = (0, react_1.useState)([]);
|
|
133
139
|
const containerRef = (0, react_1.useRef)(null);
|
|
134
140
|
const inputRef = (0, react_1.useRef)(null);
|
|
135
|
-
// Sync external value
|
|
141
|
+
// Sync external value with internal state
|
|
136
142
|
(0, react_1.useEffect)(() => {
|
|
137
|
-
|
|
138
|
-
if (value === undefined) {
|
|
143
|
+
if (value === undefined)
|
|
139
144
|
return;
|
|
140
|
-
}
|
|
141
|
-
// Explicit clear from parent
|
|
142
145
|
if (value === null) {
|
|
143
146
|
setSelectedValues([]);
|
|
144
147
|
return;
|
|
@@ -219,6 +222,7 @@ function CreatableSelect({ options = [], value, onChange, onClick, onValueChange
|
|
|
219
222
|
setFilteredOptions(newOptions ?? []);
|
|
220
223
|
}
|
|
221
224
|
catch {
|
|
225
|
+
// Error handling
|
|
222
226
|
}
|
|
223
227
|
finally {
|
|
224
228
|
setInternalLoading(false);
|
|
@@ -280,7 +284,6 @@ function CreatableSelect({ options = [], value, onChange, onClick, onValueChange
|
|
|
280
284
|
}
|
|
281
285
|
}, [isDisabled, isOpen, filteredOptions, highlightedIndex, isCreatable, inputValue, deriveLabel]);
|
|
282
286
|
const emitSelection = (0, react_1.useCallback)((next) => {
|
|
283
|
-
// Backward-compatible callback
|
|
284
287
|
if (onClick) {
|
|
285
288
|
onClick(isMultiSelect ? next ?? [] : next ? next[0] : null);
|
|
286
289
|
}
|
|
@@ -306,6 +309,7 @@ function CreatableSelect({ options = [], value, onChange, onClick, onValueChange
|
|
|
306
309
|
setHasSearched(false);
|
|
307
310
|
}
|
|
308
311
|
catch {
|
|
312
|
+
// Error handling
|
|
309
313
|
}
|
|
310
314
|
finally {
|
|
311
315
|
setInternalLoading(false);
|
|
@@ -336,10 +340,8 @@ function CreatableSelect({ options = [], value, onChange, onClick, onValueChange
|
|
|
336
340
|
}
|
|
337
341
|
setSelectedValues(next);
|
|
338
342
|
emitSelection(next);
|
|
339
|
-
// In multi-select mode, keep the input open and clear input for next search
|
|
340
343
|
setInputValue('');
|
|
341
344
|
setHasSearched(false);
|
|
342
|
-
// Don't close dropdown in multi-select mode
|
|
343
345
|
}
|
|
344
346
|
else {
|
|
345
347
|
const next = [option];
|
|
@@ -376,29 +378,27 @@ function CreatableSelect({ options = [], value, onChange, onClick, onValueChange
|
|
|
376
378
|
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: handleTriggerClick },
|
|
377
379
|
react_1.default.createElement("div", { className: "flex-grow flex items-center overflow-x-auto no-scrollbar" },
|
|
378
380
|
react_1.default.createElement("div", { className: "flex items-center gap-2 flex-nowrap" },
|
|
379
|
-
selectedValues.
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
react_1.default.createElement("button", { type: "button", onClick: (e) => clearValue(e, val), className: styles.clearButton }, renderIcon(icons.clear, styles.icon))))),
|
|
381
|
+
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" },
|
|
382
|
+
react_1.default.createElement("span", { className: "truncate" }, deriveLabel(val)),
|
|
383
|
+
react_1.default.createElement("button", { type: "button", onClick: (e) => clearValue(e, val), className: styles.clearButton }, renderIcon(icons.clear, styles.icon))))),
|
|
383
384
|
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() }))),
|
|
384
385
|
react_1.default.createElement("div", { className: "flex-shrink-0 flex items-center gap-1 ml-2" },
|
|
385
386
|
iconSrc.hasAdd && (react_1.default.createElement("button", { type: "button", onClick: handleAddIconClick, className: styles.clearButton }, renderIcon(iconSrc.hasAdd, styles.icon))),
|
|
386
387
|
styles.separator && react_1.default.createElement("div", { className: styles.separator }),
|
|
387
388
|
react_1.default.createElement("button", { type: "button", onClick: toggleDropdown }, isOpen
|
|
388
|
-
? renderIcon(iconSrc.dropdownOpen,
|
|
389
|
+
? renderIcon(iconSrc.dropdownOpen, styles.icon)
|
|
389
390
|
: renderIcon(iconSrc.dropdownClosed, styles.icon)))),
|
|
390
391
|
isOpen && (react_1.default.createElement("div", { className: styles.dropdown }, showLoading ? (react_1.default.createElement("div", { className: styles.loadingState },
|
|
391
392
|
renderIcon(iconSrc.loading, `animate-spin mr-2 ${styles.icon}`),
|
|
392
393
|
loadingMessage)) : (react_1.default.createElement(react_1.default.Fragment, null,
|
|
393
|
-
filteredOptions.length > 0 && (react_1.default.createElement(react_1.default.Fragment, null, filteredOptions.map((option, index) =>
|
|
394
|
-
isOptionSelected(option)
|
|
395
|
-
|
|
396
|
-
|
|
394
|
+
filteredOptions.length > 0 && (react_1.default.createElement(react_1.default.Fragment, null, filteredOptions.map((option, index) => {
|
|
395
|
+
const isSelected = isOptionSelected(option);
|
|
396
|
+
return (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) },
|
|
397
|
+
isSelected && (react_1.default.createElement("span", { className: styles.selectedOptionIcon }, renderIcon(icons.checkmark, styles.selectedOptionIcon))),
|
|
398
|
+
react_1.default.createElement("span", { className: isSelected ? styles.selectedOption : '' }, deriveLabel(option) || deriveValue(option))));
|
|
399
|
+
}))),
|
|
397
400
|
filteredOptions.length === 0 && !inputValue && (react_1.default.createElement("div", { className: styles.noOptions }, noOptionsMessage)),
|
|
398
|
-
filteredOptions.length === 0 &&
|
|
399
|
-
inputValue &&
|
|
400
|
-
hasSearched &&
|
|
401
|
-
renderNoResults(noResultsFound, styles.noResults ?? '', iconSrc.create),
|
|
401
|
+
filteredOptions.length === 0 && inputValue && hasSearched && (react_1.default.createElement("div", { className: styles.noResults }, renderIcon(iconSrc.create, 'w-6 h-6 mb-1'))),
|
|
402
402
|
isCreatable &&
|
|
403
403
|
inputValue &&
|
|
404
404
|
!options?.some((o) => deriveLabel(o) === inputValue) && (react_1.default.createElement("div", { className: styles.createOption, onClick: handleCreateOption },
|