Skip to content

Commit

Permalink
Merge pull request #34 from EddWills95/allow-styling-when-filled
Browse files Browse the repository at this point in the history
feat: allow styles when digit has value
  • Loading branch information
anday013 authored Jan 7, 2024
2 parents 1ab3759 + 46a9486 commit f8e7dbb
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.MD
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ The `react-native-otp-entry` component accepts the following props:
| `pinCodeTextStyle` | TextStyle | Custom styles for the text within each individual digit in the OTP entry. |
| `focusStickStyle` | ViewStyle | Custom styles for the focus stick, which indicates the focused input field. |
| `focusedPinCodeContainerStyle` | ViewStyle | Custom styles for the input field when it is focused. |
| `filledPinCodeContainerStyle` | ViewStyle | Custom styles for the input field when it has a value. |

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.

Expand Down
14 changes: 14 additions & 0 deletions src/OtpInput/OtpInput.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,20 @@ describe("OtpInput", () => {
expect(inputs).toHaveLength(numberOfDigits);
}
);

test("filledPinCodeContainerStyle should allow for new style when digit is present", () => {
renderOtpInput({
theme: { filledPinCodeContainerStyle: { borderBottomColor: "red" } },
});

const input = screen.getByTestId("otp-input-hidden");
fireEvent.changeText(input, "12");

const inputs = screen.getAllByTestId("otp-input");
expect(inputs[0]).toHaveStyle({ borderBottomColor: "red" });
expect(inputs[1]).toHaveStyle({ borderBottomColor: "red" });
expect(inputs[2]).not.toHaveStyle({ borderBottomColor: "red" });
});
});

describe("Logic", () => {
Expand Down
4 changes: 4 additions & 0 deletions src/OtpInput/OtpInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export const OtpInput = forwardRef<OtpInputRef, OtpInputProps>((props, ref) => {
pinCodeTextStyle,
focusStickStyle,
focusedPinCodeContainerStyle,
filledPinCodeContainerStyle,
} = theme;

useImperativeHandle(ref, () => ({ clear, focus, setValue: setTextWithRef }));
Expand All @@ -51,6 +52,9 @@ export const OtpInput = forwardRef<OtpInputRef, OtpInputProps>((props, ref) => {
focusedPinCodeContainerStyle && isFocusedInput
? { ...focusedPinCodeContainerStyle }
: {},
filledPinCodeContainerStyle && Boolean(char)
? { ...filledPinCodeContainerStyle }
: {},
]}
testID="otp-input"
>
Expand Down
1 change: 1 addition & 0 deletions src/OtpInput/OtpInput.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export interface Theme {
containerStyle?: ViewStyle;
inputsContainerStyle?: ViewStyle;
pinCodeContainerStyle?: ViewStyle;
filledPinCodeContainerStyle?: ViewStyle;
pinCodeTextStyle?: TextStyle;
focusStickStyle?: ViewStyle;
focusedPinCodeContainerStyle?: ViewStyle;
Expand Down
6 changes: 6 additions & 0 deletions src/OtpInput/__snapshots__/OtpInput.test.tsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ exports[`OtpInput UI should render correctly 1`] = `
"borderColor": "#A4D0A4",
},
{},
{},
]
}
testID="otp-input"
Expand Down Expand Up @@ -143,6 +144,7 @@ exports[`OtpInput UI should render correctly 1`] = `
undefined,
{},
{},
{},
]
}
testID="otp-input"
Expand Down Expand Up @@ -202,6 +204,7 @@ exports[`OtpInput UI should render correctly 1`] = `
undefined,
{},
{},
{},
]
}
testID="otp-input"
Expand Down Expand Up @@ -261,6 +264,7 @@ exports[`OtpInput UI should render correctly 1`] = `
undefined,
{},
{},
{},
]
}
testID="otp-input"
Expand Down Expand Up @@ -320,6 +324,7 @@ exports[`OtpInput UI should render correctly 1`] = `
undefined,
{},
{},
{},
]
}
testID="otp-input"
Expand Down Expand Up @@ -379,6 +384,7 @@ exports[`OtpInput UI should render correctly 1`] = `
undefined,
{},
{},
{},
]
}
testID="otp-input"
Expand Down

0 comments on commit f8e7dbb

Please sign in to comment.