Skip to content

Enhanced (SelectField) demo filtering logic and form handling... #635

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Aug 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/dialog-drawer-event-propagation.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte-ux': patch
---

fix(Dialog/Drawer): event propagation preventing outside click detection
5 changes: 5 additions & 0 deletions .changeset/multiselect-demo-enhancements.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte-ux': patch
---

docs(MultiSelect/MultiSelectField/MultiSelectMenu): Enhanced demo examples with functional item creation dialogs
5 changes: 5 additions & 0 deletions .changeset/numberstepper-documentation.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte-ux': patch
---

docs(NumberStepper): demo example with prefix/suffix slot
5 changes: 5 additions & 0 deletions .changeset/selectfield-demo-improvements.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte-ux': patch
---

docs(SelectField): demo filtering logic and form handling
5 changes: 5 additions & 0 deletions .changeset/selectfield-focus-management.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte-ux': patch
---

fix(SelectField): focus management when used within dialogs
3 changes: 3 additions & 0 deletions packages/svelte-ux/src/lib/components/Dialog.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,9 @@
classes.root
)}
on:click={onClick}
on:mouseup={(e) => {
e.stopPropagation(); // Prevent mouseup from bubbling to outside click handlers (e.g., Popover/Menu clickOutside)
}}
Comment on lines +106 to +108
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you show the example(s) where this was broken? Just so I can see the before/after. I'm not surprised to have to add this as event propagation is challenging, and wanted to do some regression testing as well.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Screencast_20250811_171911.webm

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When I updated it before i used tab on my keyboard so I didn't notice that when you click anything in the dialog the select field closes.

on:keydown={(e) => {
if (e.key === 'Escape') {
// Do not allow event to reach Popover's on:keydown
Expand Down
3 changes: 3 additions & 0 deletions packages/svelte-ux/src/lib/components/Drawer.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,9 @@
$$props.class
)}
style={$$props.style}
on:mouseup={(e) => {
e.stopPropagation(); // Prevent mouseup from bubbling to outside click handlers (e.g., Popover/Menu clickOutside)
}}
in:fly|global={{
x: placement === 'left' ? '-100%' : placement === 'right' ? '100%' : 0,
y: placement === 'top' ? '-100%' : placement === 'bottom' ? '100%' : 0,
Expand Down
1 change: 1 addition & 0 deletions packages/svelte-ux/src/lib/components/SelectField.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,7 @@
// Hide if focus not moved to menu (option clicked)
if (
fe.relatedTarget instanceof HTMLElement &&
!fe.relatedTarget.closest('[role="dialog"]') &&
!menuOptionsEl?.contains(fe.relatedTarget) && // TODO: Oddly Safari does not set `relatedTarget` to the clicked on menu option (like Chrome and Firefox) but instead appears to take `tabindex` into consideration. Currently resolves to `.options` after setting `tabindex="-1"
fe.relatedTarget !== menuOptionsEl?.offsetParent && // click on scroll bar
// Allow focus to move into auxiliary slot areas (beforeOptions, afterOptions, actions)
Expand Down
130 changes: 101 additions & 29 deletions packages/svelte-ux/src/routes/docs/components/MultiSelect/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,32 @@

import {
Button,
Dialog,
Drawer,
Form,
Icon,
MultiSelect,
MultiSelectOption,
TextField,
Toggle,
ToggleButton,
ToggleGroup,
ToggleOption,
type MenuOption,
} from 'svelte-ux';
import Preview from '$lib/components/Preview.svelte';

const options = [
let options: MenuOption[] = [
{ label: 'One', value: 1 },
{ label: 'Two', value: 2 },
{ label: 'Three', value: 3 },
{ label: 'Four', value: 4 },
];

const newOption: () => MenuOption = () => {
return { label: '', value: null };
};

const manyOptions = Array.from({ length: 100 }).map((_, i) => ({
label: `${i + 1}`,
value: i + 1,
Expand Down Expand Up @@ -168,34 +176,6 @@
</div>
</Preview>

<h2>actions slot</h2>

<Preview>
{value.length} selected
<div class="flex flex-col max-h-[360px] overflow-auto">
<MultiSelect {options} {value} on:change={(e) => (value = e.detail.value)} search>
<div slot="actions">
<Button color="primary" icon={mdiPlus}>Add item</Button>
</div>
</MultiSelect>
</div>
</Preview>

<h2>actions slot with max warning</h2>

<Preview>
{value.length} selected
<div class="flex flex-col max-h-[360px] overflow-auto">
<MultiSelect {options} {value} on:change={(e) => (value = e.detail.value)} search max={2}>
<div slot="actions" let:selection class="flex items-center">
{#if selection.isMaxSelected()}
<div class="text-sm text-danger">Maximum selection reached</div>
{/if}
</div>
</MultiSelect>
</div>
</Preview>

<h2>beforeOptions slot</h2>

<Preview>
Expand Down Expand Up @@ -254,6 +234,98 @@
</div>
</Preview>

<h2>actions slot</h2>

<Preview>
{value.length} selected
<div class="flex flex-col max-h-[360px] overflow-auto">
<MultiSelect {options} {value} on:change={(e) => (value = e.detail.value)} search>
<div slot="actions" class="p-2" on:click|stopPropagation role="none">
<Toggle let:on={open} let:toggle>
<Button icon={mdiPlus} color="primary" on:click={toggle}>New item</Button>
<Form
initial={newOption()}
on:change={(e) => {
// Convert value to number if it's a valid number, otherwise keep as string
const newOptionData = { ...e.detail };
if (
newOptionData.value !== null &&
newOptionData.value !== '' &&
!isNaN(Number(newOptionData.value))
) {
newOptionData.value = Number(newOptionData.value);
}
options = [newOptionData, ...options];
// Auto-select the newly created option
value = [...(value || []), newOptionData.value];
}}
let:draft
let:current
let:commit
let:revert
>
<Dialog
{open}
on:close={() => {
toggle();
}}
>
<div slot="title">Create new option</div>
<div class="px-6 py-3 w-96 grid gap-2">
<TextField
label="Label"
value={current.label}
on:change={(e) => {
draft.label = e.detail.value;
}}
autofocus
/>
<TextField
label="Value"
value={draft.value}
on:change={(e) => {
draft.value = e.detail.value;
}}
/>
</div>
<div slot="actions">
<Button
on:click={() => {
commit();
toggle();
}}
color="primary">Add option</Button
>
<Button
on:click={() => {
revert();
toggle();
}}>Cancel</Button
>
</div>
</Dialog>
</Form>
</Toggle>
</div>
</MultiSelect>
</div>
</Preview>

<h2>actions slot with max warning</h2>

<Preview>
{value.length} selected
<div class="flex flex-col max-h-[360px] overflow-auto">
<MultiSelect {options} {value} on:change={(e) => (value = e.detail.value)} search max={2}>
<div slot="actions" let:selection class="flex items-center">
{#if selection.isMaxSelected()}
<div class="text-sm text-danger">Maximum selection reached</div>
{/if}
</div>
</MultiSelect>
</div>
</Preview>

<h2>option slot with MultiSelectOption custom actions</h2>

<Preview>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,31 @@

import {
Button,
Dialog,
Drawer,
Form,
MultiSelectField,
MultiSelectOption,
TextField,
Toggle,
ToggleButton,
ToggleGroup,
ToggleOption,
type MenuOption,
} from 'svelte-ux';
import Preview from '$lib/components/Preview.svelte';

const options = [
let options: MenuOption[] = [
{ label: 'One', value: 1 },
{ label: 'Two', value: 2 },
{ label: 'Three', value: 3 },
{ label: 'Four', value: 4 },
];

const newOption: () => MenuOption = () => {
return { label: '', value: null };
};

const manyOptions = Array.from({ length: 100 }).map((_, i) => ({
label: `${i + 1}`,
value: i + 1,
Expand Down Expand Up @@ -169,28 +178,6 @@
/>
</Preview>

<h2>actions slot</h2>

<Preview>
<MultiSelectField {options} {value} on:change={(e) => (value = e.detail.value)}>
<div slot="actions">
<Button color="primary" icon={mdiPlus}>Add item</Button>
</div>
</MultiSelectField>
</Preview>

<h2>actions slot with max warning</h2>

<Preview>
<MultiSelectField {options} {value} on:change={(e) => (value = e.detail.value)} max={2}>
<div slot="actions" let:selection class="flex items-center">
{#if selection.isMaxSelected()}
<div class="text-sm text-danger">Maximum selection reached</div>
{/if}
</div>
</MultiSelectField>
</Preview>

<h2>beforeOptions slot</h2>

<Preview>
Expand Down Expand Up @@ -241,6 +228,91 @@
</MultiSelectField>
</Preview>

<h2>actions slot</h2>

<Preview>
<MultiSelectField {options} {value} on:change={(e) => (value = e.detail.value)}>
<div slot="actions" class="p-2" on:click|stopPropagation role="none">
<Toggle let:on={open} let:toggle>
<Button icon={mdiPlus} color="primary" on:click={toggle}>New item</Button>
<Form
initial={newOption()}
on:change={(e) => {
// Convert value to number if it's a valid number, otherwise keep as string
const newOptionData = { ...e.detail };
if (
newOptionData.value !== null &&
newOptionData.value !== '' &&
!isNaN(Number(newOptionData.value))
) {
newOptionData.value = Number(newOptionData.value);
}
options = [newOptionData, ...options];
// Auto-select the newly created option
value = [...(value || []), newOptionData.value];
}}
let:draft
let:current
let:commit
let:revert
>
<Dialog
{open}
on:close={() => {
toggle();
}}
>
<div slot="title">Create new option</div>
<div class="px-6 py-3 w-96 grid gap-2">
<TextField
label="Label"
value={current.label}
on:change={(e) => {
draft.label = e.detail.value;
}}
autofocus
/>
<TextField
label="Value"
value={draft.value}
on:change={(e) => {
draft.value = e.detail.value;
}}
/>
</div>
<div slot="actions">
<Button
on:click={() => {
commit();
toggle();
}}
color="primary">Add option</Button
>
<Button
on:click={() => {
revert();
toggle();
}}>Cancel</Button
>
</div>
</Dialog>
</Form>
</Toggle>
</div>
</MultiSelectField>
</Preview>

<h2>actions slot with max warning</h2>

<Preview>
<MultiSelectField {options} {value} on:change={(e) => (value = e.detail.value)} max={2}>
<div slot="actions" let:selection class="flex items-center">
{#if selection.isMaxSelected()}
<div class="text-sm text-danger">Maximum selection reached</div>
{/if}
</div>
</MultiSelectField>
</Preview>
<h2>within Drawer</h2>

<Preview>
Expand Down
Loading