qlu-20-ui-library 1.10.15 → 1.10.16

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.
@@ -15,61 +15,84 @@ const OTPComponent = ({ length = 6, isDisable = false, error = false, onOTPChang
15
15
  });
16
16
  }, [inputValues]);
17
17
  //this function is to handle the onPaste event for an input field
18
- const handlePaste = (event) => {
18
+ const handlePaste = (index, event) => {
19
+ event.preventDefault();
19
20
  const pastedText = event.clipboardData?.getData("text") || "";
20
- setOtp(pastedText);
21
- // Update the input values character by character
22
- const characters = pastedText.split("");
23
- characters.slice(0, 6).forEach((char, index) => {
24
- if (inputRefs.current[index]) {
25
- inputRefs.current[index].value = char;
26
- setInputValues((prevValues) => {
27
- const updatedValues = [...prevValues];
28
- updatedValues[index] = char;
29
- return updatedValues;
30
- });
21
+ // Filter to only numeric characters
22
+ const numericOnly = pastedText.replace(/\D/g, "");
23
+ const characters = numericOnly.split("").slice(0, length - index);
24
+ // Update input values and field values starting from the current index
25
+ const updatedInputValues = [...inputValues];
26
+ const updatedFieldValues = [...fieldValues];
27
+ characters.forEach((char, charIndex) => {
28
+ const targetIndex = index + charIndex;
29
+ if (targetIndex < length) {
30
+ updatedInputValues[targetIndex] = char;
31
+ updatedFieldValues[targetIndex] = char;
32
+ if (inputRefs.current[targetIndex]) {
33
+ inputRefs.current[targetIndex].value = char;
34
+ }
31
35
  }
32
36
  });
33
- };
34
- const handlePasteChange = (event, index) => {
35
- const updatedValues = [...inputValues];
36
- updatedValues[index] = event.target.value;
37
- setInputValues(updatedValues);
38
- };
39
- //sending cursor to last field after pasting OTP
40
- useEffect(() => {
41
- if (inputRefs.current[5] && inputRefs.current[5].value !== "") {
42
- inputRefs.current[5].focus();
37
+ // Clear remaining fields after the pasted content
38
+ const endIndex = Math.min(index + characters.length, length);
39
+ for (let i = endIndex; i < length; i++) {
40
+ updatedInputValues[i] = "";
41
+ updatedFieldValues[i] = "";
42
+ if (inputRefs.current[i]) {
43
+ inputRefs.current[i].value = "";
44
+ }
45
+ }
46
+ setInputValues(updatedInputValues);
47
+ setFieldValues(updatedFieldValues);
48
+ const completeOTP = updatedFieldValues.join("");
49
+ setOtp(completeOTP);
50
+ // Trigger completion callback if all fields are filled
51
+ if (updatedFieldValues.every((val) => val !== "") && onOTPComplete) {
52
+ onOTPComplete(completeOTP);
43
53
  }
44
- else if (inputRefs.current[0].value == "") {
45
- inputRefs.current[0].focus();
54
+ // Focus the last filled field or next empty field
55
+ const nextEmptyIndex = updatedFieldValues.findIndex((val) => val === "");
56
+ const focusIndex = nextEmptyIndex === -1 ? length - 1 : Math.min(nextEmptyIndex, length - 1);
57
+ if (inputRefs.current[focusIndex]) {
58
+ inputRefs.current[focusIndex].focus();
46
59
  }
47
- }, [inputValues[5]]);
60
+ };
48
61
  const handleInput = (index, event) => {
49
62
  const value = event.target.value;
63
+ // Extract only the last character if multiple characters are entered
64
+ const lastChar = value.slice(-1);
50
65
  if (value.length === 0) {
66
+ // Clear the field
51
67
  event.target.value = "";
52
- setOtp((prev) => prev.slice(0, index) + prev.slice(index + 1));
53
- setFieldValues((prev) => {
68
+ setInputValues((prev) => {
54
69
  const updatedValues = [...prev];
55
70
  updatedValues[index] = "";
56
71
  return updatedValues;
57
72
  });
58
- }
59
- else if (/^\d$/.test(value)) {
60
- event.target.value = value;
61
- setOtp((prev) => prev.slice(0, index) + value + prev.slice(index + 1));
62
73
  setFieldValues((prev) => {
63
74
  const updatedValues = [...prev];
64
- updatedValues[index] = value;
75
+ updatedValues[index] = "";
65
76
  return updatedValues;
66
77
  });
67
- if (fieldValues.filter((val) => val !== "").length + 1 === length &&
68
- onOTPComplete) {
69
- fieldValues[index] = value;
70
- const completeOTP = fieldValues.join("");
78
+ setOtp((prev) => prev.slice(0, index) + prev.slice(index + 1));
79
+ }
80
+ else if (/^\d$/.test(lastChar)) {
81
+ // Only allow numeric input
82
+ event.target.value = lastChar;
83
+ const updatedInputValues = [...inputValues];
84
+ const updatedFieldValues = [...fieldValues];
85
+ updatedInputValues[index] = lastChar;
86
+ updatedFieldValues[index] = lastChar;
87
+ setInputValues(updatedInputValues);
88
+ setFieldValues(updatedFieldValues);
89
+ const completeOTP = updatedFieldValues.join("");
90
+ setOtp(completeOTP);
91
+ // Check if OTP is complete and trigger callback
92
+ if (updatedFieldValues.every((val) => val !== "") && onOTPComplete) {
71
93
  onOTPComplete(completeOTP);
72
94
  }
95
+ // Move to next field if not the last one
73
96
  if (index < length - 1) {
74
97
  inputRefs.current[index + 1].focus();
75
98
  }
@@ -77,30 +100,45 @@ const OTPComponent = ({ length = 6, isDisable = false, error = false, onOTPChang
77
100
  event.target.blur();
78
101
  }
79
102
  }
103
+ else {
104
+ // Invalid input - reset to previous value
105
+ event.target.value = inputValues[index] || "";
106
+ }
80
107
  };
81
108
  const handleBackspace = (index, event) => {
82
- if (event.key === "Backspace" &&
83
- index > 0 &&
84
- event.currentTarget.value === "") {
85
- event.preventDefault();
86
- event.currentTarget.value = "";
87
- setOtp((prev) => prev.slice(0, index - 1) + prev.slice(index));
88
- setFieldValues((prev) => {
89
- const updatedValues = [...prev];
90
- updatedValues[index - 1] = "";
91
- return updatedValues;
92
- });
93
- inputRefs.current[index - 1].focus();
109
+ if (event.key === "Backspace") {
110
+ if (event.currentTarget.value === "" && index > 0) {
111
+ // If current field is empty, move to previous and clear it
112
+ event.preventDefault();
113
+ const updatedInputValues = [...inputValues];
114
+ const updatedFieldValues = [...fieldValues];
115
+ updatedInputValues[index - 1] = "";
116
+ updatedFieldValues[index - 1] = "";
117
+ setInputValues(updatedInputValues);
118
+ setFieldValues(updatedFieldValues);
119
+ setOtp((prev) => prev.slice(0, index - 1) + prev.slice(index));
120
+ if (inputRefs.current[index - 1]) {
121
+ inputRefs.current[index - 1].value = "";
122
+ inputRefs.current[index - 1].focus();
123
+ }
124
+ }
125
+ // If current field has value, let the default behavior clear it
126
+ // The handleInput will handle the state update
94
127
  }
95
128
  };
129
+ const handleClick = (event) => {
130
+ // Select all text when clicking on an input for easy clearing
131
+ event.currentTarget.select();
132
+ };
133
+ const handleFocus = (event) => {
134
+ // Select all text on focus for easy clearing
135
+ event.currentTarget.select();
136
+ };
96
137
  useEffect(() => {
97
138
  if (onOTPChange) {
98
139
  onOTPChange(otp);
99
140
  }
100
141
  }, [otp]);
101
- return (_jsx("div", { className: "otp-container", children: Array.from({ length }).map((_, index) => (_jsx("input", { autoComplete: "off", disabled: isDisable, type: "text", maxLength: 1, className: `otp-box ${error ? "error" : ""}`, ref: (ref) => (inputRefs.current[index] = ref), onChange: (event) => {
102
- handleInput(index, event);
103
- handlePasteChange(event, index);
104
- }, onKeyDown: (event) => handleBackspace(index, event), onPaste: index === 0 ? handlePaste : undefined }, index))) }));
142
+ return (_jsx("div", { className: "otp-container", children: Array.from({ length }).map((_, index) => (_jsx("input", { autoComplete: "off", disabled: isDisable, type: "text", inputMode: "numeric", pattern: "[0-9]*", maxLength: 1, className: `otp-box ${error ? "error" : ""}`, ref: (ref) => (inputRefs.current[index] = ref), onChange: (event) => handleInput(index, event), onKeyDown: (event) => handleBackspace(index, event), onClick: handleClick, onFocus: handleFocus, onPaste: (event) => handlePaste(index, event) }, index))) }));
105
143
  };
106
144
  export default OTPComponent;
@@ -1,5 +1,6 @@
1
1
  import { jsx as _jsx } from "react/jsx-runtime";
2
2
  import { useEffect, useState } from "react";
3
+ import clsx from "clsx";
3
4
  import "./style.scss";
4
5
  const ToggleComponent = ({ initialState, onClick, sizeVariant = "base", isDisabled = false, dataCy, }) => {
5
6
  const [isOn, setIsOn] = useState(initialState);
@@ -12,6 +13,6 @@ const ToggleComponent = ({ initialState, onClick, sizeVariant = "base", isDisabl
12
13
  setIsOn(!isOn);
13
14
  onClick();
14
15
  };
15
- return (_jsx("div", { className: `${sizeVariant === "sm" ? "smSizeToggle" : "toggle"} ${isOn ? "on" : "off"}`, onClick: handleToggle, "data-cy": dataCy, children: _jsx("div", { className: sizeVariant === "sm" ? "smSizeToggle-switch" : "toggle-switch" }) }));
16
+ return (_jsx("div", { className: clsx(sizeVariant === "sm" ? "smSizeToggle" : "toggle", isOn ? "on" : "off", isDisabled && "disabled"), onClick: handleToggle, "data-cy": dataCy, children: _jsx("div", { className: sizeVariant === "sm" ? "smSizeToggle-switch" : "toggle-switch" }) }));
16
17
  };
17
18
  export default ToggleComponent;
@@ -1,5 +1,5 @@
1
- import type { StoryObj, Meta } from '@storybook/react-vite';
2
- import OTPComponent from '../components/OTP';
1
+ import type { StoryObj, Meta } from "@storybook/react-vite";
2
+ import OTPComponent from "../components/OTP";
3
3
  declare const meta: Meta<typeof OTPComponent>;
4
4
  export default meta;
5
5
  type OTPComponentProps = StoryObj<typeof OTPComponent>;
@@ -1,7 +1,17 @@
1
- import OTPComponent from '../components/OTP';
1
+ import OTPComponent from "../components/OTP";
2
+ // Mock function to replace @storybook/test fn() for Storybook 9.x compatibility
3
+ const fn = () => {
4
+ const spy = (...args) => {
5
+ spy.calls.push(args);
6
+ spy.callCount++;
7
+ };
8
+ spy.calls = [];
9
+ spy.callCount = 0;
10
+ return spy;
11
+ };
2
12
  const meta = {
3
13
  title: "Input/OTPInputField",
4
- component: OTPComponent
14
+ component: OTPComponent,
5
15
  };
6
16
  export default meta;
7
17
  export const NavBar = {
@@ -9,6 +19,7 @@ export const NavBar = {
9
19
  length: 6,
10
20
  error: false,
11
21
  isDisable: false,
12
- onOTPComplete: (value) => alert(value)
22
+ onOTPChange: fn(),
23
+ onOTPComplete: (value) => alert(value),
13
24
  },
14
25
  };
@@ -1,5 +1,5 @@
1
- import type { StoryObj, Meta } from '@storybook/react-vite';
2
- import ToggleComponent from '../components/ToggleComponent';
1
+ import type { StoryObj, Meta } from "@storybook/react-vite";
2
+ import ToggleComponent from "../components/ToggleComponent";
3
3
  declare const meta: Meta<typeof ToggleComponent>;
4
4
  export default meta;
5
5
  type ToggleComponentProps = StoryObj<typeof ToggleComponent>;
@@ -1,9 +1,11 @@
1
- import ToggleComponent from '../components/ToggleComponent';
1
+ import ToggleComponent from "../components/ToggleComponent";
2
2
  const meta = {
3
- title: 'Button/ToggleComponent',
3
+ title: "Button/ToggleComponent",
4
4
  component: ToggleComponent,
5
5
  };
6
6
  export default meta;
7
7
  export const Primary = {
8
- args: {},
8
+ args: {
9
+ isDisabled: true,
10
+ },
9
11
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "qlu-20-ui-library",
3
- "version": "1.10.15",
3
+ "version": "1.10.16",
4
4
  "type": "module",
5
5
  "scripts": {
6
6
  "dev": "vite",