Skip to content

Commit

Permalink
fix(textarea): autoresize textarea resets when value is empty (#6031)
Browse files Browse the repository at this point in the history
  • Loading branch information
dbanksdesign authored Nov 11, 2024
1 parent 34f8d2d commit 2e3a051
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 1 deletion.
7 changes: 7 additions & 0 deletions .changeset/fluffy-plums-grin.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@aws-amplify/ui-react": patch
---

fix(textarea): autoresize textarea resets when value is empty

There was a bug with the autoresize text area where it would not reset the size when a value was cleared. This fixes that bug
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { renderHook } from '@testing-library/react-hooks';
import { useAutoresizeTextArea } from '../useAutoresizeTextarea';

describe('useAutoresizeTextArea', () => {
let textAreaRef: HTMLTextAreaElement;

beforeEach(() => {
textAreaRef = document.createElement('textarea');
jest.spyOn(window, 'addEventListener');
jest.spyOn(window, 'removeEventListener');
});

afterEach(() => {
jest.restoreAllMocks();
});

it('should set the height of the textarea', () => {
renderHook(() => useAutoresizeTextArea(textAreaRef, 'initial value'));
expect(textAreaRef.style.height).toBe('0px');
});

it('should add and remove event listener for window resize', () => {
const { unmount } = renderHook(() => useAutoresizeTextArea(textAreaRef));
expect(window.addEventListener).toHaveBeenCalledWith(
'resize',
expect.any(Function)
);

unmount();
expect(window.removeEventListener).toHaveBeenCalledWith(
'resize',
expect.any(Function)
);
});

it('should not throw error when textAreaRef is null', () => {
expect(() => {
renderHook(() => useAutoresizeTextArea(null));
}).not.toThrow();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export const useAutoresizeTextArea = (
): void => {
useEffect(() => {
const resizeTextArea = () => {
if (textAreaRef && value) {
if (textAreaRef) {
// We need to reset the height momentarily to get the correct scrollHeight for the textarea
textAreaRef.style.height = 'auto';
const { scrollHeight } = textAreaRef;
Expand Down

0 comments on commit 2e3a051

Please sign in to comment.