Skip to content

Commit

Permalink
fix: empty decorator array not rendering component properly. fix args…
Browse files Browse the repository at this point in the history
… inheritance. closes #76
  • Loading branch information
itsjavi committed Nov 24, 2023
1 parent d636fe3 commit 7d3836a
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 3 deletions.
23 changes: 21 additions & 2 deletions packages/docs/stories/buttons.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export default {
],
args: {
// default args
children: 'My Button',
children: 'Default Button children',
},
parameters: {
// overriden addon parameters
Expand All @@ -34,8 +34,27 @@ export default {
},
} satisfies StoryType

export const WithInheritedDefaultArgs: StoryType = {
title: 'With Inherited Defaults',
args: {
hoverable: true,
primary: true,
// children is inherited from the default export args
},
decorators: [
(Story, context) => {
return (
<div style={{ padding: '16px', borderRadius: '8px', border: '1px solid #888' }}>
<p>This story inherits the default args from the default export.</p>
<Story {...context?.args} />
</div>
)
},
],
}

export const WithComponentProps: StoryType = {
title: 'This title is overriden by "name"',
title: '---This title is overriden by "name"---',
name: 'With Args',
args: {
hoverable: true,
Expand Down
3 changes: 2 additions & 1 deletion packages/storylite/src/services/renderer/react.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ export function renderStory(storyComponent: SLFunctionComponent, story: StoryWit
loaded: loadedData,
}

const storyDecorators = story.decorators ?? [defaultDecorator]
const storyDecorators =
story.decorators && story.decorators.length > 0 ? story.decorators : [defaultDecorator]
const DecoratedStory = applyDecorators(storyComponent, storyDecorators, decoratorContext)

if (story.render) {
Expand Down
14 changes: 14 additions & 0 deletions packages/storylite/src/types/story.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,20 @@ export interface BaseStory<P extends SLFunctionComponent = SLFunctionComponent<{
*
*/
component?: P

/**
* If true, represents the default export of the story file.
*
* This value should not be set manually, it is set by StoryLite's story collector.
*/
readonly _isDefault?: boolean

/**
* The module ID of the story file.
*
* This value should not be set manually, it is set by StoryLite's story collector.
*/
readonly _moduleId?: string
}

/**
Expand Down
11 changes: 11 additions & 0 deletions packages/vite-plugin/src/story-collector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,18 @@ function modulesToStories(
navigation: {
...inheritedNavigation,
},
name: exportName,
_moduleId: storyIdPrefix,
_isDefault: exportName === 'default',
...story,
args: {
..._resolvedDefaultStory.args,
...story.args,
},
parameters: {
..._resolvedDefaultStory.parameters,
...story.parameters,
},
title: camelToTitleCase(titleizeFilename(storyTitle)),
decorators: mergedDecorators,
}
Expand Down
2 changes: 2 additions & 0 deletions packages/vite-plugin/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ export type BaseStory = {
title?: string
name?: string
component?: { (props?: any): any; displayName?: string }
_isDefault?: boolean
_moduleId?: string
[key: string]: any
}

Expand Down

0 comments on commit 7d3836a

Please sign in to comment.