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

Add onFocus and onBlur methods #73

Merged
merged 1 commit into from
Sep 26, 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
2 changes: 2 additions & 0 deletions README.MD
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ The `react-native-otp-entry` component accepts the following props:
| `disabled` | boolean | _Default: false_. Disables the input |
| `type` | 'alpha' \| 'numeric' \| 'alphanumeric' | The type of input. 'alpha': letters only, 'numeric': numbers only, 'alphanumeric': letters or numbers. |
| `secureTextEntry` | boolean | _Default: false_. Obscures the text entered so that sensitive text like PIN stay secure. |
| `onFocus` | () => void | A callback function is invoked when the OTP input is focused. |
| `onBlur` | () => void | A callback function is invoked when the OTP input is blurred. |

| Theme | Type | Description |
| ------------------------------- | --------- | ---------------------------------------------------------------------------------- |
Expand Down
2 changes: 2 additions & 0 deletions src/OtpInput/OtpInput.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ export interface OtpInputProps {
focusColor?: ColorValue;
onTextChange?: (text: string) => void;
onFilled?: (text: string) => void;
onFocus?: () => void;
onBlur?: () => void;
blurOnFilled?: boolean;
hideStick?: boolean;
focusStickBlinkingDuration?: number;
Expand Down
80 changes: 53 additions & 27 deletions src/OtpInput/__tests__/useOtpInput.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,33 +163,33 @@ describe("useOtpInput", () => {
});
});

test("handleTextChange() should NOT call setText() and onTextChange() with value when type is numeric and value has letters", () => {
const value = "abc123";
const mockOnTextChange = jest.fn();
jest.spyOn(React, "useState").mockImplementation(() => ["", jest.fn()]);

const { result } = renderUseOtInput({ onTextChange: mockOnTextChange, type: "numeric" });
result.current.actions.handleTextChange(value);

act(() => {
expect(result.current.forms.setText).not.toHaveBeenCalledWith(value);
expect(mockOnTextChange).not.toHaveBeenCalledWith(value);
});
});

test("handleTextChange() should NOT call setText() and onTextChange() with value when type is alphanumeric and value has special characters", () => {
const value = "a1/*-+";
const mockOnTextChange = jest.fn();
jest.spyOn(React, "useState").mockImplementation(() => ["", jest.fn()]);

const { result } = renderUseOtInput({ onTextChange: mockOnTextChange, type: "alphanumeric" });
result.current.actions.handleTextChange(value);

act(() => {
expect(result.current.forms.setText).not.toHaveBeenCalledWith(value);
expect(mockOnTextChange).not.toHaveBeenCalledWith(value);
});
});
test("handleTextChange() should NOT call setText() and onTextChange() with value when type is numeric and value has letters", () => {
const value = "abc123";
const mockOnTextChange = jest.fn();
jest.spyOn(React, "useState").mockImplementation(() => ["", jest.fn()]);

const { result } = renderUseOtInput({ onTextChange: mockOnTextChange, type: "numeric" });
result.current.actions.handleTextChange(value);

act(() => {
expect(result.current.forms.setText).not.toHaveBeenCalledWith(value);
expect(mockOnTextChange).not.toHaveBeenCalledWith(value);
});
});

test("handleTextChange() should NOT call setText() and onTextChange() with value when type is alphanumeric and value has special characters", () => {
const value = "a1/*-+";
const mockOnTextChange = jest.fn();
jest.spyOn(React, "useState").mockImplementation(() => ["", jest.fn()]);

const { result } = renderUseOtInput({ onTextChange: mockOnTextChange, type: "alphanumeric" });
result.current.actions.handleTextChange(value);

act(() => {
expect(result.current.forms.setText).not.toHaveBeenCalledWith(value);
expect(mockOnTextChange).not.toHaveBeenCalledWith(value);
});
});

test("handleTextChange() should not proceed if the input is disabled", () => {
const value = "123456";
Expand Down Expand Up @@ -249,6 +249,32 @@ describe("useOtpInput", () => {
});
});

test("handleFocus() should run user provided callback", () => {
const mockOnFocus = jest.fn();

const { result } = renderUseOtInput({
onFocus: mockOnFocus,
});
result.current.actions.handleFocus();

act(() => {
expect(mockOnFocus).toBeCalled();
});
});

test("handleBlur() should run user provided callback", () => {
const mockOnBlur = jest.fn();

const { result } = renderUseOtInput({
onBlur: mockOnBlur,
});
result.current.actions.handleBlur();

act(() => {
expect(mockOnBlur).toBeCalled();
});
});

test("should blur the input when filled if blurOnFilled is 'true'", () => {
jest.spyOn(React, "useRef").mockReturnValue({ current: { blur: jest.fn() } } as any);

Expand Down
4 changes: 4 additions & 0 deletions src/OtpInput/useOtpInput.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ export const useOtpInput = ({
autoFocus = true,
blurOnFilled,
type,
onFocus,
onBlur,
}: OtpInputProps) => {
const [text, setText] = useState("");
const [isFocused, setIsFocused] = useState(autoFocus);
Expand Down Expand Up @@ -55,10 +57,12 @@ export const useOtpInput = ({

const handleFocus = () => {
setIsFocused(true);
onFocus?.();
};

const handleBlur = () => {
setIsFocused(false);
onBlur?.();
};

return {
Expand Down
Loading