hi-tm 0.8.3 → 0.8.5

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,46 +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
- return String(labelCandidate ?? '');
113
+ const label = anyOption.label ?? anyOption.content ?? anyOption.value;
114
+ const result = String(label ?? '');
115
+ return result;
123
116
  }, [getOptionLabel]);
124
117
  const deriveValue = (0, react_1.useCallback)((option) => {
125
118
  if (getOptionValue)
126
119
  return getOptionValue(option);
127
- // Handle primitive types directly
128
- const optionType = typeof option;
129
- if (option === null ||
130
- optionType === 'string' ||
131
- optionType === 'number' ||
132
- optionType === 'boolean') {
133
- return String(option ?? '');
134
- }
135
- // Handle objects with common fields
136
120
  const anyOption = option;
137
- const base = anyOption.value ?? anyOption.label ?? anyOption.content ?? anyOption.id ?? anyOption.key;
138
- return String(base ?? deriveLabel(option));
121
+ const base = anyOption.value ?? anyOption.label ?? anyOption.content;
122
+ const str = String(base ?? deriveLabel(option));
123
+ return str;
139
124
  }, [getOptionValue, deriveLabel]);
140
125
  const [isOpen, setIsOpen] = (0, react_1.useState)(false);
141
126
  const [inputValue, setInputValue] = (0, react_1.useState)('');
@@ -146,29 +131,34 @@ function CreatableSelect({ options = [], value, onChange, onClick, onValueChange
146
131
  const [selectedValues, setSelectedValues] = (0, react_1.useState)([]);
147
132
  const containerRef = (0, react_1.useRef)(null);
148
133
  const inputRef = (0, react_1.useRef)(null);
134
+ // Sync external value prop with internal selectedValues state
149
135
  (0, react_1.useEffect)(() => {
150
136
  if (value === undefined || value === null) {
151
137
  setSelectedValues([]);
152
138
  return;
153
139
  }
154
140
  const externalValues = Array.isArray(value) ? value : [value];
141
+ // Only update if values are actually different
155
142
  const currentValuesString = JSON.stringify(selectedValues.map(deriveValue));
156
143
  const externalValuesString = JSON.stringify(externalValues.map(deriveValue));
157
144
  if (currentValuesString !== externalValuesString) {
158
145
  setSelectedValues(externalValues);
159
146
  }
160
147
  }, [value, deriveValue]);
148
+ // Sync options into filtered list when not searching
161
149
  (0, react_1.useEffect)(() => {
162
150
  if (!hasSearched) {
163
151
  setFilteredOptions(options ?? []);
164
152
  }
165
153
  }, [options, hasSearched]);
154
+ // Reset filtered list when input cleared
166
155
  (0, react_1.useEffect)(() => {
167
156
  if (inputValue === '') {
168
157
  setFilteredOptions(options ?? []);
169
158
  setHasSearched(false);
170
159
  }
171
160
  }, [options, inputValue]);
161
+ // Close on outside click
172
162
  (0, react_1.useEffect)(() => {
173
163
  const handler = (e) => {
174
164
  if (containerRef.current && !containerRef.current.contains(e.target)) {
@@ -255,6 +245,7 @@ function CreatableSelect({ options = [], value, onChange, onClick, onValueChange
255
245
  }
256
246
  }, [isDisabled, isOpen, filteredOptions, highlightedIndex, isCreatable, inputValue, deriveLabel]);
257
247
  const emitSelection = (0, react_1.useCallback)((next) => {
248
+ // Backward-compatible callback
258
249
  if (onClick) {
259
250
  onClick(isMultiSelect ? next ?? [] : next ? next[0] : null);
260
251
  }
@@ -310,8 +301,10 @@ function CreatableSelect({ options = [], value, onChange, onClick, onValueChange
310
301
  }
311
302
  setSelectedValues(next);
312
303
  emitSelection(next);
304
+ // In multi-select mode, keep the input open and clear input for next search
313
305
  setInputValue('');
314
306
  setHasSearched(false);
307
+ // Don't close dropdown in multi-select mode
315
308
  }
316
309
  else {
317
310
  const next = [option];
@@ -330,28 +323,30 @@ function CreatableSelect({ options = [], value, onChange, onClick, onValueChange
330
323
  onAddClick(true);
331
324
  }
332
325
  }, [onAddClick]);
333
- const handleInputAreaClick = (0, react_1.useCallback)((e) => {
326
+ const handleTriggerClick = (0, react_1.useCallback)((e) => {
334
327
  e.preventDefault();
335
328
  if (!isDisabled) {
336
- setIsOpen(true);
337
- if (inputRef.current) {
329
+ setIsOpen((prev) => !prev);
330
+ if (!isOpen && inputRef.current) {
338
331
  inputRef.current.focus();
339
332
  }
340
333
  }
341
- }, [isDisabled]);
334
+ }, [isDisabled, isOpen]);
342
335
  return (react_1.default.createElement("div", { ref: containerRef, className: `relative w-full ${className} ${styles.container}`, onKeyDown: handleKeyDown },
343
336
  label && react_1.default.createElement("label", { className: "block text-sm font-medium text-gray-700 mb-1" }, label),
344
- 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'}` },
345
- react_1.default.createElement("div", { className: "flex-grow flex items-center min-w-0", onClick: handleInputAreaClick }, selectedValues.length > 0 ? (react_1.default.createElement("div", { className: "flex items-center gap-2 w-full min-w-0" },
346
- 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" },
347
- react_1.default.createElement("span", { className: "truncate max-w-[100px] text-sm" }, deriveLabel(val)),
348
- react_1.default.createElement("button", { type: "button", onClick: (e) => clearValue(e, val), className: styles.clearButton }, renderIcon(icons.clear, styles.icon)))))),
349
- 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() }))),
350
345
  react_1.default.createElement("div", { className: "flex-shrink-0 flex items-center gap-1 ml-2" },
351
346
  iconSrc.hasAdd && (react_1.default.createElement("button", { type: "button", onClick: handleAddIconClick, className: styles.clearButton }, renderIcon(iconSrc.hasAdd, styles.icon))),
352
347
  styles.separator && react_1.default.createElement("div", { className: styles.separator }),
353
- react_1.default.createElement("button", { type: "button", onClick: toggleDropdown, "aria-label": isOpen ? 'Close dropdown' : 'Open dropdown' }, isOpen
354
- ? renderIcon(iconSrc.dropdownOpen, styles.icon)
348
+ react_1.default.createElement("button", { type: "button", onClick: toggleDropdown }, isOpen
349
+ ? renderIcon(iconSrc.dropdownOpen, `${styles.icon} ${styles.spinAnimation || ''}`)
355
350
  : renderIcon(iconSrc.dropdownClosed, styles.icon)))),
356
351
  isOpen && (react_1.default.createElement("div", { className: styles.dropdown }, showLoading ? (react_1.default.createElement("div", { className: styles.loadingState },
357
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.3",
3
+ "version": "0.8.5",
4
4
  "styles": "dist/styles/globals.css",
5
5
  "main": "dist/index.js",
6
6
  "exports": {