From 45fa219f8c642f4c4cf4e1b3d8087989812132cb Mon Sep 17 00:00:00 2001 From: baiamansama <79898363+baiamansama@users.noreply.github.com> Date: Wed, 18 Dec 2024 03:16:42 +0900 Subject: [PATCH] Added unFocusTextArea to manage text area blur functionality (#286) --- .../hooks/internal/useTextAreaInternal.test.ts | 15 +++++++++++++++ src/hooks/internal/useTextAreaInternal.ts | 14 ++++++++++++-- src/hooks/useTextArea.ts | 8 +++++--- 3 files changed, 32 insertions(+), 5 deletions(-) diff --git a/__tests__/hooks/internal/useTextAreaInternal.test.ts b/__tests__/hooks/internal/useTextAreaInternal.test.ts index 26a135a0..54126a4e 100644 --- a/__tests__/hooks/internal/useTextAreaInternal.test.ts +++ b/__tests__/hooks/internal/useTextAreaInternal.test.ts @@ -158,6 +158,21 @@ describe("useTextAreaInternal Hook", () => { expect(result.current.textAreaDisabled).toBe(false); }); + it("should blur on text area when blurTextArea is called", () => { + const callRcbEventMock = jest.fn(); + mockUseRcbEventInternal.mockReturnValue({ + callRcbEvent: callRcbEventMock, + }); + + const { result } = renderHook(() => useTextAreaInternal()); + + act(() => { + result.current.blurTextArea(); + }); + + expect(mockInputRef.current.blur).toHaveBeenCalled(); + }); + // let initialTextAreaDisabled = false; /* it("should toggle textAreaDisabled state", () => { diff --git a/src/hooks/internal/useTextAreaInternal.ts b/src/hooks/internal/useTextAreaInternal.ts index ac555c07..a796e261 100644 --- a/src/hooks/internal/useTextAreaInternal.ts +++ b/src/hooks/internal/useTextAreaInternal.ts @@ -105,7 +105,16 @@ export const useTextAreaInternal = () => { }, [textAreaDisabled]); /** - * Retrieves text area value. + * Blurs on text area. + */ + const blurTextArea = useCallback(() => { + if (inputRef.current) { + inputRef.current.blur(); + } + }, []); + /** + + * Retrieves text area value. */ const getTextAreaValue = useCallback(() => { if (inputRef && inputRef.current) { @@ -140,7 +149,8 @@ export const useTextAreaInternal = () => { setTextAreaValue, updateTextAreaFocus, focusTextArea, + blurTextArea, toggleTextAreaDisabled, toggleTextAreaSensitiveMode }; -}; +}; \ No newline at end of file diff --git a/src/hooks/useTextArea.ts b/src/hooks/useTextArea.ts index d808e9a4..91e5394a 100644 --- a/src/hooks/useTextArea.ts +++ b/src/hooks/useTextArea.ts @@ -17,7 +17,8 @@ export const useTextArea = () => { toggleTextAreaSensitiveMode, getTextAreaValue, setTextAreaValue, - focusTextArea + focusTextArea, + blurTextArea } = useTextAreaInternal(); return { @@ -27,6 +28,7 @@ export const useTextArea = () => { toggleTextAreaSensitiveMode, getTextAreaValue, setTextAreaValue, - focusTextArea + focusTextArea, + blurTextArea }; -}; +}; \ No newline at end of file