Skip to content

Commit

Permalink
fix: Leaving editable tab in error state (#37981)
Browse files Browse the repository at this point in the history
  • Loading branch information
hetunandu authored Dec 6, 2024
1 parent ba7c158 commit a450226
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ describe("EditableName", () => {

await userEvent.click(document.body);

expect(getByRole("tooltip").textContent).toEqual(validationError);
expect(getByRole("tooltip").textContent).toEqual("");

expect(exitEditing).toHaveBeenCalled();
expect(onNameSave).not.toHaveBeenCalledWith(invalidTitle);
Expand Down Expand Up @@ -187,7 +187,7 @@ describe("EditableName", () => {
});

fireEvent.focusOut(inputElement);
expect(getByRole("tooltip").textContent).toEqual(validationError);
expect(getByRole("tooltip").textContent).toEqual("");
expect(exitEditing).toHaveBeenCalled();
expect(onNameSave).not.toHaveBeenCalledWith(invalidTitle);
});
Expand Down
16 changes: 14 additions & 2 deletions app/client/src/IDE/Components/EditableName/EditableName.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ import React, {
useRef,
useState,
} from "react";
import { Spinner, Text, Tooltip } from "@appsmith/ads";
import { Spinner, Text as ADSText, Tooltip } from "@appsmith/ads";
import { useEventCallback, useEventListener } from "usehooks-ts";
import { usePrevious } from "@mantine/hooks";
import { useNameEditor } from "./useNameEditor";
import styled from "styled-components";

interface EditableTextProps {
name: string;
Expand All @@ -29,6 +30,10 @@ interface EditableTextProps {
inputTestId?: string;
}

export const Text = styled(ADSText)`
min-width: 3ch;
`;

export const EditableName = ({
exitEditing,
icon,
Expand Down Expand Up @@ -72,10 +77,15 @@ export const EditableName = ({
const nameError = validate(editableName);

if (editableName === name) {
// No change detected
exitWithoutSaving();
} else if (nameError === null) {
// Save the new name
exitEditing();
onNameSave(editableName);
} else {
// Exit edit mode and revert name
exitWithoutSaving();
}
}, [
editableName,
Expand Down Expand Up @@ -119,7 +129,9 @@ export const EditableName = ({
useEventListener(
"focusout",
function handleFocusOut() {
if (isEditing) {
const input = inputRef.current;

if (input) {
attemptSave();
}
},
Expand Down

0 comments on commit a450226

Please sign in to comment.