Skip to content

Commit 889383b

Browse files
JoCa96larsrickert
andauthored
chore: add relevant events logging (#1880)
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]>
1 parent 5ed9bff commit 889383b

17 files changed

+864
-2
lines changed

.changeset/tender-zebras-flow.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@sit-onyx/storybook-utils": minor
3+
---
4+
5+
feat: added withNativeEventLogging to log and document native events
Loading
Loading
Loading

apps/docs/src/development/packages/storybook-utils.md

+18
Original file line numberDiff line numberDiff line change
@@ -154,3 +154,21 @@ export default preview;
154154
```
155155

156156
:::
157+
158+
### withNativeEventLogging
159+
160+
Allows logging and documentation for the passed native event listeners in Storybook.
161+
These will be documented in a extra "Relevant HTML events" section in the Storybook documentation.
162+
163+
```ts [.storybook/preview.ts]
164+
import { withNativeEventLogging } from "@sit-onyx/storybook-utils";
165+
166+
const meta: Meta<typeof OnyxButton> = {
167+
title: "Buttons/Button",
168+
component: OnyxButton,
169+
argTypes: {
170+
somethingElse: { ...someOtherArgType },
171+
...withNativeEventLogging(["onClick"]),
172+
},
173+
};
174+
```

packages/sit-onyx/src/components/OnyxButton/OnyxButton.stories.ts

+2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import checkSmall from "@sit-onyx/icons/check-small.svg?raw";
2+
import { withNativeEventLogging } from "@sit-onyx/storybook-utils";
23
import type { Meta, StoryObj } from "@storybook/vue3";
34
import { defineIconSelectArgType } from "../../utils/storybook";
45
import OnyxButton from "./OnyxButton.vue";
@@ -17,6 +18,7 @@ const meta: Meta<typeof OnyxButton> = {
1718
component: OnyxButton,
1819
argTypes: {
1920
icon: defineIconSelectArgType(),
21+
...withNativeEventLogging(["onClick"]),
2022
},
2123
};
2224

packages/sit-onyx/src/components/OnyxIconButton/OnyxIconButton.stories.ts

+2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import trash from "@sit-onyx/icons/trash.svg?raw";
2+
import { withNativeEventLogging } from "@sit-onyx/storybook-utils";
23
import type { Meta, StoryObj } from "@storybook/vue3";
34
import { h } from "vue";
45
import { defineIconSelectArgType } from "../../utils/storybook";
@@ -13,6 +14,7 @@ const meta: Meta<typeof OnyxIconButton> = {
1314
argTypes: {
1415
icon: defineIconSelectArgType(),
1516
default: { control: { disable: true } },
17+
...withNativeEventLogging(["onClick"]),
1618
},
1719
};
1820

packages/sit-onyx/src/components/OnyxInput/OnyxInput.stories.ts

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { withNativeEventLogging } from "@sit-onyx/storybook-utils";
12
import type { Meta, StoryObj } from "@storybook/vue3";
23
import OnyxInput from "./OnyxInput.vue";
34

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

packages/sit-onyx/src/components/OnyxLink/OnyxLink.stories.ts

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { withNativeEventLogging } from "@sit-onyx/storybook-utils";
12
import type { Meta, StoryObj } from "@storybook/vue3";
23
import OnyxLink from "./OnyxLink.vue";
34

@@ -15,6 +16,7 @@ const meta: Meta<typeof OnyxLink> = {
1516
options: ["auto", true, false],
1617
control: { type: "radio" },
1718
},
19+
...withNativeEventLogging(["onClick"]),
1820
},
1921
decorators: [
2022
(story) => ({

packages/sit-onyx/src/components/OnyxNavAppArea/OnyxNavAppArea.stories.ts

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { withNativeEventLogging } from "@sit-onyx/storybook-utils";
12
import type { Meta, StoryObj } from "@storybook/vue3";
23
import OnyxNavAppArea from "./OnyxNavAppArea.vue";
34

@@ -9,6 +10,7 @@ const meta: Meta<typeof OnyxNavAppArea> = {
910
component: OnyxNavAppArea,
1011
argTypes: {
1112
default: { control: { type: "text" } },
13+
...withNativeEventLogging(["onClick"]),
1214
},
1315
};
1416

packages/sit-onyx/src/components/OnyxNavBar/modules/OnyxMenuItem/OnyxMenuItem.stories.ts

+2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import placeholder from "@sit-onyx/icons/placeholder.svg?raw";
2+
import { withNativeEventLogging } from "@sit-onyx/storybook-utils";
23
import type { Meta, StoryObj } from "@storybook/vue3";
34
import { h } from "vue";
45
import OnyxIcon from "../../../OnyxIcon/OnyxIcon.vue";
@@ -12,6 +13,7 @@ const meta: Meta<typeof OnyxMenuItem> = {
1213
component: OnyxMenuItem,
1314
argTypes: {
1415
default: { control: { type: "text" } },
16+
...withNativeEventLogging(["onClick"]),
1517
},
1618
decorators: [
1719
(story) => ({

packages/sit-onyx/src/components/OnyxRadioButton/OnyxRadioButton.stories.ts

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { withNativeEventLogging } from "@sit-onyx/storybook-utils";
12
import type { Meta, StoryObj } from "@storybook/vue3";
23
import OnyxRadioButton from "./OnyxRadioButton.vue";
34

@@ -7,6 +8,9 @@ import OnyxRadioButton from "./OnyxRadioButton.vue";
78
const meta: Meta<typeof OnyxRadioButton> = {
89
title: "Support/RadioButton",
910
component: OnyxRadioButton,
11+
argTypes: {
12+
...withNativeEventLogging(["onChange"]),
13+
},
1014
};
1115

1216
export default meta;

packages/sit-onyx/src/components/OnyxStepper/OnyxStepper.stories.ts

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { withNativeEventLogging } from "@sit-onyx/storybook-utils";
12
import type { Meta, StoryObj } from "@storybook/vue3";
23
import OnyxStepper from "./OnyxStepper.vue";
34

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

1519
export default meta;

packages/sit-onyx/src/components/OnyxTextarea/OnyxTextarea.stories.ts

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { withNativeEventLogging } from "@sit-onyx/storybook-utils";
12
import type { Meta, StoryObj } from "@storybook/vue3";
23
import OnyxTextarea from "./OnyxTextarea.vue";
34

@@ -10,6 +11,9 @@ const meta: Meta<typeof OnyxTextarea> = {
1011
template: `<div style="max-width: 24rem;"> <story /> </div>`,
1112
}),
1213
],
14+
argTypes: {
15+
...withNativeEventLogging(["onChange"]),
16+
},
1317
};
1418

1519
export default meta;

packages/sit-onyx/src/components/OnyxToastMessage/OnyxToastMessage.stories.ts

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { withNativeEventLogging } from "@sit-onyx/storybook-utils";
12
import type { Meta, StoryObj } from "@storybook/vue3";
23
import { defineIconSelectArgType } from "../../utils/storybook";
34
import OnyxToastMessage from "./OnyxToastMessage.vue";
@@ -10,6 +11,7 @@ const meta: Meta<typeof OnyxToastMessage> = {
1011
component: OnyxToastMessage,
1112
argTypes: {
1213
icon: defineIconSelectArgType(),
14+
...withNativeEventLogging(["onClick"]),
1315
},
1416
};
1517

packages/storybook-utils/src/actions.ts

+38-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import type { Decorator } from "@storybook/vue3";
22
import { useArgs } from "storybook/internal/preview-api";
3-
import type { ArgTypesEnhancer, StrictInputType } from "storybook/internal/types";
4-
import { isReactive, reactive, watch } from "vue";
3+
import type { ArgTypes, ArgTypesEnhancer, StrictInputType } from "storybook/internal/types";
4+
import { isReactive, reactive, watch, type Events } from "vue";
5+
import { EVENT_DOC_MAP } from "./events";
56

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

27+
/**
28+
* Allows logging and documentation for the passed event listener names in Storybook.
29+
* Will be documented in a extra "Relevant HTML events" section in the Storybook documentation.
30+
*
31+
* @example
32+
* ```typescript
33+
* const meta: Meta<typeof OnyxButton> = {
34+
* title: "Buttons/Button",
35+
* component: OnyxButton,
36+
* argTypes: {
37+
* somethingElse: { ...someOtherArgType },
38+
* ...withNativeEventLogging(["onClick"]),
39+
* },
40+
*};
41+
* ```
42+
*
43+
* @param relevantEvents a list of event names that should be logged
44+
* @returns Storybook ArgTypes object
45+
*/
46+
export const withNativeEventLogging = (relevantEvents: (keyof Events)[]) =>
47+
relevantEvents.reduce((argTypes, eventName) => {
48+
const { constructor, event } = EVENT_DOC_MAP[eventName];
49+
argTypes[eventName] = {
50+
name: event.name,
51+
control: false,
52+
description: `The native HTML [${event.name}](${event.url}) event which dispatches an [${constructor.name}](${constructor.url}).`,
53+
table: {
54+
category: "Relevant HTML events",
55+
type: { summary: constructor.name },
56+
},
57+
action: event.name,
58+
};
59+
return argTypes;
60+
}, {} as ArgTypes);
61+
2662
export type WithVModelDecoratorOptions = {
2763
/**
2864
* The matcher for the v-model events.

0 commit comments

Comments
 (0)