|
6 | 6 | export let Component;
|
7 | 7 | export let props = {};
|
8 | 8 | export let on = undefined;
|
| 9 | + export let argTypes = undefined; |
9 | 10 |
|
10 | 11 | let instance;
|
11 | 12 | let decoratorInstance;
|
12 | 13 |
|
13 | 14 | const svelteVersion = VERSION[0];
|
| 15 | + |
| 16 | + /* |
| 17 | + Svelte Docgen will create argTypes for events with the name 'event_eventName' |
| 18 | + The Actions addon will convert these to args because they are type: 'action' |
| 19 | + We need to filter these args out so they are not passed to the component |
| 20 | + */ |
| 21 | + let propsWithoutDocgenEvents; |
| 22 | + $: propsWithoutDocgenEvents = Object.fromEntries( |
| 23 | + Object.entries(props).filter(([key]) => !key.startsWith('event_')) |
| 24 | + ); |
14 | 25 |
|
15 |
| - if (on && svelteVersion < 5) { |
| 26 | + if (argTypes && svelteVersion < 5) { |
| 27 | + const eventsFromArgTypes = Object.fromEntries( |
| 28 | + Object.entries(argTypes) |
| 29 | + .filter(([key, value]) => value.action && props[key] != null) |
| 30 | + .map(([key, value]) => [value.action, props[key]]) |
| 31 | + ); |
16 | 32 | // Attach Svelte event listeners in Svelte v4
|
17 | 33 | // In Svelte v5 this is not possible anymore as instances are no longer classes with $on() properties, so it will be a no-op
|
18 | 34 | onMount(() => {
|
19 |
| - Object.entries(on).forEach(([eventName, eventCallback]) => { |
20 |
| - // instance can be undefined if a decorator doesn't have <slot/> |
| 35 | + Object.entries({ ...eventsFromArgTypes, ...on }).forEach(([eventName, eventCallback]) => { |
| 36 | + // instance can be undefined if a decorator doesn't have a <slot/> |
21 | 37 | const inst = instance ?? decoratorInstance;
|
22 |
| - inst?.$on?.(eventName, eventCallback) |
| 38 | + inst?.$on?.(eventName, eventCallback); |
23 | 39 | });
|
24 | 40 | });
|
25 | 41 | }
|
26 | 42 | </script>
|
27 | 43 |
|
28 | 44 | {#if decorator}
|
29 | 45 | <svelte:component this={decorator.Component} {...decorator.props} bind:this={decoratorInstance}>
|
30 |
| - <svelte:component this={Component} {...props} bind:this={instance} /> |
| 46 | + <svelte:component this={Component} {...propsWithoutDocgenEvents} bind:this={instance} /> |
31 | 47 | </svelte:component>
|
32 | 48 | {:else}
|
33 |
| - <svelte:component this={Component} {...props} bind:this={instance} /> |
| 49 | + <svelte:component this={Component} {...propsWithoutDocgenEvents} bind:this={instance} /> |
34 | 50 | {/if}
|
0 commit comments