hi-tm 0.6.7 → 0.6.9

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
- export default function CreatableSelect<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;
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: '▼',
@@ -127,44 +126,27 @@ function CreatableSelect({ options = [], value, onChange, onClick, onValueChange
127
126
  const [isOpen, setIsOpen] = (0, react_1.useState)(false);
128
127
  const [inputValue, setInputValue] = (0, react_1.useState)('');
129
128
  const [filteredOptions, setFilteredOptions] = (0, react_1.useState)(options ?? []);
130
- const [highlightedIndex, setHighlightedIndex] = (0, react_1.useState)(0);
129
+ const highlightedIndexRef = (0, react_1.useRef)(0);
131
130
  const [internalLoading, setInternalLoading] = (0, react_1.useState)(false);
132
131
  const [hasSearched, setHasSearched] = (0, react_1.useState)(false);
133
132
  const [selectedValues, setSelectedValues] = (0, react_1.useState)([]);
134
133
  const [isUserInteracting, setIsUserInteracting] = (0, react_1.useState)(false);
134
+ const [forceUpdate, setForceUpdate] = (0, react_1.useState)(0);
135
135
  const containerRef = (0, react_1.useRef)(null);
136
136
  const inputRef = (0, react_1.useRef)(null);
137
137
  const scrollContainerRef = (0, react_1.useRef)(null);
138
- const scrollPositionRef = (0, react_1.useRef)(0);
139
- // Save scroll position before re-renders
140
- const saveScrollPosition = (0, react_1.useCallback)(() => {
141
- if (scrollContainerRef.current) {
142
- scrollPositionRef.current = scrollContainerRef.current.scrollLeft;
143
- }
144
- }, []);
145
- // Restore scroll position after re-renders
146
- const restoreScrollPosition = (0, react_1.useCallback)(() => {
147
- if (scrollContainerRef.current && scrollPositionRef.current > 0) {
148
- // Only restore if the position has actually changed
149
- if (scrollContainerRef.current.scrollLeft !== scrollPositionRef.current) {
150
- scrollContainerRef.current.scrollLeft = scrollPositionRef.current;
151
- }
152
- }
153
- }, []);
154
138
  // Prevent scroll jumping by maintaining position during keyboard navigation
155
139
  const handleKeyboardNavigation = (0, react_1.useCallback)((direction) => {
156
- saveScrollPosition();
140
+ // Don't save scroll position - just update the highlighted index
157
141
  if (direction === 'up') {
158
- setHighlightedIndex((prev) => (prev > 0 ? prev - 1 : filteredOptions.length - 1));
142
+ highlightedIndexRef.current = Math.max(0, highlightedIndexRef.current - 1);
159
143
  }
160
144
  else {
161
- setHighlightedIndex((prev) => (prev < filteredOptions.length - 1 ? prev + 1 : 0));
145
+ highlightedIndexRef.current = Math.min(filteredOptions.length - 1, highlightedIndexRef.current + 1);
162
146
  }
163
- // Restore scroll position after state update
164
- requestAnimationFrame(() => {
165
- restoreScrollPosition();
166
- });
167
- }, [filteredOptions.length, saveScrollPosition, restoreScrollPosition]);
147
+ // Force re-render of dropdown options only
148
+ setForceUpdate((prev) => prev + 1);
149
+ }, [filteredOptions.length]);
168
150
  // Sync external value prop with internal selectedValues state
169
151
  (0, react_1.useEffect)(() => {
170
152
  // Don't sync if user is actively interacting with the component
@@ -213,13 +195,6 @@ function CreatableSelect({ options = [], value, onChange, onClick, onValueChange
213
195
  setFilteredOptions(options ?? []);
214
196
  }
215
197
  }, [options, hasSearched]);
216
- // Restore scroll position after highlightedIndex changes
217
- (0, react_1.useEffect)(() => {
218
- // Use requestAnimationFrame to ensure DOM has updated
219
- requestAnimationFrame(() => {
220
- restoreScrollPosition();
221
- });
222
- }, [highlightedIndex, restoreScrollPosition]);
223
198
  // Reset filtered list when input cleared
224
199
  (0, react_1.useEffect)(() => {
225
200
  if (inputValue === '') {
@@ -277,8 +252,9 @@ function CreatableSelect({ options = [], value, onChange, onClick, onValueChange
277
252
  case 'Enter':
278
253
  e.preventDefault();
279
254
  if (isOpen) {
280
- if (filteredOptions.length > 0 && highlightedIndex < filteredOptions.length) {
281
- selectOption(filteredOptions[highlightedIndex]);
255
+ if (filteredOptions.length > 0 &&
256
+ highlightedIndexRef.current < filteredOptions.length) {
257
+ selectOption(filteredOptions[highlightedIndexRef.current]);
282
258
  }
283
259
  else if (isCreatable &&
284
260
  inputValue &&
@@ -317,11 +293,10 @@ function CreatableSelect({ options = [], value, onChange, onClick, onValueChange
317
293
  isDisabled,
318
294
  isOpen,
319
295
  filteredOptions,
320
- highlightedIndex,
321
- isCreatable,
322
296
  inputValue,
323
297
  deriveLabel,
324
298
  handleKeyboardNavigation,
299
+ isCreatable,
325
300
  ]);
326
301
  const emitSelection = (0, react_1.useCallback)((next) => {
327
302
  // Backward-compatible callback
@@ -398,16 +373,36 @@ function CreatableSelect({ options = [], value, onChange, onClick, onValueChange
398
373
  onAddClick(true);
399
374
  }
400
375
  }, [onAddClick]);
376
+ 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: {
377
+ scrollBehavior: 'auto',
378
+ scrollbarWidth: 'none',
379
+ msOverflowStyle: 'none',
380
+ minWidth: 0, // Prevent flex item from growing beyond container
381
+ flexShrink: 1, // Allow shrinking
382
+ } },
383
+ react_1.default.createElement("div", { className: "flex items-center gap-2 flex-nowrap min-w-0" },
384
+ selectedValues.length > 0 &&
385
+ 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" },
386
+ react_1.default.createElement("span", { className: "truncate" }, deriveLabel(val)),
387
+ react_1.default.createElement("button", { onClick: (e) => clearValue(e, val), className: styles.clearButton }, renderIcon(icons.clear, styles.icon))))),
388
+ 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() })))), [
389
+ selectedValues,
390
+ isDisabled,
391
+ inputValue,
392
+ inputRef,
393
+ clearValue,
394
+ deriveValue,
395
+ deriveLabel,
396
+ icons.clear,
397
+ styles.input,
398
+ styles.clearButton,
399
+ placeholder,
400
+ handleInputChange,
401
+ ]);
401
402
  return (react_1.default.createElement("div", { ref: containerRef, className: `relative w-full ${className} ${styles.container}`, onKeyDown: handleKeyDown },
402
403
  label && react_1.default.createElement("label", { className: "block text-sm font-medium text-gray-700 mb-1" }, label),
403
404
  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: toggleDropdown },
404
- react_1.default.createElement("div", { ref: scrollContainerRef, className: "flex-grow flex items-center overflow-x-auto creatable-dropdown-scroll" },
405
- react_1.default.createElement("div", { className: "flex items-center gap-2 flex-nowrap" },
406
- selectedValues.length > 0 &&
407
- 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
- react_1.default.createElement("span", { className: "truncate" }, deriveLabel(val)),
409
- react_1.default.createElement("button", { onClick: (e) => clearValue(e, val), className: styles.clearButton }, renderIcon(icons.clear, styles.icon))))),
410
- 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() }))),
405
+ scrollContainer,
411
406
  react_1.default.createElement("div", { className: "flex-shrink-0 flex items-center gap-1 ml-2" },
412
407
  iconSrc.hasAdd && (react_1.default.createElement("button", { onClick: handleAddIconClick, className: styles.clearButton }, renderIcon(iconSrc.hasAdd, styles.icon))),
413
408
  styles.separator && react_1.default.createElement("div", { className: styles.separator }),
@@ -417,7 +412,9 @@ function CreatableSelect({ options = [], value, onChange, onClick, onValueChange
417
412
  isOpen && (react_1.default.createElement("div", { className: styles.dropdown }, showLoading ? (react_1.default.createElement("div", { className: styles.loadingState },
418
413
  renderIcon(iconSrc.loading, `animate-spin mr-2 ${styles.icon}`),
419
414
  loadingMessage)) : (react_1.default.createElement(react_1.default.Fragment, null,
420
- filteredOptions.length > 0 && (react_1.default.createElement(react_1.default.Fragment, null, filteredOptions.map((option, index) => (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) },
415
+ filteredOptions.length > 0 && (react_1.default.createElement(react_1.default.Fragment, null, filteredOptions.map((option, index) => (react_1.default.createElement("div", { key: `${deriveValue(option)}-${forceUpdate}`, className: `${styles.option} ${index === highlightedIndexRef.current
416
+ ? styles.highlightedOption
417
+ : 'hover:bg-gray-50'}`, onClick: () => selectOption(option), onMouseEnter: () => (highlightedIndexRef.current = index) },
421
418
  selectedValues.some((val) => deriveValue(val) === deriveValue(option)) &&
422
419
  renderIcon(iconSrc.checkmark, 'w-4 h-4 mr-2 text-blue-500'),
423
420
  react_1.default.createElement("span", { className: selectedValues.some((val) => deriveValue(val) === deriveValue(option))
@@ -436,4 +433,26 @@ function CreatableSelect({ options = [], value, onChange, onClick, onValueChange
436
433
  createOptionMessage,
437
434
  ": ",
438
435
  inputValue)))))))));
439
- }
436
+ }, (prevProps, nextProps) => {
437
+ // Custom comparison function for React.memo
438
+ // Only re-render if the props that affect the component's output have changed.
439
+ // This prevents unnecessary re-renders when options, value, or other props are shallowly equal.
440
+ return (prevProps.options === nextProps.options &&
441
+ prevProps.value === nextProps.value &&
442
+ prevProps.isLoading === nextProps.isLoading &&
443
+ prevProps.isDisabled === nextProps.isDisabled &&
444
+ prevProps.isCreatable === nextProps.isCreatable &&
445
+ prevProps.isMultiSelect === nextProps.isMultiSelect &&
446
+ prevProps.noOptionsMessage === nextProps.noOptionsMessage &&
447
+ prevProps.noResultsFound === nextProps.noResultsFound &&
448
+ prevProps.loadingMessage === nextProps.loadingMessage &&
449
+ prevProps.createOptionMessage === nextProps.createOptionMessage &&
450
+ prevProps.className === nextProps.className &&
451
+ prevProps.tailwindStyle === nextProps.tailwindStyle &&
452
+ prevProps.iconSrc === nextProps.iconSrc &&
453
+ prevProps.fetchOptions === nextProps.fetchOptions &&
454
+ prevProps.label === nextProps.label &&
455
+ prevProps.onAddClick === nextProps.onAddClick &&
456
+ prevProps.getOptionLabel === nextProps.getOptionLabel &&
457
+ prevProps.getOptionValue === nextProps.getOptionValue);
458
+ });
@@ -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: smooth;
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;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hi-tm",
3
- "version": "0.6.7",
3
+ "version": "0.6.9",
4
4
  "styles": "dist/styles/globals.css",
5
5
  "main": "dist/index.js",
6
6
  "exports": {