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

fix(EditableTypography): Enter and Esc click to end edit mode is bubbling to other places afterwards #2696

Merged
Show file tree
Hide file tree
Changes from 2 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
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React from "react";
import { fireEvent, render, cleanup, screen, waitFor } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import EditableText from "../EditableText";
import { within } from "@storybook/testing-library";

Expand Down Expand Up @@ -223,6 +224,52 @@ describe("EditableText", () => {
});
});

describe("event bubbling and propagation", () => {
it("should prevent Enter key press from propagating outside EditableText", () => {
const onChange = jest.fn();
const externalKeyHandler = jest.fn();

render(
<div onKeyDown={externalKeyHandler} data-testid="external-container">
<EditableText value="Editable text" onChange={onChange} />
</div>
);

const component = screen.getByRole("button");
fireEvent.click(component);

const input = screen.getByRole("input");
fireEvent.change(input, { target: { value: "New value" } });
userEvent.keyboard("{enter}");

expect(onChange).toHaveBeenCalledWith("New value");
expect(externalKeyHandler).not.toHaveBeenCalled();
});

it("should prevent Esc key press from propagating outside EditableText", () => {
const onChange = jest.fn();
const onEditModeChange = jest.fn();
const externalKeyHandler = jest.fn();

render(
<div onKeyDown={externalKeyHandler} data-testid="external-container">
<EditableText value="Editable text" onChange={onChange} onEditModeChange={onEditModeChange} />
</div>
);

const component = screen.getByRole("button");
fireEvent.click(component);

const input = screen.getByRole("input");
fireEvent.change(input, { target: { value: "New value" } });
userEvent.keyboard("{escape}");

expect(onChange).not.toHaveBeenCalled();
expect(onEditModeChange).toHaveBeenCalledTimes(2);
expect(externalKeyHandler).not.toHaveBeenCalled();
});
});

describe("with placeholder", () => {
it("should show a placeholder if provided and input is empty", async () => {
const onChange = jest.fn();
Expand Down Expand Up @@ -274,3 +321,4 @@ describe("EditableText", () => {
});
});
});

Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,12 @@ const EditableTypography: VibeComponent<EditableTypographyProps, HTMLElement> =
}

event.preventDefault();
event.stopPropagation();
handleInputValueChange();
}
if (event.key === keyCodes.ESCAPE) {
event.preventDefault();
event.stopPropagation();
handleEditModeChange(false);
setInputValue(value);
}
Expand Down Expand Up @@ -311,3 +314,4 @@ const EditableTypography: VibeComponent<EditableTypographyProps, HTMLElement> =
);

export default EditableTypography;

Loading