hi-tm 0.8.4 → 0.8.6

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.
@@ -50,8 +50,9 @@ const defaultStyles = {
50
50
  noResults: 'py-4 px-3 text-gray-500 flex flex-col items-center justify-center gap-2',
51
51
  clearButton: 'p-1 rounded-full hover:bg-gray-100',
52
52
  icon: 'w-4 h-4 text-gray-400',
53
- separator: 'h-4 border-l mx-2',
53
+ separator: 'h-4 border-l mx-2', // Style for the vertical line separator
54
54
  };
55
+ // Helper function to render different icon types
55
56
  const renderIcon = (icon, className = '') => {
56
57
  if (typeof icon === 'string') {
57
58
  return react_1.default.createElement("img", { src: icon || '/placeholder.svg', alt: "", className: className });
@@ -64,6 +65,7 @@ const renderIcon = (icon, className = '') => {
64
65
  return icon;
65
66
  }
66
67
  };
68
+ // Helper function to render the no results message
67
69
  const renderNoResults = (noResults, className, searchIcon) => {
68
70
  if (!noResults) {
69
71
  return (react_1.default.createElement("div", { className: className },
@@ -86,6 +88,7 @@ const renderNoResults = (noResults, className, searchIcon) => {
86
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, }) {
91
+ // Default icons
89
92
  const defaultIcons = {
90
93
  dropdownOpen: '▼',
91
94
  dropdownClosed: '▶',
@@ -96,47 +99,28 @@ function CreatableSelect({ options = [], value, onChange, onClick, onValueChange
96
99
  xmark: '✕',
97
100
  hasAdd: '➕',
98
101
  };
102
+ // Merge provided icons with defaults
99
103
  const icons = { ...defaultIcons, ...iconSrc };
100
104
  const styles = (0, react_1.useMemo)(() => ({
101
105
  ...defaultStyles,
102
106
  ...tailwindStyle,
103
107
  }), [tailwindStyle]);
108
+ // Derivers for label/value to support arbitrary option shapes
104
109
  const deriveLabel = (0, react_1.useCallback)((option) => {
105
110
  if (getOptionLabel)
106
111
  return getOptionLabel(option);
107
- // Handle primitive types directly
108
- const optionType = typeof option;
109
- if (option === null ||
110
- optionType === 'string' ||
111
- optionType === 'number' ||
112
- optionType === 'boolean') {
113
- return String(option ?? '');
114
- }
115
- // Handle objects with common fields
116
112
  const anyOption = option;
117
- const labelCandidate = anyOption.label ??
118
- anyOption.content ??
119
- anyOption.value ??
120
- anyOption.name ??
121
- anyOption.title ??
122
- anyOption.text;
123
- return String(labelCandidate ?? '');
113
+ const label = anyOption.label ?? anyOption.content ?? anyOption.value;
114
+ const result = String(label ?? '');
115
+ return result;
124
116
  }, [getOptionLabel]);
125
117
  const deriveValue = (0, react_1.useCallback)((option) => {
126
118
  if (getOptionValue)
127
119
  return getOptionValue(option);
128
- // Handle primitive types directly
129
- const optionType = typeof option;
130
- if (option === null ||
131
- optionType === 'string' ||
132
- optionType === 'number' ||
133
- optionType === 'boolean') {
134
- return String(option ?? '');
135
- }
136
- // Handle objects with common fields
137
120
  const anyOption = option;
138
- const base = anyOption.value ?? anyOption.label ?? anyOption.content ?? anyOption.id ?? anyOption.key;
139
- return String(base ?? deriveLabel(option));
121
+ const base = anyOption.value ?? anyOption.label ?? anyOption.content;
122
+ const str = String(base ?? deriveLabel(option));
123
+ return str;
140
124
  }, [getOptionValue, deriveLabel]);
141
125
  const [isOpen, setIsOpen] = (0, react_1.useState)(false);
142
126
  const [inputValue, setInputValue] = (0, react_1.useState)('');
@@ -147,36 +131,34 @@ function CreatableSelect({ options = [], value, onChange, onClick, onValueChange
147
131
  const [selectedValues, setSelectedValues] = (0, react_1.useState)([]);
148
132
  const containerRef = (0, react_1.useRef)(null);
149
133
  const inputRef = (0, react_1.useRef)(null);
134
+ // Sync external value prop with internal selectedValues state
150
135
  (0, react_1.useEffect)(() => {
151
136
  if (value === undefined || value === null) {
152
137
  setSelectedValues([]);
153
- if (!isMultiSelect) {
154
- setInputValue('');
155
- }
156
138
  return;
157
139
  }
158
140
  const externalValues = Array.isArray(value) ? value : [value];
141
+ // Only update if values are actually different
159
142
  const currentValuesString = JSON.stringify(selectedValues.map(deriveValue));
160
143
  const externalValuesString = JSON.stringify(externalValues.map(deriveValue));
161
144
  if (currentValuesString !== externalValuesString) {
162
145
  setSelectedValues(externalValues);
163
- if (!isMultiSelect) {
164
- const labelFromValue = externalValues.length > 0 ? deriveLabel(externalValues[0]) : '';
165
- setInputValue(String(labelFromValue ?? ''));
166
- }
167
146
  }
168
- }, [value, deriveValue, deriveLabel, isMultiSelect]);
147
+ }, [value, deriveValue]);
148
+ // Sync options into filtered list when not searching
169
149
  (0, react_1.useEffect)(() => {
170
150
  if (!hasSearched) {
171
151
  setFilteredOptions(options ?? []);
172
152
  }
173
153
  }, [options, hasSearched]);
154
+ // Reset filtered list when input cleared
174
155
  (0, react_1.useEffect)(() => {
175
156
  if (inputValue === '') {
176
157
  setFilteredOptions(options ?? []);
177
158
  setHasSearched(false);
178
159
  }
179
160
  }, [options, inputValue]);
161
+ // Close on outside click
180
162
  (0, react_1.useEffect)(() => {
181
163
  const handler = (e) => {
182
164
  if (containerRef.current && !containerRef.current.contains(e.target)) {
@@ -263,6 +245,7 @@ function CreatableSelect({ options = [], value, onChange, onClick, onValueChange
263
245
  }
264
246
  }, [isDisabled, isOpen, filteredOptions, highlightedIndex, isCreatable, inputValue, deriveLabel]);
265
247
  const emitSelection = (0, react_1.useCallback)((next) => {
248
+ // Backward-compatible callback
266
249
  if (onClick) {
267
250
  onClick(isMultiSelect ? next ?? [] : next ? next[0] : null);
268
251
  }
@@ -299,7 +282,7 @@ function CreatableSelect({ options = [], value, onChange, onClick, onValueChange
299
282
  if (!isDisabled) {
300
283
  setIsOpen((prev) => !prev);
301
284
  if (!isOpen && inputRef.current) {
302
- inputRef.current.focus();
285
+ inputRef.current.focus({ preventScroll: true });
303
286
  }
304
287
  }
305
288
  }, [isDisabled, isOpen]);
@@ -318,14 +301,16 @@ function CreatableSelect({ options = [], value, onChange, onClick, onValueChange
318
301
  }
319
302
  setSelectedValues(next);
320
303
  emitSelection(next);
304
+ // In multi-select mode, keep the input open and clear input for next search
321
305
  setInputValue('');
322
306
  setHasSearched(false);
307
+ // Don't close dropdown in multi-select mode
323
308
  }
324
309
  else {
325
310
  const next = [option];
326
311
  setSelectedValues(next);
327
312
  emitSelection(next);
328
- setInputValue(deriveLabel(option));
313
+ setInputValue('');
329
314
  setIsOpen(false);
330
315
  setHasSearched(false);
331
316
  }
@@ -338,28 +323,30 @@ function CreatableSelect({ options = [], value, onChange, onClick, onValueChange
338
323
  onAddClick(true);
339
324
  }
340
325
  }, [onAddClick]);
341
- const handleInputAreaClick = (0, react_1.useCallback)((e) => {
326
+ const handleTriggerClick = (0, react_1.useCallback)((e) => {
342
327
  e.preventDefault();
343
328
  if (!isDisabled) {
344
- setIsOpen(true);
345
- if (inputRef.current) {
346
- inputRef.current.focus();
329
+ setIsOpen((prev) => !prev);
330
+ if (!isOpen && inputRef.current) {
331
+ inputRef.current.focus({ preventScroll: true });
347
332
  }
348
333
  }
349
- }, [isDisabled]);
334
+ }, [isDisabled, isOpen]);
350
335
  return (react_1.default.createElement("div", { ref: containerRef, className: `relative w-full ${className} ${styles.container}`, onKeyDown: handleKeyDown },
351
336
  label && react_1.default.createElement("label", { className: "block text-sm font-medium text-gray-700 mb-1" }, label),
352
- 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'}` },
353
- react_1.default.createElement("div", { className: "flex-grow flex items-center min-w-0", onClick: handleInputAreaClick }, isMultiSelect && selectedValues.length > 0 ? (react_1.default.createElement("div", { className: "flex items-center gap-2 w-full min-w-0" },
354
- react_1.default.createElement("div", { className: "flex items-center gap-1 overflow-x-auto no-scrollbar" }, 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" },
355
- react_1.default.createElement("span", { className: "truncate max-w-[100px] text-sm" }, deriveLabel(val)),
356
- react_1.default.createElement("button", { type: "button", onClick: (e) => clearValue(e, val), className: styles.clearButton }, renderIcon(icons.clear, styles.icon)))))),
357
- react_1.default.createElement("input", { ref: inputRef, type: "text", className: `${styles.input} ${isDisabled ? 'bg-gray-100' : ''} min-w-[60px] flex-shrink-0`, placeholder: "", value: inputValue, onChange: handleInputChange, disabled: isDisabled, onClick: (e) => e.stopPropagation() }))) : (react_1.default.createElement("input", { ref: inputRef, type: "text", className: `${styles.input} ${isDisabled ? 'bg-gray-100' : ''}`, placeholder: placeholder, value: inputValue, onChange: handleInputChange, disabled: isDisabled, onClick: (e) => e.stopPropagation() }))),
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: handleTriggerClick },
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", { type: "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() }))),
358
345
  react_1.default.createElement("div", { className: "flex-shrink-0 flex items-center gap-1 ml-2" },
359
346
  iconSrc.hasAdd && (react_1.default.createElement("button", { type: "button", onClick: handleAddIconClick, className: styles.clearButton }, renderIcon(iconSrc.hasAdd, styles.icon))),
360
347
  styles.separator && react_1.default.createElement("div", { className: styles.separator }),
361
- react_1.default.createElement("button", { type: "button", onClick: toggleDropdown, "aria-label": isOpen ? 'Close dropdown' : 'Open dropdown' }, isOpen
362
- ? renderIcon(iconSrc.dropdownOpen, styles.icon)
348
+ react_1.default.createElement("button", { type: "button", onClick: toggleDropdown }, isOpen
349
+ ? renderIcon(iconSrc.dropdownOpen, `${styles.icon} ${styles.spinAnimation || ''}`)
363
350
  : renderIcon(iconSrc.dropdownClosed, styles.icon)))),
364
351
  isOpen && (react_1.default.createElement("div", { className: styles.dropdown }, showLoading ? (react_1.default.createElement("div", { className: styles.loadingState },
365
352
  renderIcon(iconSrc.loading, `animate-spin mr-2 ${styles.icon}`),
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hi-tm",
3
- "version": "0.8.4",
3
+ "version": "0.8.6",
4
4
  "styles": "dist/styles/globals.css",
5
5
  "main": "dist/index.js",
6
6
  "exports": {