Skip to content

Commit

Permalink
format
Browse files Browse the repository at this point in the history
  • Loading branch information
literalpie committed Feb 18, 2024
1 parent a421193 commit 1b7fee1
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 36 deletions.
6 changes: 3 additions & 3 deletions packages/qwik-app/src/components/button/button.stories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import { Button } from "./button";

const meta = {
title: "Button",
args: { },
args: {},
argTypes: {
onClick$: { action: 'onClick'},
onClick$: { action: "onClick" },
},
component: Button,
} satisfies Meta<ButtonProps>;
Expand All @@ -16,4 +16,4 @@ export default meta;
type Story = StoryObj<ButtonProps>;

// More on writing stories with args: https://storybook.js.org/docs/7.0/react/writing-stories/args
export const Primary: Story = {};
export const Primary: Story = {};
17 changes: 4 additions & 13 deletions packages/qwik-app/src/components/button/button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,10 @@ export interface ButtonProps {
onClick$?: PropFunction<onClickEvent> | undefined;
}

export type onClickEvent = (
event: MouseEvent,
element: Element
) => void;
export type onClickEvent = (event: MouseEvent, element: Element) => void;

export const Button = component$<ButtonProps>(
({ label="click me!", onClick$ }) => {
return (
<button
onClick$={onClick$}
>
{label}
</button>
);
}
({ label = "click me!", onClick$ }) => {
return <button onClick$={onClick$}>{label}</button>;
},
);
18 changes: 11 additions & 7 deletions packages/storybook-framework-qwik/src/addArgsHelpers.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
// This file is entirely copied from @storybook/addon-actions (changing the action import)

import type { Args, Renderer, ArgsEnhancer } from '@storybook/types';
import { action } from '@storybook/addon-actions';
import type { Args, Renderer, ArgsEnhancer } from "@storybook/types";
import { action } from "@storybook/addon-actions";

// interface ActionsParameter {
// disable?: boolean;
// argTypesRegex?: RegExp;
// }

const isInInitialArgs = (name: string, initialArgs: Args) =>
typeof initialArgs[name] === 'undefined' && !(name in initialArgs);
typeof initialArgs[name] === "undefined" && !(name in initialArgs);

/**
* Automatically add action args for argTypes whose name
* matches a regex, such as `^on.*` for react-style `onClick` etc.
*/

export const inferActionsFromArgTypesRegex: ArgsEnhancer<Renderer> = (context) => {
export const inferActionsFromArgTypesRegex: ArgsEnhancer<Renderer> = (
context,
) => {
const {
initialArgs,
argTypes,
Expand All @@ -28,7 +30,7 @@ export const inferActionsFromArgTypesRegex: ArgsEnhancer<Renderer> = (context) =

const argTypesRegex = new RegExp(actions.argTypesRegex);
const argTypesMatchingRegex = Object.entries(argTypes).filter(
([name]) => !!argTypesRegex.test(name)
([name]) => !!argTypesRegex.test(name),
);

return argTypesMatchingRegex.reduce((acc, [name, argType]) => {
Expand All @@ -53,12 +55,14 @@ export const addActionsFromArgTypes: ArgsEnhancer<Renderer> = (context) => {
}

const argTypesWithAction = Object.entries(argTypes).filter(
([name, argType]) => !!argType['action']
([name, argType]) => !!argType["action"],
);

return argTypesWithAction.reduce((acc, [name, argType]) => {
if (isInInitialArgs(name, initialArgs)) {
acc[name] = action(typeof argType['action'] === 'string' ? argType['action'] : name);
acc[name] = action(
typeof argType["action"] === "string" ? argType["action"] : name,
);
}
return acc;
}, {} as Args);
Expand Down
26 changes: 13 additions & 13 deletions packages/storybook-framework-qwik/src/preview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ import { inlinedQrl, render as renderQwik } from "@builder.io/qwik";
import { ArgsEnhancer, ArgsStoryFn, RenderContext } from "@storybook/types";
import { QwikRenderer } from "./types.js";
import { componentToJSX } from "./component-to-jsx.js";
import { addActionsFromArgTypes, inferActionsFromArgTypesRegex } from "./addArgsHelpers.js";
import {
addActionsFromArgTypes,
inferActionsFromArgTypesRegex,
} from "./addArgsHelpers.js";
export { parameters, argTypesEnhancers } from "./docs/config.js";


// returns the Qwik component as a JSX element (</MyComponent>)
// If a story has a custom renderer, it will replace this function.
export const render: ArgsStoryFn<QwikRenderer<unknown>> = (args, context) => {
Expand Down Expand Up @@ -41,17 +43,15 @@ const actionsArgsEnhancers: ArgsEnhancer[] = [
inferActionsFromArgTypesRegex,
];


export const argsEnhancers: ArgsEnhancer[] =
// use the argsEnhancers from addon-actions, then wrap the actions in Qwik's inlinedQrl function so things work.
actionsArgsEnhancers.map((actionsEnhancer: ArgsEnhancer)=>{
return ((context)=>{
export const argsEnhancers: ArgsEnhancer[] =
// use the argsEnhancers from addon-actions, then wrap the actions in Qwik's inlinedQrl function so things work.
actionsArgsEnhancers.map((actionsEnhancer: ArgsEnhancer) => {
return ((context) => {
const argsWithActions = actionsEnhancer(context);
let finalArgs:any = {}
Object.keys(argsWithActions).forEach(key=>{
let finalArgs: any = {};
Object.keys(argsWithActions).forEach((key) => {
finalArgs[key] = inlinedQrl(argsWithActions[key], key);
})
});
return finalArgs;

}) as ArgsEnhancer
})
}) as ArgsEnhancer;
});

0 comments on commit 1b7fee1

Please sign in to comment.