Skip to content
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

Svelte: Supports action auto configuration #18174

Merged
merged 2 commits into from
Jul 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 5 additions & 12 deletions app/svelte/src/client/docs/extractArgTypes.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,26 +66,22 @@ describe('Extracting Arguments', () => {
expect(results).toMatchInlineSnapshot(`
Object {
"event_afterUpdate": Object {
"action": "afterUpdate",
"control": false,
"description": "After Update",
"name": "afterUpdate",
"table": Object {
"category": "events",
},
"type": Object {
"name": "other",
"value": "void",
},
},
"event_click": Object {
"action": "click",
"control": false,
"description": "Click Event",
"name": "click",
"table": Object {
"category": "events",
},
"type": Object {
"name": "other",
"value": "void",
},
},
"rounded": Object {
"control": Object {
Expand All @@ -108,17 +104,14 @@ describe('Extracting Arguments', () => {
},
},
"slot_default": Object {
"control": false,
"description": "Default Slot

\`{rounded}\`",
"name": "default",
"table": Object {
"category": "slots",
},
"type": Object {
"name": "other",
"value": "void",
},
},
"text": Object {
"control": Object {
Expand Down
5 changes: 3 additions & 2 deletions app/svelte/src/client/docs/extractArgTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,9 @@ export const createArgTypes = (docgen: SvelteComponentDoc) => {
docgen.events.forEach((item) => {
results[`event_${item.name}`] = {
name: item.name,
action: item.name,
control: false,
description: item.description,
type: { name: 'other', value: 'void' },
table: {
category: 'events',
},
Expand All @@ -67,10 +68,10 @@ export const createArgTypes = (docgen: SvelteComponentDoc) => {
docgen.slots.forEach((item) => {
results[`slot_${item.name}`] = {
name: item.name,
control: false,
description: [item.description, item.params?.map((p) => `\`${p.name}\``).join(' ')]
.filter((p) => p)
.join('\n\n'),
type: { name: 'other', value: 'void' },
table: {
category: 'slots',
},
Expand Down
3 changes: 2 additions & 1 deletion app/svelte/src/client/preview/render.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ function cleanUpPreviousStory() {
}

export function renderToDOM(
{ storyFn, kind, name, showMain, showError }: RenderContext<SvelteFramework>,
{ storyFn, kind, name, storyContext, showMain, showError }: RenderContext<SvelteFramework>,
domElement: HTMLElement
) {
cleanUpPreviousStory();
Expand All @@ -31,6 +31,7 @@ export function renderToDOM(
target,
props: {
storyFn,
storyContext,
name,
kind,
showError,
Expand Down
9 changes: 8 additions & 1 deletion app/svelte/templates/PreviewRender.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
export let kind;
export let storyFn;
export let showError;
export let storyContext;

const {
/** @type {SvelteComponent} */
Expand All @@ -18,6 +19,12 @@
WrapperData = {},
} = storyFn();

const eventsFromArgTypes = Object.fromEntries(Object.entries(storyContext.argTypes)
.filter(([k, v]) => v.action && props[k] != null)
.map(([k, v]) => [v.action, props[k]]));

const events = {...eventsFromArgTypes, ...on};

if (!Component) {
showError({
title: `Expecting a Svelte component from the story: "${name}" of "${kind}".`,
Expand All @@ -34,4 +41,4 @@
decoratorProps={WrapperData}
component={Component}
props={props}
{on}/>
on={events}/>
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
</script>

<h1>Button view</h1>
<Button {rounded} on:click={handleClick}>{text}: {count}</Button>
<Button {rounded} on:click on:click={handleClick}>{text}: {count}</Button>
<p>A little text to show this is a view.</p>
<p>If we need to test components in a Svelte environment, for instance to test slot behaviour,</p>
<p>then wrapping the component up in a view</p>
Expand Down