forstok-ui-lib 5.11.12 → 5.11.13
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.
- package/dist/index.d.ts +3 -2
- package/dist/index.js +16 -1
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +16 -1
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
- package/src/components/input/otp.tsx +62 -0
- package/src/components/input/styles.ts +16 -0
- package/src/typeds/base.typed.ts +2 -1
package/package.json
CHANGED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { InputOtpContainer } from './styles';
|
|
2
|
+
import { TChangeEvent, TKeyboadEvent, TPasteEvent, TState } from '../../typeds';
|
|
3
|
+
|
|
4
|
+
const InputOTPComponent = ({ otpLength = 6, value, onChange, ...props }: {otpLength: number, value: string, onChange: TState<string>}) => {
|
|
5
|
+
|
|
6
|
+
const handleChange: TChangeEvent = (e) => {
|
|
7
|
+
const target = e.target as HTMLInputElement;
|
|
8
|
+
const inputValue = target.value;
|
|
9
|
+
const inputIndex = target.dataset.index ? parseInt(target.dataset.index) : 0;
|
|
10
|
+
if (inputValue === '' || /^\d$/.test(inputValue)) {
|
|
11
|
+
let updatedValue = value.split('');
|
|
12
|
+
updatedValue[inputIndex] = inputValue;
|
|
13
|
+
const newOtpValue = updatedValue.join('');
|
|
14
|
+
onChange(newOtpValue);
|
|
15
|
+
if (inputValue !== '' && inputIndex < otpLength - 1) {
|
|
16
|
+
(document.getElementById(`otp-inputbox-${inputIndex + 1}`) as HTMLInputElement)?.focus();
|
|
17
|
+
};
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
const handleKeyDown: TKeyboadEvent = (e) => {
|
|
22
|
+
const target = e.target as HTMLInputElement;
|
|
23
|
+
const inputIndex = target.dataset.index ? parseInt(target.dataset.index) : 0;
|
|
24
|
+
if (e.key === 'Backspace' && !value[inputIndex] && inputIndex > 0) {
|
|
25
|
+
(document.getElementById(`otp-inputbox-${inputIndex - 1}`) as HTMLInputElement)?.focus();
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
const handlePaste: TPasteEvent<HTMLDivElement> = (e) => {
|
|
30
|
+
e.preventDefault();
|
|
31
|
+
const pastedValue = e.clipboardData.getData('Text').replace(/\D/g, '').slice(0, otpLength);
|
|
32
|
+
onChange(pastedValue);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
let inputEls = [];
|
|
36
|
+
for (let i = 0; i < otpLength; i++) {
|
|
37
|
+
inputEls.push(
|
|
38
|
+
<InputOtpContainer
|
|
39
|
+
key={i}
|
|
40
|
+
data-index={i}
|
|
41
|
+
id={`otp-inputbox-${i}`}
|
|
42
|
+
name={`otp-input-${i}`}
|
|
43
|
+
autoComplete='one-time-code'
|
|
44
|
+
inputMode='numeric'
|
|
45
|
+
required
|
|
46
|
+
value={value[i] || ''}
|
|
47
|
+
onChange={(e) => handleChange(e)}
|
|
48
|
+
onKeyDown={(e) => handleKeyDown(e)}
|
|
49
|
+
onFocus={(e) => {e.target.addEventListener('wheel', function (e) { e.preventDefault() }, { passive: false }); e.target.select()}}
|
|
50
|
+
{...props}
|
|
51
|
+
/>
|
|
52
|
+
);
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
return (
|
|
56
|
+
<div onPaste={handlePaste}>
|
|
57
|
+
{inputEls}
|
|
58
|
+
</div>
|
|
59
|
+
);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export default InputOTPComponent;
|
|
@@ -89,4 +89,20 @@ export const InputLabel = styled.label`
|
|
|
89
89
|
top: 50%;
|
|
90
90
|
transform: translateY(-50%);
|
|
91
91
|
color: var(--mt-clr);
|
|
92
|
+
`
|
|
93
|
+
export const InputOtpContainer = styled.input`
|
|
94
|
+
text-align: center;
|
|
95
|
+
height: 40px;
|
|
96
|
+
width: 38px;
|
|
97
|
+
font-size: 20px;
|
|
98
|
+
margin-right: 5px;
|
|
99
|
+
&:last-child {
|
|
100
|
+
margin-right: 0;
|
|
101
|
+
}
|
|
102
|
+
@media only screen and (min-width: 768px) {
|
|
103
|
+
height: 55px;
|
|
104
|
+
width: 45px;
|
|
105
|
+
font-size: 28px;
|
|
106
|
+
margin-right: 16px;
|
|
107
|
+
}
|
|
92
108
|
`
|
package/src/typeds/base.typed.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { MouseEvent, ChangeEvent, DragEvent, Dispatch, SetStateAction, KeyboardEvent, FocusEvent } from 'react';
|
|
1
|
+
import type { MouseEvent, ChangeEvent, DragEvent, Dispatch, SetStateAction, KeyboardEvent, FocusEvent, ClipboardEvent } from 'react';
|
|
2
2
|
|
|
3
3
|
type RemoveUnderscoreFirstLetter<S extends string> =
|
|
4
4
|
S extends `${infer FirstLetter}${infer U}`
|
|
@@ -24,6 +24,7 @@ export type TChangeEvent = (e: ChangeEvent) => void;
|
|
|
24
24
|
export type TDragEvent = (e:DragEvent) => void;
|
|
25
25
|
export type TEnterEvent = (e:KeyboardEvent<HTMLInputElement | HTMLElement> | FocusEvent<HTMLInputElement | HTMLElement | Element> | MouseEvent<HTMLElement>) => void;
|
|
26
26
|
export type TKeyboadEvent = (e: KeyboardEvent<HTMLInputElement>) => void;
|
|
27
|
+
export type TPasteEvent<T> = (e:ClipboardEvent<T>) => void;
|
|
27
28
|
export type TPage = {
|
|
28
29
|
totalCount: number
|
|
29
30
|
totalPageCount: number
|