Skip to content

Commit

Permalink
Replace SelectList with SelectField w/ MenuField API sync.
Browse files Browse the repository at this point in the history
- Updates SelectField to support the same features as SelectList.
- Updates QuickSearch to use SelectField.
- Declared MenuOption & moved to types folder.
- Updated MenuField/SelectField to reference MenuOption.
- Switched to use of 'label' instead of 'name' for options.
- Added `activeOptionIcon` to SelectField.
  • Loading branch information
willnationsdev committed Dec 14, 2023
1 parent e65d5e2 commit 317aacf
Show file tree
Hide file tree
Showing 13 changed files with 337 additions and 590 deletions.
5 changes: 5 additions & 0 deletions .changeset/quick-avocados-hope.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte-ux': minor
---

Removes SelectList. Updates SelectField features to support SelectList's use case via property/attribute overrides. Updates QuickSearch to use SelectField. Defines MenuOption type & updates MenuField & SelectField to use it; this results in renaming of SelectField options' `name` field to become `label`, standardizing the API across the two. Also adds `activeOptionIcon` to SelectField so users can opt-in to dynamically updating the field icon based on the selected option.
2 changes: 1 addition & 1 deletion packages/svelte-ux/src/lib/components/Menu.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
export let menuItemsEl: HTMLMenuElement | undefined = undefined;
function onClick(e) {
function onClick(e: MouseEvent) {
try {
if (e.target === menuItemsEl) {
// Clicked within menu but outside of any items
Expand Down
11 changes: 7 additions & 4 deletions packages/svelte-ux/src/lib/components/MenuField.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,9 @@
import MenuItem from './MenuItem.svelte';
import Button from './Button.svelte';
import { getComponentTheme } from './theme';
import type { MenuOption } from '$lib/types/options';
type Options = Array<{ label: string; value: any; icon?: string; group?: string }>;
export let options: Options;
export let options: MenuOption[] = [];
export let value: any = null;
export let menuProps: ComponentProps<Menu> | undefined = {
autoPlacement: true,
Expand Down Expand Up @@ -59,6 +58,10 @@
const dispatch = createEventDispatcher();
$: dispatch('change', { value });
function setValue(val: any): void {
value = val;
}
</script>

<Field
Expand Down Expand Up @@ -110,7 +113,7 @@
matchWidth
{...menuProps}
>
<slot {options} {selected} close={() => (open = false)} setValue={(val) => (value = val)}>
<slot {options} {selected} close={() => (open = false)} {setValue}>
<menu class="group p-1">
{#each options as option, index (option.value)}
{@const previousOption = options[index - 1]}
Expand Down
17 changes: 11 additions & 6 deletions packages/svelte-ux/src/lib/components/QuickSearch.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
import Button from '$lib/components/Button.svelte';
import Dialog from '$lib/components/Dialog.svelte';
import SelectList from '$lib/components/SelectList.svelte';
import SelectField from '$lib/components/SelectField.svelte';
import { getComponentTheme } from './theme';
import { cls } from '$lib/utils/styles';
import { smScreen } from '$lib/stores';
import { autoFocus, selectOnFocus } from '$lib/actions';
import type { MenuOption } from '$lib/types/options';
export let options: { name: string; value: string; group?: string }[] = [];
export let options: MenuOption[] = [];
export let classes: {
root?: string;
Expand All @@ -18,10 +20,11 @@
let open = false;
let fieldActions = (node: any) => [autoFocus(node), selectOnFocus(node)];
/*
TODO:
- [ ] Sticky search
- [ ] Refine SelectList / reuse with SelectField (and maybe MultiSelect)
- [ ] Improve size of Dialog (move class to Dialog without breaking overflow)
- [ ] Load descriptions/meta from +page.ts
- [ ] Improve dialog positioning on small viewports (consistent top/bottom with max height)
Expand Down Expand Up @@ -63,18 +66,20 @@
}}
>
<div class="overflow-auto max-h-[min(90dvh,600px)] min-w-[400px] py-1">
<SelectList
<SelectField
icon={mdiMagnify}
placeholder="Search..."
hideToggleIcon={true}
inlineOptions={true}
{options}
{fieldActions}
on:change
on:change={(e) => (open = false)}
on:change={() => (open = false)}
classes={{
field: {
container: 'border-none hover:shadow-none group-focus-within:shadow-none',
},
group: 'capitalize',
options: 'border-t mt-1 px-1',
}}
/>
</div>
Expand Down
Loading

0 comments on commit 317aacf

Please sign in to comment.