Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: merge containers into one #58

Merged
merged 2 commits into from
May 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion README.MD
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,9 @@ The `react-native-otp-entry` component accepts the following props:
| `filledPinCodeContainerStyle` | ViewStyle | Custom styles for the input field when it has a value. |
| `disabledPinCodeContainerStyle` | ViewStyle | Custom styles for the input field when it is disabled. |

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.
**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.

**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.

![Theme](otp.drawio.svg)

Expand Down
5 changes: 1 addition & 4 deletions src/OtpInput/OtpInput.styles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,8 @@ import { StyleSheet } from "react-native";

export const styles = StyleSheet.create({
container: {
width: "100%",
flexDirection: "row",
},
inputsContainer: {
flexDirection: "row",
flex: 1,
justifyContent: "space-between",
},
codeContainer: {
Expand Down
62 changes: 30 additions & 32 deletions src/OtpInput/OtpInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,39 +57,37 @@ export const OtpInput = forwardRef<OtpInputRef, OtpInputProps>((props, ref) => {
};

return (
<View style={[styles.container, containerStyle]}>
<View style={[styles.inputsContainer, inputsContainerStyle]}>
{Array(numberOfDigits)
.fill(0)
.map((_, index) => {
const char = text[index];
const isFocusedInput = index === focusedInputIndex && !disabled && Boolean(isFocused);
const isFilledLastInput = text.length === numberOfDigits && index === text.length - 1;
const isFocusedContainer = isFocusedInput || (isFilledLastInput && Boolean(isFocused));
<View style={[styles.container, containerStyle, inputsContainerStyle]}>
{Array(numberOfDigits)
.fill(0)
.map((_, index) => {
const char = text[index];
const isFocusedInput = index === focusedInputIndex && !disabled && Boolean(isFocused);
const isFilledLastInput = text.length === numberOfDigits && index === text.length - 1;
const isFocusedContainer = isFocusedInput || (isFilledLastInput && Boolean(isFocused));

return (
<Pressable
key={`${char}-${index}`}
disabled={disabled}
onPress={handlePress}
style={generatePinCodeContainerStyle(isFocusedContainer, char)}
testID="otp-input"
>
{isFocusedInput && !hideStick ? (
<VerticalStick
focusColor={focusColor}
style={focusStickStyle}
focusStickBlinkingDuration={focusStickBlinkingDuration}
/>
) : (
<Text style={[styles.codeText, pinCodeTextStyle]}>
{char && secureTextEntry ? "•" : char}
</Text>
)}
</Pressable>
);
})}
</View>
return (
<Pressable
key={`${char}-${index}`}
disabled={disabled}
onPress={handlePress}
style={generatePinCodeContainerStyle(isFocusedContainer, char)}
testID="otp-input"
>
{isFocusedInput && !hideStick ? (
<VerticalStick
focusColor={focusColor}
style={focusStickStyle}
focusStickBlinkingDuration={focusStickBlinkingDuration}
/>
) : (
<Text style={[styles.codeText, pinCodeTextStyle]}>
{char && secureTextEntry ? "•" : char}
</Text>
)}
</Pressable>
);
})}
<TextInput
value={text}
onChangeText={handleTextChange}
Expand Down
Loading
Loading