Skip to content

Commit

Permalink
feat(EditableText): show placeholder when empty (#1921)
Browse files Browse the repository at this point in the history
  • Loading branch information
talkor authored Feb 5, 2024
1 parent 2ccfdff commit 71cd328
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 3 deletions.
1 change: 1 addition & 0 deletions src/components/EditableText/EditableText.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ const EditableText: VibeComponent<EditableTextProps, HTMLElement> & {
data-testid={dataTestId || getTestId(ComponentDefaultTestId.EDITABLE_TEXT, id)}
component={Text}
typographyClassName={cx(getStyle(styles, camelCase(type + "-" + weight)), styles.typography)}
clearable
{...editableTypographyProps}
/>
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import React from "react";
import { fireEvent, render, cleanup, screen } from "@testing-library/react";
import { fireEvent, render, cleanup, screen, waitFor } from "@testing-library/react";
import "@testing-library/jest-dom";
import EditableText from "../EditableText";
import { within } from "@storybook/testing-library";

describe("EditableText", () => {
afterEach(() => {
Expand Down Expand Up @@ -50,4 +51,29 @@ describe("EditableText", () => {
});
});
});

describe("with placeholder", () => {
it("should show a placeholder if provided and input is empty", async () => {
const placeholderText = "Placeholder text";
const value = "Editable test";
render(<EditableText value={value} placeholder={placeholderText} />);

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

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

expect(input).toHaveValue("");
expect(screen.getByPlaceholderText(placeholderText)).toBeInTheDocument();

await waitFor(() => {
fireEvent.keyDown(input, { key: "Enter" });
});

expect(within(screen.getByRole("button")).getByText(placeholderText)).toBeInTheDocument();
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,9 @@
visibility: hidden;
white-space: pre;
}

&.placeholder {
color: var(--secondary-text-color);
}
}
}
10 changes: 8 additions & 2 deletions src/components/EditableTypography/EditableTypography.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ interface EditableTypographyProps extends VibeComponentProps, EditableTypography
component: ElementType;
/** Controls the style of the typography component in view mode */
typographyClassName: string;
/** Shows placeholder when empty, if provided */
clearable?: boolean;
}

const EditableTypography: VibeComponent<EditableTypographyProps, HTMLElement> = forwardRef(
Expand All @@ -48,6 +50,7 @@ const EditableTypography: VibeComponent<EditableTypographyProps, HTMLElement> =
readOnly = false,
ariaLabel = "",
placeholder,
clearable,
typographyClassName,
component: TypographyComponent,
isEditMode,
Expand Down Expand Up @@ -90,7 +93,9 @@ const EditableTypography: VibeComponent<EditableTypographyProps, HTMLElement> =

function handleInputValueChange() {
handleEditModeChange(false);
if (!inputValue || value === inputValue) {

const shouldShowPlaceholderWhenEmpty = clearable && placeholder;
if ((!inputValue && !shouldShowPlaceholderWhenEmpty) || value === inputValue) {
setInputValue(value);
return;
}
Expand Down Expand Up @@ -167,7 +172,8 @@ const EditableTypography: VibeComponent<EditableTypographyProps, HTMLElement> =
aria-hidden={isEditing}
className={cx(styles.typography, typographyClassName, {
[styles.hidden]: isEditing,
[styles.disabled]: readOnly
[styles.disabled]: readOnly,
[styles.placeholder]: !inputValue && placeholder
})}
tabIndex={0}
tooltipProps={tooltipProps}
Expand Down

0 comments on commit 71cd328

Please sign in to comment.