Skip to content

Commit

Permalink
Add event listeners to Svelte known properties
Browse files Browse the repository at this point in the history
  • Loading branch information
edoardocavazza committed Jul 16, 2024
1 parent 2ef631a commit e982307
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/polite-rockets-beg.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@chialab/plasma': patch
---

Add event listeners to Svelte known properties.
8 changes: 5 additions & 3 deletions src/svelte.ts
Original file line number Diff line number Diff line change
Expand Up @@ -244,9 +244,11 @@ import { ${declaration.name} as Base${declaration.name} } from '${options.entryp
import { ${getAttributes(definition.extend ?? definition.name).split('<')[0]} } from 'svelte/elements';
`;

const propertiesTypings = filterPublicMemebers(declaration).map(
(member) => `${member.name}?: Base${declaration.name}['${member.name}'];`
);
const propertiesTypings = filterPublicMemebers(declaration)
.map((member) => `${member.name}?: Base${declaration.name}['${member.name}'];`)
.concat(
declaration.events?.map((declaration) => `on${declaration.name}?: (event: CustomEvent) => boolean;`) ?? []
);

const eventsTypings = [];
if (declaration.events) {
Expand Down
2 changes: 2 additions & 0 deletions test/src/svelte/TestElement.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ declare const __propDef: {
numericProp?: BaseTestElement['numericProp'];
objectProp?: BaseTestElement['objectProp'];
defaultValue?: BaseTestElement['defaultValue'];
onstringchange?: (event: CustomEvent) => boolean;
};
events: {
'stringchange': CustomEvent;
Expand All @@ -23,4 +24,5 @@ export type TestElementProps = typeof __propDef.props;
export type TestElementEvents = typeof __propDef.events;
export type TestElementSlots = typeof __propDef.slots;
export class TestElement extends SvelteComponent<TestElementProps, TestElementEvents, TestElementSlots> {
getElement(): BaseTestElement;
}
7 changes: 7 additions & 0 deletions test/src/svelte/TestElement.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@
};
}
let __element;
export function getElement() {
return __element;
}
export let stringProp = undefined;
export let booleanProp = undefined;
export let numericProp = undefined;
Expand All @@ -26,6 +32,7 @@

<!-- svelte-ignore attribute_avoid_is -->
<test-element
bind:this={__element}
{...$$restProps}
use:__sync={{
stringProp,
Expand Down
2 changes: 2 additions & 0 deletions test/src/svelte/TestLink.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ declare const __propDef: {
numericProp?: BaseTestLink['numericProp'];
objectProp?: BaseTestLink['objectProp'];
defaultValue?: BaseTestLink['defaultValue'];
onstringchange?: (event: CustomEvent) => boolean;
};
events: {
'stringchange': CustomEvent;
Expand All @@ -23,4 +24,5 @@ export type TestLinkProps = typeof __propDef.props;
export type TestLinkEvents = typeof __propDef.events;
export type TestLinkSlots = typeof __propDef.slots;
export class TestLink extends SvelteComponent<TestLinkProps, TestLinkEvents, TestLinkSlots> {
getElement(): BaseTestLink;
}
7 changes: 7 additions & 0 deletions test/src/svelte/TestLink.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@
};
}
let __element;
export function getElement() {
return __element;
}
export let stringProp = undefined;
export let booleanProp = undefined;
export let numericProp = undefined;
Expand All @@ -26,6 +32,7 @@

<!-- svelte-ignore attribute_avoid_is -->
<a
bind:this={__element}
is="test-link"
{...$$restProps}
use:__sync={{
Expand Down

0 comments on commit e982307

Please sign in to comment.