Skip to content

Commit cdc22a5

Browse files
Copilotmfranzke
andauthored
Fix Storybook SB_PREVIEW_API_0002 error for DBCustomSelect stories (#6315)
* Initial plan * fix: add explicit fn() args for event handlers to prevent SB_PREVIEW_API_0002 error Co-authored-by: mfranzke <787658+mfranzke@users.noreply.github.com> * fix: skip explicit fn() args generation for Angular to prevent TypeScript errors Co-authored-by: mfranzke <787658+mfranzke@users.noreply.github.com> * fix: exclude action event handler argTypes from Angular meta to fix TypeScript errors Co-authored-by: mfranzke <787658+mfranzke@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: mfranzke <787658+mfranzke@users.noreply.github.com>
1 parent 58c517c commit cdc22a5

File tree

2 files changed

+34
-6
lines changed

2 files changed

+34
-6
lines changed

packages/components/configs/plugins/storybook/get-meta-object.cjs

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,14 @@ const extractMetadata = (target, name, meta) => {
1515
title = metadata.storybookTitle;
1616
}
1717
if (metadata.storybookArgTypes) {
18-
// Transform argTypes for Angular (convert event handlers)
18+
// For Angular, skip action-based event handler entries entirely —
19+
// Angular resolves @Output() bindings automatically and these keys
20+
// are not valid properties on the typed Props interface, causing
21+
// TypeScript compilation errors.
1922
if (target === 'angular') {
2023
Object.entries(metadata.storybookArgTypes).forEach(
2124
([key, value]) => {
22-
if (key.startsWith('on') && value?.action) {
23-
const newKey = key.slice(2).toLowerCase();
24-
argTypes[newKey] = { ...value, action: newKey };
25-
} else {
25+
if (!(key.startsWith('on') && value?.action)) {
2626
argTypes[key] = value;
2727
}
2828
}
@@ -36,6 +36,24 @@ const extractMetadata = (target, name, meta) => {
3636
return { title, argTypes };
3737
};
3838

39+
/**
40+
* Generates explicit fn() args for argTypes that define an action.
41+
* This replaces implicit Storybook actions (created by argTypesRegex) with
42+
* explicit fn() spies, preventing SB_PREVIEW_API_0002 errors when event
43+
* handlers are called during component rendering.
44+
* @param {Object} argTypes - ArgTypes object
45+
* @returns {string} Generated args section or empty string
46+
*/
47+
const getFnArgs = (argTypes) => {
48+
const fnArgEntries = Object.entries(argTypes)
49+
.filter(([, value]) => value?.action)
50+
.map(([key]) => `\t"${key}": fn()`);
51+
52+
return fnArgEntries.length > 0
53+
? `args: {\n${fnArgEntries.join(',\n')}\n\t},`
54+
: '';
55+
};
56+
3957
/**
4058
* Generates the Storybook meta object for a component
4159
* @param {Object} params - Parameters object
@@ -66,6 +84,11 @@ const getMetaObject = ({ target, componentName, name, meta, allImports }) => {
6684
],`;
6785
}
6886

87+
// Angular handles event actions differently via its own mechanism; adding
88+
// explicit fn() args would generate properties that don't exist in the
89+
// Angular component's typed Props interface, causing TypeScript errors.
90+
const argsSection = target !== 'angular' ? getFnArgs(argTypes) : '';
91+
6992
return `
7093
const meta: Meta<${metaType}> = {
7194
title: 'Components/${componentName}/${title}',
@@ -75,6 +98,7 @@ const meta: Meta<${metaType}> = {
7598
layout: 'centered'
7699
},
77100
tags: ['autodocs'],
101+
${argsSection}
78102
argTypes: ${JSON.stringify(argTypes)}
79103
} satisfies Meta<${metaType}>;
80104

packages/components/src/components/custom-select/examples/_custom-select.arg.types.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,5 +58,9 @@ export const StorybookCustomSelectArgTypes: Record<string, InputType> = {
5858
amountText: { control: 'text' },
5959
mobileCloseButtonText: { control: 'text' },
6060
open: { control: 'boolean' },
61-
autofocus: { control: 'boolean' }
61+
autofocus: { control: 'boolean' },
62+
onAmountChange: { action: 'onAmountChange' },
63+
onOptionSelected: { action: 'onOptionSelected' },
64+
onDropdownToggle: { action: 'onDropdownToggle' },
65+
onSearch: { action: 'onSearch' }
6266
};

0 commit comments

Comments
 (0)