hi-tm 0.8.1 → 0.8.4
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.
|
@@ -104,15 +104,38 @@ function CreatableSelect({ options = [], value, onChange, onClick, onValueChange
|
|
|
104
104
|
const deriveLabel = (0, react_1.useCallback)((option) => {
|
|
105
105
|
if (getOptionLabel)
|
|
106
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
|
|
107
116
|
const anyOption = option;
|
|
108
|
-
const
|
|
109
|
-
|
|
117
|
+
const labelCandidate = anyOption.label ??
|
|
118
|
+
anyOption.content ??
|
|
119
|
+
anyOption.value ??
|
|
120
|
+
anyOption.name ??
|
|
121
|
+
anyOption.title ??
|
|
122
|
+
anyOption.text;
|
|
123
|
+
return String(labelCandidate ?? '');
|
|
110
124
|
}, [getOptionLabel]);
|
|
111
125
|
const deriveValue = (0, react_1.useCallback)((option) => {
|
|
112
126
|
if (getOptionValue)
|
|
113
127
|
return getOptionValue(option);
|
|
128
|
+
// Handle primitive types directly
|
|
129
|
+
const optionType = typeof option;
|
|
130
|
+
if (option === null ||
|
|
131
|
+
optionType === 'string' ||
|
|
132
|
+
optionType === 'number' ||
|
|
133
|
+
optionType === 'boolean') {
|
|
134
|
+
return String(option ?? '');
|
|
135
|
+
}
|
|
136
|
+
// Handle objects with common fields
|
|
114
137
|
const anyOption = option;
|
|
115
|
-
const base = anyOption.value ?? anyOption.label ?? anyOption.content;
|
|
138
|
+
const base = anyOption.value ?? anyOption.label ?? anyOption.content ?? anyOption.id ?? anyOption.key;
|
|
116
139
|
return String(base ?? deriveLabel(option));
|
|
117
140
|
}, [getOptionValue, deriveLabel]);
|
|
118
141
|
const [isOpen, setIsOpen] = (0, react_1.useState)(false);
|
|
@@ -127,6 +150,9 @@ function CreatableSelect({ options = [], value, onChange, onClick, onValueChange
|
|
|
127
150
|
(0, react_1.useEffect)(() => {
|
|
128
151
|
if (value === undefined || value === null) {
|
|
129
152
|
setSelectedValues([]);
|
|
153
|
+
if (!isMultiSelect) {
|
|
154
|
+
setInputValue('');
|
|
155
|
+
}
|
|
130
156
|
return;
|
|
131
157
|
}
|
|
132
158
|
const externalValues = Array.isArray(value) ? value : [value];
|
|
@@ -134,8 +160,12 @@ function CreatableSelect({ options = [], value, onChange, onClick, onValueChange
|
|
|
134
160
|
const externalValuesString = JSON.stringify(externalValues.map(deriveValue));
|
|
135
161
|
if (currentValuesString !== externalValuesString) {
|
|
136
162
|
setSelectedValues(externalValues);
|
|
163
|
+
if (!isMultiSelect) {
|
|
164
|
+
const labelFromValue = externalValues.length > 0 ? deriveLabel(externalValues[0]) : '';
|
|
165
|
+
setInputValue(String(labelFromValue ?? ''));
|
|
166
|
+
}
|
|
137
167
|
}
|
|
138
|
-
}, [value, deriveValue]);
|
|
168
|
+
}, [value, deriveValue, deriveLabel, isMultiSelect]);
|
|
139
169
|
(0, react_1.useEffect)(() => {
|
|
140
170
|
if (!hasSearched) {
|
|
141
171
|
setFilteredOptions(options ?? []);
|
|
@@ -295,7 +325,7 @@ function CreatableSelect({ options = [], value, onChange, onClick, onValueChange
|
|
|
295
325
|
const next = [option];
|
|
296
326
|
setSelectedValues(next);
|
|
297
327
|
emitSelection(next);
|
|
298
|
-
setInputValue(
|
|
328
|
+
setInputValue(deriveLabel(option));
|
|
299
329
|
setIsOpen(false);
|
|
300
330
|
setHasSearched(false);
|
|
301
331
|
}
|
|
@@ -320,7 +350,7 @@ function CreatableSelect({ options = [], value, onChange, onClick, onValueChange
|
|
|
320
350
|
return (react_1.default.createElement("div", { ref: containerRef, className: `relative w-full ${className} ${styles.container}`, onKeyDown: handleKeyDown },
|
|
321
351
|
label && react_1.default.createElement("label", { className: "block text-sm font-medium text-gray-700 mb-1" }, label),
|
|
322
352
|
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'}` },
|
|
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" },
|
|
353
|
+
react_1.default.createElement("div", { className: "flex-grow flex items-center min-w-0", onClick: handleInputAreaClick }, isMultiSelect && selectedValues.length > 0 ? (react_1.default.createElement("div", { className: "flex items-center gap-2 w-full min-w-0" },
|
|
324
354
|
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" },
|
|
325
355
|
react_1.default.createElement("span", { className: "truncate max-w-[100px] text-sm" }, deriveLabel(val)),
|
|
326
356
|
react_1.default.createElement("button", { type: "button", onClick: (e) => clearValue(e, val), className: styles.clearButton }, renderIcon(icons.clear, styles.icon)))))),
|