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

refactor: migrated storybooks to use utils #40

Merged
merged 8 commits into from
Jan 20, 2021
Prev Previous commit
Next Next commit
refactor(story): ♻️ remove console logs & added default args
  • Loading branch information
navin-moorthy committed Jan 20, 2021
commit 1ebeb461803b228fe4222e127c99374bc8926f0b
67 changes: 31 additions & 36 deletions src/button/stories/Button.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,57 +38,34 @@ const base = storyTemplate<ButtonProps>(Button, {
variant: "primary",
});

export const Default = base({ size: "lg", children: "Button" });
export const Default = base({});

export const ExtendedVariant = base({
// @ts-ignore
size: "xxl",
// @ts-ignore
variant: "tertiary",
children: "Button",
});

export const LeftIcon = base({
size: "lg",
variant: "primary",
prefix: <SearchIcon />,
children: "Button",
});

export const RightIcon = base({
size: "lg",
variant: "primary",
suffix: <CaretDownIcon />,
children: "Button",
});

export const BothIcon = base({
size: "lg",
variant: "primary",
suffix: <CaretDownIcon />,
prefix: <SearchIcon />,
children: "Button",
});

export const LoadingIcon = base({
suffix: <CaretDownIcon />,
prefix: <SearchIcon />,
children: "Button",
isLoading: true,
});

const iconButtonBase = storyTemplate<IconButtonProps>(args => {
return (
<IconButton aria-label="picture" {...args}>
<SearchIcon />
</IconButton>
);
});
export const OnlyIcon = iconButtonBase({ size: "lg", variant: "primary" });

const closeButtonBase = storyTemplate<IconButtonProps>(CloseButtonDefault);
export const CloseButton = closeButtonBase({ size: "lg", variant: "primary" });

const CustomSpinner = () => {
const theme = useTheme();

Expand All @@ -103,35 +80,53 @@ const CustomSpinner = () => {
export const CustomLoadingElement = base({
suffix: <CaretDownIcon />,
prefix: <SearchIcon />,
children: "Button",
isLoading: true,
spinner: <CustomSpinner />,
});

const buttonGroupBase = storyTemplate<ButtonGroupProps>(args => (
<ButtonGroup {...args}>
<Button>Button 1</Button>
<Button>Button 2</Button>
<Button>Button 3</Button>
</ButtonGroup>
));
const iconButtonBase = storyTemplate<IconButtonProps>(
args => {
return (
<IconButton aria-label="picture" {...args}>
<SearchIcon />
</IconButton>
);
},
{ size: "lg", variant: "primary" },
);

export const GroupDefault = buttonGroupBase({ className: "space-x-4" });
export const OnlyIcon = iconButtonBase({});

export const GroupCollapsed = buttonGroupBase({
isAttached: true,
const closeButtonBase = storyTemplate<IconButtonProps>(CloseButtonDefault, {
size: "lg",
variant: "primary",
});

export const CloseButton = closeButtonBase({});

const buttonGroupBase = storyTemplate<ButtonGroupProps>(
args => (
<ButtonGroup {...args}>
<Button>Button 1</Button>
<Button>Button 2</Button>
<Button>Button 3</Button>
</ButtonGroup>
),
{ size: "lg", variant: "primary" },
);

export const GroupDefault = buttonGroupBase({ className: "space-x-4" });

export const GroupCollapsed = buttonGroupBase({ isAttached: true });

export const GroupSecondary = buttonGroupBase({
isAttached: true,
size: "lg",
variant: "secondary",
});

const ButtonGroupExample: Story<ButtonGroupProps> = args => {
const tab = useTabState();

return (
<>
<TabList as={ButtonGroup} {...args} {...tab} aria-label="My tabs">
Expand Down
5 changes: 2 additions & 3 deletions src/utils/storybookUtils.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as React from "react";
import { Story } from "@storybook/react";

import theme from "../theme/defaultTheme";

export const storyTemplate = <ComponentProps,>(
Expand All @@ -13,17 +14,15 @@ export const storyTemplate = <ComponentProps,>(

export const createUnionControl = (keys: any) => {
return {
control: { type: "select", options: Object.keys(keys) },
control: { type: "inline-radio", options: Object.keys(keys) },
};
};

export const createControls = (
component: string,
options?: { unions: string[]; ignore: string[] },
) => {
console.log(component, options);
const controls = options?.unions.reduce((cur, key) => {
console.log((theme as Record<string, any>)[component][key]);
return {
...cur,
[key]: createUnionControl((theme as Record<string, any>)[component][key]),
Expand Down