hi-tm 0.9.6 → 0.9.7
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,7 +131,7 @@ function CreatableSelect({ options = [], value, onChange, onClick, onValueChange
|
|
|
131
131
|
const [internalLoading, setInternalLoading] = (0, react_1.useState)(false);
|
|
132
132
|
const [hasSearched, setHasSearched] = (0, react_1.useState)(false);
|
|
133
133
|
const [selectedValues, setSelectedValues] = (0, react_1.useState)([]);
|
|
134
|
-
const [
|
|
134
|
+
const [showAllOptions, setShowAllOptions] = (0, react_1.useState)(false);
|
|
135
135
|
const containerRef = (0, react_1.useRef)(null);
|
|
136
136
|
const inputRef = (0, react_1.useRef)(null);
|
|
137
137
|
// Sync external value (ids or option objects) with internal selectedValues state
|
|
@@ -208,15 +208,41 @@ function CreatableSelect({ options = [], value, onChange, onClick, onValueChange
|
|
|
208
208
|
document.removeEventListener("click", handler);
|
|
209
209
|
};
|
|
210
210
|
}, []);
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
211
|
+
// const handleInputChange = useCallback(
|
|
212
|
+
// async (e: ChangeEvent<HTMLInputElement>) => {
|
|
213
|
+
// const next = e.target.value;
|
|
214
|
+
// setInputValue(next);
|
|
215
|
+
// setIsOpen(true);
|
|
216
|
+
// if (onChange) onChange(e);
|
|
217
|
+
// if (fetchOptions && next.trim() !== "") {
|
|
218
|
+
// setInternalLoading(true);
|
|
219
|
+
// setHasSearched(true);
|
|
220
|
+
// try {
|
|
221
|
+
// const newOptions = await fetchOptions(next);
|
|
222
|
+
// setFilteredOptions(newOptions ?? []);
|
|
223
|
+
// } catch {
|
|
224
|
+
// } finally {
|
|
225
|
+
// setInternalLoading(false);
|
|
226
|
+
// }
|
|
227
|
+
// } else if (next.trim() === "") {
|
|
228
|
+
// setFilteredOptions(options ?? []);
|
|
229
|
+
// setHasSearched(false);
|
|
230
|
+
// } else {
|
|
231
|
+
// const searchText = String(next).trim().toLowerCase();
|
|
232
|
+
// const filtered = (options ?? []).filter((option) =>
|
|
233
|
+
// deriveLabel(option).toLowerCase().includes(searchText)
|
|
234
|
+
// );
|
|
235
|
+
// setFilteredOptions(filtered);
|
|
236
|
+
// setHasSearched(true);
|
|
237
|
+
// }
|
|
238
|
+
// },
|
|
239
|
+
// [onChange, fetchOptions, options, deriveLabel]
|
|
240
|
+
// );
|
|
216
241
|
const handleInputChange = (0, react_1.useCallback)(async (e) => {
|
|
217
242
|
const next = e.target.value;
|
|
218
243
|
setInputValue(next);
|
|
219
244
|
setIsOpen(true);
|
|
245
|
+
setShowAllOptions(false); // ریست کردن حالت نمایش همه
|
|
220
246
|
if (onChange)
|
|
221
247
|
onChange(e);
|
|
222
248
|
if (fetchOptions && next.trim() !== "") {
|
|
@@ -377,15 +403,41 @@ function CreatableSelect({ options = [], value, onChange, onClick, onValueChange
|
|
|
377
403
|
onAddClick(true);
|
|
378
404
|
}
|
|
379
405
|
}, [onAddClick]);
|
|
380
|
-
const handleTriggerClick =
|
|
406
|
+
// const handleTriggerClick = useCallback(
|
|
407
|
+
// (e: React.MouseEvent) => {
|
|
408
|
+
// e.preventDefault();
|
|
409
|
+
// if (!isDisabled) {
|
|
410
|
+
// setIsOpen((prev) => !prev);
|
|
411
|
+
// if (!isOpen && inputRef.current) {
|
|
412
|
+
// inputRef.current.focus({ preventScroll: true });
|
|
413
|
+
// }
|
|
414
|
+
// }
|
|
415
|
+
// },
|
|
416
|
+
// [isDisabled, isOpen]
|
|
417
|
+
// );
|
|
418
|
+
const handleTriggerClick = (0, react_1.useCallback)(async (e) => {
|
|
381
419
|
e.preventDefault();
|
|
382
420
|
if (!isDisabled) {
|
|
383
421
|
setIsOpen((prev) => !prev);
|
|
384
422
|
if (!isOpen && inputRef.current) {
|
|
385
423
|
inputRef.current.focus({ preventScroll: true });
|
|
386
424
|
}
|
|
425
|
+
// اگر اینپوت خالی است و روی آن کلیک شد، لیست کامل را نمایش دهید
|
|
426
|
+
if (inputValue.trim() === "" && fetchOptions) {
|
|
427
|
+
setInternalLoading(true);
|
|
428
|
+
try {
|
|
429
|
+
const allOptions = await fetchOptions("");
|
|
430
|
+
setFilteredOptions(allOptions ?? []);
|
|
431
|
+
setShowAllOptions(true);
|
|
432
|
+
}
|
|
433
|
+
catch {
|
|
434
|
+
}
|
|
435
|
+
finally {
|
|
436
|
+
setInternalLoading(false);
|
|
437
|
+
}
|
|
438
|
+
}
|
|
387
439
|
}
|
|
388
|
-
}, [isDisabled, isOpen]);
|
|
440
|
+
}, [isDisabled, isOpen, inputValue, fetchOptions]);
|
|
389
441
|
return (react_1.default.createElement("div", { ref: containerRef, className: `relative w-full ${className} ${styles.container}`, onKeyDown: handleKeyDown },
|
|
390
442
|
label && (react_1.default.createElement("label", { className: "block text-sm font-medium text-gray-700 mb-1" }, label)),
|
|
391
443
|
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 },
|
|
@@ -395,9 +447,7 @@ function CreatableSelect({ options = [], value, onChange, onClick, onValueChange
|
|
|
395
447
|
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" },
|
|
396
448
|
react_1.default.createElement("span", { className: "truncate" }, deriveLabel(val)),
|
|
397
449
|
react_1.default.createElement("button", { type: "button", onClick: (e) => clearValue(e, val), className: styles.clearButton }, renderIcon(icons.clear, styles.icon))))),
|
|
398
|
-
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()
|
|
399
|
-
setOnFocus(true);
|
|
400
|
-
} }))),
|
|
450
|
+
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() }))),
|
|
401
451
|
react_1.default.createElement("div", { className: "flex-shrink-0 flex items-center gap-1 ml-2" },
|
|
402
452
|
iconSrc.hasAdd && (react_1.default.createElement("button", { type: "button", onClick: handleAddIconClick, className: styles.clearButton }, renderIcon(iconSrc.hasAdd, styles.icon))),
|
|
403
453
|
styles.separator && react_1.default.createElement("div", { className: styles.separator }),
|