hi-tm 0.8.6 → 0.8.8
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.
|
@@ -131,20 +131,49 @@ function CreatableSelect({ options = [], value, onChange, onClick, onValueChange
|
|
|
131
131
|
const [selectedValues, setSelectedValues] = (0, react_1.useState)([]);
|
|
132
132
|
const containerRef = (0, react_1.useRef)(null);
|
|
133
133
|
const inputRef = (0, react_1.useRef)(null);
|
|
134
|
-
// Sync external value
|
|
134
|
+
// Sync external value (ids or option objects) with internal selectedValues state
|
|
135
135
|
(0, react_1.useEffect)(() => {
|
|
136
136
|
if (value === undefined || value === null) {
|
|
137
137
|
setSelectedValues([]);
|
|
138
138
|
return;
|
|
139
139
|
}
|
|
140
|
-
const
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
140
|
+
const external = Array.isArray(value) ? value : [value];
|
|
141
|
+
const desiredKeys = external.map((v) => {
|
|
142
|
+
const t = typeof v;
|
|
143
|
+
if (t === 'string' || t === 'number')
|
|
144
|
+
return String(v);
|
|
145
|
+
return deriveValue(v);
|
|
146
|
+
});
|
|
147
|
+
const availableOptions = options ?? [];
|
|
148
|
+
const keyToOption = new Map(availableOptions.map((opt) => [deriveValue(opt), opt]));
|
|
149
|
+
const orderedMatched = [];
|
|
150
|
+
desiredKeys.forEach((k, idx) => {
|
|
151
|
+
const fromOptions = keyToOption.get(k);
|
|
152
|
+
if (fromOptions) {
|
|
153
|
+
orderedMatched.push(fromOptions);
|
|
154
|
+
}
|
|
155
|
+
else {
|
|
156
|
+
const original = external[idx];
|
|
157
|
+
if (typeof original === 'object' && original !== null) {
|
|
158
|
+
orderedMatched.push(original);
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
});
|
|
162
|
+
// Deduplicate while preserving order
|
|
163
|
+
const seen = new Set();
|
|
164
|
+
const uniqueOrdered = orderedMatched.filter((opt) => {
|
|
165
|
+
const k = deriveValue(opt);
|
|
166
|
+
if (seen.has(k))
|
|
167
|
+
return false;
|
|
168
|
+
seen.add(k);
|
|
169
|
+
return true;
|
|
170
|
+
});
|
|
171
|
+
const currentKeys = selectedValues.map(deriveValue);
|
|
172
|
+
const nextKeys = uniqueOrdered.map(deriveValue);
|
|
173
|
+
if (JSON.stringify(currentKeys) !== JSON.stringify(nextKeys)) {
|
|
174
|
+
setSelectedValues(uniqueOrdered);
|
|
146
175
|
}
|
|
147
|
-
}, [value, deriveValue]);
|
|
176
|
+
}, [value, options, deriveValue]);
|
|
148
177
|
// Sync options into filtered list when not searching
|
|
149
178
|
(0, react_1.useEffect)(() => {
|
|
150
179
|
if (!hasSearched) {
|