Skip to content

Commit

Permalink
APP-6324: Make ToggleButtons purely presentational (#585)
Browse files Browse the repository at this point in the history
  • Loading branch information
ethanlookpotts authored Oct 7, 2024
1 parent 6b1c55d commit 247338a
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 12 deletions.
2 changes: 1 addition & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@viamrobotics/prime-core",
"version": "0.0.157",
"version": "0.0.158",
"repository": {
"type": "git",
"url": "https://github.com/viamrobotics/prime.git",
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/lib/__tests__/toggle-button.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ describe('ToggleButtons', () => {

await userEvent.click(button);

expect(onInput).toHaveBeenCalled();
expect(button).toHaveAttribute('aria-pressed', 'true');
expect(onInput).toHaveBeenCalledOnce();
expect(onInput).toHaveBeenCalledWith(new CustomEvent('input'));
});

it('Prevents option input if disabled', async () => {
Expand Down
1 change: 0 additions & 1 deletion packages/core/src/lib/toggle-buttons.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ $: getButtonClasses = (option: string) => {
};
const handleClick = (value: string) => {
selected = value;
dispatch('input', value);
};
</script>
Expand Down
21 changes: 14 additions & 7 deletions packages/core/src/routes/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,12 @@ const htmlSnippet = `
<li>Ummm…to eBay?</li>
</ul>`.trim();
const toggleButtonSelecteds = [undefined, 'Opt 1', 'Opt 1', 'Opt 1'];
const createHandleToggleButtons =
(index: number) => (event: CustomEvent<string>) => {
toggleButtonSelecteds[index] = event.detail;
};
let hoverDelayMS = 1000;
const onHoverDelayMsInput = (event: Event) => {
hoverDelayMS = Number.parseInt((event.target as HTMLInputElement).value, 10);
Expand Down Expand Up @@ -1825,31 +1831,32 @@ const onHoverDelayMsInput = (event: Event) => {
<div class="flex items-end gap-4">
<ToggleButtons
options={['Opt 1', 'Opt 2', 'Opt 3']}
on:input={(event) => {
// eslint-disable-next-line no-console
console.log('ToggleButtons input', event);
}}
selected={toggleButtonSelecteds[0]}
on:input={createHandleToggleButtons(0)}
/>

<ToggleButtons
options={['Opt 1', 'Opt 2']}
selected="Opt 1"
selected={toggleButtonSelecteds[1]}
on:input={createHandleToggleButtons(1)}
>
<svelte:fragment slot="legend">Preselected toggle</svelte:fragment>
</ToggleButtons>

<ToggleButtons
options={['Opt 1', 'Opt 2', 'Opt 3']}
selected="Opt 1"
disabled
selected={toggleButtonSelecteds[2]}
on:input={createHandleToggleButtons(2)}
>
<svelte:fragment slot="legend">Disabled toggle</svelte:fragment>
</ToggleButtons>

<ToggleButtons
options={['Opt 1', 'Opt 2', 'Opt 3']}
selected="Opt 1"
cx="w-full"
selected={toggleButtonSelecteds[3]}
on:input={createHandleToggleButtons(3)}
>
<svelte:fragment slot="legend">Full width</svelte:fragment>
</ToggleButtons>
Expand Down
8 changes: 7 additions & 1 deletion packages/storybook/src/stories/toggle-buttons.stories.svelte
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
<script lang="ts">
import { Meta, Story } from '@storybook/addon-svelte-csf';
import { Label, ToggleButtons } from '@viamrobotics/prime-core';
let basicSelected = 'Fan lizard';
const onBasicSelected = (event: CustomEvent<string>) => {
basicSelected = event.detail;
};
</script>

<Meta title="Elements/ToggleButtons" />
Expand All @@ -14,7 +19,8 @@ import { Label, ToggleButtons } from '@viamrobotics/prime-core';
>
<ToggleButtons
options={['Fan lizard', 'Mountain banshee', 'Buzzard wasp']}
selected="Fan lizard"
selected={basicSelected}
on:input={onBasicSelected}
/>
</div>
</Label>
Expand Down

0 comments on commit 247338a

Please sign in to comment.