@hoddy-ui/core 1.0.35 → 1.0.36
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/index.ts +1 -0
- package/package.json +1 -1
- package/src/Components/OTPInput.tsx +75 -40
- package/src/theme/index.tsx +0 -1
- package/src/types.ts +9 -0
package/index.ts
CHANGED
|
@@ -19,6 +19,7 @@ export { default as Spinner } from "./src/Components/Spinner";
|
|
|
19
19
|
export * from "./src/Components/TextField";
|
|
20
20
|
export { default as TextField } from "./src/Components/TextField";
|
|
21
21
|
export { default as Typography } from "./src/Components/Typography";
|
|
22
|
+
export * from "./src/Components/OTPInput";
|
|
22
23
|
// Others
|
|
23
24
|
// export * from "./src/config";
|
|
24
25
|
export * from "./src/hooks";
|
package/package.json
CHANGED
|
@@ -1,47 +1,82 @@
|
|
|
1
|
-
import React, { useState } from "react";
|
|
2
|
-
import {
|
|
1
|
+
import React, { FC, useMemo, useState } from "react";
|
|
2
|
+
import { TextInput, View } from "react-native";
|
|
3
|
+
import { ScaledSheet, ms } from "react-native-size-matters";
|
|
4
|
+
import { useColors } from "../hooks";
|
|
5
|
+
import { OTPInputProps } from "../types";
|
|
3
6
|
|
|
4
|
-
export const OTPInput = (
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
7
|
+
export const OTPInput: FC<OTPInputProps> = ({
|
|
8
|
+
length = 6,
|
|
9
|
+
onChange = () => {},
|
|
10
|
+
value = "",
|
|
11
|
+
variant = "outlined",
|
|
12
|
+
spacing = 1,
|
|
13
|
+
size = 45,
|
|
14
|
+
}) => {
|
|
15
|
+
const inputRefs = useMemo(
|
|
16
|
+
() =>
|
|
17
|
+
Array(length)
|
|
18
|
+
.fill(0)
|
|
19
|
+
.map((_) => React.createRef<TextInput>()),
|
|
20
|
+
[length]
|
|
21
|
+
);
|
|
13
22
|
|
|
14
|
-
const
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
23
|
+
const colors = useColors();
|
|
24
|
+
const styles = ScaledSheet.create({
|
|
25
|
+
root: {},
|
|
26
|
+
container: { flexDirection: "row" },
|
|
27
|
+
input: {
|
|
28
|
+
height: ms(size),
|
|
29
|
+
width: ms(size),
|
|
30
|
+
borderColor: colors.white[5],
|
|
31
|
+
backgroundColor: variant === "contained" ? colors.white[3] : undefined,
|
|
32
|
+
borderWidth: variant === "outlined" ? 1 : 0,
|
|
33
|
+
borderBottomWidth: variant === "contained" ? 0 : 1,
|
|
34
|
+
marginHorizontal: ms(spacing * 5),
|
|
35
|
+
textAlign: "center",
|
|
36
|
+
borderRadius: variant === "text" ? 0 : 10,
|
|
37
|
+
color: colors.dark.main,
|
|
38
|
+
fontSize: ms(size * 0.5),
|
|
39
|
+
},
|
|
40
|
+
});
|
|
18
41
|
|
|
19
42
|
return (
|
|
20
|
-
<View style={styles.
|
|
21
|
-
{
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
43
|
+
<View style={styles.root}>
|
|
44
|
+
<View style={styles.container}>
|
|
45
|
+
{[...Array(length)].map((_, index) => (
|
|
46
|
+
<TextInput
|
|
47
|
+
ref={inputRefs[index]}
|
|
48
|
+
onChangeText={(val) => {
|
|
49
|
+
if (val.length === 1) {
|
|
50
|
+
if (index !== length - 1) inputRefs[index + 1].current?.focus();
|
|
51
|
+
let text = value;
|
|
52
|
+
|
|
53
|
+
text = text.slice(0, index) + val + text.slice(index + 1);
|
|
54
|
+
onChange(text);
|
|
55
|
+
} else if (val.length === 0) {
|
|
56
|
+
if (index !== 0) {
|
|
57
|
+
inputRefs[index - 1].current?.focus();
|
|
58
|
+
let text = value;
|
|
59
|
+
|
|
60
|
+
text = text.slice(0, index);
|
|
61
|
+
onChange(text);
|
|
62
|
+
} else onChange("");
|
|
63
|
+
} else {
|
|
64
|
+
let text = val.replace(/\D/g, "").slice(0, length);
|
|
65
|
+
onChange(text);
|
|
66
|
+
inputRefs[
|
|
67
|
+
text.length < length - 1 ? text.length : length - 1
|
|
68
|
+
]?.current?.focus();
|
|
69
|
+
}
|
|
70
|
+
}}
|
|
71
|
+
value={value[index] || ""}
|
|
72
|
+
// maxLength={1}
|
|
73
|
+
blurOnSubmit={false}
|
|
74
|
+
keyboardType="number-pad"
|
|
75
|
+
key={index}
|
|
76
|
+
style={[styles.input]}
|
|
77
|
+
/>
|
|
78
|
+
))}
|
|
79
|
+
</View>
|
|
31
80
|
</View>
|
|
32
81
|
);
|
|
33
82
|
};
|
|
34
|
-
|
|
35
|
-
const styles = StyleSheet.create({
|
|
36
|
-
container: {
|
|
37
|
-
marginTop: 20,
|
|
38
|
-
paddingHorizontal: 16,
|
|
39
|
-
},
|
|
40
|
-
input: {
|
|
41
|
-
height: 40,
|
|
42
|
-
borderColor: "gray",
|
|
43
|
-
borderWidth: 1,
|
|
44
|
-
paddingHorizontal: 10,
|
|
45
|
-
marginBottom: 10,
|
|
46
|
-
},
|
|
47
|
-
});
|
package/src/theme/index.tsx
CHANGED
package/src/types.ts
CHANGED
|
@@ -283,3 +283,12 @@ export interface SelectMenuProps {
|
|
|
283
283
|
secondary?: string;
|
|
284
284
|
helperText?: string;
|
|
285
285
|
}
|
|
286
|
+
|
|
287
|
+
export interface OTPInputProps {
|
|
288
|
+
length?: number;
|
|
289
|
+
onChange: (value: string) => void;
|
|
290
|
+
value: string;
|
|
291
|
+
variant?: "outlined" | "text" | "contained";
|
|
292
|
+
spacing?: number;
|
|
293
|
+
size?: number;
|
|
294
|
+
}
|