Skip to content

Commit

Permalink
fix: styles override and support for text input props
Browse files Browse the repository at this point in the history
  • Loading branch information
sachdevavaibhav committed May 17, 2024
1 parent 0a8bd1a commit 79afbd9
Showing 1 changed file with 29 additions and 14 deletions.
43 changes: 29 additions & 14 deletions src/components/OtpInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
} from 'react-native';
import type { StyleProp, ViewStyle, TextStyle } from 'react-native';
import { TextInput, Text } from 'react-native-paper';
import type { TextInputProps } from 'react-native-paper';

type OtpInputProps = {
maxLength: number;
Expand All @@ -18,6 +19,7 @@ type OtpInputProps = {
otpTextStyle?: StyleProp<TextStyle>;
otpBorderColor?: string;
otpBorderFocusedColor?: string;
textInputProps?: TextInputProps;
};

export default function OtpInput({
Expand All @@ -30,6 +32,7 @@ export default function OtpInput({
otpTextStyle,
otpBorderColor = '#F6F6F6',
otpBorderFocusedColor = '#6200EE',
textInputProps,
}: OtpInputProps) {
const [isInputBoxFocused, setIsInputBoxFocused] = React.useState(autoFocus);
const [otp, setOtp] = React.useState('');
Expand Down Expand Up @@ -58,11 +61,31 @@ export default function OtpInput({
const handleOnBlur = () => {
setIsInputBoxFocused(false);
};

const containerStyleObject = StyleSheet.flatten([
defaultStyles.container,
containerStyle,
]);

const otpContainerStylesObject = StyleSheet.flatten([
defaultStyles.otpContainer,
otpContainerStyle,
]);

const otpBoxStyleObject = StyleSheet.flatten([
defaultStyles.otpBox,
otpBoxStyle,
]);

const otpTextStyleObject = StyleSheet.flatten([
defaultStyles.otpText,
otpTextStyle,
]);
return (
<View style={containerStyle ? containerStyle : styles.container}>
<View style={containerStyleObject}>
<TextInput
mode="outlined"
style={styles.textInput}
style={defaultStyles.textInput}
theme={{
roundness: 10,
}}
Expand All @@ -73,11 +96,9 @@ export default function OtpInput({
onBlur={handleOnBlur}
keyboardType="numeric"
autoFocus={autoFocus}
{...textInputProps}
/>
<Pressable
style={otpContainerStyle ? otpContainerStyle : styles.otpContainer}
onPress={handleOnPress}
>
<Pressable style={otpContainerStylesObject} onPress={handleOnPress}>
{boxArray.map((_, index) => {
const isCurrentValue = index === otp.length;
const isLastValue = index === maxLength - 1;
Expand All @@ -86,10 +107,6 @@ export default function OtpInput({
const isValueFocused =
isCurrentValue || (isLastValue && isCodeComplete);

const otpBoxStyleObject = otpBoxStyle
? (otpBoxStyle as ViewStyle)
: styles.otpBox;

return (
<View
key={index}
Expand All @@ -101,9 +118,7 @@ export default function OtpInput({
: otpBorderColor,
}}
>
<Text style={otpTextStyle ? otpTextStyle : styles.otpText}>
{otp[index] || ''}
</Text>
<Text style={otpTextStyleObject}>{otp[index] || ''}</Text>
</View>
);
})}
Expand All @@ -112,7 +127,7 @@ export default function OtpInput({
);
}

const styles = StyleSheet.create({
const defaultStyles = StyleSheet.create({
container: {
justifyContent: 'center',
alignItems: 'center',
Expand Down

0 comments on commit 79afbd9

Please sign in to comment.