hi-tm 0.7.1 → 0.7.3

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,4 +1,3 @@
1
1
  import React from 'react';
2
2
  import type { SelectOption, CreatableSelectProps } from '../../types/DropDown/CreatableDropDown';
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;
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;
@@ -33,6 +33,7 @@ var __importStar = (this && this.__importStar) || (function () {
33
33
  };
34
34
  })();
35
35
  Object.defineProperty(exports, "__esModule", { value: true });
36
+ exports.default = CreatableSelect;
36
37
  const react_1 = __importStar(require("react"));
37
38
  // Default Tailwind styles
38
39
  const defaultStyles = {
@@ -86,7 +87,7 @@ const renderNoResults = (noResults, className, searchIcon) => {
86
87
  return noResults;
87
88
  }
88
89
  };
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, }) {
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, }) {
90
91
  // Default icons
91
92
  const defaultIcons = {
92
93
  dropdownOpen: '▼',
@@ -126,81 +127,37 @@ exports.default = react_1.default.memo(function CreatableSelect({ options = [],
126
127
  const [isOpen, setIsOpen] = (0, react_1.useState)(false);
127
128
  const [inputValue, setInputValue] = (0, react_1.useState)('');
128
129
  const [filteredOptions, setFilteredOptions] = (0, react_1.useState)(options ?? []);
129
- const highlightedIndexRef = (0, react_1.useRef)(0);
130
+ const [highlightedIndex, setHighlightedIndex] = (0, react_1.useState)(0);
130
131
  const [internalLoading, setInternalLoading] = (0, react_1.useState)(false);
131
132
  const [hasSearched, setHasSearched] = (0, react_1.useState)(false);
132
133
  const [selectedValues, setSelectedValues] = (0, react_1.useState)([]);
133
- const [isUserInteracting, setIsUserInteracting] = (0, react_1.useState)(false);
134
- const [forceUpdate, setForceUpdate] = (0, react_1.useState)(0);
135
134
  const containerRef = (0, react_1.useRef)(null);
136
135
  const inputRef = (0, react_1.useRef)(null);
137
- const scrollContainerRef = (0, react_1.useRef)(null);
138
- // Prevent scroll jumping by maintaining position during keyboard navigation
139
- const handleKeyboardNavigation = (0, react_1.useCallback)((direction) => {
140
- // Set user interaction flag to prevent external sync
141
- setIsUserInteracting(true);
142
- // Don't save scroll position - just update the highlighted index
143
- if (direction === 'up') {
144
- highlightedIndexRef.current = Math.max(0, highlightedIndexRef.current - 1);
145
- }
146
- else {
147
- highlightedIndexRef.current = Math.min(filteredOptions.length - 1, highlightedIndexRef.current + 1);
148
- }
149
- // Force re-render of dropdown options only
150
- setForceUpdate((prev) => prev + 1);
151
- // Reset the flag after a delay
152
- setTimeout(() => {
153
- setIsUserInteracting(false);
154
- }, 1000);
155
- }, [filteredOptions.length]);
136
+ const prevExternalIdsRef = (0, react_1.useRef)(null);
137
+ const mountedRef = (0, react_1.useRef)(false);
156
138
  // Sync external value prop with internal selectedValues state
157
139
  (0, react_1.useEffect)(() => {
158
- console.log('=== useEffect sync triggered ===');
159
- console.log('isUserInteracting:', isUserInteracting);
160
- console.log('external value:', value);
161
- console.log('current selectedValues:', selectedValues);
162
- console.log('isMultiSelect:', isMultiSelect);
163
- // Don't sync if user is actively interacting with the component
164
- if (isUserInteracting) {
165
- console.log('Skipping sync - user is interacting');
140
+ mountedRef.current = true;
141
+ // Do not clear on transient null/undefined values
142
+ if (value === null || value === undefined) {
166
143
  return;
167
144
  }
168
- console.log('Proceeding with sync...');
169
- // Only sync if the external value is actually different from current selectedValues
170
- const currentValues = selectedValues;
171
- const externalValue = value;
172
- if (externalValue === null || externalValue === undefined) {
173
- if (currentValues.length > 0) {
174
- console.log('Setting selectedValues to empty array');
175
- setSelectedValues([]);
176
- }
177
- }
178
- else if (Array.isArray(externalValue)) {
179
- if (isMultiSelect) {
180
- const externalArray = externalValue;
181
- // Only update if the arrays are actually different
182
- if (JSON.stringify(externalArray) !== JSON.stringify(currentValues)) {
183
- console.log('Setting selectedValues to array:', externalArray);
184
- setSelectedValues(externalArray);
185
- }
186
- }
187
- else {
188
- const firstItem = externalValue.length > 0 ? [externalValue[0]] : [];
189
- if (JSON.stringify(firstItem) !== JSON.stringify(currentValues)) {
190
- console.log('Setting selectedValues to first item of array:', firstItem);
191
- setSelectedValues(firstItem);
192
- }
193
- }
194
- }
195
- else {
196
- const singleItem = [externalValue];
197
- if (JSON.stringify(singleItem) !== JSON.stringify(currentValues)) {
198
- console.log('Setting selectedValues to single item:', singleItem);
199
- setSelectedValues(singleItem);
200
- }
145
+ const arrayValue = Array.isArray(value) ? value : [value];
146
+ const normalizedArray = isMultiSelect
147
+ ? arrayValue
148
+ : arrayValue.length > 0
149
+ ? [arrayValue[0]]
150
+ : [];
151
+ const externalIds = normalizedArray.map((opt) => deriveValue(opt));
152
+ const prevIds = prevExternalIdsRef.current;
153
+ const hasChanged = !prevIds ||
154
+ prevIds.length !== externalIds.length ||
155
+ externalIds.some((id, idx) => id !== prevIds[idx]);
156
+ if (hasChanged) {
157
+ setSelectedValues(normalizedArray);
158
+ prevExternalIdsRef.current = externalIds;
201
159
  }
202
- console.log('=== useEffect sync completed ===');
203
- }, [value, isMultiSelect, isUserInteracting]);
160
+ }, [value, isMultiSelect, deriveValue]);
204
161
  // Sync options into filtered list when not searching
205
162
  (0, react_1.useEffect)(() => {
206
163
  if (!hasSearched) {
@@ -214,12 +171,6 @@ exports.default = react_1.default.memo(function CreatableSelect({ options = [],
214
171
  setHasSearched(false);
215
172
  }
216
173
  }, [options, inputValue]);
217
- // Debug: Log when selectedValues changes
218
- (0, react_1.useEffect)(() => {
219
- console.log('=== selectedValues changed ===');
220
- console.log('New selectedValues:', selectedValues);
221
- console.log('Stack trace:', new Error().stack);
222
- }, [selectedValues]);
223
174
  // Close on outside click
224
175
  (0, react_1.useEffect)(() => {
225
176
  const handler = (e) => {
@@ -236,7 +187,6 @@ exports.default = react_1.default.memo(function CreatableSelect({ options = [],
236
187
  const next = e.target.value;
237
188
  setInputValue(next);
238
189
  setIsOpen(true);
239
- setIsUserInteracting(true);
240
190
  if (onChange)
241
191
  onChange(e);
242
192
  if (fetchOptions && next.trim() !== '') {
@@ -264,22 +214,14 @@ exports.default = react_1.default.memo(function CreatableSelect({ options = [],
264
214
  }
265
215
  }, [onChange, fetchOptions, options, deriveLabel]);
266
216
  const handleKeyDown = (0, react_1.useCallback)((e) => {
267
- console.log('=== handleKeyDown called ===');
268
- console.log('key:', e.key);
269
- console.log('isDisabled:', isDisabled);
270
- console.log('current selectedValues:', selectedValues);
271
217
  if (isDisabled)
272
218
  return;
273
- // Set user interaction flag for all keyboard events
274
- console.log('Setting isUserInteracting to true');
275
- setIsUserInteracting(true);
276
219
  switch (e.key) {
277
220
  case 'Enter':
278
221
  e.preventDefault();
279
222
  if (isOpen) {
280
- if (filteredOptions.length > 0 &&
281
- highlightedIndexRef.current < filteredOptions.length) {
282
- selectOption(filteredOptions[highlightedIndexRef.current]);
223
+ if (filteredOptions.length > 0 && highlightedIndex < filteredOptions.length) {
224
+ selectOption(filteredOptions[highlightedIndex]);
283
225
  }
284
226
  else if (isCreatable &&
285
227
  inputValue &&
@@ -298,7 +240,7 @@ exports.default = react_1.default.memo(function CreatableSelect({ options = [],
298
240
  case 'ArrowUp':
299
241
  e.preventDefault();
300
242
  if (isOpen) {
301
- handleKeyboardNavigation('up');
243
+ setHighlightedIndex((prev) => (prev > 0 ? prev - 1 : filteredOptions.length - 1));
302
244
  }
303
245
  else {
304
246
  setIsOpen(true);
@@ -307,28 +249,14 @@ exports.default = react_1.default.memo(function CreatableSelect({ options = [],
307
249
  case 'ArrowDown':
308
250
  e.preventDefault();
309
251
  if (isOpen) {
310
- handleKeyboardNavigation('down');
252
+ setHighlightedIndex((prev) => (prev < filteredOptions.length - 1 ? prev + 1 : 0));
311
253
  }
312
254
  else {
313
255
  setIsOpen(true);
314
256
  }
315
257
  break;
316
258
  }
317
- // Reset the flag after a delay
318
- setTimeout(() => {
319
- console.log('Resetting isUserInteracting to false');
320
- setIsUserInteracting(false);
321
- }, 1000);
322
- }, [
323
- isDisabled,
324
- isOpen,
325
- filteredOptions,
326
- inputValue,
327
- deriveLabel,
328
- handleKeyboardNavigation,
329
- isCreatable,
330
- selectedValues,
331
- ]);
259
+ }, [isDisabled, isOpen, filteredOptions, highlightedIndex, isCreatable, inputValue, deriveLabel]);
332
260
  const emitSelection = (0, react_1.useCallback)((next) => {
333
261
  // Backward-compatible callback
334
262
  if (onClick) {
@@ -362,24 +290,13 @@ exports.default = react_1.default.memo(function CreatableSelect({ options = [],
362
290
  }
363
291
  }, [inputValue, isCreatable, onCreateOption]);
364
292
  const toggleDropdown = (0, react_1.useCallback)(() => {
365
- console.log('=== toggleDropdown called ===');
366
- console.log('isDisabled:', isDisabled);
367
- console.log('current isOpen:', isOpen);
368
- console.log('current selectedValues:', selectedValues);
369
293
  if (!isDisabled) {
370
- console.log('Setting isUserInteracting to true');
371
- setIsUserInteracting(true);
372
294
  setIsOpen((prev) => !prev);
373
295
  if (!isOpen && inputRef.current) {
374
296
  inputRef.current.focus();
375
297
  }
376
- // Reset the flag after a delay
377
- setTimeout(() => {
378
- console.log('Resetting isUserInteracting to false');
379
- setIsUserInteracting(false);
380
- }, 1000);
381
298
  }
382
- }, [isDisabled, isOpen, selectedValues]);
299
+ }, [isDisabled, isOpen]);
383
300
  const clearValue = (0, react_1.useCallback)((e, option) => {
384
301
  e.stopPropagation();
385
302
  const next = selectedValues.filter((val) => deriveValue(val) !== deriveValue(option));
@@ -387,8 +304,6 @@ exports.default = react_1.default.memo(function CreatableSelect({ options = [],
387
304
  emitSelection(next);
388
305
  }, [selectedValues, deriveValue, emitSelection]);
389
306
  const selectOption = (0, react_1.useCallback)((option) => {
390
- // Set user interaction flag to prevent external sync
391
- setIsUserInteracting(true);
392
307
  if (isMultiSelect) {
393
308
  const next = [...selectedValues];
394
309
  if (!next.some((val) => deriveValue(val) === deriveValue(option))) {
@@ -409,10 +324,6 @@ exports.default = react_1.default.memo(function CreatableSelect({ options = [],
409
324
  setIsOpen(false);
410
325
  setHasSearched(false);
411
326
  }
412
- // Reset the flag after a delay
413
- setTimeout(() => {
414
- setIsUserInteracting(false);
415
- }, 1000);
416
327
  }, [isMultiSelect, selectedValues, deriveValue, emitSelection]);
417
328
  const showLoading = isLoading || internalLoading;
418
329
  const handleAddIconClick = (0, react_1.useCallback)((e) => {
@@ -421,48 +332,29 @@ exports.default = react_1.default.memo(function CreatableSelect({ options = [],
421
332
  onAddClick(true);
422
333
  }
423
334
  }, [onAddClick]);
424
- 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: {
425
- scrollBehavior: 'auto',
426
- scrollbarWidth: 'none',
427
- msOverflowStyle: 'none',
428
- minWidth: 0, // Prevent flex item from growing beyond container
429
- flexShrink: 1, // Allow shrinking
430
- } },
431
- react_1.default.createElement("div", { className: "flex items-center gap-2 flex-nowrap min-w-0" },
432
- selectedValues.length > 0 &&
433
- 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" },
434
- react_1.default.createElement("span", { className: "truncate" }, deriveLabel(val)),
435
- react_1.default.createElement("button", { onClick: (e) => clearValue(e, val), className: styles.clearButton }, renderIcon(icons.clear, styles.icon))))),
436
- 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() })))), [
437
- selectedValues,
438
- isDisabled,
439
- inputValue,
440
- inputRef,
441
- clearValue,
442
- deriveValue,
443
- deriveLabel,
444
- icons.clear,
445
- styles.input,
446
- styles.clearButton,
447
- placeholder,
448
- handleInputChange,
449
- ]);
450
335
  return (react_1.default.createElement("div", { ref: containerRef, className: `relative w-full ${className} ${styles.container}`, onKeyDown: handleKeyDown },
451
336
  label && react_1.default.createElement("label", { className: "block text-sm font-medium text-gray-700 mb-1" }, label),
452
337
  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 },
453
- scrollContainer,
338
+ react_1.default.createElement("div", { className: "flex-grow flex items-center overflow-x-auto no-scrollbar" },
339
+ react_1.default.createElement("div", { className: "flex items-center gap-2 flex-nowrap" },
340
+ selectedValues.length > 0 &&
341
+ 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" },
342
+ react_1.default.createElement("span", { className: "truncate" }, deriveLabel(val)),
343
+ react_1.default.createElement("button", { onClick: (e) => clearValue(e, val), className: styles.clearButton }, renderIcon(icons.clear, styles.icon))))),
344
+ 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() }))),
454
345
  react_1.default.createElement("div", { className: "flex-shrink-0 flex items-center gap-1 ml-2" },
455
346
  iconSrc.hasAdd && (react_1.default.createElement("button", { onClick: handleAddIconClick, className: styles.clearButton }, renderIcon(iconSrc.hasAdd, styles.icon))),
456
347
  styles.separator && react_1.default.createElement("div", { className: styles.separator }),
457
- isOpen
348
+ react_1.default.createElement("button", { type: "button", onMouseDown: (e) => e.preventDefault(), onClick: (e) => {
349
+ e.stopPropagation();
350
+ toggleDropdown();
351
+ }, className: styles.clearButton }, isOpen
458
352
  ? renderIcon(iconSrc.dropdownOpen, `${styles.icon} ${styles.spinAnimation || ''}`)
459
- : renderIcon(iconSrc.dropdownClosed, styles.icon))),
353
+ : renderIcon(iconSrc.dropdownClosed, styles.icon)))),
460
354
  isOpen && (react_1.default.createElement("div", { className: styles.dropdown }, showLoading ? (react_1.default.createElement("div", { className: styles.loadingState },
461
355
  renderIcon(iconSrc.loading, `animate-spin mr-2 ${styles.icon}`),
462
356
  loadingMessage)) : (react_1.default.createElement(react_1.default.Fragment, null,
463
- 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
464
- ? styles.highlightedOption
465
- : 'hover:bg-gray-50'}`, onClick: () => selectOption(option), onMouseEnter: () => (highlightedIndexRef.current = index) },
357
+ 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) },
466
358
  selectedValues.some((val) => deriveValue(val) === deriveValue(option)) &&
467
359
  renderIcon(iconSrc.checkmark, 'w-4 h-4 mr-2 text-blue-500'),
468
360
  react_1.default.createElement("span", { className: selectedValues.some((val) => deriveValue(val) === deriveValue(option))
@@ -481,26 +373,4 @@ exports.default = react_1.default.memo(function CreatableSelect({ options = [],
481
373
  createOptionMessage,
482
374
  ": ",
483
375
  inputValue)))))))));
484
- }, (prevProps, nextProps) => {
485
- // Custom comparison function for React.memo
486
- // Only re-render if the props that affect the component's output have changed.
487
- // This prevents unnecessary re-renders when options, value, or other props are shallowly equal.
488
- return (prevProps.options === nextProps.options &&
489
- prevProps.value === nextProps.value &&
490
- prevProps.isLoading === nextProps.isLoading &&
491
- prevProps.isDisabled === nextProps.isDisabled &&
492
- prevProps.isCreatable === nextProps.isCreatable &&
493
- prevProps.isMultiSelect === nextProps.isMultiSelect &&
494
- prevProps.noOptionsMessage === nextProps.noOptionsMessage &&
495
- prevProps.noResultsFound === nextProps.noResultsFound &&
496
- prevProps.loadingMessage === nextProps.loadingMessage &&
497
- prevProps.createOptionMessage === nextProps.createOptionMessage &&
498
- prevProps.className === nextProps.className &&
499
- prevProps.tailwindStyle === nextProps.tailwindStyle &&
500
- prevProps.iconSrc === nextProps.iconSrc &&
501
- prevProps.fetchOptions === nextProps.fetchOptions &&
502
- prevProps.label === nextProps.label &&
503
- prevProps.onAddClick === nextProps.onAddClick &&
504
- prevProps.getOptionLabel === nextProps.getOptionLabel &&
505
- prevProps.getOptionValue === nextProps.getOptionValue);
506
- });
376
+ }
@@ -87,29 +87,6 @@ li.activeSidebar-subset::after {
87
87
  .noDisplay-scrollbar::-webkit-scrollbar {
88
88
  display: none; /* For Chrome, Safari, and Opera */
89
89
  }
90
-
91
- /* Custom scroll behavior for CreatableDropDown */
92
- .creatable-dropdown-scroll {
93
- scroll-behavior: auto !important;
94
- scrollbar-width: none;
95
- -ms-overflow-style: none;
96
- will-change: auto;
97
- transform: translateZ(0);
98
- backface-visibility: hidden;
99
- }
100
-
101
- .creatable-dropdown-scroll::-webkit-scrollbar {
102
- display: none;
103
- }
104
-
105
- .creatable-dropdown-scroll:focus-within {
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;
112
- }
113
90
  .danger {
114
91
  background-color: #ffffff;
115
92
  color: #f54180;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hi-tm",
3
- "version": "0.7.1",
3
+ "version": "0.7.3",
4
4
  "styles": "dist/styles/globals.css",
5
5
  "main": "dist/index.js",
6
6
  "exports": {