Skip to content

Commit

Permalink
chore: add relevant events logging (#1880)
Browse files Browse the repository at this point in the history
A new Storybook Util `withNativeEventLoggingFor` is introduced.
It generates Storybook `ArgTypes` that allow us to document and log
relevant native events in Storybook.
These can be merged with other `ArgTypes`.
The native events will be documented in a new section "Relevant HTML
Events".

Relates to #1603

---------

Co-authored-by: Lars Rickert <[email protected]>
  • Loading branch information
JoCa96 and larsrickert authored Sep 18, 2024
1 parent 5ed9bff commit 889383b
Show file tree
Hide file tree
Showing 17 changed files with 864 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/tender-zebras-flow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@sit-onyx/storybook-utils": minor
---

feat: added withNativeEventLogging to log and document native events
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
18 changes: 18 additions & 0 deletions apps/docs/src/development/packages/storybook-utils.md
Original file line number Diff line number Diff line change
Expand Up @@ -154,3 +154,21 @@ export default preview;
```

:::

### withNativeEventLogging

Allows logging and documentation for the passed native event listeners in Storybook.
These will be documented in a extra "Relevant HTML events" section in the Storybook documentation.

```ts [.storybook/preview.ts]
import { withNativeEventLogging } from "@sit-onyx/storybook-utils";

const meta: Meta<typeof OnyxButton> = {
title: "Buttons/Button",
component: OnyxButton,
argTypes: {
somethingElse: { ...someOtherArgType },
...withNativeEventLogging(["onClick"]),
},
};
```
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import checkSmall from "@sit-onyx/icons/check-small.svg?raw";
import { withNativeEventLogging } from "@sit-onyx/storybook-utils";
import type { Meta, StoryObj } from "@storybook/vue3";
import { defineIconSelectArgType } from "../../utils/storybook";
import OnyxButton from "./OnyxButton.vue";
Expand All @@ -17,6 +18,7 @@ const meta: Meta<typeof OnyxButton> = {
component: OnyxButton,
argTypes: {
icon: defineIconSelectArgType(),
...withNativeEventLogging(["onClick"]),
},
};

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import trash from "@sit-onyx/icons/trash.svg?raw";
import { withNativeEventLogging } from "@sit-onyx/storybook-utils";
import type { Meta, StoryObj } from "@storybook/vue3";
import { h } from "vue";
import { defineIconSelectArgType } from "../../utils/storybook";
Expand All @@ -13,6 +14,7 @@ const meta: Meta<typeof OnyxIconButton> = {
argTypes: {
icon: defineIconSelectArgType(),
default: { control: { disable: true } },
...withNativeEventLogging(["onClick"]),
},
};

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { withNativeEventLogging } from "@sit-onyx/storybook-utils";
import type { Meta, StoryObj } from "@storybook/vue3";
import OnyxInput from "./OnyxInput.vue";

Expand All @@ -16,6 +17,7 @@ const meta: Meta<typeof OnyxInput> = {
],
argTypes: {
pattern: { control: { type: "text" } },
...withNativeEventLogging(["onInput", "onChange", "onFocusin", "onFocusout"]),
},
};

Expand Down
2 changes: 2 additions & 0 deletions packages/sit-onyx/src/components/OnyxLink/OnyxLink.stories.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { withNativeEventLogging } from "@sit-onyx/storybook-utils";
import type { Meta, StoryObj } from "@storybook/vue3";
import OnyxLink from "./OnyxLink.vue";

Expand All @@ -15,6 +16,7 @@ const meta: Meta<typeof OnyxLink> = {
options: ["auto", true, false],
control: { type: "radio" },
},
...withNativeEventLogging(["onClick"]),
},
decorators: [
(story) => ({
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { withNativeEventLogging } from "@sit-onyx/storybook-utils";
import type { Meta, StoryObj } from "@storybook/vue3";
import OnyxNavAppArea from "./OnyxNavAppArea.vue";

Expand All @@ -9,6 +10,7 @@ const meta: Meta<typeof OnyxNavAppArea> = {
component: OnyxNavAppArea,
argTypes: {
default: { control: { type: "text" } },
...withNativeEventLogging(["onClick"]),
},
};

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import placeholder from "@sit-onyx/icons/placeholder.svg?raw";
import { withNativeEventLogging } from "@sit-onyx/storybook-utils";
import type { Meta, StoryObj } from "@storybook/vue3";
import { h } from "vue";
import OnyxIcon from "../../../OnyxIcon/OnyxIcon.vue";
Expand All @@ -12,6 +13,7 @@ const meta: Meta<typeof OnyxMenuItem> = {
component: OnyxMenuItem,
argTypes: {
default: { control: { type: "text" } },
...withNativeEventLogging(["onClick"]),
},
decorators: [
(story) => ({
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { withNativeEventLogging } from "@sit-onyx/storybook-utils";
import type { Meta, StoryObj } from "@storybook/vue3";
import OnyxRadioButton from "./OnyxRadioButton.vue";

Expand All @@ -7,6 +8,9 @@ import OnyxRadioButton from "./OnyxRadioButton.vue";
const meta: Meta<typeof OnyxRadioButton> = {
title: "Support/RadioButton",
component: OnyxRadioButton,
argTypes: {
...withNativeEventLogging(["onChange"]),
},
};

export default meta;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { withNativeEventLogging } from "@sit-onyx/storybook-utils";
import type { Meta, StoryObj } from "@storybook/vue3";
import OnyxStepper from "./OnyxStepper.vue";

Expand All @@ -10,6 +11,9 @@ const meta: Meta<typeof OnyxStepper> = {
template: `<div style="width: 16rem;"> <story /> </div>`,
}),
],
argTypes: {
...withNativeEventLogging(["onChange"]),
},
};

export default meta;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { withNativeEventLogging } from "@sit-onyx/storybook-utils";
import type { Meta, StoryObj } from "@storybook/vue3";
import OnyxTextarea from "./OnyxTextarea.vue";

Expand All @@ -10,6 +11,9 @@ const meta: Meta<typeof OnyxTextarea> = {
template: `<div style="max-width: 24rem;"> <story /> </div>`,
}),
],
argTypes: {
...withNativeEventLogging(["onChange"]),
},
};

export default meta;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { withNativeEventLogging } from "@sit-onyx/storybook-utils";
import type { Meta, StoryObj } from "@storybook/vue3";
import { defineIconSelectArgType } from "../../utils/storybook";
import OnyxToastMessage from "./OnyxToastMessage.vue";
Expand All @@ -10,6 +11,7 @@ const meta: Meta<typeof OnyxToastMessage> = {
component: OnyxToastMessage,
argTypes: {
icon: defineIconSelectArgType(),
...withNativeEventLogging(["onClick"]),
},
};

Expand Down
40 changes: 38 additions & 2 deletions packages/storybook-utils/src/actions.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import type { Decorator } from "@storybook/vue3";
import { useArgs } from "storybook/internal/preview-api";
import type { ArgTypesEnhancer, StrictInputType } from "storybook/internal/types";
import { isReactive, reactive, watch } from "vue";
import type { ArgTypes, ArgTypesEnhancer, StrictInputType } from "storybook/internal/types";
import { isReactive, reactive, watch, type Events } from "vue";
import { EVENT_DOC_MAP } from "./events";

/**
* Adds actions for all argTypes of the 'event' category, so that they are logged via the actions plugin.
Expand All @@ -23,6 +24,41 @@ export const enhanceEventArgTypes: ArgTypesEnhancer = ({ argTypes }) => {
return argTypes;
};

/**
* Allows logging and documentation for the passed event listener names in Storybook.
* Will be documented in a extra "Relevant HTML events" section in the Storybook documentation.
*
* @example
* ```typescript
* const meta: Meta<typeof OnyxButton> = {
* title: "Buttons/Button",
* component: OnyxButton,
* argTypes: {
* somethingElse: { ...someOtherArgType },
* ...withNativeEventLogging(["onClick"]),
* },
*};
* ```
*
* @param relevantEvents a list of event names that should be logged
* @returns Storybook ArgTypes object
*/
export const withNativeEventLogging = (relevantEvents: (keyof Events)[]) =>
relevantEvents.reduce((argTypes, eventName) => {
const { constructor, event } = EVENT_DOC_MAP[eventName];
argTypes[eventName] = {
name: event.name,
control: false,
description: `The native HTML [${event.name}](${event.url}) event which dispatches an [${constructor.name}](${constructor.url}).`,
table: {
category: "Relevant HTML events",
type: { summary: constructor.name },
},
action: event.name,
};
return argTypes;
}, {} as ArgTypes);

export type WithVModelDecoratorOptions = {
/**
* The matcher for the v-model events.
Expand Down
Loading

0 comments on commit 889383b

Please sign in to comment.