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

[Feature] SwitchGroup 컴포넌트 구현 #32

Closed
wants to merge 17 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
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
3 changes: 2 additions & 1 deletion packages/scripts/generateBuildConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,10 @@ const generateExports = (files: string[]) => {
for (const file of files) {
const filePath = `./${file}`;
const distPath = `./dist/${file}`;
const typePath = `./dist/components/${file}`;

exportsObj[filePath] = {
types: `${distPath}/index.d.ts`,
types: `${typePath}/index.d.ts`,
require: `${distPath}.cjs`,
import: `${distPath}.js`,
};
Expand Down
16 changes: 8 additions & 8 deletions packages/wow-ui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,24 +21,24 @@
"exports": {
"./styles.css": "./dist/styles.css",
"./Box": {
"types": "./dist/Box/index.d.ts",
"types": "./dist/components/Box/index.d.ts",
"require": "./dist/Box.cjs",
"import": "./dist/Box.js"
},
"./Button": {
"types": "./dist/Button/index.d.ts",
"types": "./dist/components/Button/index.d.ts",
"require": "./dist/Button.cjs",
"import": "./dist/Button.js"
},
"./Toggle": {
"types": "./dist/components/Toggle/index.d.ts",
"require": "./dist/Toggle.cjs",
"import": "./dist/Toggle.js"
},
"./Switch": {
"types": "./dist/Switch/index.d.ts",
"types": "./dist/components/Switch/index.d.ts",
"require": "./dist/Switch.cjs",
"import": "./dist/Switch.js"
},
"./SwitchGroup": {
"types": "./dist/components/SwitchGroup/index.d.ts",
"require": "./dist/SwitchGroup.cjs",
"import": "./dist/SwitchGroup.js"
}
},
"keywords": [],
Expand Down
1 change: 1 addition & 0 deletions packages/wow-ui/rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export default {
Box: "./src/components/Box",
Button: "./src/components/Button",
Switch: "./src/components/Switch",
SwitchGroup: "./src/components/SwitchGroup",
},
output: [
{
Expand Down
18 changes: 9 additions & 9 deletions packages/wow-ui/src/components/Switch/Switch.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,19 +58,19 @@ const meta = {
table: {
type: { summary: "() => void" },
defaultValue: { summary: null },
control: {
type: "function",
},
},
control: {
type: "function",
},
},
onClick: {
description: "스위치 클릭 시 동작할 이벤트입니다.",
table: {
type: { summary: "() => void" },
defaultValue: { summary: null },
control: {
type: "function",
},
},
control: {
type: "function",
},
},
onKeyDown: {
Expand All @@ -79,9 +79,9 @@ const meta = {
table: {
type: { summary: "() => void" },
defaultValue: { summary: null },
control: {
type: "function",
},
},
control: {
type: "function",
},
},
},
Expand Down
2 changes: 1 addition & 1 deletion packages/wow-ui/src/components/Switch/Switch.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import fireEvent from "@testing-library/user-event";

import Switch from "@/components/Switch";

describe("toggle", () => {
describe("switch", () => {
let rendered: RenderResult;

beforeEach(() => {
Expand Down
7 changes: 3 additions & 4 deletions packages/wow-ui/src/components/Switch/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -83,21 +83,20 @@ const Switch = forwardRef(
};

return (
<Flex alignItems="center" gap="0.5rem">
<Flex alignItems="center" gap="xs">
<styled.label
htmlFor="switch"
className={switchStyle({
type: isDisabled ? "disabled" : isChecked ? "checked" : "unchecked",
})}
>
<input
id="switch"
ref={ref}
{...rest}
aria-checked={isChecked}
aria-disabled={isDisabled}
aria-label="switch"
className={inputStyle()}
disabled={isDisabled}
type="checkbox"
onClick={handleClick}
onKeyDown={handleKeyDown}
Expand Down Expand Up @@ -131,7 +130,7 @@ const switchStyle = cva({
_hover: { bgColor: "sub" },
_pressed: { bgColor: "lightDisabled" },
},
disabled: { bgColor: "lightDisabled" },
disabled: { bgColor: "lightDisabled", cursor: "default" },
},
},
defaultVariants: {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
import type { Meta, StoryObj } from "@storybook/react";
import { useState } from "react";

import Switch from "@/components/Switch";
import SwitchGroup from "@/components/SwitchGroup";

const meta = {
title: "UI/SwitchGroup",
component: SwitchGroup,
tags: ["autodocs"],
parameters: {
componentSubtitle: "스위치 그룹 컴포넌트",
},
argTypes: {
children: {
description: "렌더링할 자식 요소를 나타냅니다.",
table: {
type: { summary: "ReactNode" },
defaultValue: { summary: null },
},
control: {
type: "function",
},
type: {
name: "function",
required: true,
},
},
value: {
description: "value는 외부에서 제어할 활성 상태를 나타냅니다.",
table: {
type: { summary: "boolean[]" },
defaultValue: { summary: null },
},
control: {
type: "array",
},
},
onChange: {
description: "외부 활성 상태가 변경될 때 호출될 콜백 함수를 나타냅니다.",
table: {
type: { summary: "(index: number) => void" },
defaultValue: { summary: null },
},
control: {
type: "function",
},
},
},
} satisfies Meta<typeof SwitchGroup>;

export default meta;

type Story = StoryObj<typeof meta>;

export const Primary: Story = {
args: {
children: (
<>
<Switch text="Switch 1" />
<Switch isDisabled text="Switch 2" />
<Switch text="Switch 3" />
</>
Comment on lines +58 to +63
Copy link
Member

Choose a reason for hiding this comment

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

image

p2;
children 관련해서 타입 에러가 발생합니다.

Suggested change
children: (
<>
<Switch text="Switch 1" />
<Switch isDisabled text="Switch 2" />
<Switch text="Switch 3" />
</>
children: [
<Switch text="Switch 1" />,
<Switch isDisabled text="Switch 2" />,
<Switch text="Switch 3" />,
],

),
},
};

const ControlledSwitchGroup = () => {
const [isCheckedState, setIsCheckedState] = useState<boolean[]>([
false,
true,
false,
]);

const handleChange = (index: number) => {
setIsCheckedState((prev) =>
prev.map((prevState, i) => (index === i ? !prevState : prevState))
);
};

return (
<SwitchGroup value={isCheckedState} onChange={handleChange}>
<Switch isDisabled text="Switch 1" />
<Switch text="Switch 2" />
<Switch text="Switch 3" />
</SwitchGroup>
);
};

export const ControlledState: Story = {
render: () => <ControlledSwitchGroup />,
args: {
children: null,
Copy link
Member

Choose a reason for hiding this comment

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

p2;
children 관련해서 타입 에러가 발생합니다. 관련 렌더링 코드를 삭제하고 바로 export하면 어떨까요?

Suggested change
children: null,
export const ControlledSwitchGroup = () => {
const [isCheckedState, setIsCheckedState] = useState<boolean[]>([
false,
true,
false,
]);
const handleChange = (index: number) => {
setIsCheckedState((prev) =>
prev.map((prevState, i) => (index === i ? !prevState : prevState))
);
};
return (
<SwitchGroup value={isCheckedState} onChange={handleChange}>
<Switch isDisabled text="Switch 1" />
<Switch text="Switch 2" />
<Switch text="Switch 3" />
</SwitchGroup>
);
};

},
};
110 changes: 110 additions & 0 deletions packages/wow-ui/src/components/SwitchGroup/SwitchGroup.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
import type { RenderResult } from "@testing-library/react";
import { render, waitFor } from "@testing-library/react";
import fireEvent from "@testing-library/user-event";

import Switch from "@/components/Switch";
import SwitchGroup from "@/components/SwitchGroup";

describe("switch group", () => {
let rendered: RenderResult;

beforeEach(() => {
rendered = render(
<SwitchGroup>
<Switch text="Switch 1" />
<Switch isDisabled text="Switch 2" />
<Switch text="Switch 3" />
</SwitchGroup>
);
});

it("should render switch components with its own state", () => {
const switchComponents = rendered.getAllByRole("checkbox");

expect(switchComponents[0]).toHaveAttribute("aria-checked", "false");
expect(switchComponents[1]).toHaveAttribute("aria-disabled", "true");
expect(switchComponents[2]).toHaveAttribute("aria-checked", "false");
});
});

describe("with external value provided", () => {
it("should render switch components with external value provided", () => {
const rendered = render(
<SwitchGroup value={[true, false, true]}>
<Switch text="Switch 1" />
<Switch text="Switch 2" />
<Switch text="Switch 3" />
</SwitchGroup>
);
const switchComponents = rendered.getAllByRole("checkbox");

expect(switchComponents[0]).toHaveAttribute("aria-checked", "true");
expect(switchComponents[1]).toHaveAttribute("aria-checked", "false");
expect(switchComponents[2]).toHaveAttribute("aria-checked", "true");
});

it("should render switch components with default unchecked value provided", () => {
const rendered = render(
<SwitchGroup value={[]}>
<Switch text="Switch 1" />
<Switch text="Switch 2" />
<Switch text="Switch 3" />
</SwitchGroup>
);
const switchComponents = rendered.getAllByRole("checkbox");

expect(switchComponents[0]).toHaveAttribute("aria-checked", "false");
expect(switchComponents[1]).toHaveAttribute("aria-checked", "false");
expect(switchComponents[2]).toHaveAttribute("aria-checked", "false");
});

it("should toggle state when onChange event fired", async () => {
const checkedStates = [true, false, true];
const handleChange = jest.fn((index: number) => {
checkedStates[index] = !checkedStates[index];
});
const rendered = render(
<SwitchGroup value={checkedStates} onChange={handleChange}>
<Switch text="Switch 1" />
<Switch text="Switch 2" />
<Switch text="Switch 3" />
</SwitchGroup>
);
const switchComponents = rendered.getAllByRole("checkbox");

fireEvent.click(switchComponents[0]!);
fireEvent.click(switchComponents[1]!);
fireEvent.click(switchComponents[2]!);

await waitFor(() => {
expect(checkedStates[0]).toBe(false);
expect(checkedStates[1]).toBe(true);
expect(checkedStates[2]).toBe(false);
});
});

it("should not toggle state when toggle is disabled", async () => {
const checkedStates = [true, false, true];
const handleChange = jest.fn((index: number) => {
checkedStates[index] = !checkedStates[index];
});
const rendered = render(
<SwitchGroup value={checkedStates} onChange={handleChange}>
<Switch text="Switch 1" />
<Switch isDisabled text="Switch 2" />
<Switch text="Switch 3" />
</SwitchGroup>
);
const switchComponents = rendered.getAllByRole("checkbox");

fireEvent.click(switchComponents[0]!);
fireEvent.click(switchComponents[1]!);
fireEvent.click(switchComponents[2]!);

await waitFor(() => {
expect(checkedStates[0]).toBe(false);
expect(checkedStates[1]).toBe(false);
expect(checkedStates[2]).toBe(false);
});
});
});
Loading
Loading