From 951112cb6574bd96ea4a43a62f0ecd989f62b89d Mon Sep 17 00:00:00 2001 From: Daniel Sil Date: Tue, 3 Oct 2023 13:08:02 +0200 Subject: [PATCH] chore(Switch): migrate to Tailwind --- .../tmp-tailwind/Switch/Switch.stories.tsx | 79 +++++++++++++++++++ .../Switch/__tests__/index.test.tsx | 39 +++++++++ .../src/tmp-tailwind/Switch/index.tsx | 59 ++++++++++++++ 3 files changed, 177 insertions(+) create mode 100644 packages/orbit-components/src/tmp-tailwind/Switch/Switch.stories.tsx create mode 100644 packages/orbit-components/src/tmp-tailwind/Switch/__tests__/index.test.tsx create mode 100644 packages/orbit-components/src/tmp-tailwind/Switch/index.tsx diff --git a/packages/orbit-components/src/tmp-tailwind/Switch/Switch.stories.tsx b/packages/orbit-components/src/tmp-tailwind/Switch/Switch.stories.tsx new file mode 100644 index 0000000000..9679306512 --- /dev/null +++ b/packages/orbit-components/src/tmp-tailwind/Switch/Switch.stories.tsx @@ -0,0 +1,79 @@ +import * as React from "react"; +import { action } from "@storybook/addon-actions"; +import { select, text, boolean } from "@storybook/addon-knobs"; + +import RenderInRtl from "../../utils/rtl/RenderInRtl"; +import * as Icons from "../../icons"; + +import Switch from "."; + +const getIcons = (name: string, defaultIcon: string) => + select(name, [null, ...Object.keys(Icons)], defaultIcon); + +const getIcon = (source: string | null) => source && Icons[source]; + +export default { + title: "Tailwind/Switch", +}; + +export const Default = () => { + const checked = boolean("checked", true); + return ; +}; + +Default.story = { + name: "Default Switch", +}; + +export const CustomIcon = () => { + const checked = boolean("checked", true); + const Icon = getIcon(getIcons("icon", "Lock")); + return } />; +}; + +CustomIcon.story = { + name: "Custom icon", + parameters: { + info: "You can try all possible configurations of this component. However, check Orbit.Kiwi for more detailed design guidelines.", + }, +}; + +export const Playground = () => { + const checked = boolean("checked", false); + const dataTest = text("dataTest", ""); + const Icon = getIcon(getIcons("icon", "Lock")); + const disabled = boolean("disabled", false); + + return ( + } + /> + ); +}; + +Playground.story = { + parameters: { + info: "You can try all possible configurations of this component. However, check Orbit.Kiwi for more detailed design guidelines.", + }, +}; + +export const Rtl = () => { + const checked = boolean("checked", true); + return ( + + + + ); +}; + +Rtl.story = { + name: "RTL", + + parameters: { + info: "This is a preview of this component in RTL setup.", + }, +}; diff --git a/packages/orbit-components/src/tmp-tailwind/Switch/__tests__/index.test.tsx b/packages/orbit-components/src/tmp-tailwind/Switch/__tests__/index.test.tsx new file mode 100644 index 0000000000..c05c70a0c3 --- /dev/null +++ b/packages/orbit-components/src/tmp-tailwind/Switch/__tests__/index.test.tsx @@ -0,0 +1,39 @@ +import * as React from "react"; +import userEvent from "@testing-library/user-event"; + +import { screen, render, fireEvent } from "../../../test-utils"; +import KEY_CODE_MAP from "../../../common/keyMaps"; +import Switch from ".."; + +describe("Switch", () => { + const user = userEvent.setup(); + + it("should have expected DOM output", async () => { + const dataTest = "test"; + const onChange = jest.fn(); + const onFocus = jest.fn(); + + render(); + + await user.click(screen.getByRole("switch")); + expect(onChange).toHaveBeenCalled(); + + fireEvent.keyDown(screen.getByTestId("test"), { keyCode: KEY_CODE_MAP.SPACE }); + fireEvent.keyDown(screen.getByTestId("test"), { keyCode: KEY_CODE_MAP.ENTER }); + expect(onChange).toHaveBeenCalledTimes(3); + + await user.tab(); + expect(onFocus).toHaveBeenCalled(); + expect(screen.getByRole("switch")).toHaveAttribute("checked"); + expect(screen.getByTestId(dataTest)).toBeInTheDocument(); + }); + + it("should be disabled", async () => { + const onChange = jest.fn(); + render(); + + await user.click(screen.getByRole("switch")); + expect(onChange).not.toHaveBeenCalled(); + expect(screen.getByRole("switch")).toHaveAttribute("disabled"); + }); +}); diff --git a/packages/orbit-components/src/tmp-tailwind/Switch/index.tsx b/packages/orbit-components/src/tmp-tailwind/Switch/index.tsx new file mode 100644 index 0000000000..7230fe7f62 --- /dev/null +++ b/packages/orbit-components/src/tmp-tailwind/Switch/index.tsx @@ -0,0 +1,59 @@ +"use client"; + +import * as React from "react"; +import cx from "clsx"; + +import Circle from "../../icons/Circle"; +import handleKeyDown from "../../utils/handleKeyDown"; +import type { Props } from "../../Switch/types"; + +const Switch = React.forwardRef( + ({ onChange, checked, dataTest, id, icon, onBlur, onFocus, disabled, ariaLabelledby }, ref) => { + return ( + + ); + }, +); + +Switch.displayName = "Switch"; + +export default Switch;