Skip to content

Commit 946ae31

Browse files
authored
ButtonGroup: cleanup & update storybook (#626)
1 parent 3ae3288 commit 946ae31

File tree

3 files changed

+68
-64
lines changed

3 files changed

+68
-64
lines changed

src/components/ButtonGroup/ButtonGroup.stories.ts

Lines changed: 0 additions & 24 deletions
This file was deleted.
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import { useState } from "react";
2+
3+
import { Meta, StoryObj } from "@storybook/react";
4+
5+
import { ButtonGroup } from "./ButtonGroup";
6+
7+
const meta: Meta<typeof ButtonGroup> = {
8+
component: ButtonGroup,
9+
title: "Buttons/ButtonGroup",
10+
tags: ["button-group", "autodocs"],
11+
render: ({ selected, ...props }) => {
12+
const [value, setValue] = useState(selected);
13+
14+
return (
15+
<ButtonGroup
16+
{...props}
17+
selected={value}
18+
onClick={setValue}
19+
/>
20+
);
21+
},
22+
};
23+
24+
export default meta;
25+
26+
export const Playground: StoryObj<typeof ButtonGroup> = {
27+
args: {
28+
options: [
29+
{ label: "Option 1", value: "option1" },
30+
{ label: "Option 2", value: "option2" },
31+
{ label: "Option 3", value: "option3" },
32+
],
33+
fillWidth: false,
34+
type: "default",
35+
selected: "option3",
36+
},
37+
};

src/components/ButtonGroup/ButtonGroup.tsx

Lines changed: 31 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -21,62 +21,46 @@ export interface ButtonGroupProps
2121
export const ButtonGroup = ({
2222
options,
2323
selected,
24-
fillWidth,
24+
fillWidth = false,
2525
onClick,
26-
type,
26+
type = "default",
2727
...props
2828
}: ButtonGroupProps) => {
29-
const lastIndex = options.length - 1;
30-
const btns = options.map(({ value, label, ...props }, index) => {
31-
const position: ButtonPosition =
32-
index === 0 ? "left" : index === lastIndex ? "right" : "center";
33-
return (
34-
<Button
35-
key={value}
36-
$active={value === selected}
37-
$position={position}
38-
$fillWidth={fillWidth}
39-
$type={type}
40-
onClick={() => onClick?.(value)}
41-
role="button"
42-
{...props}
43-
>
44-
{label}
45-
</Button>
46-
);
47-
});
29+
const buttons = options.map(({ value, label, ...props }) => (
30+
<Button
31+
key={value}
32+
$active={value === selected}
33+
$fillWidth={fillWidth}
34+
$type={type}
35+
onClick={() => onClick?.(value)}
36+
role="button"
37+
{...props}
38+
>
39+
{label}
40+
</Button>
41+
));
42+
4843
return (
4944
<ButtonGroupWrapper
5045
$fillWidth={fillWidth}
5146
$type={type}
5247
{...props}
5348
>
54-
{btns}
49+
{buttons}
5550
</ButtonGroupWrapper>
5651
);
5752
};
5853

59-
type ButtonPosition = "left" | "center" | "right";
60-
61-
interface ButtonProps {
62-
$active: boolean;
63-
$position: ButtonPosition;
64-
theme: DefaultTheme;
65-
$fillWidth?: boolean;
66-
$type?: ButtonGroupType;
67-
}
68-
69-
const ButtonGroupWrapper = styled.div<{ $fillWidth?: boolean; $type?: ButtonGroupType }>`
54+
const ButtonGroupWrapper = styled.div<{ $fillWidth: boolean; $type: ButtonGroupType }>`
7055
display: inline-flex;
7156
box-sizing: border-box;
7257
flex-direction: row;
7358
justify-content: center;
7459
align-items: center;
75-
padding: ${({ theme, $type = "default" }) =>
60+
padding: ${({ theme, $type }) =>
7661
`${theme.click.button.group.space.panel[$type].x} ${theme.click.button.group.space.panel[$type].y}`};
77-
gap: ${({ theme, $type = "default" }) =>
78-
theme.click.button.group.space.panel[$type].gap};
79-
border: ${({ theme, $type = "default" }) =>
62+
gap: ${({ theme, $type }) => theme.click.button.group.space.panel[$type].gap};
63+
border: ${({ theme, $type }) =>
8064
$type === "default"
8165
? `1px solid ${theme.click.button.group.color.panel.stroke[$type]}`
8266
: "none"};
@@ -85,6 +69,13 @@ const ButtonGroupWrapper = styled.div<{ $fillWidth?: boolean; $type?: ButtonGrou
8569
width: ${({ $fillWidth }) => ($fillWidth ? "100%" : "auto")};
8670
`;
8771

72+
interface ButtonProps {
73+
$active: boolean;
74+
theme: DefaultTheme;
75+
$fillWidth: boolean;
76+
$type: ButtonGroupType;
77+
}
78+
8879
const Button = styled.button.attrs<ButtonProps>((props: ButtonProps) => ({
8980
"aria-pressed": props.$active,
9081
}))`
@@ -99,10 +90,10 @@ const Button = styled.button.attrs<ButtonProps>((props: ButtonProps) => ({
9990
: theme.click.button.group.color.background.default};
10091
color: ${({ theme }) => theme.click.button.group.color.text.default};
10192
font: ${({ theme }) => theme.click.button.group.typography.label.default};
102-
padding: ${({ theme, $type = "default" }) =>
93+
padding: ${({ theme, $type }) =>
10394
`${theme.click.button.group.space.button[$type].y} ${theme.click.button.group.space.button[$type].x}`};
104-
${({ $fillWidth = false }) => ($fillWidth ? "flex: 1;" : "")};
105-
border-radius: ${({ theme, $type = "default" }) =>
95+
${({ $fillWidth }) => ($fillWidth ? "flex: 1;" : "")};
96+
border-radius: ${({ theme, $type }) =>
10697
theme.click.button.group.radii.button[$type].all};
10798
cursor: pointer;
10899
border: none;

0 commit comments

Comments
 (0)