-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Vue3: Improve CSF3 types #19602
Merged
Merged
Vue3: Improve CSF3 types #19602
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
d0899ea
implement sound CSF3 types for Vue3
kasperpeulen 3cae418
fix jest tests
kasperpeulen ac328e2
Merge remote-tracking branch 'origin/next' into future/CSF3-vue3
kasperpeulen dbbf7e4
update yarn.lock
kasperpeulen c9aa816
use prettier on vue files
kasperpeulen File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<script setup lang="ts"> | ||
defineProps<{ disabled: boolean; label: string }>(); | ||
|
||
const emit = defineEmits<{ | ||
(e: 'myChangeEvent', id: number): void; | ||
(e: 'myClickEvent', id: number): void; | ||
}>(); | ||
</script> | ||
|
||
<template> | ||
<button :disabled="disabled" @change="emit('myChangeEvent', 0)" @click="emit('myClickEvent', 0)"> | ||
{{ label }} | ||
</button> | ||
</template> | ||
|
||
<style scoped></style> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<script setup lang="ts"> | ||
defineProps<{ decoratorArg: string }>(); | ||
</script> | ||
|
||
<template> | ||
Decorator: {decoratorArg} | ||
<slot></slot> | ||
</template> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<script setup lang="ts"> | ||
defineProps<{ decoratorArg2: string }>(); | ||
</script> | ||
|
||
<template> | ||
Decorator: {decoratorArg2} | ||
<slot></slot> | ||
</template> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,192 @@ | ||
import { satisfies } from '@storybook/core-common'; | ||
import { ComponentAnnotations, StoryAnnotations } from '@storybook/csf'; | ||
import { expectTypeOf } from 'expect-type'; | ||
import { SetOptional } from 'type-fest'; | ||
import { ComponentOptions, FunctionalComponent, h } from 'vue'; | ||
import Button from './__tests__/Button.vue'; | ||
import Decorator2TsVue from './__tests__/Decorator2.vue'; | ||
import DecoratorTsVue from './__tests__/Decorator.vue'; | ||
import { DecoratorFn, Meta, StoryObj } from './public-types'; | ||
import { VueFramework } from './types'; | ||
|
||
describe('Meta', () => { | ||
test('Generic parameter of Meta can be a component', () => { | ||
const meta: Meta<typeof Button> = { | ||
component: Button, | ||
args: { label: 'good', disabled: false }, | ||
}; | ||
|
||
expectTypeOf(meta).toEqualTypeOf< | ||
ComponentAnnotations< | ||
VueFramework, | ||
{ | ||
readonly disabled: boolean; | ||
readonly label: string; | ||
onMyChangeEvent?: (id: number) => any; | ||
onMyClickEvent?: (id: number) => any; | ||
} | ||
> | ||
>(); | ||
}); | ||
|
||
test('Generic parameter of Meta can be the props of the component', () => { | ||
const meta: Meta<{ disabled: boolean; label: string }> = { | ||
component: Button, | ||
args: { label: 'good', disabled: false }, | ||
}; | ||
|
||
expectTypeOf(meta).toEqualTypeOf< | ||
ComponentAnnotations<VueFramework, { disabled: boolean; label: string }> | ||
>(); | ||
}); | ||
|
||
test('Events are inferred from component', () => { | ||
const meta: Meta<typeof Button> = { | ||
component: Button, | ||
args: { | ||
label: 'good', | ||
disabled: false, | ||
onMyChangeEvent: (value) => { | ||
expectTypeOf(value).toEqualTypeOf<number>(); | ||
}, | ||
}, | ||
render: (args) => { | ||
return h(Button, { | ||
...args, | ||
onMyChangeEvent: (value) => { | ||
expectTypeOf(value).toEqualTypeOf<number>(); | ||
}, | ||
}); | ||
}, | ||
}; | ||
}); | ||
}); | ||
|
||
describe('StoryObj', () => { | ||
type ButtonProps = { | ||
readonly disabled: boolean; | ||
readonly label: string; | ||
onMyChangeEvent?: ((id: number) => any) | undefined; | ||
onMyClickEvent?: ((id: number) => any) | undefined; | ||
}; | ||
|
||
test('✅ Required args may be provided partial in meta and the story', () => { | ||
const meta = satisfies<Meta<typeof Button>>()({ | ||
component: Button, | ||
args: { label: 'good' }, | ||
}); | ||
|
||
type Actual = StoryObj<typeof meta>; | ||
type Expected = StoryAnnotations<VueFramework, ButtonProps, SetOptional<ButtonProps, 'label'>>; | ||
expectTypeOf<Actual>().toEqualTypeOf<Expected>(); | ||
}); | ||
|
||
test('❌ The combined shape of meta args and story args must match the required args.', () => { | ||
{ | ||
const meta = satisfies<Meta<typeof Button>>()({ component: Button }); | ||
|
||
type Expected = StoryAnnotations<VueFramework, ButtonProps, ButtonProps>; | ||
expectTypeOf<StoryObj<typeof meta>>().toEqualTypeOf<Expected>(); | ||
} | ||
{ | ||
const meta = satisfies<Meta<typeof Button>>()({ | ||
component: Button, | ||
args: { label: 'good' }, | ||
}); | ||
// @ts-expect-error disabled not provided ❌ | ||
const Basic: StoryObj<typeof meta> = {}; | ||
|
||
type Expected = StoryAnnotations< | ||
VueFramework, | ||
ButtonProps, | ||
SetOptional<ButtonProps, 'label'> | ||
>; | ||
expectTypeOf(Basic).toEqualTypeOf<Expected>(); | ||
} | ||
{ | ||
const meta = satisfies<Meta<{ label: string; disabled: boolean }>>()({ component: Button }); | ||
const Basic: StoryObj<typeof meta> = { | ||
// @ts-expect-error disabled not provided ❌ | ||
args: { label: 'good' }, | ||
}; | ||
|
||
type Expected = StoryAnnotations<VueFramework, ButtonProps, ButtonProps>; | ||
expectTypeOf(Basic).toEqualTypeOf<Expected>(); | ||
} | ||
}); | ||
|
||
test('Component can be used as generic parameter for StoryObj', () => { | ||
expectTypeOf<StoryObj<typeof Button>>().toEqualTypeOf< | ||
StoryAnnotations<VueFramework, ButtonProps> | ||
>(); | ||
}); | ||
}); | ||
|
||
type ThemeData = 'light' | 'dark'; | ||
|
||
type ComponentProps<Component> = Component extends ComponentOptions<infer P> | ||
? P | ||
: Component extends FunctionalComponent<infer P> | ||
? P | ||
: never; | ||
|
||
describe('Story args can be inferred', () => { | ||
test('Correct args are inferred when type is widened for render function', () => { | ||
type Props = ComponentProps<typeof Button> & { theme: ThemeData }; | ||
|
||
const meta = satisfies<Meta<Props>>()({ | ||
component: Button, | ||
args: { disabled: false }, | ||
render: (args) => { | ||
return h('div', [h('div', `Use the theme ${args.theme}`), h(Button, args)]); | ||
}, | ||
}); | ||
|
||
const Basic: StoryObj<typeof meta> = { args: { theme: 'light', label: 'good' } }; | ||
|
||
type Expected = StoryAnnotations<VueFramework, Props, SetOptional<Props, 'disabled'>>; | ||
expectTypeOf(Basic).toEqualTypeOf<Expected>(); | ||
}); | ||
|
||
const withDecorator: DecoratorFn<{ decoratorArg: string }> = ( | ||
storyFn, | ||
{ args: { decoratorArg } } | ||
) => h(DecoratorTsVue, { decoratorArg }, h(storyFn())); | ||
|
||
test('Correct args are inferred when type is widened for decorators', () => { | ||
type Props = ComponentProps<typeof Button> & { decoratorArg: string }; | ||
|
||
const meta = satisfies<Meta<Props>>()({ | ||
component: Button, | ||
args: { disabled: false }, | ||
decorators: [withDecorator], | ||
}); | ||
|
||
const Basic: StoryObj<typeof meta> = { args: { decoratorArg: 'title', label: 'good' } }; | ||
|
||
type Expected = StoryAnnotations<VueFramework, Props, SetOptional<Props, 'disabled'>>; | ||
expectTypeOf(Basic).toEqualTypeOf<Expected>(); | ||
}); | ||
|
||
test('Correct args are inferred when type is widened for multiple decorators', () => { | ||
type Props = ComponentProps<typeof Button> & { decoratorArg: string; decoratorArg2: string }; | ||
|
||
const secondDecorator: DecoratorFn<{ decoratorArg2: string }> = ( | ||
storyFn, | ||
{ args: { decoratorArg2 } } | ||
) => h(Decorator2TsVue, { decoratorArg2 }, h(storyFn())); | ||
|
||
const meta = satisfies<Meta<Props>>()({ | ||
component: Button, | ||
args: { disabled: false }, | ||
decorators: [withDecorator, secondDecorator], | ||
}); | ||
|
||
const Basic: StoryObj<typeof meta> = { | ||
args: { decoratorArg: '', decoratorArg2: '', label: 'good' }, | ||
}; | ||
|
||
type Expected = StoryAnnotations<VueFramework, Props, SetOptional<Props, 'disabled'>>; | ||
expectTypeOf(Basic).toEqualTypeOf<Expected>(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,8 +13,7 @@ export const render: ArgsStoryFn<VueFramework> = (props, context) => { | |
); | ||
} | ||
|
||
// TODO remove this hack | ||
return h(Component as Parameters<typeof h>[0], props); | ||
return h(Component, props); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🎉 nice!!! ❤️ |
||
}; | ||
|
||
let setupFunction = (app: any) => {}; | ||
|
@@ -40,7 +39,7 @@ export function renderToDOM( | |
return h(element); | ||
}, | ||
}); | ||
storybookApp.config.errorHandler = showException; | ||
storybookApp.config.errorHandler = (e: unknown) => showException(e as Error); | ||
element = storyFn(); | ||
|
||
if (!element) { | ||
|
@@ -56,9 +55,7 @@ export function renderToDOM( | |
|
||
showMain(); | ||
|
||
if (map.has(domElement)) { | ||
map.get(domElement).unmount(); | ||
} | ||
map.get(domElement)?.unmount(); | ||
|
||
storybookApp.mount(domElement); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Generally we've gone with a convention that a type variable is prefaced with
T
to distinguish from a concrete type. WDYT?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't know, not a big fan, but also not necessarily against it. I understand that it may be something useful if you want to do:
But I'm also not sure if it really becomes more readable if you use it everywhere.
I'm okay with adopting it, I would have to change some stuff then in the other renderers as well :D
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shall I adopt this convention in a separate PR?