Skip to content

Commit

Permalink
fix: merge containers into one (#58)
Browse files Browse the repository at this point in the history
* fix: merge containers into one

* docs: add a tip for spacing between inputs
  • Loading branch information
anday013 authored May 28, 2024
1 parent 89dc143 commit eb71388
Show file tree
Hide file tree
Showing 4 changed files with 333 additions and 346 deletions.
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

0 comments on commit eb71388

Please sign in to comment.