1
0
mirror of https://github.com/immich-app/immich.git synced 2024-12-26 10:50:29 +02:00

feat(web): rework combobox and add clear button (#7317)

* feat(web): rework combobox

* simplify statement and use transition-all
This commit is contained in:
Michel Heusschen 2024-02-22 14:12:33 +01:00 committed by GitHub
parent 749b182f97
commit 46f85618db
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 84 additions and 58 deletions

View File

@ -28,7 +28,7 @@
const initialOption = timezones.find((item) => item.value === 'UTC' + initialDate.toFormat('ZZ')); const initialOption = timezones.find((item) => item.value === 'UTC' + initialDate.toFormat('ZZ'));
let selectedOption = { let selectedOption = initialOption && {
label: initialOption?.label || '', label: initialOption?.label || '',
value: initialOption?.value || '', value: initialOption?.value || '',
}; };
@ -36,7 +36,7 @@
let selectedDate = initialDate.toFormat("yyyy-MM-dd'T'HH:mm"); let selectedDate = initialDate.toFormat("yyyy-MM-dd'T'HH:mm");
// Keep local time if not it's really confusing // 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<{ const dispatch = createEventDispatcher<{
cancel: void; cancel: void;
@ -82,7 +82,7 @@
</div> </div>
<div class="flex flex-col w-full mt-2"> <div class="flex flex-col w-full mt-2">
<label for="timezone">Timezone</label> <label for="timezone">Timezone</label>
<Combobox bind:selectedOption options={timezones} placeholder="Search timezone..." /> <Combobox bind:selectedOption id="timezone" options={timezones} placeholder="Search timezone..." />
</div> </div>
</div> </div>
</ConfirmDialogue> </ConfirmDialogue>

View File

@ -1,12 +1,7 @@
<script lang="ts" context="module"> <script lang="ts" context="module">
// Necessary for eslint
/* eslint-disable @typescript-eslint/no-explicit-any */
type T = any;
export type Type = 'button' | 'submit' | 'reset';
export type ComboBoxOption = { export type ComboBoxOption = {
label: string; label: string;
value: T; value: string;
}; };
</script> </script>
@ -15,34 +10,32 @@
import Icon from '$lib/components/elements/icon.svelte'; import Icon from '$lib/components/elements/icon.svelte';
import { clickOutside } from '$lib/utils/click-outside'; 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 { 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 options: ComboBoxOption[] = [];
export let selectedOption: ComboBoxOption | undefined = undefined; export let selectedOption: ComboBoxOption | undefined;
export let placeholder = ''; export let placeholder = '';
export const label = '';
export let noLabel = false;
let isOpen = false; let isOpen = false;
let searchQuery = ''; let searchQuery = selectedOption?.label || '';
$: filteredOptions = options.filter((option) => option.label.toLowerCase().includes(searchQuery.toLowerCase())); $: filteredOptions = options.filter((option) => option.label.toLowerCase().includes(searchQuery.toLowerCase()));
const dispatch = createEventDispatcher<{ const dispatch = createEventDispatcher<{
select: ComboBoxOption; select: ComboBoxOption | undefined;
click: void; click: void;
}>(); }>();
let handleClick = () => { const handleClick = () => {
searchQuery = ''; searchQuery = '';
isOpen = !isOpen; isOpen = true;
dispatch('click'); dispatch('click');
}; };
let handleOutClick = () => { let handleOutClick = () => {
searchQuery = '';
isOpen = false; isOpen = false;
}; };
@ -51,49 +44,77 @@
dispatch('select', option); dispatch('select', option);
isOpen = false; isOpen = false;
}; };
const onClear = () => {
selectedOption = undefined;
searchQuery = '';
dispatch('select', selectedOption);
};
</script> </script>
<div class="relative" use:clickOutside on:outclick={handleOutClick}> <div class="relative" use:clickOutside on:outclick={handleOutClick}>
<button {type} class="immich-form-input text-sm text-left w-full min-h-[48px] transition-all" on:click={handleClick} <div>
>{#if !noLabel} {#if isOpen}
{selectedOption?.label || ''} <div class="absolute inset-y-0 left-0 flex items-center pl-3">
<div class="dark:text-immich-dark-fg/75">
<Icon path={mdiMagnify} />
</div>
</div>
{/if} {/if}
<div class="absolute right-0 top-0 h-full flex px-4 justify-center items-center content-between">
<Icon path={mdiUnfoldMoreHorizontal} /> <input
{id}
{placeholder}
role="combobox"
aria-expanded={isOpen}
aria-controls={id}
class="immich-form-input text-sm text-left w-full !pr-12 transition-all"
class:!pl-8={isOpen}
class:!rounded-b-none={isOpen}
class:cursor-pointer={!isOpen}
value={isOpen ? '' : selectedOption?.label || ''}
on:input={(e) => (searchQuery = e.currentTarget.value)}
on:focus={handleClick}
/>
<div
class="absolute right-0 top-0 h-full flex px-4 justify-center items-center content-between"
class:pr-2={selectedOption}
class:pointer-events-none={!selectedOption}
>
{#if selectedOption}
<IconButton color="transparent-gray" on:click={onClear} title="Clear value">
<Icon path={mdiClose} />
</IconButton>
{:else if !isOpen}
<Icon path={mdiUnfoldMoreHorizontal} />
{/if}
</div> </div>
</button> </div>
{#if isOpen} {#if isOpen}
<div <div
transition:fly={{ y: -25, duration: 250 }} role="listbox"
class="absolute w-full top-full mt-2 bg-white dark:bg-gray-800 rounded-lg border border-gray-300 dark:border-gray-900 z-10" transition:fly={{ duration: 250 }}
class="absolute text-left w-full max-h-64 overflow-y-auto bg-white dark:bg-gray-800 rounded-b-lg border border-t-0 border-gray-300 dark:border-gray-900 z-10"
> >
<div class="relative border-b flex"> {#if filteredOptions.length === 0}
<div class="absolute inset-y-0 left-0 flex items-center pl-3"> <div class="px-4 py-2 font-medium">No results</div>
<div class="dark:text-immich-dark-fg/75"> {/if}
<button {type} class="flex items-center"> {#each filteredOptions as option (option.label)}
<Icon path={mdiMagnify} /> {@const selected = option.label === selectedOption?.label}
</button> <button
</div> type="button"
</div> role="option"
aria-selected={selected}
<!-- svelte-ignore a11y-autofocus --> class="text-left w-full px-4 py-2 hover:bg-gray-100 dark:hover:bg-gray-700 transition-all"
<input bind:value={searchQuery} autofocus {placeholder} class="ml-9 grow bg-transparent py-2" /> class:bg-gray-300={selected}
</div> class:dark:bg-gray-600={selected}
<div class="h-64 overflow-y-auto"> on:click={() => handleSelect(option)}
{#each filteredOptions as option (option.label)} >
<button {option.label}
{type} </button>
class="block text-left w-full px-4 py-2 cursor-pointer hover:bg-gray-100 dark:hover:bg-gray-700 transition-all {/each}
${option.label === selectedOption?.label ? 'bg-gray-300 dark:bg-gray-600' : ''}
"
class:bg-gray-300={option.label === selectedOption?.label}
on:click={() => handleSelect(option)}
>
{option.label}
</button>
{/each}
</div>
</div> </div>
{/if} {/if}
</div> </div>

View File

@ -404,8 +404,9 @@
<div class="flex justify-between gap-5 mt-3"> <div class="flex justify-between gap-5 mt-3">
<div class="w-full"> <div class="w-full">
<p class="text-sm text-black dark:text-white">Country</p> <label class="text-sm text-black dark:text-white" for="search-place-country">Country</label>
<Combobox <Combobox
id="search-place-country"
options={suggestions.country} options={suggestions.country}
bind:selectedOption={filter.location.country} bind:selectedOption={filter.location.country}
placeholder="Search country..." placeholder="Search country..."
@ -414,8 +415,9 @@
</div> </div>
<div class="w-full"> <div class="w-full">
<p class="text-sm text-black dark:text-white">State</p> <label class="text-sm text-black dark:text-white" for="search-place-state">State</label>
<Combobox <Combobox
id="search-place-state"
options={suggestions.state} options={suggestions.state}
bind:selectedOption={filter.location.state} bind:selectedOption={filter.location.state}
placeholder="Search state..." placeholder="Search state..."
@ -424,8 +426,9 @@
</div> </div>
<div class="w-full"> <div class="w-full">
<p class="text-sm text-black dark:text-white">City</p> <label class="text-sm text-black dark:text-white" for="search-place-city">City</label>
<Combobox <Combobox
id="search-place-city"
options={suggestions.city} options={suggestions.city}
bind:selectedOption={filter.location.city} bind:selectedOption={filter.location.city}
placeholder="Search city..." placeholder="Search city..."
@ -446,8 +449,9 @@
<div class="flex justify-between gap-5 mt-3"> <div class="flex justify-between gap-5 mt-3">
<div class="w-full"> <div class="w-full">
<p class="text-sm text-black dark:text-white">Make</p> <label class="text-sm text-black dark:text-white" for="search-camera-make">Make</label>
<Combobox <Combobox
id="search-camera-make"
options={suggestions.make} options={suggestions.make}
bind:selectedOption={filter.camera.make} bind:selectedOption={filter.camera.make}
placeholder="Search camera make..." placeholder="Search camera make..."
@ -457,8 +461,9 @@
</div> </div>
<div class="w-full"> <div class="w-full">
<p class="text-sm text-black dark:text-white">Model</p> <label class="text-sm text-black dark:text-white" for="search-camera-model">Model</label>
<Combobox <Combobox
id="search-camera-model"
options={suggestions.model} options={suggestions.model}
bind:selectedOption={filter.camera.model} bind:selectedOption={filter.camera.model}
placeholder="Search camera model..." placeholder="Search camera model..."