hi-tm 0.7.2 → 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,80 +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
- const isProcessingRef = (0, react_1.useRef)(false);
139
- // Prevent scroll jumping by maintaining position during keyboard navigation
140
- const handleKeyboardNavigation = (0, react_1.useCallback)((direction) => {
141
- // Set user interaction flag to prevent external sync
142
- setIsUserInteracting(true);
143
- // Don't save scroll position - just update the highlighted index
144
- if (direction === 'up') {
145
- highlightedIndexRef.current = Math.max(0, highlightedIndexRef.current - 1);
146
- }
147
- else {
148
- highlightedIndexRef.current = Math.min(filteredOptions.length - 1, highlightedIndexRef.current + 1);
149
- }
150
- // Force re-render of dropdown options only
151
- setForceUpdate((prev) => prev + 1);
152
- // Reset the flag after a delay
153
- setTimeout(() => {
154
- setIsUserInteracting(false);
155
- }, 1000);
156
- }, [filteredOptions.length]);
136
+ const prevExternalIdsRef = (0, react_1.useRef)(null);
137
+ const mountedRef = (0, react_1.useRef)(false);
157
138
  // Sync external value prop with internal selectedValues state
158
139
  (0, react_1.useEffect)(() => {
159
- console.log('=== useEffect sync triggered ===');
160
- console.log('isUserInteracting:', isUserInteracting);
161
- console.log('external value:', value);
162
- console.log('current selectedValues:', selectedValues);
163
- console.log('isMultiSelect:', isMultiSelect);
164
- // Don't sync if user is actively interacting with the component
165
- if (isUserInteracting) {
166
- console.log('Skipping sync - user is interacting');
167
- return;
168
- }
169
- console.log('Proceeding with sync...');
170
- // Only sync if the external value is actually different from current selectedValues
171
- const currentValues = selectedValues;
172
- const externalValue = value;
173
- // Early return if values are already the same
174
- if (JSON.stringify(externalValue) === JSON.stringify(currentValues)) {
175
- console.log('Values are already the same, skipping sync');
140
+ mountedRef.current = true;
141
+ // Do not clear on transient null/undefined values
142
+ if (value === null || value === undefined) {
176
143
  return;
177
144
  }
178
- if (externalValue === null || externalValue === undefined) {
179
- if (currentValues.length > 0) {
180
- console.log('Setting selectedValues to empty array');
181
- setSelectedValues([]);
182
- }
183
- }
184
- else if (Array.isArray(externalValue)) {
185
- if (isMultiSelect) {
186
- const externalArray = externalValue;
187
- console.log('Setting selectedValues to array:', externalArray);
188
- setSelectedValues(externalArray);
189
- }
190
- else {
191
- const firstItem = externalValue.length > 0 ? [externalValue[0]] : [];
192
- console.log('Setting selectedValues to first item of array:', firstItem);
193
- setSelectedValues(firstItem);
194
- }
195
- }
196
- else {
197
- const singleItem = [externalValue];
198
- console.log('Setting selectedValues to single item:', singleItem);
199
- setSelectedValues(singleItem);
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;
200
159
  }
201
- console.log('=== useEffect sync completed ===');
202
- }, [value, isMultiSelect, isUserInteracting]);
160
+ }, [value, isMultiSelect, deriveValue]);
203
161
  // Sync options into filtered list when not searching
204
162
  (0, react_1.useEffect)(() => {
205
163
  if (!hasSearched) {
@@ -213,12 +171,6 @@ exports.default = react_1.default.memo(function CreatableSelect({ options = [],
213
171
  setHasSearched(false);
214
172
  }
215
173
  }, [options, inputValue]);
216
- // Debug: Log when selectedValues changes
217
- (0, react_1.useEffect)(() => {
218
- console.log('=== selectedValues changed ===');
219
- console.log('New selectedValues:', selectedValues);
220
- console.log('Stack trace:', new Error().stack);
221
- }, [selectedValues]);
222
174
  // Close on outside click
223
175
  (0, react_1.useEffect)(() => {
224
176
  const handler = (e) => {
@@ -235,7 +187,6 @@ exports.default = react_1.default.memo(function CreatableSelect({ options = [],
235
187
  const next = e.target.value;
236
188
  setInputValue(next);
237
189
  setIsOpen(true);
238
- setIsUserInteracting(true);
239
190
  if (onChange)
240
191
  onChange(e);
241
192
  if (fetchOptions && next.trim() !== '') {
@@ -263,28 +214,14 @@ exports.default = react_1.default.memo(function CreatableSelect({ options = [],
263
214
  }
264
215
  }, [onChange, fetchOptions, options, deriveLabel]);
265
216
  const handleKeyDown = (0, react_1.useCallback)((e) => {
266
- console.log('=== handleKeyDown called ===');
267
- console.log('key:', e.key);
268
- console.log('isDisabled:', isDisabled);
269
- console.log('current selectedValues:', selectedValues);
270
217
  if (isDisabled)
271
218
  return;
272
- // Prevent double calls by checking if we're already processing
273
- if (isProcessingRef.current) {
274
- console.log('Already processing keyboard event, skipping...');
275
- return;
276
- }
277
- // Set user interaction flag for all keyboard events
278
- console.log('Setting isUserInteracting to true');
279
- setIsUserInteracting(true);
280
- isProcessingRef.current = true;
281
219
  switch (e.key) {
282
220
  case 'Enter':
283
221
  e.preventDefault();
284
222
  if (isOpen) {
285
- if (filteredOptions.length > 0 &&
286
- highlightedIndexRef.current < filteredOptions.length) {
287
- selectOption(filteredOptions[highlightedIndexRef.current]);
223
+ if (filteredOptions.length > 0 && highlightedIndex < filteredOptions.length) {
224
+ selectOption(filteredOptions[highlightedIndex]);
288
225
  }
289
226
  else if (isCreatable &&
290
227
  inputValue &&
@@ -303,7 +240,7 @@ exports.default = react_1.default.memo(function CreatableSelect({ options = [],
303
240
  case 'ArrowUp':
304
241
  e.preventDefault();
305
242
  if (isOpen) {
306
- handleKeyboardNavigation('up');
243
+ setHighlightedIndex((prev) => (prev > 0 ? prev - 1 : filteredOptions.length - 1));
307
244
  }
308
245
  else {
309
246
  setIsOpen(true);
@@ -312,29 +249,14 @@ exports.default = react_1.default.memo(function CreatableSelect({ options = [],
312
249
  case 'ArrowDown':
313
250
  e.preventDefault();
314
251
  if (isOpen) {
315
- handleKeyboardNavigation('down');
252
+ setHighlightedIndex((prev) => (prev < filteredOptions.length - 1 ? prev + 1 : 0));
316
253
  }
317
254
  else {
318
255
  setIsOpen(true);
319
256
  }
320
257
  break;
321
258
  }
322
- // Reset the flags after a delay
323
- setTimeout(() => {
324
- console.log('Resetting isUserInteracting to false');
325
- setIsUserInteracting(false);
326
- isProcessingRef.current = false;
327
- }, 2000); // Increased to 2 seconds
328
- }, [
329
- isDisabled,
330
- isOpen,
331
- filteredOptions,
332
- inputValue,
333
- deriveLabel,
334
- handleKeyboardNavigation,
335
- isCreatable,
336
- selectedValues,
337
- ]);
259
+ }, [isDisabled, isOpen, filteredOptions, highlightedIndex, isCreatable, inputValue, deriveLabel]);
338
260
  const emitSelection = (0, react_1.useCallback)((next) => {
339
261
  // Backward-compatible callback
340
262
  if (onClick) {
@@ -368,31 +290,13 @@ exports.default = react_1.default.memo(function CreatableSelect({ options = [],
368
290
  }
369
291
  }, [inputValue, isCreatable, onCreateOption]);
370
292
  const toggleDropdown = (0, react_1.useCallback)(() => {
371
- console.log('=== toggleDropdown called ===');
372
- console.log('isDisabled:', isDisabled);
373
- console.log('current isOpen:', isOpen);
374
- console.log('current selectedValues:', selectedValues);
375
293
  if (!isDisabled) {
376
- // Prevent double calls by checking if we're already processing
377
- if (isProcessingRef.current) {
378
- console.log('Already processing, skipping...');
379
- return;
380
- }
381
- console.log('Setting isUserInteracting to true');
382
- setIsUserInteracting(true);
383
- isProcessingRef.current = true;
384
294
  setIsOpen((prev) => !prev);
385
295
  if (!isOpen && inputRef.current) {
386
296
  inputRef.current.focus();
387
297
  }
388
- // Reset the flags after a longer delay to prevent conflicts
389
- setTimeout(() => {
390
- console.log('Resetting isUserInteracting to false');
391
- setIsUserInteracting(false);
392
- isProcessingRef.current = false;
393
- }, 2000); // Increased to 2 seconds
394
298
  }
395
- }, [isDisabled, isOpen, selectedValues]);
299
+ }, [isDisabled, isOpen]);
396
300
  const clearValue = (0, react_1.useCallback)((e, option) => {
397
301
  e.stopPropagation();
398
302
  const next = selectedValues.filter((val) => deriveValue(val) !== deriveValue(option));
@@ -400,8 +304,6 @@ exports.default = react_1.default.memo(function CreatableSelect({ options = [],
400
304
  emitSelection(next);
401
305
  }, [selectedValues, deriveValue, emitSelection]);
402
306
  const selectOption = (0, react_1.useCallback)((option) => {
403
- // Set user interaction flag to prevent external sync
404
- setIsUserInteracting(true);
405
307
  if (isMultiSelect) {
406
308
  const next = [...selectedValues];
407
309
  if (!next.some((val) => deriveValue(val) === deriveValue(option))) {
@@ -422,10 +324,6 @@ exports.default = react_1.default.memo(function CreatableSelect({ options = [],
422
324
  setIsOpen(false);
423
325
  setHasSearched(false);
424
326
  }
425
- // Reset the flag after a delay
426
- setTimeout(() => {
427
- setIsUserInteracting(false);
428
- }, 1000);
429
327
  }, [isMultiSelect, selectedValues, deriveValue, emitSelection]);
430
328
  const showLoading = isLoading || internalLoading;
431
329
  const handleAddIconClick = (0, react_1.useCallback)((e) => {
@@ -434,48 +332,29 @@ exports.default = react_1.default.memo(function CreatableSelect({ options = [],
434
332
  onAddClick(true);
435
333
  }
436
334
  }, [onAddClick]);
437
- 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: {
438
- scrollBehavior: 'auto',
439
- scrollbarWidth: 'none',
440
- msOverflowStyle: 'none',
441
- minWidth: 0, // Prevent flex item from growing beyond container
442
- flexShrink: 1, // Allow shrinking
443
- } },
444
- react_1.default.createElement("div", { className: "flex items-center gap-2 flex-nowrap min-w-0" },
445
- selectedValues.length > 0 &&
446
- 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" },
447
- react_1.default.createElement("span", { className: "truncate" }, deriveLabel(val)),
448
- react_1.default.createElement("button", { onClick: (e) => clearValue(e, val), className: styles.clearButton }, renderIcon(icons.clear, styles.icon))))),
449
- 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() })))), [
450
- selectedValues,
451
- isDisabled,
452
- inputValue,
453
- inputRef,
454
- clearValue,
455
- deriveValue,
456
- deriveLabel,
457
- icons.clear,
458
- styles.input,
459
- styles.clearButton,
460
- placeholder,
461
- handleInputChange,
462
- ]);
463
335
  return (react_1.default.createElement("div", { ref: containerRef, className: `relative w-full ${className} ${styles.container}`, onKeyDown: handleKeyDown },
464
336
  label && react_1.default.createElement("label", { className: "block text-sm font-medium text-gray-700 mb-1" }, label),
465
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 },
466
- 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() }))),
467
345
  react_1.default.createElement("div", { className: "flex-shrink-0 flex items-center gap-1 ml-2" },
468
346
  iconSrc.hasAdd && (react_1.default.createElement("button", { onClick: handleAddIconClick, className: styles.clearButton }, renderIcon(iconSrc.hasAdd, styles.icon))),
469
347
  styles.separator && react_1.default.createElement("div", { className: styles.separator }),
470
- 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
471
352
  ? renderIcon(iconSrc.dropdownOpen, `${styles.icon} ${styles.spinAnimation || ''}`)
472
- : renderIcon(iconSrc.dropdownClosed, styles.icon))),
353
+ : renderIcon(iconSrc.dropdownClosed, styles.icon)))),
473
354
  isOpen && (react_1.default.createElement("div", { className: styles.dropdown }, showLoading ? (react_1.default.createElement("div", { className: styles.loadingState },
474
355
  renderIcon(iconSrc.loading, `animate-spin mr-2 ${styles.icon}`),
475
356
  loadingMessage)) : (react_1.default.createElement(react_1.default.Fragment, null,
476
- 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
477
- ? styles.highlightedOption
478
- : '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) },
479
358
  selectedValues.some((val) => deriveValue(val) === deriveValue(option)) &&
480
359
  renderIcon(iconSrc.checkmark, 'w-4 h-4 mr-2 text-blue-500'),
481
360
  react_1.default.createElement("span", { className: selectedValues.some((val) => deriveValue(val) === deriveValue(option))
@@ -494,26 +373,4 @@ exports.default = react_1.default.memo(function CreatableSelect({ options = [],
494
373
  createOptionMessage,
495
374
  ": ",
496
375
  inputValue)))))))));
497
- }, (prevProps, nextProps) => {
498
- // Custom comparison function for React.memo
499
- // Only re-render if the props that affect the component's output have changed.
500
- // This prevents unnecessary re-renders when options, value, or other props are shallowly equal.
501
- return (prevProps.options === nextProps.options &&
502
- prevProps.value === nextProps.value &&
503
- prevProps.isLoading === nextProps.isLoading &&
504
- prevProps.isDisabled === nextProps.isDisabled &&
505
- prevProps.isCreatable === nextProps.isCreatable &&
506
- prevProps.isMultiSelect === nextProps.isMultiSelect &&
507
- prevProps.noOptionsMessage === nextProps.noOptionsMessage &&
508
- prevProps.noResultsFound === nextProps.noResultsFound &&
509
- prevProps.loadingMessage === nextProps.loadingMessage &&
510
- prevProps.createOptionMessage === nextProps.createOptionMessage &&
511
- prevProps.className === nextProps.className &&
512
- prevProps.tailwindStyle === nextProps.tailwindStyle &&
513
- prevProps.iconSrc === nextProps.iconSrc &&
514
- prevProps.fetchOptions === nextProps.fetchOptions &&
515
- prevProps.label === nextProps.label &&
516
- prevProps.onAddClick === nextProps.onAddClick &&
517
- prevProps.getOptionLabel === nextProps.getOptionLabel &&
518
- prevProps.getOptionValue === nextProps.getOptionValue);
519
- });
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.2",
3
+ "version": "0.7.3",
4
4
  "styles": "dist/styles/globals.css",
5
5
  "main": "dist/index.js",
6
6
  "exports": {