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
chore: added createControls utility
  • Loading branch information
anuraghazra committed Jan 19, 2021
commit 9688d2268adb52858b093b179968baef6d31eb25
2 changes: 2 additions & 0 deletions src/alert/Alert.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -130,3 +130,5 @@ export const AlertIcon = forwardRefWithAs<
</Role>
);
});

export default Alert;
13 changes: 11 additions & 2 deletions src/button/stories/Button.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,21 @@ import {
import { useTheme } from "../../theme";
import { Spinner } from "../../spinner";
import { SearchIcon, CaretDownIcon } from "../../icons";
import { storyTemplate } from "../../utils/storybookUtils";
import { createControls, storyTemplate } from "../../utils/storybookUtils";

export default {
title: "Button",
component: Button,
} as Meta<ButtonProps>;
argTypes: createControls("button", {
unions: ["size", "variant"],
ignore: [
"unstable_system",
"unstable_clickOnEnter",
"unstable_clickOnSpace",
"wrapElement",
],
}),
} as Meta;

const base = storyTemplate<ButtonProps>(Button, {
children: "Button",
Expand Down
30 changes: 30 additions & 0 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,>(
Component: React.FC,
Expand All @@ -9,3 +10,32 @@ export const storyTemplate = <ComponentProps,>(
base.args = { ...defaultArgs, ...props };
return base;
};

export const createUnionControl = (keys: any) => {
return {
control: { type: "select", 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]),
};
}, {});

const ignoredControls = options?.ignore.reduce((cur, key) => {
return {
...cur,
[key]: { table: { disable: true } },
};
}, {});

return { ...controls, ...ignoredControls };
};