hi-tm 0.9.5 → 0.9.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.
@@ -1,3 +1,3 @@
1
- import React from 'react';
2
- import type { SelectOption, CreatableSelectProps } from '../../types/DropDown/CreatableDropDown';
1
+ import React from "react";
2
+ import type { SelectOption, CreatableSelectProps } from "../../types/DropDown/CreatableDropDown";
3
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;
@@ -37,29 +37,29 @@ exports.default = CreatableSelect;
37
37
  const react_1 = __importStar(require("react"));
38
38
  // Default Tailwind styles
39
39
  const defaultStyles = {
40
- container: '',
41
- trigger: 'flex items-center border rounded-md px-3 py-2 bg-white cursor-pointer',
42
- input: 'w-full outline-none',
43
- dropdown: 'absolute z-10 w-full mt-1 bg-white border border-gray-300 rounded-md shadow-lg max-h-60 overflow-y-auto',
44
- option: 'py-2 px-3 cursor-pointer flex items-center',
45
- selectedOption: 'font-medium',
46
- highlightedOption: 'bg-blue-100',
47
- createOption: 'flex items-center gap-2 py-2 px-3 cursor-pointer hover:bg-blue-50',
48
- loadingState: 'flex items-center justify-center py-2 text-gray-500',
49
- noOptions: 'py-2 px-3 text-gray-500',
50
- noResults: 'py-4 px-3 text-gray-500 flex flex-col items-center justify-center gap-2',
51
- clearButton: 'p-1 rounded-full hover:bg-gray-100',
52
- icon: 'w-4 h-4 text-gray-400',
53
- separator: 'h-4 border-l mx-2', // Style for the vertical line separator
40
+ container: "",
41
+ trigger: "flex items-center border rounded-md px-3 py-2 bg-white cursor-pointer",
42
+ input: "w-full outline-none",
43
+ dropdown: "absolute z-10 w-full mt-1 bg-white border border-gray-300 rounded-md shadow-lg max-h-60 overflow-y-auto",
44
+ option: "py-2 px-3 cursor-pointer flex items-center",
45
+ selectedOption: "font-medium",
46
+ highlightedOption: "bg-blue-100",
47
+ createOption: "flex items-center gap-2 py-2 px-3 cursor-pointer hover:bg-blue-50",
48
+ loadingState: "flex items-center justify-center py-2 text-gray-500",
49
+ noOptions: "py-2 px-3 text-gray-500",
50
+ noResults: "py-4 px-3 text-gray-500 flex flex-col items-center justify-center gap-2",
51
+ clearButton: "p-1 rounded-full hover:bg-gray-100",
52
+ icon: "w-4 h-4 text-gray-400",
53
+ separator: "h-4 border-l mx-2", // Style for the vertical line separator
54
54
  };
55
55
  // Helper function to render different icon types
56
- const renderIcon = (icon, className = '') => {
57
- if (typeof icon === 'string') {
58
- return react_1.default.createElement("img", { src: icon || '/placeholder.svg', alt: "", className: className });
56
+ const renderIcon = (icon, className = "") => {
57
+ if (typeof icon === "string") {
58
+ return (react_1.default.createElement("img", { src: icon || "/placeholder.svg", alt: "", className: className }));
59
59
  }
60
- else if (typeof icon === 'object' && 'src' in icon) {
60
+ else if (typeof icon === "object" && "src" in icon) {
61
61
  const img = icon;
62
- return react_1.default.createElement("img", { src: img.src || '/placeholder.svg', alt: img.alt || '', className: className });
62
+ return (react_1.default.createElement("img", { src: img.src || "/placeholder.svg", alt: img.alt || "", className: className }));
63
63
  }
64
64
  else {
65
65
  return icon;
@@ -69,35 +69,37 @@ const renderIcon = (icon, className = '') => {
69
69
  const renderNoResults = (noResults, className, searchIcon) => {
70
70
  if (!noResults) {
71
71
  return (react_1.default.createElement("div", { className: className },
72
- renderIcon(searchIcon, 'w-6 h-6 mb-1'),
72
+ renderIcon(searchIcon, "w-6 h-6 mb-1"),
73
73
  react_1.default.createElement("span", null, "\u0645\u0648\u0631\u062F\u06CC \u06CC\u0627\u0641\u062A \u0646\u0634\u062F")));
74
74
  }
75
- if (typeof noResults === 'string') {
75
+ if (typeof noResults === "string") {
76
76
  return react_1.default.createElement("div", { className: className }, noResults);
77
77
  }
78
- else if (typeof noResults === 'object' && noResults && 'text' in noResults) {
78
+ else if (typeof noResults === "object" &&
79
+ noResults &&
80
+ "text" in noResults) {
79
81
  const withText = noResults;
80
82
  return (react_1.default.createElement("div", { className: className },
81
83
  withText.icon
82
- ? renderIcon(withText.icon, 'w-6 h-6 mb-1')
83
- : renderIcon(searchIcon, 'w-6 h-6 mb-1'),
84
+ ? renderIcon(withText.icon, "w-6 h-6 mb-1")
85
+ : renderIcon(searchIcon, "w-6 h-6 mb-1"),
84
86
  react_1.default.createElement("span", null, withText.text)));
85
87
  }
86
88
  else {
87
89
  return noResults;
88
90
  }
89
91
  };
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, }) {
92
+ 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
93
  // Default icons
92
94
  const defaultIcons = {
93
- dropdownOpen: '▼',
94
- dropdownClosed: '▶',
95
- checkmark: '✓',
96
- loading: '⏳',
97
- clear: '✕',
98
- create: '➕',
99
- xmark: '✕',
100
- hasAdd: '➕',
95
+ dropdownOpen: "▼",
96
+ dropdownClosed: "▶",
97
+ checkmark: "✓",
98
+ loading: "⏳",
99
+ clear: "✕",
100
+ create: "➕",
101
+ xmark: "✕",
102
+ hasAdd: "➕",
101
103
  };
102
104
  // Merge provided icons with defaults
103
105
  const icons = { ...defaultIcons, ...iconSrc };
@@ -111,7 +113,7 @@ function CreatableSelect({ options = [], value, onChange, onClick, onValueChange
111
113
  return getOptionLabel(option);
112
114
  const anyOption = option;
113
115
  const label = anyOption.label ?? anyOption.content ?? anyOption.value;
114
- const result = String(label ?? '');
116
+ const result = String(label ?? "");
115
117
  return result;
116
118
  }, [getOptionLabel]);
117
119
  const deriveValue = (0, react_1.useCallback)((option) => {
@@ -123,12 +125,13 @@ function CreatableSelect({ options = [], value, onChange, onClick, onValueChange
123
125
  return str;
124
126
  }, [getOptionValue, deriveLabel]);
125
127
  const [isOpen, setIsOpen] = (0, react_1.useState)(false);
126
- const [inputValue, setInputValue] = (0, react_1.useState)('');
128
+ const [inputValue, setInputValue] = (0, react_1.useState)("");
127
129
  const [filteredOptions, setFilteredOptions] = (0, react_1.useState)(options ?? []);
128
130
  const [highlightedIndex, setHighlightedIndex] = (0, react_1.useState)(0);
129
131
  const [internalLoading, setInternalLoading] = (0, react_1.useState)(false);
130
132
  const [hasSearched, setHasSearched] = (0, react_1.useState)(false);
131
133
  const [selectedValues, setSelectedValues] = (0, react_1.useState)([]);
134
+ const [onFocus, setOnFocus] = (0, react_1.useState)(true);
132
135
  const containerRef = (0, react_1.useRef)(null);
133
136
  const inputRef = (0, react_1.useRef)(null);
134
137
  // Sync external value (ids or option objects) with internal selectedValues state
@@ -145,7 +148,7 @@ function CreatableSelect({ options = [], value, onChange, onClick, onValueChange
145
148
  const external = Array.isArray(value) ? value : [value];
146
149
  const desiredKeys = external.map((v) => {
147
150
  const t = typeof v;
148
- if (t === 'string' || t === 'number')
151
+ if (t === "string" || t === "number")
149
152
  return String(v);
150
153
  return deriveValue(v);
151
154
  });
@@ -159,7 +162,7 @@ function CreatableSelect({ options = [], value, onChange, onClick, onValueChange
159
162
  }
160
163
  else {
161
164
  const original = external[idx];
162
- if (typeof original === 'object' && original !== null) {
165
+ if (typeof original === "object" && original !== null) {
163
166
  orderedMatched.push(original);
164
167
  }
165
168
  }
@@ -187,7 +190,7 @@ function CreatableSelect({ options = [], value, onChange, onClick, onValueChange
187
190
  }, [options, hasSearched]);
188
191
  // Reset filtered list when input cleared
189
192
  (0, react_1.useEffect)(() => {
190
- if (inputValue === '') {
193
+ if (inputValue === "") {
191
194
  setFilteredOptions(options ?? []);
192
195
  setHasSearched(false);
193
196
  }
@@ -195,22 +198,28 @@ function CreatableSelect({ options = [], value, onChange, onClick, onValueChange
195
198
  // Close on outside click
196
199
  (0, react_1.useEffect)(() => {
197
200
  const handler = (e) => {
198
- if (containerRef.current && !containerRef.current.contains(e.target)) {
201
+ if (containerRef.current &&
202
+ !containerRef.current.contains(e.target)) {
199
203
  setIsOpen(false);
200
204
  }
201
205
  };
202
- document.addEventListener('click', handler);
206
+ document.addEventListener("click", handler);
203
207
  return () => {
204
- document.removeEventListener('click', handler);
208
+ document.removeEventListener("click", handler);
205
209
  };
206
210
  }, []);
211
+ (0, react_1.useEffect)(() => {
212
+ if (onFocus === true) {
213
+ setIsOpen(true);
214
+ }
215
+ }, [onFocus]);
207
216
  const handleInputChange = (0, react_1.useCallback)(async (e) => {
208
217
  const next = e.target.value;
209
218
  setInputValue(next);
210
219
  setIsOpen(true);
211
220
  if (onChange)
212
221
  onChange(e);
213
- if (fetchOptions && next.trim() !== '') {
222
+ if (fetchOptions && next.trim() !== "") {
214
223
  setInternalLoading(true);
215
224
  setHasSearched(true);
216
225
  try {
@@ -223,7 +232,7 @@ function CreatableSelect({ options = [], value, onChange, onClick, onValueChange
223
232
  setInternalLoading(false);
224
233
  }
225
234
  }
226
- else if (next.trim() === '') {
235
+ else if (next.trim() === "") {
227
236
  setFilteredOptions(options ?? []);
228
237
  setHasSearched(false);
229
238
  }
@@ -238,10 +247,11 @@ function CreatableSelect({ options = [], value, onChange, onClick, onValueChange
238
247
  if (isDisabled)
239
248
  return;
240
249
  switch (e.key) {
241
- case 'Enter':
250
+ case "Enter":
242
251
  e.preventDefault();
243
252
  if (isOpen) {
244
- if (filteredOptions.length > 0 && highlightedIndex < filteredOptions.length) {
253
+ if (filteredOptions.length > 0 &&
254
+ highlightedIndex < filteredOptions.length) {
245
255
  selectOption(filteredOptions[highlightedIndex]);
246
256
  }
247
257
  else if (isCreatable &&
@@ -255,29 +265,37 @@ function CreatableSelect({ options = [], value, onChange, onClick, onValueChange
255
265
  setIsOpen(true);
256
266
  }
257
267
  break;
258
- case 'Escape':
268
+ case "Escape":
259
269
  setIsOpen(false);
260
270
  break;
261
- case 'ArrowUp':
271
+ case "ArrowUp":
262
272
  e.preventDefault();
263
273
  if (isOpen) {
264
- setHighlightedIndex((prev) => (prev > 0 ? prev - 1 : filteredOptions.length - 1));
274
+ setHighlightedIndex((prev) => prev > 0 ? prev - 1 : filteredOptions.length - 1);
265
275
  }
266
276
  else {
267
277
  setIsOpen(true);
268
278
  }
269
279
  break;
270
- case 'ArrowDown':
280
+ case "ArrowDown":
271
281
  e.preventDefault();
272
282
  if (isOpen) {
273
- setHighlightedIndex((prev) => (prev < filteredOptions.length - 1 ? prev + 1 : 0));
283
+ setHighlightedIndex((prev) => prev < filteredOptions.length - 1 ? prev + 1 : 0);
274
284
  }
275
285
  else {
276
286
  setIsOpen(true);
277
287
  }
278
288
  break;
279
289
  }
280
- }, [isDisabled, isOpen, filteredOptions, highlightedIndex, isCreatable, inputValue, deriveLabel]);
290
+ }, [
291
+ isDisabled,
292
+ isOpen,
293
+ filteredOptions,
294
+ highlightedIndex,
295
+ isCreatable,
296
+ inputValue,
297
+ deriveLabel,
298
+ ]);
281
299
  const emitSelection = (0, react_1.useCallback)((next) => {
282
300
  // Backward-compatible callback
283
301
  if (onClick) {
@@ -292,16 +310,18 @@ function CreatableSelect({ options = [], value, onChange, onClick, onValueChange
292
310
  return;
293
311
  setInternalLoading(true);
294
312
  try {
295
- const created = onCreateOption ? await onCreateOption(inputValue) : undefined;
313
+ const created = onCreateOption
314
+ ? await onCreateOption(inputValue)
315
+ : undefined;
296
316
  const safeInputValue = String(inputValue);
297
317
  const fallback = {
298
318
  label: safeInputValue,
299
319
  content: safeInputValue,
300
- value: safeInputValue.replace(/\s+/g, '-'),
320
+ value: safeInputValue.replace(/\s+/g, "-"),
301
321
  };
302
322
  const optionToAdd = created ?? fallback;
303
323
  selectOption(optionToAdd);
304
- setInputValue('');
324
+ setInputValue("");
305
325
  setHasSearched(false);
306
326
  }
307
327
  catch {
@@ -336,7 +356,7 @@ function CreatableSelect({ options = [], value, onChange, onClick, onValueChange
336
356
  setSelectedValues(next);
337
357
  emitSelection(next);
338
358
  // In multi-select mode, keep the input open and clear input for next search
339
- setInputValue('');
359
+ setInputValue("");
340
360
  setHasSearched(false);
341
361
  // Don't close dropdown in multi-select mode
342
362
  }
@@ -344,7 +364,7 @@ function CreatableSelect({ options = [], value, onChange, onClick, onValueChange
344
364
  const next = [option];
345
365
  setSelectedValues(next);
346
366
  emitSelection(next);
347
- setInputValue('');
367
+ setInputValue("");
348
368
  setIsOpen(false);
349
369
  setHasSearched(false);
350
370
  }
@@ -367,35 +387,39 @@ function CreatableSelect({ options = [], value, onChange, onClick, onValueChange
367
387
  }
368
388
  }, [isDisabled, isOpen]);
369
389
  return (react_1.default.createElement("div", { ref: containerRef, className: `relative w-full ${className} ${styles.container}`, onKeyDown: handleKeyDown },
370
- label && react_1.default.createElement("label", { className: "block text-sm font-medium text-gray-700 mb-1" }, label),
371
- 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 },
390
+ label && (react_1.default.createElement("label", { className: "block text-sm font-medium text-gray-700 mb-1" }, label)),
391
+ 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 },
372
392
  react_1.default.createElement("div", { className: "flex-grow flex items-center overflow-x-auto no-scrollbar" },
373
393
  react_1.default.createElement("div", { className: "flex items-center gap-2 flex-nowrap" },
374
394
  selectedValues.length > 0 &&
375
395
  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" },
376
396
  react_1.default.createElement("span", { className: "truncate" }, deriveLabel(val)),
377
397
  react_1.default.createElement("button", { type: "button", onClick: (e) => clearValue(e, val), className: styles.clearButton }, renderIcon(icons.clear, styles.icon))))),
378
- 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() }))),
398
+ 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(), onFocus: () => {
399
+ setOnFocus(true);
400
+ } }))),
379
401
  react_1.default.createElement("div", { className: "flex-shrink-0 flex items-center gap-1 ml-2" },
380
402
  iconSrc.hasAdd && (react_1.default.createElement("button", { type: "button", onClick: handleAddIconClick, className: styles.clearButton }, renderIcon(iconSrc.hasAdd, styles.icon))),
381
403
  styles.separator && react_1.default.createElement("div", { className: styles.separator }),
382
404
  react_1.default.createElement("button", { type: "button", onClick: toggleDropdown }, isOpen
383
- ? renderIcon(iconSrc.dropdownOpen, `${styles.icon} ${styles.spinAnimation || ''}`)
405
+ ? renderIcon(iconSrc.dropdownOpen, `${styles.icon} ${styles.spinAnimation || ""}`)
384
406
  : renderIcon(iconSrc.dropdownClosed, styles.icon)))),
385
407
  isOpen && (react_1.default.createElement("div", { className: styles.dropdown }, showLoading ? (react_1.default.createElement("div", { className: styles.loadingState },
386
408
  renderIcon(iconSrc.loading, `animate-spin mr-2 ${styles.icon}`),
387
409
  loadingMessage)) : (react_1.default.createElement(react_1.default.Fragment, null,
388
- 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) },
410
+ 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
411
+ ? styles.highlightedOption
412
+ : "hover:bg-gray-50"}`, onClick: () => selectOption(option), onMouseEnter: () => setHighlightedIndex(index) },
389
413
  selectedValues.some((val) => deriveValue(val) === deriveValue(option)) &&
390
- renderIcon(iconSrc.checkmark, 'w-4 h-4 mr-2 text-blue-500'),
414
+ renderIcon(iconSrc.checkmark, "w-4 h-4 mr-2 text-blue-500"),
391
415
  react_1.default.createElement("span", { className: selectedValues.some((val) => deriveValue(val) === deriveValue(option))
392
416
  ? styles.selectedOption
393
- : '' }, deriveLabel(option) || deriveValue(option))))))),
417
+ : "" }, deriveLabel(option) || deriveValue(option))))))),
394
418
  filteredOptions.length === 0 && !inputValue && (react_1.default.createElement("div", { className: styles.noOptions }, noOptionsMessage)),
395
419
  filteredOptions.length === 0 &&
396
420
  inputValue &&
397
421
  hasSearched &&
398
- renderNoResults(noResultsFound, styles.noResults ?? '', iconSrc.create),
422
+ renderNoResults(noResultsFound, styles.noResults ?? "", iconSrc.create),
399
423
  isCreatable &&
400
424
  inputValue &&
401
425
  !options?.some((o) => deriveLabel(o) === inputValue) && (react_1.default.createElement("div", { className: styles.createOption, onClick: handleCreateOption },
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hi-tm",
3
- "version": "0.9.5",
3
+ "version": "0.9.6",
4
4
  "styles": "dist/styles/globals.css",
5
5
  "main": "dist/index.js",
6
6
  "exports": {