Skip to content

Commit

Permalink
Merge pull request #26824 from storybookjs/kasper/update-csf
Browse files Browse the repository at this point in the history
CSF: Fix typings for control and other properties of argTypes
(cherry picked from commit 09af3bf)
  • Loading branch information
kasperpeulen authored and yannbf committed Apr 19, 2024
1 parent 5f6a831 commit 8fd4633
Show file tree
Hide file tree
Showing 41 changed files with 156 additions and 135 deletions.
6 changes: 4 additions & 2 deletions code/addons/controls/src/ControlsPanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,10 @@ export const ControlsPanel: FC = () => {
const hasControls = Object.values(rows).some((arg) => arg?.control);

const withPresetColors = Object.entries(rows).reduce((acc, [key, arg]) => {
if (arg?.control?.type !== 'color' || arg?.control?.presetColors) acc[key] = arg;
else acc[key] = { ...arg, control: { ...arg.control, presetColors } };
const control = arg?.control;
if (typeof control !== 'object' || control?.type !== 'color' || control?.presetColors)
acc[key] = arg;
else acc[key] = { ...arg, control: { ...control, presetColors } };
return acc;
}, {} as ArgTypes);

Expand Down
2 changes: 1 addition & 1 deletion code/addons/links/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
"prep": "node --loader ../../../scripts/node_modules/esbuild-register/loader.js -r ../../../scripts/node_modules/esbuild-register/register.js ../../../scripts/prepare/addon-bundle.ts"
},
"dependencies": {
"@storybook/csf": "^0.1.2",
"@storybook/csf": "^0.1.4",
"@storybook/global": "^5.0.0",
"ts-dedent": "^2.0.0"
},
Expand Down
2 changes: 1 addition & 1 deletion code/frameworks/angular/src/client/docs/compodoc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ const extractDefaultValueFromComments = (property: Property, value: any) => {

const extractDefaultValue = (property: Property) => {
try {
let value: string | boolean = property.defaultValue?.replace(/^'(.*)'$/, '$1');
let value: string = property.defaultValue?.replace(/^'(.*)'$/, '$1');
value = castDefaultValue(property, value);

if (value == null && property.jsdoctags?.length > 0) {
Expand Down
2 changes: 1 addition & 1 deletion code/lib/codemod/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
"@babel/core": "^7.23.2",
"@babel/preset-env": "^7.23.2",
"@babel/types": "^7.23.0",
"@storybook/csf": "^0.1.2",
"@storybook/csf": "^0.1.4",
"@storybook/csf-tools": "workspace:*",
"@storybook/node-logger": "workspace:*",
"@storybook/types": "workspace:*",
Expand Down
2 changes: 1 addition & 1 deletion code/lib/core-server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
"@storybook/channels": "workspace:*",
"@storybook/core-common": "workspace:*",
"@storybook/core-events": "workspace:*",
"@storybook/csf": "^0.1.2",
"@storybook/csf": "^0.1.4",
"@storybook/csf-tools": "workspace:*",
"@storybook/docs-mdx": "3.0.0",
"@storybook/global": "^5.0.0",
Expand Down
2 changes: 1 addition & 1 deletion code/lib/csf-tools/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
"@babel/parser": "^7.23.0",
"@babel/traverse": "^7.23.2",
"@babel/types": "^7.23.0",
"@storybook/csf": "^0.1.2",
"@storybook/csf": "^0.1.4",
"@storybook/types": "workspace:*",
"fs-extra": "^11.1.0",
"recast": "^0.23.5",
Expand Down
2 changes: 1 addition & 1 deletion code/lib/manager-api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
"@storybook/channels": "workspace:*",
"@storybook/client-logger": "workspace:*",
"@storybook/core-events": "workspace:*",
"@storybook/csf": "^0.1.2",
"@storybook/csf": "^0.1.4",
"@storybook/global": "^5.0.0",
"@storybook/icons": "^1.2.5",
"@storybook/router": "workspace:*",
Expand Down
2 changes: 1 addition & 1 deletion code/lib/preview-api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
"@storybook/channels": "workspace:*",
"@storybook/client-logger": "workspace:*",
"@storybook/core-events": "workspace:*",
"@storybook/csf": "^0.1.2",
"@storybook/csf": "^0.1.4",
"@storybook/global": "^5.0.0",
"@storybook/types": "workspace:*",
"@types/qs": "^6.9.5",
Expand Down
1 change: 1 addition & 0 deletions code/lib/preview-api/src/modules/store/args.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,7 @@ describe('validateOptions', () => {
});

it('ignores options and logs an error if options is not an array', () => {
// @ts-expect-error This should give TS error indeed (finally!)
expect(validateOptions({ a: 1 }, { a: { options: { 2: 'two' } } })).toStrictEqual({ a: 1 });
expect(once.error).toHaveBeenCalledWith(
expect.stringContaining("Invalid argType: 'a.options' should be an array")
Expand Down
8 changes: 4 additions & 4 deletions code/lib/preview-api/src/modules/store/csf/prepareStory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -250,10 +250,10 @@ export function prepareContext<
return acc;
}

const mappingFn = (originalValue: any) =>
originalValue in targetedContext.argTypes[key].mapping
? targetedContext.argTypes[key].mapping[originalValue]
: originalValue;
const mappingFn = (originalValue: any) => {
const mapping = targetedContext.argTypes[key].mapping;
return mapping && originalValue in mapping ? mapping[originalValue] : originalValue;
};

acc[key] = Array.isArray(val) ? val.map(mappingFn) : mappingFn(val);

Expand Down
6 changes: 4 additions & 2 deletions code/lib/preview-api/src/modules/store/inferControls.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ describe('inferControls', () => {
})
);

expect(inferredControls.background.control.type).toEqual('color');
const control = inferredControls.background.control;
expect(typeof control === 'object' && control.type).toEqual('color');
});

it('should return inferred type when using color matcher but arg passed is not a string', () => {
Expand Down Expand Up @@ -97,7 +98,8 @@ describe('inferControls', () => {
);

expect(warnSpy).toHaveBeenCalled();
expect(inferredControls.background.control.type).toEqual(type.name);
const control = inferredControls.background.control;
expect(typeof control === 'object' && control.type).toEqual(type.name);
});
});
});
Expand Down
2 changes: 1 addition & 1 deletion code/lib/source-loader/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
"prep": "node --loader ../../../scripts/node_modules/esbuild-register/loader.js -r ../../../scripts/node_modules/esbuild-register/register.js ../../../scripts/prepare/bundle.ts"
},
"dependencies": {
"@storybook/csf": "^0.1.2",
"@storybook/csf": "^0.1.4",
"@storybook/types": "workspace:*",
"estraverse": "^5.2.0",
"lodash": "^4.17.21",
Expand Down
2 changes: 1 addition & 1 deletion code/lib/types/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
"file-system-cache": "2.3.0"
},
"devDependencies": {
"@storybook/csf": "^0.1.2",
"@storybook/csf": "^0.1.4",
"@types/fs-extra": "^11.0.1",
"@types/node": "^18.0.0",
"typescript": "^5.3.2"
Expand Down
6 changes: 3 additions & 3 deletions code/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@
"@storybook/core-events": "workspace:*",
"@storybook/core-server": "workspace:*",
"@storybook/core-webpack": "workspace:*",
"@storybook/csf": "^0.1.2",
"@storybook/csf": "^0.1.4",
"@storybook/csf-plugin": "workspace:*",
"@storybook/csf-tools": "workspace:*",
"@storybook/docs-tools": "workspace:*",
Expand Down Expand Up @@ -261,6 +261,7 @@
"type": "opencollective",
"url": "https://opencollective.com/storybook"
},
"deferredNextVersion": "8.0.9",
"pr-log": {
"skipLabels": [
"cleanup"
Expand Down Expand Up @@ -295,6 +296,5 @@
"Dependency Upgrades"
]
]
},
"deferredNextVersion": "8.0.9"
}
}
4 changes: 2 additions & 2 deletions code/renderers/react/src/docs/extractArgTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ export const extractArgTypes: ArgTypesExtractor = (component) => {
description,
type: { required, ...sbType },
table: {
type,
type: type ?? undefined,
jsDocTags,
defaultValue: defaultSummary,
defaultValue: defaultSummary ?? undefined,
},
};
return acc;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"description": "specify icon="search" or icon={IconComponent}",
"name": "icon",
"table": {
"defaultValue": null,
"defaultValue": undefined,
"jsDocTags": undefined,
"type": {
"detail": undefined,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"description": "",
"name": "aProperty",
"table": {
"defaultValue": null,
"defaultValue": undefined,
"jsDocTags": undefined,
"type": {
"detail": undefined,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"description": "No background or border if static alert",
"name": "blank",
"table": {
"defaultValue": null,
"defaultValue": undefined,
"jsDocTags": undefined,
"type": {
"detail": undefined,
Expand All @@ -25,7 +25,7 @@
"description": "Allows icon override, accepts material icon name",
"name": "icon",
"table": {
"defaultValue": null,
"defaultValue": undefined,
"jsDocTags": undefined,
"type": {
"detail": undefined,
Expand All @@ -44,7 +44,7 @@
"description": "",
"name": "message",
"table": {
"defaultValue": null,
"defaultValue": undefined,
"jsDocTags": undefined,
"type": {
"detail": undefined,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"description": "",
"name": "bar",
"table": {
"defaultValue": null,
"defaultValue": undefined,
"jsDocTags": undefined,
"type": {
"detail": undefined,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"description": "Please work...",
"name": "test",
"table": {
"defaultValue": null,
"defaultValue": undefined,
"jsDocTags": undefined,
"type": {
"detail": undefined,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"description": "",
"name": "classes",
"table": {
"defaultValue": null,
"defaultValue": undefined,
"jsDocTags": undefined,
"type": {
"detail": undefined,
Expand Down Expand Up @@ -47,7 +47,7 @@
"description": "",
"name": "icon",
"table": {
"defaultValue": null,
"defaultValue": undefined,
"jsDocTags": undefined,
"type": {
"detail": undefined,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"description": "",
"name": "areas",
"table": {
"defaultValue": null,
"defaultValue": undefined,
"jsDocTags": undefined,
"type": {
"detail": "[object]",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"description": "A message alerting about Empire activities.",
"name": "message",
"table": {
"defaultValue": null,
"defaultValue": undefined,
"jsDocTags": undefined,
"type": {
"detail": undefined,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"description": "",
"name": "label",
"table": {
"defaultValue": null,
"defaultValue": undefined,
"jsDocTags": undefined,
"type": {
"detail": undefined,
Expand All @@ -22,7 +22,7 @@
"description": "",
"name": "onClick",
"table": {
"defaultValue": null,
"defaultValue": undefined,
"jsDocTags": undefined,
"type": {
"detail": undefined,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"description": "",
"name": "other",
"table": {
"defaultValue": null,
"defaultValue": undefined,
"jsDocTags": undefined,
"type": {
"detail": undefined,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"description": "",
"name": "heads",
"table": {
"defaultValue": null,
"defaultValue": undefined,
"jsDocTags": undefined,
"type": {
"detail": undefined,
Expand All @@ -23,7 +23,7 @@
"description": "",
"name": "onAddClick",
"table": {
"defaultValue": null,
"defaultValue": undefined,
"jsDocTags": undefined,
"type": {
"detail": undefined,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"description": "The size (replaces width)",
"name": "size",
"table": {
"defaultValue": null,
"defaultValue": undefined,
"jsDocTags": undefined,
"type": {
"detail": undefined,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"description": "",
"name": "checked",
"table": {
"defaultValue": null,
"defaultValue": undefined,
"jsDocTags": undefined,
"type": {
"detail": undefined,
Expand All @@ -25,7 +25,7 @@
"description": "",
"name": "defaultChecked",
"table": {
"defaultValue": null,
"defaultValue": undefined,
"jsDocTags": undefined,
"type": {
"detail": undefined,
Expand All @@ -44,7 +44,7 @@
"description": "The input content value",
"name": "value",
"table": {
"defaultValue": null,
"defaultValue": undefined,
"jsDocTags": undefined,
"type": {
"detail": undefined,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"description": "",
"name": "bar",
"table": {
"defaultValue": null,
"defaultValue": undefined,
"jsDocTags": undefined,
"type": {
"detail": undefined,
Expand All @@ -31,7 +31,7 @@
"description": "",
"name": "foo",
"table": {
"defaultValue": null,
"defaultValue": undefined,
"jsDocTags": undefined,
"type": {
"detail": undefined,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"description": "",
"name": "spacing",
"table": {
"defaultValue": null,
"defaultValue": undefined,
"jsDocTags": undefined,
"type": {
"detail": undefined,
Expand Down
Loading

0 comments on commit 8fd4633

Please sign in to comment.