import React, { useState } from "react"; import { screen, userEvent } from "@storybook/testing-library"; import { expect, jest } from "@storybook/jest"; import type { Meta, StoryFn } from "@storybook/react"; import { Icon } from "../icon"; import { Button } from "../button"; import Asset from "@washingtonpost/wpds-assets/asset/settings"; import { VisuallyHidden } from "../visually-hidden"; import { styled, theme } from "../theme"; import { InputText as Component } from "./"; export default { title: "InputText", component: Component, argTypes: { label: { defaultValue: "Label", }, type: { defaultValue: "text", }, icon: { defaultValue: "left", }, onButtonIconClick: jest.fn(), children: { options: ["None", "Settings Icon"], mapping: { None: undefined, "Settings Icon": ( ), }, }, }, args: { label: "Label", type: "text", icon: "left", onButtonIconClick: jest.fn(), }, } as Meta; export const InputText = { parameters: { chromatic: { disableSnapshot: true }, }, }; export const AutoFocus = { args: { autoFocus: true, }, }; const SynchContainer = styled("div", { display: "flex", alignItems: "center", gap: theme.space["050"], }); const SynchTemplate = () => { const [val1, setVal1] = useState("Value"); const [val2, setVal2] = useState(""); return ( setVal1(e.target.value)} />
setVal2(e.target.value)} />
); }; export const InputSynch = { render: SynchTemplate, }; const Column = styled("div", { display: "flex", flexDirection: "column", width: "100%", gap: "$100", }); const Heading = styled("h2", { color: theme.colors.primary, fontSize: theme.fontSizes["100"], marginBlock: 0, }); const ChromaticTemplate = () => ( Standard input Icon Placements Types Behaviors ); export const Chromatic = { render: ChromaticTemplate, }; const FormTemplate: StoryFn = (args) => { const [iconClicked, setIconClicked] = useState(false); const [submitClicked, setSubmitClicked] = useState(false); return (
{ e.preventDefault(); setSubmitClicked(true); }} > { setIconClicked(true); }} buttonIconType={args.buttonIconType} />
icon clicked: {iconClicked.toString()}
submit clicked: {submitClicked.toString()}
); }; export const SearchTypeInForm = { render: FormTemplate, args: { buttonIconType: "button", }, parameters: { chromatic: { disableSnapshot: true }, }, argTypes: { buttonIconText: { table: { disable: true } }, disabled: { table: { disable: true } }, error: { table: { disable: true } }, label: { table: { disable: true } }, icon: { table: { disable: true } }, name: { table: { disable: true } }, id: { table: { disable: true } }, placeholder: { table: { disable: true } }, required: { table: { disable: true } }, success: { table: { disable: true } }, value: { table: { disable: true } }, defaultValue: { table: { disable: true } }, type: { table: { disable: true } }, css: { table: { disable: true } }, errorMessage: { table: { disable: true } }, helperText: { table: { disable: true } }, children: { table: { disable: true } }, }, }; const InteractionsTemplate: StoryFn = (args) => ( <> Settings ); export const Interactions = { render: InteractionsTemplate, parameters: { chromatic: { disableSnapshot: true }, }, play: async ({ args }) => { // radix Label needs a tick to associate labels with inputs await sleep(0); const label1 = screen.getAllByText("Field 1")[0]; await expect(label1).toHaveStyle("font-size: 16px"); await userEvent.type(screen.getAllByRole("textbox")[0], "user text", { delay: 100, }); await expect(label1).toHaveStyle("font-size: 12px"); await userEvent.tab(); await sleep(250); const label2 = screen.getAllByText("Field 2")[0]; await expect(label2).toHaveStyle("font-size: 12px"); await userEvent.tab(); await sleep(250); await expect(label2).toHaveStyle("font-size: 16px"); await userEvent.click(screen.getAllByRole("button")[0]); await expect(args.onButtonIconClick).toHaveBeenCalled(); }, }; // Function to emulate pausing between interactions function sleep(ms) { return new Promise((resolve) => setTimeout(resolve, ms)); } export const TypeSearch = { args: { type: "search", }, parameters: { chromatic: { disableSnapshot: true }, }, }; export const AutoFilledTypeText = { args: { type: "text", name: "my-name", autoComplete: "given-name", id: "my-name", }, parameters: { chromatic: { disableSnapshot: true }, }, };