Skip to content

Commit

Permalink
Merge pull request #18174 from j3rem1e/svelte-action
Browse files Browse the repository at this point in the history
Svelte: Supports action auto configuration
  • Loading branch information
ndelangen authored Jul 6, 2022
2 parents 2d3cfe5 + b6d8f2c commit 2246efc
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 17 deletions.
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

0 comments on commit 2246efc

Please sign in to comment.