Skip to content

Commit

Permalink
Merge pull request #103 from lburgess/bugfix/component-annotations-su…
Browse files Browse the repository at this point in the history
…bcomponents-type

fix: story ComponentAnnotations['subcomponents'] to correctly use its own type for subcomponents rather than attempt to inherit from the component
  • Loading branch information
kasperpeulen authored Dec 24, 2024
2 parents 2560342 + 81ecdd0 commit b832db2
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
20 changes: 19 additions & 1 deletion src/story.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
/* global HTMLElement */
import { expectTypeOf } from 'expect-type';
import {
Renderer,
Args,
ArgsFromMeta,
ArgsStoryFn,
ComponentAnnotations,
DecoratorFunction,
LoaderFunction,
ProjectAnnotations,
Renderer,
StoryAnnotationsOrFn,
StrictArgs,
} from './story.js';
Expand All @@ -34,6 +34,8 @@ type ButtonArgs = {

const Button = (props: ButtonArgs) => 'Button';

const ButtonIcon = ({ icon }: { icon: string }) => `ButtonIcon ${icon}`;

let a = 1;
async function doSomething() {
a = 2;
Expand Down Expand Up @@ -118,6 +120,22 @@ CSF1Story.story = {
args: { a: 1 },
};

const simpleWithSubComponents: XMeta = {
title: 'simple',
component: Button,
tags: ['foo', 'bar'],
decorators: [(storyFn, context) => `withDecorator(${storyFn(context)})`],
parameters: { a: () => null, b: NaN, c: Symbol('symbol') },
loaders: [() => Promise.resolve({ d: '3' })],
async beforeEach() {
await doSomething();
return cleanup;
},
args: { x: '1' },
argTypes: { x: { type: { name: 'string' } } },
subcomponents: { ButtonIcon }, // the fact that this line does not cause a Typescript compilation error itself means that the type is correct
};

const CSF2Story: XStory = () => 'Named Story';
CSF2Story.storyName = 'Another name for story';
CSF2Story.tags = ['foo', 'bar'];
Expand Down
2 changes: 1 addition & 1 deletion src/story.ts
Original file line number Diff line number Diff line change
Expand Up @@ -538,7 +538,7 @@ export interface ComponentAnnotations<TRenderer extends Renderer = Renderer, TAr
*
* By defining them each component will have its tab in the args table.
*/
subcomponents?: Record<string, TRenderer['component']>;
subcomponents?: Record<string, (TRenderer & { T: any })['component']>;

/**
* Function that is executed after the story is rendered.
Expand Down

0 comments on commit b832db2

Please sign in to comment.