From af5019ba917ca89c314de1240dfc5a1beb61b4ed Mon Sep 17 00:00:00 2001 From: Max Kohler Date: Fri, 12 Sep 2025 10:05:41 +0200 Subject: [PATCH] Add prop, test --- .../src/Switcher/Switcher.stories.svelte | 32 +++++++++++++++++-- components/src/Switcher/Switcher.svelte | 23 +++++++++++-- 2 files changed, 50 insertions(+), 5 deletions(-) diff --git a/components/src/Switcher/Switcher.stories.svelte b/components/src/Switcher/Switcher.stories.svelte index 6b3f3aff..9ecdac7c 100644 --- a/components/src/Switcher/Switcher.stories.svelte +++ b/components/src/Switcher/Switcher.stories.svelte @@ -1,13 +1,15 @@ - @@ -42,3 +44,29 @@ /> + + { + const canvas = within(canvasElement); + await step('onchange handler is called', async () => { + const optionA = canvas.getByLabelText('Option A'); + const optionB = canvas.getByLabelText('Option B'); + await userEvent.click(optionB); + expect(onChangeSpy).toHaveBeenCalledTimes(1); + await userEvent.click(optionA); + expect(onChangeSpy).toHaveBeenCalledTimes(2); + }); + }} +> + + + + diff --git a/components/src/Switcher/Switcher.svelte b/components/src/Switcher/Switcher.svelte index 0545eae0..7e64b94c 100644 --- a/components/src/Switcher/Switcher.svelte +++ b/components/src/Switcher/Switcher.svelte @@ -15,12 +15,22 @@ */ size?: 'default' | 'small'; /** - * The currently-selected option + * The currently-selected option (bindable) */ value: string | null; + /** + * Fired when the selected value changes (Prefer `value` if possible) + */ + onchange?: (e: Event) => void; } - let { label, options, size = 'default', value = $bindable(null) }: SwitcherProps = $props(); + let { + label, + options, + size = 'default', + value = $bindable(null), + onchange + }: SwitcherProps = $props(); const groupId = $props.id(); const groupName = 'select-' + groupId; @@ -38,7 +48,14 @@ - + {/each}