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

Support removing array items #237

Merged
merged 3 commits into from
Jun 22, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 5 additions & 0 deletions e2e-tests/src/components/Banner.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ export interface BannerProps {
obj?: {
nestedString?: string;
nestedBool?: boolean;
nestedLitArr?: boolean[];
nestedObj?: {
nestedNum?: number;
nestedColor?: HexColor;
nestedExpArr?: string[];
};
};
}
Expand All @@ -21,6 +23,9 @@ export const initialProps: BannerProps = {
num: 5,
bool: true,
title: "initial title",
obj: {
nestedLitArr: [true, false],
},
};

export default function Banner(props: BannerProps) {
Expand Down
1 change: 1 addition & 0 deletions e2e-tests/tests/nested-props.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { expect } from "@playwright/test";
import { studioTest } from "./infra/studioTest.js";

studioTest("renders nested props", async ({ page, studioPage }) => {
await studioPage.addElement("Banner", "Components");
await studioPage.setActiveComponent("Banner");
await expect(page.getByTestId("EditorSidebar")).toHaveScreenshot({
maxDiffPixelRatio: 0.015,
Expand Down
28 changes: 26 additions & 2 deletions packages/studio/src/components/ArrayPropEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { renderPropEditor } from "./PropEditors";
import { ReactComponent as Plus } from "../icons/plus.svg";
import PropValueHelpers from "../utils/PropValueHelpers";
import classNames from "classnames";
import RemovableElement from "./RemovableElement";

interface ArrayPropEditorProps {
propName: string;
Expand Down Expand Up @@ -131,19 +132,42 @@ function LiteralEditor({
[itemType, value, updateItems]
);

const generateRemoveItem = useCallback(
(index: number) => () => {
const updatedItems = [...value];
updatedItems.splice(index, 1);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can try toSpliced here!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like support for it is being added in TS, but they haven't released a version that includes it yet

updateItems(updatedItems);
},
[value, updateItems]
);

const buttonClasses = classNames("ml-3", {
"self-start mt-2": itemType.type === PropValueType.Object,
"mb-2": itemType.type !== PropValueType.Object,
});

return (
<>
{value.length > 0 && (
<div className="pt-2 ml-2 border-l-2">
{Object.entries(propValues).map(([name, propVal]) => {
{Object.entries(propValues).map(([name, propVal], index) => {
const editor = renderPropEditor(
name,
{ ...itemType, required: false },
propVal,
updateItem,
true
);
return <div key={name}>{editor}</div>;
return (
<RemovableElement
key={name}
onRemove={generateRemoveItem(index)}
buttonClasses={buttonClasses}
ariaLabel={`Remove ${name}`}
>
{editor}
</RemovableElement>
);
})}
</div>
)}
Expand Down
27 changes: 27 additions & 0 deletions packages/studio/src/components/RemovableElement.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { PropsWithChildren } from "react";
import { ReactComponent as X } from "../icons/x.svg";

export interface RemovableElementProps {
onRemove: () => void;
buttonClasses?: string;
ariaLabel?: string;
}

export default function RemovableElement(
props: PropsWithChildren<RemovableElementProps>
) {
const { children, onRemove, buttonClasses, ariaLabel } = props;

return (
<div className="flex w-full">
{children}
<button
onClick={onRemove}
className={buttonClasses}
aria-label={ariaLabel}
>
<X />
</button>
</div>
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,17 @@ describe("Array prop", () => {
value: "document.strings",
valueType: PropValueType.Array,
};
const literalPropVal: PropVal = {
kind: PropValueKind.Literal,
valueType: PropValueType.Array,
value: [
{
kind: PropValueKind.Expression,
valueType: PropValueType.string,
value: "document.name",
},
],
};
const propShape: PropShape = {
arr: {
type: PropValueType.Array,
Expand Down Expand Up @@ -456,31 +467,20 @@ describe("Array prop", () => {
});

it("correctly adds item to existing literal value", async () => {
const propVal: PropVal = {
kind: PropValueKind.Literal,
valueType: PropValueType.Array,
value: [
{
kind: PropValueKind.Expression,
valueType: PropValueType.string,
value: "document.name",
},
],
};
mockStoreActiveComponent({
activeComponent: {
...activeComponentState,
props: { arr: propVal },
props: { arr: literalPropVal },
},
});
render(<ActiveComponentPropEditorsWrapper />);

await userEvent.click(screen.getByRole("button", { name: "Add Item" }));
expect(getComponentProps()).toEqual({
arr: {
...propVal,
...literalPropVal,
value: [
...propVal.value,
...literalPropVal.value,
{
kind: PropValueKind.Literal,
valueType: PropValueType.string,
Expand All @@ -494,4 +494,64 @@ describe("Array prop", () => {
);
expect(screen.getByRole("textbox", { name: "Item 2" })).toHaveValue("");
});

it("correctly removes item when there are multiple", async () => {
const propVal: PropVal = {
...literalPropVal,
value: [
{
kind: PropValueKind.Expression,
valueType: PropValueType.string,
value: "document.title",
},
...literalPropVal.value,
],
};
mockStoreActiveComponent({
activeComponent: {
...activeComponentState,
props: { arr: propVal },
},
});
render(<ActiveComponentPropEditorsWrapper />);

await userEvent.click(
screen.getByRole("button", { name: "Remove Item 1" })
);
expect(getComponentProps()).toEqual({ arr: literalPropVal });
expect(screen.getByRole("textbox", { name: "Item 1" })).toHaveValue(
"[[name]]"
);
expect(screen.queryByRole("textbox", { name: "Item 2" })).toBeNull();
const expressionInput = screen.getByRole("textbox", {
name: (text) => text.startsWith("arr"),
});
expect(expressionInput).toHaveValue("");
expect(expressionInput).toBeDisabled();
});

it("correctly removes item when there is only one", async () => {
mockStoreActiveComponent({
activeComponent: {
...activeComponentState,
props: { arr: literalPropVal },
},
});
render(<ActiveComponentPropEditorsWrapper />);

await userEvent.click(
screen.getByRole("button", { name: "Remove Item 1" })
);
expect(getComponentProps()).toEqual({
arr: {
...literalPropVal,
value: [],
},
});
expect(screen.queryByRole("textbox", { name: "Item 1" })).toBeNull();
const expressionInput = screen.getByRole("textbox");
expect(expressionInput).toHaveValue("");
expect(expressionInput).toBeEnabled();
expect(screen.queryByRole("tooltip")).toBeNull();
});
});