Skip to content

Commit 55a723a

Browse files
authored
expose blur ref (#103)
1 parent 45ff916 commit 55a723a

File tree

6 files changed

+25
-3
lines changed

6 files changed

+25
-3
lines changed

README.MD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ The `react-native-otp-entry` component exposes these functions with `ref`:
144144
| ---------- | ------------------------ | ---------------------------------- |
145145
| `clear` | () => void; | Clears the value of the OTP input. |
146146
| `focus` | () => void; | Focus of the OTP input. |
147+
| `blur` | () => void; | Blurs the OTP input. |
147148
| `setValue` | (value: string) => void; | Sets the value of the OTP input. |
148149

149150
## License

src/OtpInput/OtpInput.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { useOtpInput } from "./useOtpInput";
99
export const OtpInput = forwardRef<OtpInputRef, OtpInputProps>((props, ref) => {
1010
const {
1111
models: { text, inputRef, focusedInputIndex, isFocused, placeholder },
12-
actions: { clear, handlePress, handleTextChange, focus, handleFocus, handleBlur },
12+
actions: { clear, handlePress, handleTextChange, focus, handleFocus, handleBlur, blur },
1313
forms: { setTextWithRef },
1414
} = useOtpInput(props);
1515
const {
@@ -37,7 +37,7 @@ export const OtpInput = forwardRef<OtpInputRef, OtpInputProps>((props, ref) => {
3737
placeholderTextStyle,
3838
} = theme;
3939

40-
useImperativeHandle(ref, () => ({ clear, focus, setValue: setTextWithRef }));
40+
useImperativeHandle(ref, () => ({ clear, focus, setValue: setTextWithRef, blur }));
4141

4242
const generatePinCodeContainerStyle = (isFocusedContainer: boolean, char: string) => {
4343
const stylesArray = [styles.codeContainer, pinCodeContainerStyle];

src/OtpInput/OtpInput.types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ export interface OtpInputRef {
2424
clear: () => void;
2525
focus: () => void;
2626
setValue: (value: string) => void;
27+
blur: () => void;
2728
}
2829

2930
export interface Theme {

src/OtpInput/__tests__/useOtpInput.test.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,17 @@ describe("useOtpInput", () => {
4848
});
4949
});
5050

51+
test("blur() should blur input", () => {
52+
jest.spyOn(React, "useRef").mockReturnValueOnce({ current: { blur: jest.fn() } } as any);
53+
54+
const { result } = renderUseOtInput();
55+
result.current.actions.blur();
56+
57+
act(() => {
58+
expect(result.current.models.inputRef.current?.blur).toHaveBeenCalled();
59+
});
60+
});
61+
5162
test("setTextWithRef() should only call setText the first 'numberOfDigits' characters", () => {
5263
jest.spyOn(React, "useState").mockImplementation(() => ["", jest.fn()]);
5364
const { result } = renderUseOtInput();

src/OtpInput/useOtpInput.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,10 @@ export const useOtpInput = ({
6161
inputRef.current?.focus();
6262
};
6363

64+
const blur = () => {
65+
inputRef.current?.blur();
66+
};
67+
6468
const handleFocus = () => {
6569
setIsFocused(true);
6670
onFocus?.();
@@ -73,7 +77,7 @@ export const useOtpInput = ({
7377

7478
return {
7579
models: { text, inputRef, focusedInputIndex, isFocused, placeholder },
76-
actions: { handlePress, handleTextChange, clear, focus, handleFocus, handleBlur },
80+
actions: { handlePress, handleTextChange, clear, focus, blur, handleFocus, handleBlur },
7781
forms: { setText, setTextWithRef },
7882
};
7983
};

src/index.d.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,11 @@ declare module "OTPInput" {
107107
* @param value - The value to be set.
108108
*/
109109
setValue: (value: string) => void;
110+
111+
/**
112+
* Blur the OTP input.
113+
*/
114+
blur: () => void;
110115
}
111116

112117
export interface Theme {

0 commit comments

Comments
 (0)