Skip to content

Open source website builder and Webflow alternative. Webstudio is an advanced visual builder that connects to any headless CMS, supports all CSS properties, and can be hosted anywhere, including with us.

tests for the search field

Oleg Isonenca163610883e
diff --git a/packages/design-system/src/components/search-field.stories.tsx b/packages/design-system/src/components/search-field.stories.tsx
new file mode 100644
index 0000000000..8e21643517
--- /dev/null
+++ b/packages/design-system/src/components/search-field.stories.tsx
@@ -0,0 +1,45 @@
+import { expect } from "@storybook/jest";
+import { type ComponentStory } from "@storybook/react";
+import { userEvent, waitFor, within } from "@storybook/testing-library";
+import React, { useState } from "react";
+import { SearchField } from "./search-field";
+
+export default {
+ component: SearchField,
+};
+
+export const Empty: ComponentStory<typeof SearchField> = () => {
+ return <SearchField />;
+};
+
+export const WithInitialValue: ComponentStory<typeof SearchField> = () => {
+ return <SearchField value={"somevalue"} title="Search" />;
+};
+
+let updateValue: (value: string) => void;
+
+export const UpdateValue: ComponentStory<typeof SearchField> = () => {
+ const [value, setValue] = useState("something");
+ updateValue = setValue;
+ return <SearchField value={value} title="Search" />;
+};
+
+UpdateValue.play = async ({ canvasElement }) => {
+ const canvas = within(canvasElement);
+ const search = canvas.getByTitle<HTMLInputElement>("Search");
+ expect(search.value).toBe("something");
+ updateValue?.("test");
+ await waitFor(() => expect(search.value).toBe("test"));
+};
+
+export const Reset: ComponentStory<typeof SearchField> = () => {
+ return <SearchField value="something" title="Search" />;
+};
+
+Reset.play = async ({ canvasElement }) => {
+ const canvas = within(canvasElement);
+ const search = canvas.getByTitle<HTMLInputElement>("Search");
+ await userEvent.click(canvas.getByTitle("Reset search"));
+ await waitFor(() => expect(search.value).toBe(""));
+ expect(await canvas.queryByTitle("Reset search")).toBeNull();
+};
diff --git a/packages/design-system/src/components/search-field.tsx b/packages/design-system/src/components/search-field.tsx
index 5da634de4a..20f1c5a817 100644
--- a/packages/design-system/src/components/search-field.tsx
+++ b/packages/design-system/src/components/search-field.tsx
@@ -4,6 +4,7 @@ import {
forwardRef,
useRef,
useState,
+ useEffect,
} from "react";
import { Cross2Icon, MagnifyingGlassIcon } from "@webstudio-is/icons";
import { TextField } from "./text-field";
@@ -11,11 +12,14 @@ import { IconButton } from "./icon-button";
const SearchFieldBase: ForwardRefRenderFunction<
HTMLInputElement,
- ComponentProps<typeof TextField> & { onCancel: () => void }
+ ComponentProps<typeof TextField> & { onCancel?: () => void }
> = (props, ref) => {
- const { onChange, onCancel, ...rest } = props;
- const [value, setValue] = useState("");
+ const { onChange, onCancel, value: propsValue = "", ...rest } = props;
+ const [value, setValue] = useState(String(propsValue));
const inputRef = useRef<HTMLInputElement>(null);
+ useEffect(() => {
+ setValue(String(propsValue));
+ }, [propsValue]);
return (
<TextField
{...rest}
@@ -32,6 +36,7 @@ const SearchFieldBase: ForwardRefRenderFunction<
value.length > 0 && (
<IconButton
aria-label="Reset search"
+ title="Reset search"
tabIndex={-1}
onClick={() => {
inputRef.current?.focus();