hi-tm 0.3.7 → 0.3.8

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,5 +1,4 @@
1
1
  "use strict";
2
- // InputMultiselect.tsx
3
2
  'use client';
4
3
  var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
5
4
  if (k2 === undefined) k2 = k;
@@ -44,58 +43,76 @@ const InputMultiselect = ({ label, required, textError, placeholder, onChangeHan
44
43
  margin: 'mt-[-6px]',
45
44
  width: 'w-full',
46
45
  }, }) => {
47
- const [items, setItems] = (0, react_1.useState)(value || []);
46
+ const [items, setItems] = (0, react_1.useState)([]);
48
47
  const [currentValue, setCurrentValue] = (0, react_1.useState)('');
49
48
  const [showError, setShowError] = (0, react_1.useState)(false);
49
+ const isErrorVisible = () => {
50
+ // Show error if there's a textError OR if there's unconfirmed input
51
+ return (textError && textError.trim() !== '') || showError;
52
+ };
53
+ const getErrorMessage = () => {
54
+ if (textError && textError.trim() !== '') {
55
+ return textError.trim();
56
+ }
57
+ if (showError && currentValue.trim() !== '') {
58
+ return 'برای ثبت بارکد کلید Enter را فشار دهید';
59
+ }
60
+ return '';
61
+ };
62
+ // Initialize items from value prop only once
50
63
  (0, react_1.useEffect)(() => {
51
- if (value && value.length > 0) {
64
+ if (value && value.length > 0 && items.length === 0) {
52
65
  setItems(value);
53
66
  }
54
- }, [value]);
67
+ }, [value, items.length]);
55
68
  const handleChange = (e) => {
56
69
  setCurrentValue(e.target.value);
57
- if (e.target.value.trim() !== '') {
70
+ // Hide error when user starts typing
71
+ if (showError) {
58
72
  setShowError(false);
59
73
  }
60
74
  };
61
75
  const handleKeyDown = (e) => {
62
76
  if (e.key === 'Enter' && currentValue.trim() !== '') {
63
77
  e.preventDefault();
64
- setItems((prevItems) => [...prevItems, currentValue.trim()]);
78
+ const newItems = [...items, currentValue.trim()];
79
+ setItems(newItems);
65
80
  setCurrentValue('');
66
- setShowError(false);
81
+ setShowError(false); // Reset error on Enter
82
+ // Call onChangeHandler directly here instead of in useEffect
83
+ onChangeHandler(newItems);
67
84
  }
68
85
  propHandleKeyDown?.(e);
69
86
  };
70
87
  const handleBlur = (e) => {
71
88
  if (currentValue.trim() !== '') {
72
- setShowError(true);
73
- }
74
- else {
75
- setShowError(false);
89
+ setShowError(true); // Show error if there's unconfirmed input
76
90
  }
77
91
  onBlurHandler?.(e);
78
92
  };
79
- const handleFocus = (e) => {
80
- setShowError(false);
81
- onFocusHandler?.(e);
82
- };
83
- (0, react_1.useEffect)(() => {
84
- onChangeHandler(items);
85
- }, [items, onChangeHandler]);
86
- const handleRemoveItem = (index) => {
87
- setItems((prevItems) => prevItems.filter((_, i) => i !== index));
88
- };
89
- return (react_1.default.createElement("div", { className: `${style.width} ${style.margin} relative` },
93
+ // Remove the problematic useEffect that was causing the loop
94
+ // useEffect((): void => {
95
+ // onChangeHandler(items);
96
+ // }, [items]);
97
+ const handleRemoveItem = (0, react_1.useCallback)((index) => {
98
+ setItems((prevItems) => {
99
+ const newItems = prevItems.filter((_, i) => i !== index);
100
+ // Call onChangeHandler directly here
101
+ onChangeHandler(newItems);
102
+ return newItems;
103
+ });
104
+ }, [onChangeHandler]);
105
+ return (react_1.default.createElement("div", { className: `${style.width} ${style.margin} relative ` },
90
106
  react_1.default.createElement("label", { htmlFor: label, className: "flex items-center gap-2 mx-3" },
91
107
  react_1.default.createElement("p", { className: "text-sm text-text-color-primary font-medium cursor-pointer" }, label),
92
108
  react_1.default.createElement("span", { className: `text-red-primary font-bold ${required ? 'block' : 'hidden'}` }, "*")),
93
- react_1.default.createElement("div", { className: `w-full h-10 flex gap-1 items-center justify-between bg-inherit outline-none border rounded-lg text-sm px-2 ${!textError ? 'border-gray-tertiary focus:border-gray-quaternary' : 'border-red-primary'}` },
109
+ react_1.default.createElement("div", { className: `w-full h-10 flex gap-1 items-center justify-between bg-inherit outline-none border rounded-lg text-sm px-2 ${isErrorVisible()
110
+ ? 'border-red-primary'
111
+ : 'border-gray-tertiary focus:border-gray-quaternary'}` },
94
112
  items.map((item, index) => (react_1.default.createElement("span", { key: index, className: "bg-gray-secondary text-text-color-primary px-2 py-1 rounded flex items-center justify-center gap-1" },
95
113
  item,
96
114
  react_1.default.createElement(image_1.default, { src: closeIconSRC, alt: "close", width: 22, height: 22, onClick: () => handleRemoveItem(index), className: "inline-block cursor-pointer" })))),
97
- react_1.default.createElement("input", { ref: inputRef, type: "text", value: currentValue, id: label, placeholder: items.length === 0 ? placeholder : '', onChange: handleChange, autoComplete: autoComplete ? 'on' : 'off', autoFocus: autoFocus, className: "outline-none bg-inherit flex-grow", required: required, onFocus: handleFocus, onKeyUp: onKeyUpHandler, onKeyDown: handleKeyDown, onBlur: handleBlur })),
98
- textError && react_1.default.createElement("p", { className: "text-sm mt-1 text-red-primary" }, textError),
99
- showError && !textError && (react_1.default.createElement("p", { className: "text-sm mt-1 text-red-primary" }, "\u0628\u0631\u0627\u06CC \u062B\u0628\u062A \u0628\u0627\u0631\u06A9\u062F \u06A9\u0644\u06CC\u062F Enter \u0631\u0627 \u0641\u0634\u0627\u0631 \u062F\u0647\u06CC\u062F"))));
115
+ react_1.default.createElement("input", { ref: inputRef, type: "text", value: currentValue, id: label, placeholder: items.length === 0 ? placeholder : '', onChange: handleChange, autoComplete: autoComplete ? 'on' : 'off', autoFocus: autoFocus, className: "outline-none bg-inherit flex-grow", required: required, onFocus: onFocusHandler, onKeyUp: onKeyUpHandler, onKeyDown: handleKeyDown, onBlur: handleBlur })),
116
+ isErrorVisible() && react_1.default.createElement("p", { className: "text-sm mt-1 text-red-primary" }, getErrorMessage())));
100
117
  };
101
118
  exports.default = InputMultiselect;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hi-tm",
3
- "version": "0.3.7",
3
+ "version": "0.3.8",
4
4
  "styles": "dist/styles/globals.css",
5
5
  "main": "dist/index.js",
6
6
  "exports": {