Skip to content
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

MultiSelectOption variant support #445

Merged
merged 3 commits into from
Jul 31, 2024
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/fifty-pears-wash.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte-ux': minor
---

[MultiSelectMenu] Add optionProps to easily set `variant` on MultiSelectOption
5 changes: 5 additions & 0 deletions .changeset/serious-starfishes-pump.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte-ux': minor
---

[MultiSelectField] Add optionProps to easily set `variant` on MultiSelectOption
5 changes: 5 additions & 0 deletions .changeset/tricky-jars-live.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte-ux': minor
---

[MultiSelectOption] Add variant support (checkbox (default), checkmark, and fill)
17 changes: 15 additions & 2 deletions packages/svelte-ux/src/lib/components/MultiSelect.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
export let inlineSearch = false;
export let autoFocusSearch = false;
export let placeholder = 'Search items';
export let optionProps: Partial<ComponentProps<MultiSelectOption>> | undefined = undefined;

/** Wrap options in `InfiniteScroll` to amortize rendering of a large number of options */
export let infiniteScroll = false;
Expand Down Expand Up @@ -202,7 +203,13 @@
{disabled}
{onChange}
>
<MultiSelectOption {checked} {indeterminate} {disabled} on:change={onChange}>
<MultiSelectOption
{checked}
{indeterminate}
{disabled}
{...optionProps}
on:change={onChange}
>
{label}
</MultiSelectOption>
</slot>
Expand Down Expand Up @@ -254,7 +261,13 @@
{disabled}
{onChange}
>
<MultiSelectOption {checked} {indeterminate} {disabled} on:change={onChange}>
<MultiSelectOption
{checked}
{indeterminate}
{disabled}
{...optionProps}
on:change={onChange}
>
{label}
</MultiSelectOption>
</slot>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
export let max: number | undefined = undefined;
export let placement: Placement = 'bottom-start';
export let infiniteScroll = false;
export let optionProps: Partial<ComponentProps<MultiSelectOption>> | undefined = undefined;

// TextField props
export let label = '';
Expand Down Expand Up @@ -235,7 +236,13 @@
let:onChange
>
<slot name="option" {option} {label} {value} {checked} {indeterminate} {disabled} {onChange}>
<MultiSelectOption {checked} {indeterminate} {disabled} on:change={onChange}>
<MultiSelectOption
{checked}
{indeterminate}
{disabled}
{...optionProps}
on:change={onChange}
>
{label}
</MultiSelectOption>
</slot>
Expand Down
9 changes: 8 additions & 1 deletion packages/svelte-ux/src/lib/components/MultiSelectMenu.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
export let searchText = '';
/** Maximum number of options that can be selected */
export let max: number | undefined = undefined;
export let optionProps: Partial<ComponentProps<MultiSelectOption>> | undefined = undefined;

export let classes: {
root?: string;
Expand Down Expand Up @@ -93,7 +94,13 @@
let:onChange
>
<slot name="option" {option} {label} {value} {checked} {indeterminate} {disabled} {onChange}>
<MultiSelectOption {checked} {indeterminate} {disabled} on:change={onChange}>
<MultiSelectOption
{checked}
{indeterminate}
{disabled}
{...optionProps}
on:change={onChange}
>
{label}
</MultiSelectOption>
</slot>
Expand Down
72 changes: 54 additions & 18 deletions packages/svelte-ux/src/lib/components/MultiSelectOption.svelte
Original file line number Diff line number Diff line change
@@ -1,20 +1,25 @@
<script lang="ts">
import type { ComponentProps } from 'svelte';
import { createEventDispatcher, type ComponentProps } from 'svelte';

import Checkbox from './Checkbox.svelte';
import { cls } from '../utils/styles.js';
import { getComponentClasses } from './theme.js';
import Button from './Button.svelte';
import { mdiCheck } from '@mdi/js';

export let checked: boolean;
export let indeterminate = false;
export let disabled = false;
export let variant: 'checkbox' | 'checkmark' | 'fill' = 'checkbox';

export let classes: {
root?: string;
checkbox?: ComponentProps<Checkbox>['classes'];
container?: string;
} = {};
const settingsClasses = getComponentClasses('MultiSelectOption');

const dispatch = createEventDispatcher<{ change: null }>();
</script>

<div
Expand All @@ -28,28 +33,59 @@
role="option"
aria-selected={checked}
>
<Checkbox
bind:checked
bind:indeterminate
on:change
{disabled}
classes={{
root: 'px-2 rounded hover:bg-surface-content/5',
label: 'py-2',
...settingsClasses.checkbox,
...classes.checkbox,
}}
>
<div
{#if variant === 'checkbox'}
<Checkbox
bind:checked
bind:indeterminate
on:change={() => dispatch('change')}
{disabled}
classes={{
root: 'px-2 rounded hover:bg-surface-content/5',
label: 'py-2',
...settingsClasses.checkbox,
...classes.checkbox,
}}
>
<div
class={cls(
'ml-1 inline-block text-sm text-surface-content',
!disabled && 'cursor-pointer',
settingsClasses.container,
classes.container
)}
>
<slot />
</div>
</Checkbox>
{:else if variant === 'checkmark'}
<Button
icon={checked ? mdiCheck : 'M0'}
{disabled}
class={cls(
'px-2 text-sm font-normal text-surface-content hover:bg-surface-content/5',
'justify-start',
settingsClasses.container,
classes.container
)}
on:click={() => dispatch('change')}
>
<slot />
</Button>
{:else if variant === 'fill'}
<Button
{disabled}
class={cls(
'ml-1 inline-block text-sm text-surface-content',
!disabled && 'cursor-pointer',
'px-2 my-px text-sm font-normal',
'justify-start',
settingsClasses.container,
classes.container
)}
variant={checked ? 'fill-light' : 'default'}
color={checked ? 'primary' : 'default'}
on:click={() => dispatch('change')}
>
<slot />
</div>
</Checkbox>
</Button>
{/if}
<slot name="actions" />
</div>
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,30 @@
<MultiSelect {options} {value} on:change={(e) => (value = e.detail.value)} />
</Preview>

<h2>options variant: checkmark</h2>

<Preview>
{value.length} selected
<MultiSelect
{options}
{value}
optionProps={{ variant: 'checkmark' }}
on:change={(e) => (value = e.detail.value)}
/>
</Preview>

<h2>options variant: fill</h2>

<Preview>
{value.length} selected
<MultiSelect
{options}
{value}
optionProps={{ variant: 'fill' }}
on:change={(e) => (value = e.detail.value)}
/>
</Preview>

<h2>inlineSearch</h2>

<Preview>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,34 @@
<MultiSelectField label="Numbers" {options} {value} on:change={(e) => (value = e.detail.value)} />
</Preview>

<h2>options variants</h2>

<Preview>
<div class="grid gap-3">
<MultiSelectField
label="checkbox (default)"
{options}
{value}
optionProps={{ variant: 'checkbox' }}
on:change={(e) => (value = e.detail.value)}
/>
<MultiSelectField
label="checkmark"
{options}
{value}
optionProps={{ variant: 'checkmark' }}
on:change={(e) => (value = e.detail.value)}
/>
<MultiSelectField
label="fill"
{options}
{value}
optionProps={{ variant: 'fill' }}
on:change={(e) => (value = e.detail.value)}
/>
</div>
</Preview>

<h2>disabled</h2>

<Preview>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,57 @@
</span>
</Preview>

<h2>options variants</h2>

<Preview>
<div class="inline-grid gap-3">
<ToggleButton let:on={open} let:toggleOff transition={false}>
{value.length} selected
<MultiSelectMenu
{options}
{value}
optionProps={{ variant: 'checkbox' }}
on:change={(e) => {
// @ts-expect-error
value = e.detail.value;
}}
{open}
on:close={toggleOff}
/>
</ToggleButton>

<ToggleButton let:on={open} let:toggleOff transition={false}>
{value.length} selected
<MultiSelectMenu
{options}
{value}
optionProps={{ variant: 'checkmark' }}
on:change={(e) => {
// @ts-expect-error
value = e.detail.value;
}}
{open}
on:close={toggleOff}
/>
</ToggleButton>

<ToggleButton let:on={open} let:toggleOff transition={false}>
{value.length} selected
<MultiSelectMenu
{options}
{value}
optionProps={{ variant: 'fill' }}
on:change={(e) => {
// @ts-expect-error
value = e.detail.value;
}}
{open}
on:close={toggleOff}
/>
</ToggleButton>
</div>
</Preview>

<h2>inlineSearch</h2>

<Preview>
Expand Down