Skip to content

Commit eb71388

Browse files
authored
fix: merge containers into one (#58)
* fix: merge containers into one * docs: add a tip for spacing between inputs
1 parent 89dc143 commit eb71388

4 files changed

Lines changed: 333 additions & 346 deletions

File tree

README.MD

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,9 @@ The `react-native-otp-entry` component accepts the following props:
104104
| `filledPinCodeContainerStyle` | ViewStyle | Custom styles for the input field when it has a value. |
105105
| `disabledPinCodeContainerStyle` | ViewStyle | Custom styles for the input field when it is disabled. |
106106

107-
Note: The `ViewStyle` and `TextStyle` types are imported from `react-native` and represent the style objects used in React Native for views and text, respectively.
107+
**Note:** The `ViewStyle` and `TextStyle` types are imported from `react-native` and represent the style objects used in React Native for views and text, respectively.
108+
109+
**Tip:** If you have difficulties while applying `gap` or in any other style property to set a suitable space between the OTP input containers, please set the `width` in `containerStyle` to `'auto'` or `undefined`, as it is been set to `'100%'` by default.
108110

109111
![Theme](otp.drawio.svg)
110112

src/OtpInput/OtpInput.styles.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,8 @@ import { StyleSheet } from "react-native";
22

33
export const styles = StyleSheet.create({
44
container: {
5+
width: "100%",
56
flexDirection: "row",
6-
},
7-
inputsContainer: {
8-
flexDirection: "row",
9-
flex: 1,
107
justifyContent: "space-between",
118
},
129
codeContainer: {

src/OtpInput/OtpInput.tsx

Lines changed: 30 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -57,39 +57,37 @@ export const OtpInput = forwardRef<OtpInputRef, OtpInputProps>((props, ref) => {
5757
};
5858

5959
return (
60-
<View style={[styles.container, containerStyle]}>
61-
<View style={[styles.inputsContainer, inputsContainerStyle]}>
62-
{Array(numberOfDigits)
63-
.fill(0)
64-
.map((_, index) => {
65-
const char = text[index];
66-
const isFocusedInput = index === focusedInputIndex && !disabled && Boolean(isFocused);
67-
const isFilledLastInput = text.length === numberOfDigits && index === text.length - 1;
68-
const isFocusedContainer = isFocusedInput || (isFilledLastInput && Boolean(isFocused));
60+
<View style={[styles.container, containerStyle, inputsContainerStyle]}>
61+
{Array(numberOfDigits)
62+
.fill(0)
63+
.map((_, index) => {
64+
const char = text[index];
65+
const isFocusedInput = index === focusedInputIndex && !disabled && Boolean(isFocused);
66+
const isFilledLastInput = text.length === numberOfDigits && index === text.length - 1;
67+
const isFocusedContainer = isFocusedInput || (isFilledLastInput && Boolean(isFocused));
6968

70-
return (
71-
<Pressable
72-
key={`${char}-${index}`}
73-
disabled={disabled}
74-
onPress={handlePress}
75-
style={generatePinCodeContainerStyle(isFocusedContainer, char)}
76-
testID="otp-input"
77-
>
78-
{isFocusedInput && !hideStick ? (
79-
<VerticalStick
80-
focusColor={focusColor}
81-
style={focusStickStyle}
82-
focusStickBlinkingDuration={focusStickBlinkingDuration}
83-
/>
84-
) : (
85-
<Text style={[styles.codeText, pinCodeTextStyle]}>
86-
{char && secureTextEntry ? "•" : char}
87-
</Text>
88-
)}
89-
</Pressable>
90-
);
91-
})}
92-
</View>
69+
return (
70+
<Pressable
71+
key={`${char}-${index}`}
72+
disabled={disabled}
73+
onPress={handlePress}
74+
style={generatePinCodeContainerStyle(isFocusedContainer, char)}
75+
testID="otp-input"
76+
>
77+
{isFocusedInput && !hideStick ? (
78+
<VerticalStick
79+
focusColor={focusColor}
80+
style={focusStickStyle}
81+
focusStickBlinkingDuration={focusStickBlinkingDuration}
82+
/>
83+
) : (
84+
<Text style={[styles.codeText, pinCodeTextStyle]}>
85+
{char && secureTextEntry ? "•" : char}
86+
</Text>
87+
)}
88+
</Pressable>
89+
);
90+
})}
9391
<TextInput
9492
value={text}
9593
onChangeText={handleTextChange}

0 commit comments

Comments
 (0)