This repository has been archived by the owner on Feb 23, 2024. It is now read-only.
generated from storybookjs/addon-kit
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implemented bare minimum list component
Co-authored-by: liwn9527
- Loading branch information
1 parent
5432948
commit b878c05
Showing
5 changed files
with
130 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import React, { useState } from "react"; | ||
import type { Meta, StoryObj } from "@storybook/react"; | ||
import { userEvent, within } from "@storybook/testing-library"; | ||
import { expect } from "@storybook/jest"; | ||
|
||
import { List } from "./List"; | ||
import { ListItem } from "./ListItem/ListItem"; | ||
|
||
const meta: Meta<typeof List> = { | ||
title: "List", | ||
component: List, | ||
}; | ||
|
||
export default meta; | ||
|
||
export const Default: StoryObj<typeof meta> = { | ||
render: () => { | ||
const [workingIndex, setWorkingIndex] = useState(1); | ||
|
||
return ( | ||
<> | ||
<List> | ||
<ListItem isCompleted={workingIndex >= 1} nthItem={1}> | ||
Hello World | ||
</ListItem> | ||
<ListItem isCompleted={workingIndex >= 2} nthItem={2}> | ||
Bonjour le monde | ||
</ListItem> | ||
<ListItem isCompleted={workingIndex >= 3} nthItem={3}> | ||
你好, 世界 | ||
</ListItem> | ||
</List> | ||
<br /> | ||
<button type="button" onClick={() => setWorkingIndex(workingIndex + 1)}> | ||
Complete | ||
</button> | ||
</> | ||
); | ||
}, | ||
play: ({ canvasElement }) => { | ||
const canvas = within(canvasElement.parentElement); | ||
const button = canvas.getByText("Complete"); | ||
const liEls = canvas.getAllByRole("listitem"); | ||
|
||
expect(liEls[0].ariaLabel).toBe("complete"); | ||
expect(liEls[1].ariaLabel).toBe("not complete"); | ||
expect(liEls[2].ariaLabel).toBe("not complete"); | ||
|
||
userEvent.click(button); | ||
|
||
expect(liEls[0].ariaLabel).toBe("complete"); | ||
// change | ||
expect(liEls[1].ariaLabel).toBe("complete"); | ||
expect(liEls[2].ariaLabel).toBe("not complete"); | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { styled } from "@storybook/theming"; | ||
|
||
export const ListWrapper = styled.ul(() => ({ | ||
display: "flex", | ||
flexDirection: "column", | ||
rowGap: 16, | ||
padding: 0, | ||
margin: 0, | ||
})); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import React from "react"; | ||
import { ListWrapper } from "./List.styled"; | ||
|
||
interface ListProps { | ||
children: React.ReactNode; | ||
} | ||
|
||
export const List = ({ children }: ListProps) => { | ||
return <ListWrapper>{children}</ListWrapper>; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { styled } from "@storybook/theming"; | ||
|
||
export const ListItemWrapper = styled.li(() => ({ | ||
display: "flex", | ||
alignItems: "flex-start", | ||
columnGap: 12, | ||
})); | ||
|
||
export const ListItemContentWrapper = styled.div(({ theme }) => ({ | ||
fontFamily: theme.typography.fonts.base, | ||
color: theme.color.darker, | ||
fontSize: "13px", | ||
})); | ||
|
||
export const ListItemIndexWrapper = styled.div<{ isCompleted: boolean }>( | ||
({ isCompleted, theme }) => ({ | ||
display: "flex", | ||
alignItems: "center", | ||
justifyContent: "center", | ||
border: !isCompleted && `1px solid ${theme.color.medium}`, | ||
minWidth: 20, | ||
width: 20, | ||
height: 20, | ||
borderRadius: "50%", | ||
backgroundColor: isCompleted ? theme.color.green : "white", | ||
fontFamily: theme.typography.fonts.base, | ||
fontSize: 10, | ||
fontWeight: 600, | ||
color: theme.color.dark, | ||
}) | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import React from "react"; | ||
import { Icons } from "@storybook/components"; | ||
import { | ||
ListItemContentWrapper, | ||
ListItemIndexWrapper, | ||
ListItemWrapper, | ||
} from "./ListItem.styled"; | ||
|
||
interface ListItemProps { | ||
children: React.ReactNode; | ||
nthItem: number; | ||
isCompleted?: boolean; | ||
} | ||
|
||
export const ListItem = ({ children, nthItem, isCompleted }: ListItemProps) => { | ||
return ( | ||
<ListItemWrapper aria-label={isCompleted ? "complete" : "not complete"}> | ||
<ListItemIndexWrapper isCompleted={isCompleted}> | ||
{isCompleted ? <Icons icon="check" color="white" /> : nthItem} | ||
</ListItemIndexWrapper> | ||
<ListItemContentWrapper>{children}</ListItemContentWrapper> | ||
</ListItemWrapper> | ||
); | ||
}; |