hi-tm 0.6.5 → 0.6.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.
@@ -70,7 +70,7 @@ const renderNoResults = (noResults, className, searchIcon) => {
70
70
  if (!noResults) {
71
71
  return (react_1.default.createElement("div", { className: className },
72
72
  renderIcon(searchIcon, 'w-6 h-6 mb-1'),
73
- react_1.default.createElement("span", null, "No results found")));
73
+ react_1.default.createElement("span", null, "\u0645\u0648\u0631\u062F\u06CC \u06CC\u0627\u0641\u062A \u0646\u0634\u062F")));
74
74
  }
75
75
  if (typeof noResults === 'string') {
76
76
  return react_1.default.createElement("div", { className: className }, noResults);
@@ -87,7 +87,7 @@ const renderNoResults = (noResults, className, searchIcon) => {
87
87
  return noResults;
88
88
  }
89
89
  };
90
- function CreatableSelect({ options = [], value, onChange, onClick, onValueChange, onCreateOption, placeholder = 'Select...', isLoading = false, isDisabled = false, isCreatable = true, isMultiSelect = false, noOptionsMessage = 'No options available', noResultsFound, loadingMessage = 'Loading...', createOptionMessage = 'Create option', 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, }) {
91
91
  // Default icons
92
92
  const defaultIcons = {
93
93
  dropdownOpen: '▼',
@@ -112,6 +112,7 @@ function CreatableSelect({ options = [], value, onChange, onClick, onValueChange
112
112
  const anyOption = option;
113
113
  const label = anyOption.label ?? anyOption.content ?? anyOption.value;
114
114
  const result = String(label ?? '');
115
+ console.log('deriveLabel for option:', option, 'returning:', result);
115
116
  return result;
116
117
  }, [getOptionLabel]);
117
118
  const deriveValue = (0, react_1.useCallback)((option) => {
@@ -120,6 +121,7 @@ function CreatableSelect({ options = [], value, onChange, onClick, onValueChange
120
121
  const anyOption = option;
121
122
  const base = anyOption.value ?? anyOption.label ?? anyOption.content;
122
123
  const str = String(base ?? deriveLabel(option));
124
+ console.log('deriveValue for option:', option, 'returning:', str);
123
125
  return str;
124
126
  }, [getOptionValue, deriveLabel]);
125
127
  const [isOpen, setIsOpen] = (0, react_1.useState)(false);
@@ -132,26 +134,51 @@ function CreatableSelect({ options = [], value, onChange, onClick, onValueChange
132
134
  const [isUserInteracting, setIsUserInteracting] = (0, react_1.useState)(false);
133
135
  const containerRef = (0, react_1.useRef)(null);
134
136
  const inputRef = (0, react_1.useRef)(null);
135
- const selectedValuesRef = (0, react_1.useRef)([]);
136
- // Keep ref in sync with state
137
- (0, react_1.useEffect)(() => {
138
- selectedValuesRef.current = selectedValues;
139
- }, [selectedValues]);
140
- // Debug: Log when selectedValues changes
141
- (0, react_1.useEffect)(() => {
142
- console.log('selectedValues changed to:', selectedValues);
143
- }, [selectedValues]);
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
+ // Prevent scroll jumping by maintaining position during keyboard navigation
155
+ const handleKeyboardNavigation = (0, react_1.useCallback)((direction) => {
156
+ saveScrollPosition();
157
+ if (direction === 'up') {
158
+ setHighlightedIndex((prev) => (prev > 0 ? prev - 1 : filteredOptions.length - 1));
159
+ }
160
+ else {
161
+ setHighlightedIndex((prev) => (prev < filteredOptions.length - 1 ? prev + 1 : 0));
162
+ }
163
+ // Restore scroll position after state update
164
+ requestAnimationFrame(() => {
165
+ restoreScrollPosition();
166
+ });
167
+ }, [filteredOptions.length, saveScrollPosition, restoreScrollPosition]);
144
168
  // Sync external value prop with internal selectedValues state
145
169
  (0, react_1.useEffect)(() => {
146
170
  // Don't sync if user is actively interacting with the component
147
171
  if (isUserInteracting) {
172
+ console.log('Skipping sync - user is interacting');
148
173
  return;
149
174
  }
175
+ console.log('useEffect triggered with value:', value, 'isMultiSelect:', isMultiSelect);
150
176
  // Only sync if the external value is actually different from current selectedValues
151
- const currentValues = selectedValuesRef.current;
177
+ const currentValues = selectedValues;
152
178
  const externalValue = value;
153
179
  if (externalValue === null || externalValue === undefined) {
154
180
  if (currentValues.length > 0) {
181
+ console.log('Setting selectedValues to empty array');
155
182
  setSelectedValues([]);
156
183
  }
157
184
  }
@@ -160,12 +187,14 @@ function CreatableSelect({ options = [], value, onChange, onClick, onValueChange
160
187
  const externalArray = externalValue;
161
188
  // Only update if the arrays are actually different
162
189
  if (JSON.stringify(externalArray) !== JSON.stringify(currentValues)) {
190
+ console.log('Setting selectedValues to array:', externalArray);
163
191
  setSelectedValues(externalArray);
164
192
  }
165
193
  }
166
194
  else {
167
195
  const firstItem = externalValue.length > 0 ? [externalValue[0]] : [];
168
196
  if (JSON.stringify(firstItem) !== JSON.stringify(currentValues)) {
197
+ console.log('Setting selectedValues to first item of array:', firstItem);
169
198
  setSelectedValues(firstItem);
170
199
  }
171
200
  }
@@ -173,16 +202,24 @@ function CreatableSelect({ options = [], value, onChange, onClick, onValueChange
173
202
  else {
174
203
  const singleItem = [externalValue];
175
204
  if (JSON.stringify(singleItem) !== JSON.stringify(currentValues)) {
205
+ console.log('Setting selectedValues to single item:', singleItem);
176
206
  setSelectedValues(singleItem);
177
207
  }
178
208
  }
179
- }, [value, isMultiSelect, isUserInteracting]);
209
+ }, [value, isMultiSelect, selectedValues, isUserInteracting]);
180
210
  // Sync options into filtered list when not searching
181
211
  (0, react_1.useEffect)(() => {
182
212
  if (!hasSearched) {
183
213
  setFilteredOptions(options ?? []);
184
214
  }
185
215
  }, [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]);
186
223
  // Reset filtered list when input cleared
187
224
  (0, react_1.useEffect)(() => {
188
225
  if (inputValue === '') {
@@ -207,8 +244,6 @@ function CreatableSelect({ options = [], value, onChange, onClick, onValueChange
207
244
  setInputValue(next);
208
245
  setIsOpen(true);
209
246
  setIsUserInteracting(true);
210
- // Reset the flag after a delay to allow for user interactions
211
- setTimeout(() => setIsUserInteracting(false), 1000);
212
247
  if (onChange)
213
248
  onChange(e);
214
249
  if (fetchOptions && next.trim() !== '') {
@@ -262,7 +297,7 @@ function CreatableSelect({ options = [], value, onChange, onClick, onValueChange
262
297
  case 'ArrowUp':
263
298
  e.preventDefault();
264
299
  if (isOpen) {
265
- setHighlightedIndex((prev) => (prev > 0 ? prev - 1 : filteredOptions.length - 1));
300
+ handleKeyboardNavigation('up');
266
301
  }
267
302
  else {
268
303
  setIsOpen(true);
@@ -271,14 +306,23 @@ function CreatableSelect({ options = [], value, onChange, onClick, onValueChange
271
306
  case 'ArrowDown':
272
307
  e.preventDefault();
273
308
  if (isOpen) {
274
- setHighlightedIndex((prev) => (prev < filteredOptions.length - 1 ? prev + 1 : 0));
309
+ handleKeyboardNavigation('down');
275
310
  }
276
311
  else {
277
312
  setIsOpen(true);
278
313
  }
279
314
  break;
280
315
  }
281
- }, [isDisabled, isOpen, filteredOptions, highlightedIndex, isCreatable, inputValue, deriveLabel]);
316
+ }, [
317
+ isDisabled,
318
+ isOpen,
319
+ filteredOptions,
320
+ highlightedIndex,
321
+ isCreatable,
322
+ inputValue,
323
+ deriveLabel,
324
+ handleKeyboardNavigation,
325
+ ]);
282
326
  const emitSelection = (0, react_1.useCallback)((next) => {
283
327
  // Backward-compatible callback
284
328
  if (onClick) {
@@ -326,15 +370,11 @@ function CreatableSelect({ options = [], value, onChange, onClick, onValueChange
326
370
  emitSelection(next);
327
371
  }, [selectedValues, deriveValue, emitSelection]);
328
372
  const selectOption = (0, react_1.useCallback)((option) => {
329
- console.log('selectOption called with:', option);
330
- console.log('Current selectedValues:', selectedValues);
331
- console.log('isMultiSelect:', isMultiSelect);
332
373
  if (isMultiSelect) {
333
374
  const next = [...selectedValues];
334
375
  if (!next.some((val) => deriveValue(val) === deriveValue(option))) {
335
376
  next.push(option);
336
377
  }
337
- console.log('New selectedValues (multi):', next);
338
378
  setSelectedValues(next);
339
379
  emitSelection(next);
340
380
  // In multi-select mode, keep the input open and clear input for next search
@@ -344,7 +384,6 @@ function CreatableSelect({ options = [], value, onChange, onClick, onValueChange
344
384
  }
345
385
  else {
346
386
  const next = [option];
347
- console.log('New selectedValues (single):', next);
348
387
  setSelectedValues(next);
349
388
  emitSelection(next);
350
389
  setInputValue('');
@@ -362,7 +401,7 @@ function CreatableSelect({ options = [], value, onChange, onClick, onValueChange
362
401
  return (react_1.default.createElement("div", { ref: containerRef, className: `relative w-full ${className} ${styles.container}`, onKeyDown: handleKeyDown },
363
402
  label && react_1.default.createElement("label", { className: "block text-sm font-medium text-gray-700 mb-1" }, label),
364
403
  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 },
365
- react_1.default.createElement("div", { className: "flex-grow flex items-center overflow-x-auto no-scrollbar" },
404
+ react_1.default.createElement("div", { ref: scrollContainerRef, className: "flex-grow flex items-center overflow-x-auto creatable-dropdown-scroll" },
366
405
  react_1.default.createElement("div", { className: "flex items-center gap-2 flex-nowrap" },
367
406
  selectedValues.length > 0 &&
368
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" },
@@ -370,18 +409,17 @@ function CreatableSelect({ options = [], value, onChange, onClick, onValueChange
370
409
  react_1.default.createElement("button", { onClick: (e) => clearValue(e, val), className: styles.clearButton }, renderIcon(icons.clear, styles.icon))))),
371
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() }))),
372
411
  react_1.default.createElement("div", { className: "flex-shrink-0 flex items-center gap-1 ml-2" },
373
- showLoading && renderIcon(icons.loading, `animate-spin ${styles.icon}`),
374
- icons.hasAdd && onAddClick !== undefined && (react_1.default.createElement("button", { onClick: handleAddIconClick, className: styles.clearButton }, renderIcon(icons.hasAdd, styles.icon))),
412
+ iconSrc.hasAdd && (react_1.default.createElement("button", { onClick: handleAddIconClick, className: styles.clearButton }, renderIcon(iconSrc.hasAdd, styles.icon))),
375
413
  styles.separator && react_1.default.createElement("div", { className: styles.separator }),
376
414
  isOpen
377
- ? renderIcon(icons.dropdownOpen, `${styles.icon} ${styles.spinAnimation || ''}`)
378
- : renderIcon(icons.dropdownClosed, styles.icon))),
415
+ ? renderIcon(iconSrc.dropdownOpen, `${styles.icon} ${styles.spinAnimation || ''}`)
416
+ : renderIcon(iconSrc.dropdownClosed, styles.icon))),
379
417
  isOpen && (react_1.default.createElement("div", { className: styles.dropdown }, showLoading ? (react_1.default.createElement("div", { className: styles.loadingState },
380
- renderIcon(icons.loading, `animate-spin mr-2 ${styles.icon}`),
418
+ renderIcon(iconSrc.loading, `animate-spin mr-2 ${styles.icon}`),
381
419
  loadingMessage)) : (react_1.default.createElement(react_1.default.Fragment, null,
382
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) },
383
421
  selectedValues.some((val) => deriveValue(val) === deriveValue(option)) &&
384
- renderIcon(icons.checkmark, 'w-4 h-4 mr-2 text-blue-500'),
422
+ renderIcon(iconSrc.checkmark, 'w-4 h-4 mr-2 text-blue-500'),
385
423
  react_1.default.createElement("span", { className: selectedValues.some((val) => deriveValue(val) === deriveValue(option))
386
424
  ? styles.selectedOption
387
425
  : '' }, deriveLabel(option) || deriveValue(option))))))),
@@ -389,11 +427,11 @@ function CreatableSelect({ options = [], value, onChange, onClick, onValueChange
389
427
  filteredOptions.length === 0 &&
390
428
  inputValue &&
391
429
  hasSearched &&
392
- renderNoResults(noResultsFound, styles.noResults ?? '', icons.create),
430
+ renderNoResults(noResultsFound, styles.noResults ?? '', iconSrc.create),
393
431
  isCreatable &&
394
432
  inputValue &&
395
433
  !options?.some((o) => deriveLabel(o) === inputValue) && (react_1.default.createElement("div", { className: styles.createOption, onClick: handleCreateOption },
396
- renderIcon(icons.create, styles.icon),
434
+ renderIcon(iconSrc.create, styles.icon),
397
435
  react_1.default.createElement("span", null,
398
436
  createOptionMessage,
399
437
  ": ",
@@ -87,6 +87,21 @@ 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: smooth;
94
+ scrollbar-width: none;
95
+ -ms-overflow-style: none;
96
+ }
97
+
98
+ .creatable-dropdown-scroll::-webkit-scrollbar {
99
+ display: none;
100
+ }
101
+
102
+ .creatable-dropdown-scroll:focus-within {
103
+ scroll-behavior: auto;
104
+ }
90
105
  .danger {
91
106
  background-color: #ffffff;
92
107
  color: #f54180;
@@ -67,7 +67,7 @@ export type CreatableSelectProps<T = SelectOption> = {
67
67
  label?: string;
68
68
  iconSrc?: IconSourceProps;
69
69
  fetchOptions?: (inputValue: string) => Promise<T[]>;
70
- onAddClick?: (e: boolean) => void;
70
+ onAddClick: (e: boolean) => void;
71
71
  isMultiSelect?: boolean;
72
72
  getOptionLabel?: OptionLabelGetter<T>;
73
73
  getOptionValue?: OptionValueGetter<T>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hi-tm",
3
- "version": "0.6.5",
3
+ "version": "0.6.7",
4
4
  "styles": "dist/styles/globals.css",
5
5
  "main": "dist/index.js",
6
6
  "exports": {