combobox: improve props, fix placement with scroll
This commit is contained in:
@@ -28,29 +28,15 @@
|
||||
import { Portal } from '@jsrob/svelte-portal';
|
||||
import { browser } from '$app/environment';
|
||||
import { scale } from 'svelte/transition';
|
||||
import { generateIdentifier } from './util.js';
|
||||
|
||||
let {
|
||||
name,
|
||||
value = $bindable<ComboboxItem | undefined>(undefined),
|
||||
open = $bindable(false),
|
||||
usePreview = 'auto',
|
||||
matchWidth = false,
|
||||
items,
|
||||
required = false,
|
||||
invalidMessage,
|
||||
label,
|
||||
placeholder,
|
||||
notFoundMessage = 'No results found',
|
||||
onvalidate,
|
||||
onchange
|
||||
}: {
|
||||
name: string;
|
||||
interface Props {
|
||||
name?: string;
|
||||
value?: ComboboxItem;
|
||||
highlighted?: ComboboxItem;
|
||||
open?: boolean;
|
||||
usePreview?: boolean | 'auto';
|
||||
matchWidth?: boolean;
|
||||
items: ComboboxItem[];
|
||||
options: ComboboxItem[];
|
||||
required?: boolean;
|
||||
invalidMessage?: string;
|
||||
label?: string;
|
||||
@@ -58,7 +44,25 @@
|
||||
notFoundMessage?: string;
|
||||
onvalidate?: (e: InputValidatorEvent) => void;
|
||||
onchange?: (e: ComboboxChangeEvent) => void;
|
||||
} = $props();
|
||||
}
|
||||
|
||||
let {
|
||||
name,
|
||||
value = $bindable<ComboboxItem | undefined>(undefined),
|
||||
open = $bindable(false),
|
||||
usePreview = 'auto',
|
||||
matchWidth = false,
|
||||
options,
|
||||
required = false,
|
||||
invalidMessage,
|
||||
label,
|
||||
placeholder,
|
||||
notFoundMessage = 'No results found',
|
||||
onvalidate,
|
||||
onchange
|
||||
}: Props = $props();
|
||||
|
||||
let id = $derived(generateIdentifier('combobox', name));
|
||||
|
||||
let valid = $state(true);
|
||||
let searchValue = $state('');
|
||||
@@ -71,8 +75,8 @@
|
||||
|
||||
const filteredItems = $derived(
|
||||
searchValue === ''
|
||||
? items
|
||||
: items.filter((item) => getLabel(item).toLowerCase().includes(searchValue.toLowerCase()))
|
||||
? options
|
||||
: options.filter((item) => getLabel(item).toLowerCase().includes(searchValue.toLowerCase()))
|
||||
);
|
||||
|
||||
let highlighted = $derived.by((): ComboboxItem | undefined => {
|
||||
@@ -95,15 +99,15 @@
|
||||
return filteredItems.findIndex((item) => item.value === highlighted?.value);
|
||||
};
|
||||
|
||||
const minWidth: Action<HTMLDivElement, { items: ComboboxItem[] }> = (container, buildOpts) => {
|
||||
const minWidth: Action<HTMLDivElement, { options: ComboboxItem[] }> = (container, buildOpts) => {
|
||||
const f = (opts: typeof buildOpts) => {
|
||||
if (matchWidth && searchInput) {
|
||||
container.style.width = searchInput.scrollWidth + 'px';
|
||||
return;
|
||||
}
|
||||
|
||||
const items = opts.items;
|
||||
const avg = items.reduce((acc, item) => acc + getLabel(item).length, 0) / items.length;
|
||||
const options = opts.options;
|
||||
const avg = options.reduce((acc, item) => acc + getLabel(item).length, 0) / options.length;
|
||||
container.style.width = `${avg * 2.5}ch`;
|
||||
console.log(`minWidth: ${avg * 2.5}ch`);
|
||||
};
|
||||
@@ -139,13 +143,15 @@
|
||||
const outerMargin = 24;
|
||||
|
||||
if (availableSpaceBelow < availableSpaceAbove) {
|
||||
overlay.style.bottom = `${window.innerHeight - targetRect.top}px`;
|
||||
// overlay should be above the target
|
||||
overlay.style.bottom = `${window.innerHeight - targetRect.top - window.scrollY}px`;
|
||||
overlay.style.top = 'auto';
|
||||
overlay.style.maxHeight = `${availableSpaceAbove - outerMargin}px`;
|
||||
pickerPosition = 'top';
|
||||
overlay.dataset.side = 'top';
|
||||
} else {
|
||||
overlay.style.top = `${targetRect.bottom}px`;
|
||||
// overlay should be below the target
|
||||
overlay.style.top = `${targetRect.bottom + window.scrollY}px`;
|
||||
overlay.style.bottom = 'auto';
|
||||
overlay.style.maxHeight = `${availableSpaceBelow - outerMargin}px`;
|
||||
pickerPosition = 'bottom';
|
||||
@@ -197,7 +203,7 @@
|
||||
}
|
||||
|
||||
// when the picker is opened, update its position
|
||||
// when the picker is closed, clear any search value (next time it opens, all items will be shown)
|
||||
// when the picker is closed, clear any search value (next time it opens, all options will be shown)
|
||||
// when the picker is closed, reset highlighted to the currently selected value (if any)
|
||||
$effect(() => {
|
||||
if (open) {
|
||||
@@ -253,7 +259,7 @@
|
||||
open && pickerPosition === 'top' && 'mb-[var(--outer-gap)]',
|
||||
open && pickerPosition === 'bottom' && 'mt-[var(--outer-gap)]'
|
||||
]}
|
||||
use:minWidth={{ items: items }}
|
||||
use:minWidth={{ options: options }}
|
||||
bind:this={pickerContainer}
|
||||
transition:scale={{ duration: 200 }}
|
||||
role="listbox"
|
||||
@@ -272,7 +278,7 @@
|
||||
aria-label={getLabel(item)}
|
||||
aria-disabled={item.disabled}
|
||||
class={[
|
||||
'picker-item mb-0.5 flex min-h-10 flex-wrap items-center py-2.5 pr-1.5 pl-5',
|
||||
'picker-item options-center mb-0.5 flex min-h-10 flex-wrap py-2.5 pr-1.5 pl-5',
|
||||
'rounded-sm text-sm capitalize outline-hidden select-none',
|
||||
'hover:bg-accent-500/30 dark:hover:bg-accent-700/30',
|
||||
item.value === highlighted?.value && 'bg-accent-500/80 dark:bg-accent-700/80'
|
||||
@@ -321,12 +327,13 @@
|
||||
<div>
|
||||
<!-- Combobox Label -->
|
||||
{#if label}
|
||||
<Label for={name}>{label}</Label>
|
||||
<Label for={id}>{label}</Label>
|
||||
{/if}
|
||||
|
||||
<!-- Hidden input stores currently selected value -->
|
||||
<input
|
||||
{name}
|
||||
{id}
|
||||
value={value?.value ?? ''}
|
||||
class="hidden"
|
||||
use:validate={validateOpts}
|
||||
@@ -344,7 +351,7 @@
|
||||
<!-- Error message if invalid -->
|
||||
{#if invalidMessage}
|
||||
<div class={['opacity-0 transition-opacity', !valid && 'opacity-100']}>
|
||||
<Label for={name} error>{invalidMessage}</Label>
|
||||
<Label for={id} error>{invalidMessage}</Label>
|
||||
</div>
|
||||
{/if}
|
||||
</div>
|
||||
|
||||
Reference in New Issue
Block a user