From 46f85618dbc560f5a7acafb87925e19bcea798e7 Mon Sep 17 00:00:00 2001 From: Michel Heusschen <59014050+michelheusschen@users.noreply.github.com> Date: Thu, 22 Feb 2024 14:12:33 +0100 Subject: [PATCH] feat(web): rework combobox and add clear button (#7317) * feat(web): rework combobox * simplify statement and use transition-all --- .../shared-components/change-date.svelte | 6 +- .../shared-components/combobox.svelte | 121 ++++++++++-------- .../search-bar/search-filter-box.svelte | 15 ++- 3 files changed, 84 insertions(+), 58 deletions(-) diff --git a/web/src/lib/components/shared-components/change-date.svelte b/web/src/lib/components/shared-components/change-date.svelte index a6818ee1fd..23789a123f 100644 --- a/web/src/lib/components/shared-components/change-date.svelte +++ b/web/src/lib/components/shared-components/change-date.svelte @@ -28,7 +28,7 @@ const initialOption = timezones.find((item) => item.value === 'UTC' + initialDate.toFormat('ZZ')); - let selectedOption = { + let selectedOption = initialOption && { label: initialOption?.label || '', value: initialOption?.value || '', }; @@ -36,7 +36,7 @@ let selectedDate = initialDate.toFormat("yyyy-MM-dd'T'HH:mm"); // Keep local time if not it's really confusing - $: date = DateTime.fromISO(selectedDate).setZone(selectedOption.value, { keepLocalTime: true }); + $: date = DateTime.fromISO(selectedDate).setZone(selectedOption?.value, { keepLocalTime: true }); const dispatch = createEventDispatcher<{ cancel: void; @@ -82,7 +82,7 @@
- +
diff --git a/web/src/lib/components/shared-components/combobox.svelte b/web/src/lib/components/shared-components/combobox.svelte index d0ce0e25a9..44994c2955 100644 --- a/web/src/lib/components/shared-components/combobox.svelte +++ b/web/src/lib/components/shared-components/combobox.svelte @@ -1,12 +1,7 @@ @@ -15,34 +10,32 @@ import Icon from '$lib/components/elements/icon.svelte'; import { clickOutside } from '$lib/utils/click-outside'; - import { mdiMagnify, mdiUnfoldMoreHorizontal } from '@mdi/js'; + import { mdiMagnify, mdiUnfoldMoreHorizontal, mdiClose } from '@mdi/js'; import { createEventDispatcher } from 'svelte'; + import IconButton from '../elements/buttons/icon-button.svelte'; - export let type: Type = 'button'; + export let id: string | undefined = undefined; export let options: ComboBoxOption[] = []; - export let selectedOption: ComboBoxOption | undefined = undefined; + export let selectedOption: ComboBoxOption | undefined; export let placeholder = ''; - export const label = ''; - export let noLabel = false; let isOpen = false; - let searchQuery = ''; + let searchQuery = selectedOption?.label || ''; $: filteredOptions = options.filter((option) => option.label.toLowerCase().includes(searchQuery.toLowerCase())); const dispatch = createEventDispatcher<{ - select: ComboBoxOption; + select: ComboBoxOption | undefined; click: void; }>(); - let handleClick = () => { + const handleClick = () => { searchQuery = ''; - isOpen = !isOpen; + isOpen = true; dispatch('click'); }; let handleOutClick = () => { - searchQuery = ''; isOpen = false; }; @@ -51,49 +44,77 @@ dispatch('select', option); isOpen = false; }; + + const onClear = () => { + selectedOption = undefined; + searchQuery = ''; + dispatch('select', selectedOption); + };
- +
{#if isOpen}
-
-
-
- -
-
- - - -
-
- {#each filteredOptions as option (option.label)} - - {/each} -
+ {#if filteredOptions.length === 0} +
No results
+ {/if} + {#each filteredOptions as option (option.label)} + {@const selected = option.label === selectedOption?.label} + + {/each}
{/if} diff --git a/web/src/lib/components/shared-components/search-bar/search-filter-box.svelte b/web/src/lib/components/shared-components/search-bar/search-filter-box.svelte index 71b2c9d3a5..00ceb6a872 100644 --- a/web/src/lib/components/shared-components/search-bar/search-filter-box.svelte +++ b/web/src/lib/components/shared-components/search-bar/search-filter-box.svelte @@ -404,8 +404,9 @@
-

Country

+
-

State

+
-

City

+
-

Make

+
-

Model

+