Skip to content

Commit 79afbd9

Browse files
fix: styles override and support for text input props
1 parent 0a8bd1a commit 79afbd9

File tree

1 file changed

+29
-14
lines changed

1 file changed

+29
-14
lines changed

src/components/OtpInput.tsx

+29-14
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
} from 'react-native';
88
import type { StyleProp, ViewStyle, TextStyle } from 'react-native';
99
import { TextInput, Text } from 'react-native-paper';
10+
import type { TextInputProps } from 'react-native-paper';
1011

1112
type OtpInputProps = {
1213
maxLength: number;
@@ -18,6 +19,7 @@ type OtpInputProps = {
1819
otpTextStyle?: StyleProp<TextStyle>;
1920
otpBorderColor?: string;
2021
otpBorderFocusedColor?: string;
22+
textInputProps?: TextInputProps;
2123
};
2224

2325
export default function OtpInput({
@@ -30,6 +32,7 @@ export default function OtpInput({
3032
otpTextStyle,
3133
otpBorderColor = '#F6F6F6',
3234
otpBorderFocusedColor = '#6200EE',
35+
textInputProps,
3336
}: OtpInputProps) {
3437
const [isInputBoxFocused, setIsInputBoxFocused] = React.useState(autoFocus);
3538
const [otp, setOtp] = React.useState('');
@@ -58,11 +61,31 @@ export default function OtpInput({
5861
const handleOnBlur = () => {
5962
setIsInputBoxFocused(false);
6063
};
64+
65+
const containerStyleObject = StyleSheet.flatten([
66+
defaultStyles.container,
67+
containerStyle,
68+
]);
69+
70+
const otpContainerStylesObject = StyleSheet.flatten([
71+
defaultStyles.otpContainer,
72+
otpContainerStyle,
73+
]);
74+
75+
const otpBoxStyleObject = StyleSheet.flatten([
76+
defaultStyles.otpBox,
77+
otpBoxStyle,
78+
]);
79+
80+
const otpTextStyleObject = StyleSheet.flatten([
81+
defaultStyles.otpText,
82+
otpTextStyle,
83+
]);
6184
return (
62-
<View style={containerStyle ? containerStyle : styles.container}>
85+
<View style={containerStyleObject}>
6386
<TextInput
6487
mode="outlined"
65-
style={styles.textInput}
88+
style={defaultStyles.textInput}
6689
theme={{
6790
roundness: 10,
6891
}}
@@ -73,11 +96,9 @@ export default function OtpInput({
7396
onBlur={handleOnBlur}
7497
keyboardType="numeric"
7598
autoFocus={autoFocus}
99+
{...textInputProps}
76100
/>
77-
<Pressable
78-
style={otpContainerStyle ? otpContainerStyle : styles.otpContainer}
79-
onPress={handleOnPress}
80-
>
101+
<Pressable style={otpContainerStylesObject} onPress={handleOnPress}>
81102
{boxArray.map((_, index) => {
82103
const isCurrentValue = index === otp.length;
83104
const isLastValue = index === maxLength - 1;
@@ -86,10 +107,6 @@ export default function OtpInput({
86107
const isValueFocused =
87108
isCurrentValue || (isLastValue && isCodeComplete);
88109

89-
const otpBoxStyleObject = otpBoxStyle
90-
? (otpBoxStyle as ViewStyle)
91-
: styles.otpBox;
92-
93110
return (
94111
<View
95112
key={index}
@@ -101,9 +118,7 @@ export default function OtpInput({
101118
: otpBorderColor,
102119
}}
103120
>
104-
<Text style={otpTextStyle ? otpTextStyle : styles.otpText}>
105-
{otp[index] || ''}
106-
</Text>
121+
<Text style={otpTextStyleObject}>{otp[index] || ''}</Text>
107122
</View>
108123
);
109124
})}
@@ -112,7 +127,7 @@ export default function OtpInput({
112127
);
113128
}
114129

115-
const styles = StyleSheet.create({
130+
const defaultStyles = StyleSheet.create({
116131
container: {
117132
justifyContent: 'center',
118133
alignItems: 'center',

0 commit comments

Comments
 (0)