Skip to content

Commit

Permalink
Add args and argTypes to context for Storybook compat (#449)
Browse files Browse the repository at this point in the history
* Add args and argTypes to context for Storybook compat

* fix test
  • Loading branch information
tajo committed Jun 12, 2023
1 parent 802b351 commit d3e0200
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 10 deletions.
5 changes: 5 additions & 0 deletions .changeset/five-cooks-shake.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@ladle/react": minor
---

Add args and argTypes to context for Storybook compat
25 changes: 19 additions & 6 deletions e2e/decorators/src/legacy-params.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,30 @@
import type { StoryDefault, Story } from "@ladle/react";

export default {
args: {
flag: "czech",
},
argTypes: {
size: {
options: ["small", "medium", "big", "huuuuge"],
control: { type: "select" }, // or type: multi-select
},
},
parameters: {
one: 1,
two: 2,
},
decorators: [
(Stories: React.FC, context) => (
<>
Decorator {context.parameters.one}
<Stories />
</>
),
(Stories: React.FC, context) => {
console.log(context);
return (
<>
Decorator {context.parameters.one}
{context.args.flag}
<Stories />
</>
);
},
(Stories: React.FC, context) => (
<>
Decorator {context.parameters.two}
Expand Down
4 changes: 3 additions & 1 deletion e2e/decorators/tests/legacy-params.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,7 @@ test("legacy parameters are passed as a context into decorators", async ({
page,
}) => {
await page.goto("/?story=legacy-params--world");
await expect(page.locator("main")).toHaveText("Decorator 5Decorator 1world");
await expect(page.locator("main")).toHaveText(
"Decorator 5Decorator 1czechworld",
);
});
4 changes: 2 additions & 2 deletions e2e/decorators/tests/params.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ test("meta.json is correctly using defaults and overrides", async ({
"legacy-params--world": {
filePath: "src/legacy-params.stories.tsx",
levels: ["Legacy params"],
locEnd: 26,
locStart: 24,
locEnd: 39,
locStart: 37,
meta: {},
name: "World",
},
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"private": true,
"packageManager": "pnpm@8.6.1",
"scripts": {
"lint": "eslint \"./packages/*/{lib,src}/**/*.{ts,tsx}\" --max-warnings=0",
"lint": "eslint \"./{packages,e2e}/*/{lib,src,tests}/**/*.{ts,tsx}\" --max-warnings=0",
"test": "turbo run test --concurrency=1",
"build": "turbo run build",
"typecheck": "tsc",
Expand Down
6 changes: 6 additions & 0 deletions packages/ladle/lib/app/src/compose-enhancers.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,15 @@ export default function composeEnhancers(module: any, storyName: string) {
const getBindedDecorator = (i: number) => {
return React.useRef(() => {
const context = useLadleContext();
const args: { [key: string]: any } = {};
Object.keys(context.globalState.control).forEach(
(key) => (args[key] = context.globalState.control[key].value),
);
return decorators[i](i === 0 ? WithArgs : getBindedDecorator(i - 1), {
...context,
parameters,
argTypes: props.argTypes,
args,
});
}).current;
};
Expand Down
2 changes: 2 additions & 0 deletions packages/ladle/lib/shared/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,8 @@ export type StoryProps = {
dispatch: React.Dispatch<GlobalAction>;
globalState: GlobalState;
parameters: { [key: string]: any };
args: { [key: string]: any };
argTypes: { [key: string]: any };
};

export type StoryDecorator<P = {}> = (
Expand Down

1 comment on commit d3e0200

@vercel
Copy link

@vercel vercel bot commented on d3e0200 Jun 12, 2023

Choose a reason for hiding this comment

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

Successfully deployed to the following URLs:

ladle – ./

ladle.dev
ladle-miksu.vercel.app
ladle-git-main-miksu.vercel.app
ladle.vercel.app

Please sign in to comment.