Skip to content

Commit

Permalink
Merge pull request #6998 from Sage/FE-6873
Browse files Browse the repository at this point in the history
fix(textarea): ensure expandable height shrinks when new lines are removed
  • Loading branch information
nuria1110 authored Oct 8, 2024
2 parents 5700920 + ad93620 commit c089a91
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 6 deletions.
2 changes: 2 additions & 0 deletions src/components/textarea/textarea.component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,8 @@ export const Textarea = React.forwardRef(

const scrollPosition = scrollElement?.scrollTop;

// Reset height to allow shrinking when lines are removed
textarea.style.height = "auto";
// Set the height so all content is shown
textarea.style.height = `${Math.max(
textarea.scrollHeight,
Expand Down
40 changes: 34 additions & 6 deletions src/components/textarea/textarea.pw.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -673,19 +673,47 @@ test.describe("Props tests for Textarea component", () => {
await mount(<TextareaComponent expandable={expandableValue} />);

const textareaChildrenElement = textareaChildren(page);
await textareaChildrenElement.type("t");
await textareaChildrenElement.press("t");
await textareaChildrenElement.press("Enter");
await textareaChildrenElement.type("e");
await textareaChildrenElement.press("e");
await textareaChildrenElement.press("Enter");
await textareaChildrenElement.type("s");
await textareaChildrenElement.press("s");
await textareaChildrenElement.press("Enter");
await textareaChildrenElement.type("t");
await textareaChildrenElement.press("t");

await expect(textareaChildrenElement).toHaveCSS("height", `${height}px`);
});
});
});

test(`should verify expandable Textarea shrinks back to original height when lines are removed`, async ({
mount,
page,
}) => {
await mount(<TextareaComponent expandable />);

const textareaChildrenElement = textareaChildren(page);
await textareaChildrenElement.press("t");
await textareaChildrenElement.press("Enter");
await textareaChildrenElement.press("e");
await textareaChildrenElement.press("Enter");
await textareaChildrenElement.press("s");
await textareaChildrenElement.press("Enter");
await textareaChildrenElement.press("t");

await expect(textareaChildrenElement).toHaveCSS("height", "92px");

await textareaChildrenElement.press("Backspace");
await textareaChildrenElement.press("Backspace");

await expect(textareaChildrenElement).toHaveCSS("height", "75px");

await textareaChildrenElement.press("Backspace");
await textareaChildrenElement.press("Backspace");

await expect(textareaChildrenElement).toHaveCSS("height", "64px");
});

test.describe("Event tests for Textarea component", () => {
const inputValue = "1";
test("should call onChange callback when a type event is triggered", async ({
Expand All @@ -702,7 +730,7 @@ test.describe("Event tests for Textarea component", () => {
);

const textareaChildrenElement = textareaChildren(page);
await textareaChildrenElement.type(inputValue);
await textareaChildrenElement.fill(inputValue);

expect(callbackCount).toEqual(1);
});
Expand Down Expand Up @@ -1042,7 +1070,7 @@ test("should not change the scroll position of a scrollable container when typin
const textareaChildrenElement = textareaChildren(page);
await textareaChildrenElement.click();
await textareaChildrenElement.press("ArrowRight");
await textareaChildrenElement.type("foo");
await textareaChildrenElement.press("f");

const scrollTop = await formContentElement.evaluate((el) => el.scrollTop);

Expand Down

0 comments on commit c089a91

Please sign in to comment.