hi-tm 0.6.8 → 0.7.0
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.
|
@@ -1,3 +1,4 @@
|
|
|
1
1
|
import React from 'react';
|
|
2
2
|
import type { SelectOption, CreatableSelectProps } from '../../types/DropDown/CreatableDropDown';
|
|
3
|
-
|
|
3
|
+
declare const _default: React.MemoExoticComponent<(<T = SelectOption>({ options, value, onChange, onClick, onValueChange, onCreateOption, placeholder, isLoading, isDisabled, isCreatable, isMultiSelect, noOptionsMessage, noResultsFound, loadingMessage, createOptionMessage, className, tailwindStyle, iconSrc, fetchOptions, label, onAddClick, getOptionLabel, getOptionValue, }: CreatableSelectProps<T>) => React.JSX.Element)>;
|
|
4
|
+
export default _default;
|
|
@@ -33,7 +33,6 @@ var __importStar = (this && this.__importStar) || (function () {
|
|
|
33
33
|
};
|
|
34
34
|
})();
|
|
35
35
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
|
-
exports.default = CreatableSelect;
|
|
37
36
|
const react_1 = __importStar(require("react"));
|
|
38
37
|
// Default Tailwind styles
|
|
39
38
|
const defaultStyles = {
|
|
@@ -87,7 +86,7 @@ const renderNoResults = (noResults, className, searchIcon) => {
|
|
|
87
86
|
return noResults;
|
|
88
87
|
}
|
|
89
88
|
};
|
|
90
|
-
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, }) {
|
|
89
|
+
exports.default = react_1.default.memo(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
90
|
// Default icons
|
|
92
91
|
const defaultIcons = {
|
|
93
92
|
dropdownOpen: '▼',
|
|
@@ -136,25 +135,11 @@ function CreatableSelect({ options = [], value, onChange, onClick, onValueChange
|
|
|
136
135
|
const containerRef = (0, react_1.useRef)(null);
|
|
137
136
|
const inputRef = (0, react_1.useRef)(null);
|
|
138
137
|
const scrollContainerRef = (0, react_1.useRef)(null);
|
|
139
|
-
const scrollPositionRef = (0, react_1.useRef)(0);
|
|
140
|
-
// Save scroll position before re-renders
|
|
141
|
-
const saveScrollPosition = (0, react_1.useCallback)(() => {
|
|
142
|
-
if (scrollContainerRef.current) {
|
|
143
|
-
scrollPositionRef.current = scrollContainerRef.current.scrollLeft;
|
|
144
|
-
}
|
|
145
|
-
}, []);
|
|
146
|
-
// Restore scroll position after re-renders
|
|
147
|
-
const restoreScrollPosition = (0, react_1.useCallback)(() => {
|
|
148
|
-
if (scrollContainerRef.current && scrollPositionRef.current > 0) {
|
|
149
|
-
// Only restore if the position has actually changed
|
|
150
|
-
if (scrollContainerRef.current.scrollLeft !== scrollPositionRef.current) {
|
|
151
|
-
scrollContainerRef.current.scrollLeft = scrollPositionRef.current;
|
|
152
|
-
}
|
|
153
|
-
}
|
|
154
|
-
}, []);
|
|
155
138
|
// Prevent scroll jumping by maintaining position during keyboard navigation
|
|
156
139
|
const handleKeyboardNavigation = (0, react_1.useCallback)((direction) => {
|
|
157
|
-
|
|
140
|
+
// Set user interaction flag to prevent external sync
|
|
141
|
+
setIsUserInteracting(true);
|
|
142
|
+
// Don't save scroll position - just update the highlighted index
|
|
158
143
|
if (direction === 'up') {
|
|
159
144
|
highlightedIndexRef.current = Math.max(0, highlightedIndexRef.current - 1);
|
|
160
145
|
}
|
|
@@ -163,11 +148,11 @@ function CreatableSelect({ options = [], value, onChange, onClick, onValueChange
|
|
|
163
148
|
}
|
|
164
149
|
// Force re-render of dropdown options only
|
|
165
150
|
setForceUpdate((prev) => prev + 1);
|
|
166
|
-
//
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
});
|
|
170
|
-
}, [filteredOptions.length
|
|
151
|
+
// Reset the flag after a delay
|
|
152
|
+
setTimeout(() => {
|
|
153
|
+
setIsUserInteracting(false);
|
|
154
|
+
}, 1000);
|
|
155
|
+
}, [filteredOptions.length]);
|
|
171
156
|
// Sync external value prop with internal selectedValues state
|
|
172
157
|
(0, react_1.useEffect)(() => {
|
|
173
158
|
// Don't sync if user is actively interacting with the component
|
|
@@ -209,20 +194,13 @@ function CreatableSelect({ options = [], value, onChange, onClick, onValueChange
|
|
|
209
194
|
setSelectedValues(singleItem);
|
|
210
195
|
}
|
|
211
196
|
}
|
|
212
|
-
}, [value, isMultiSelect,
|
|
197
|
+
}, [value, isMultiSelect, isUserInteracting]);
|
|
213
198
|
// Sync options into filtered list when not searching
|
|
214
199
|
(0, react_1.useEffect)(() => {
|
|
215
200
|
if (!hasSearched) {
|
|
216
201
|
setFilteredOptions(options ?? []);
|
|
217
202
|
}
|
|
218
203
|
}, [options, hasSearched]);
|
|
219
|
-
// Restore scroll position after filteredOptions changes
|
|
220
|
-
(0, react_1.useEffect)(() => {
|
|
221
|
-
// Use requestAnimationFrame to ensure DOM has updated
|
|
222
|
-
requestAnimationFrame(() => {
|
|
223
|
-
restoreScrollPosition();
|
|
224
|
-
});
|
|
225
|
-
}, [filteredOptions, restoreScrollPosition]);
|
|
226
204
|
// Reset filtered list when input cleared
|
|
227
205
|
(0, react_1.useEffect)(() => {
|
|
228
206
|
if (inputValue === '') {
|
|
@@ -276,6 +254,8 @@ function CreatableSelect({ options = [], value, onChange, onClick, onValueChange
|
|
|
276
254
|
const handleKeyDown = (0, react_1.useCallback)((e) => {
|
|
277
255
|
if (isDisabled)
|
|
278
256
|
return;
|
|
257
|
+
// Set user interaction flag for all keyboard events
|
|
258
|
+
setIsUserInteracting(true);
|
|
279
259
|
switch (e.key) {
|
|
280
260
|
case 'Enter':
|
|
281
261
|
e.preventDefault();
|
|
@@ -317,6 +297,10 @@ function CreatableSelect({ options = [], value, onChange, onClick, onValueChange
|
|
|
317
297
|
}
|
|
318
298
|
break;
|
|
319
299
|
}
|
|
300
|
+
// Reset the flag after a delay
|
|
301
|
+
setTimeout(() => {
|
|
302
|
+
setIsUserInteracting(false);
|
|
303
|
+
}, 1000);
|
|
320
304
|
}, [
|
|
321
305
|
isDisabled,
|
|
322
306
|
isOpen,
|
|
@@ -360,10 +344,15 @@ function CreatableSelect({ options = [], value, onChange, onClick, onValueChange
|
|
|
360
344
|
}, [inputValue, isCreatable, onCreateOption]);
|
|
361
345
|
const toggleDropdown = (0, react_1.useCallback)(() => {
|
|
362
346
|
if (!isDisabled) {
|
|
347
|
+
setIsUserInteracting(true);
|
|
363
348
|
setIsOpen((prev) => !prev);
|
|
364
349
|
if (!isOpen && inputRef.current) {
|
|
365
350
|
inputRef.current.focus();
|
|
366
351
|
}
|
|
352
|
+
// Reset the flag after a delay
|
|
353
|
+
setTimeout(() => {
|
|
354
|
+
setIsUserInteracting(false);
|
|
355
|
+
}, 1000);
|
|
367
356
|
}
|
|
368
357
|
}, [isDisabled, isOpen]);
|
|
369
358
|
const clearValue = (0, react_1.useCallback)((e, option) => {
|
|
@@ -373,6 +362,8 @@ function CreatableSelect({ options = [], value, onChange, onClick, onValueChange
|
|
|
373
362
|
emitSelection(next);
|
|
374
363
|
}, [selectedValues, deriveValue, emitSelection]);
|
|
375
364
|
const selectOption = (0, react_1.useCallback)((option) => {
|
|
365
|
+
// Set user interaction flag to prevent external sync
|
|
366
|
+
setIsUserInteracting(true);
|
|
376
367
|
if (isMultiSelect) {
|
|
377
368
|
const next = [...selectedValues];
|
|
378
369
|
if (!next.some((val) => deriveValue(val) === deriveValue(option))) {
|
|
@@ -393,6 +384,10 @@ function CreatableSelect({ options = [], value, onChange, onClick, onValueChange
|
|
|
393
384
|
setIsOpen(false);
|
|
394
385
|
setHasSearched(false);
|
|
395
386
|
}
|
|
387
|
+
// Reset the flag after a delay
|
|
388
|
+
setTimeout(() => {
|
|
389
|
+
setIsUserInteracting(false);
|
|
390
|
+
}, 1000);
|
|
396
391
|
}, [isMultiSelect, selectedValues, deriveValue, emitSelection]);
|
|
397
392
|
const showLoading = isLoading || internalLoading;
|
|
398
393
|
const handleAddIconClick = (0, react_1.useCallback)((e) => {
|
|
@@ -401,8 +396,14 @@ function CreatableSelect({ options = [], value, onChange, onClick, onValueChange
|
|
|
401
396
|
onAddClick(true);
|
|
402
397
|
}
|
|
403
398
|
}, [onAddClick]);
|
|
404
|
-
const scrollContainer = (0, react_1.useMemo)(() => (react_1.default.createElement("div", { ref: scrollContainerRef, className: "flex-grow flex items-center overflow-x-auto creatable-dropdown-scroll", key: "scroll-container-stable"
|
|
405
|
-
|
|
399
|
+
const scrollContainer = (0, react_1.useMemo)(() => (react_1.default.createElement("div", { ref: scrollContainerRef, className: "flex-grow flex items-center overflow-x-auto creatable-dropdown-scroll", key: "scroll-container-stable", style: {
|
|
400
|
+
scrollBehavior: 'auto',
|
|
401
|
+
scrollbarWidth: 'none',
|
|
402
|
+
msOverflowStyle: 'none',
|
|
403
|
+
minWidth: 0, // Prevent flex item from growing beyond container
|
|
404
|
+
flexShrink: 1, // Allow shrinking
|
|
405
|
+
} },
|
|
406
|
+
react_1.default.createElement("div", { className: "flex items-center gap-2 flex-nowrap min-w-0" },
|
|
406
407
|
selectedValues.length > 0 &&
|
|
407
408
|
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" },
|
|
408
409
|
react_1.default.createElement("span", { className: "truncate" }, deriveLabel(val)),
|
|
@@ -455,4 +456,26 @@ function CreatableSelect({ options = [], value, onChange, onClick, onValueChange
|
|
|
455
456
|
createOptionMessage,
|
|
456
457
|
": ",
|
|
457
458
|
inputValue)))))))));
|
|
458
|
-
}
|
|
459
|
+
}, (prevProps, nextProps) => {
|
|
460
|
+
// Custom comparison function for React.memo
|
|
461
|
+
// Only re-render if the props that affect the component's output have changed.
|
|
462
|
+
// This prevents unnecessary re-renders when options, value, or other props are shallowly equal.
|
|
463
|
+
return (prevProps.options === nextProps.options &&
|
|
464
|
+
prevProps.value === nextProps.value &&
|
|
465
|
+
prevProps.isLoading === nextProps.isLoading &&
|
|
466
|
+
prevProps.isDisabled === nextProps.isDisabled &&
|
|
467
|
+
prevProps.isCreatable === nextProps.isCreatable &&
|
|
468
|
+
prevProps.isMultiSelect === nextProps.isMultiSelect &&
|
|
469
|
+
prevProps.noOptionsMessage === nextProps.noOptionsMessage &&
|
|
470
|
+
prevProps.noResultsFound === nextProps.noResultsFound &&
|
|
471
|
+
prevProps.loadingMessage === nextProps.loadingMessage &&
|
|
472
|
+
prevProps.createOptionMessage === nextProps.createOptionMessage &&
|
|
473
|
+
prevProps.className === nextProps.className &&
|
|
474
|
+
prevProps.tailwindStyle === nextProps.tailwindStyle &&
|
|
475
|
+
prevProps.iconSrc === nextProps.iconSrc &&
|
|
476
|
+
prevProps.fetchOptions === nextProps.fetchOptions &&
|
|
477
|
+
prevProps.label === nextProps.label &&
|
|
478
|
+
prevProps.onAddClick === nextProps.onAddClick &&
|
|
479
|
+
prevProps.getOptionLabel === nextProps.getOptionLabel &&
|
|
480
|
+
prevProps.getOptionValue === nextProps.getOptionValue);
|
|
481
|
+
});
|
package/dist/styles/globals.css
CHANGED
|
@@ -90,9 +90,12 @@ li.activeSidebar-subset::after {
|
|
|
90
90
|
|
|
91
91
|
/* Custom scroll behavior for CreatableDropDown */
|
|
92
92
|
.creatable-dropdown-scroll {
|
|
93
|
-
scroll-behavior:
|
|
93
|
+
scroll-behavior: auto !important;
|
|
94
94
|
scrollbar-width: none;
|
|
95
95
|
-ms-overflow-style: none;
|
|
96
|
+
will-change: auto;
|
|
97
|
+
transform: translateZ(0);
|
|
98
|
+
backface-visibility: hidden;
|
|
96
99
|
}
|
|
97
100
|
|
|
98
101
|
.creatable-dropdown-scroll::-webkit-scrollbar {
|
|
@@ -100,7 +103,12 @@ li.activeSidebar-subset::after {
|
|
|
100
103
|
}
|
|
101
104
|
|
|
102
105
|
.creatable-dropdown-scroll:focus-within {
|
|
103
|
-
scroll-behavior: auto;
|
|
106
|
+
scroll-behavior: auto !important;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/* Prevent scroll container from being affected by re-renders */
|
|
110
|
+
.creatable-dropdown-scroll * {
|
|
111
|
+
will-change: auto;
|
|
104
112
|
}
|
|
105
113
|
.danger {
|
|
106
114
|
background-color: #ffffff;
|