rename ComboboxItem -> ComboboxOption

This commit is contained in:
Elijah Duffy
2025-07-04 02:32:06 -07:00
parent 12a79f0680
commit cc27f0599c
4 changed files with 31 additions and 25 deletions

View File

@@ -1,16 +1,16 @@
<script lang="ts" module> <script lang="ts" module>
export type ComboboxItem = { export type ComboboxOption = {
value: string; value: string;
label?: string; label?: string;
infotext?: string; infotext?: string;
preview?: string; preview?: string;
disabled?: boolean; disabled?: boolean;
icon?: Snippet<[item: ComboboxItem]>; icon?: Snippet<[item: ComboboxOption]>;
render?: Snippet<[item: ComboboxItem]>; render?: Snippet<[item: ComboboxOption]>;
}; };
const getLabel = (item: ComboboxItem | undefined): string => item?.label ?? item?.value ?? ''; const getLabel = (item: ComboboxOption | undefined): string => item?.label ?? item?.value ?? '';
const getPreview = (item: ComboboxItem | undefined): string => item?.preview ?? getLabel(item); const getPreview = (item: ComboboxOption | undefined): string => item?.preview ?? getLabel(item);
</script> </script>
<script lang="ts"> <script lang="ts">
@@ -29,24 +29,24 @@
interface Props { interface Props {
name?: string; name?: string;
value?: ComboboxItem; value?: ComboboxOption;
open?: boolean; open?: boolean;
usePreview?: boolean | 'auto'; usePreview?: boolean | 'auto';
matchWidth?: boolean; matchWidth?: boolean;
options: ComboboxItem[]; options: ComboboxOption[];
required?: boolean; required?: boolean;
invalidMessage?: string; invalidMessage?: string | null;
label?: string; label?: string;
placeholder?: string; placeholder?: string;
notFoundMessage?: string; notFoundMessage?: string;
class?: ClassValue | null | undefined; class?: ClassValue | null | undefined;
onvalidate?: (e: InputValidatorEvent) => void; onvalidate?: (e: InputValidatorEvent) => void;
onchange?: (item: ComboboxItem) => void; onchange?: (item: ComboboxOption) => void;
} }
let { let {
name, name,
value = $bindable<ComboboxItem | undefined>(undefined), value = $bindable<ComboboxOption | undefined>(undefined),
open = $bindable(false), open = $bindable(false),
usePreview = 'auto', usePreview = 'auto',
matchWidth = false, matchWidth = false,
@@ -78,7 +78,7 @@
: options.filter((item) => getLabel(item).toLowerCase().includes(searchValue.toLowerCase())) : options.filter((item) => getLabel(item).toLowerCase().includes(searchValue.toLowerCase()))
); );
let highlighted = $derived.by((): ComboboxItem | undefined => { let highlighted = $derived.by((): ComboboxOption | undefined => {
if (filteredItems.length === 0) return undefined; if (filteredItems.length === 0) return undefined;
if (value !== undefined && filteredItems.find((v) => v.value === value?.value)) return value; if (value !== undefined && filteredItems.find((v) => v.value === value?.value)) return value;
return filteredItems[0]; return filteredItems[0];
@@ -98,7 +98,10 @@
return filteredItems.findIndex((item) => item.value === highlighted?.value); return filteredItems.findIndex((item) => item.value === highlighted?.value);
}; };
const minWidth: Action<HTMLDivElement, { options: ComboboxItem[] }> = (container, buildOpts) => { const minWidth: Action<HTMLDivElement, { options: ComboboxOption[] }> = (
container,
buildOpts
) => {
const f = (opts: typeof buildOpts) => { const f = (opts: typeof buildOpts) => {
if (matchWidth && searchInput) { if (matchWidth && searchInput) {
container.style.width = searchInput.scrollWidth + 'px'; container.style.width = searchInput.scrollWidth + 'px';
@@ -348,9 +351,11 @@
</div> </div>
<!-- Error message if invalid --> <!-- Error message if invalid -->
<div class={['opacity-0 transition-opacity', !valid && 'opacity-100']}> {#if invalidMessage !== null}
<Label for={id} error>{invalidMessage}</Label> <div class={['opacity-0 transition-opacity', !valid && 'opacity-100']}>
</div> <Label for={id} error>{invalidMessage}</Label>
</div>
{/if}
</div> </div>
{#snippet searchInputBox(caret: boolean = true)} {#snippet searchInputBox(caret: boolean = true)}

View File

@@ -1,7 +1,7 @@
<script lang="ts"> <script lang="ts">
import Label from './Label.svelte'; import Label from './Label.svelte';
import { Country, type ICountry } from 'country-state-city'; import { Country, type ICountry } from 'country-state-city';
import Combobox, { type ComboboxItem } from './Combobox.svelte'; import Combobox, { type ComboboxOption } from './Combobox.svelte';
import StyledRawInput from './StyledRawInput.svelte'; import StyledRawInput from './StyledRawInput.svelte';
import { AsYouType, type PhoneNumber, type CountryCode } from 'libphonenumber-js'; import { AsYouType, type PhoneNumber, type CountryCode } from 'libphonenumber-js';
import { generateIdentifier } from './util'; import { generateIdentifier } from './util';
@@ -32,7 +32,7 @@
const id = $derived(generateIdentifier('phone-input', name)); const id = $derived(generateIdentifier('phone-input', name));
let lastRawValue = $state(''); let lastRawValue = $state('');
let country = $state<ICountry | undefined>(); let country = $state<ICountry | undefined>();
let selectedCountryItem = $state<ComboboxItem | undefined>(); let selectedCountryItem = $state<ComboboxOption | undefined>();
let countriesOpen = $state<boolean>(false); let countriesOpen = $state<boolean>(false);
let countriesValid = $state<boolean>(true); let countriesValid = $state<boolean>(true);
let numberValid = $state<boolean>(true); let numberValid = $state<boolean>(true);
@@ -45,7 +45,7 @@
}, },
{} as Record<string, ICountry> {} as Record<string, ICountry>
); );
const options: ComboboxItem[] = countries.map((country) => ({ const options: ComboboxOption[] = countries.map((country) => ({
value: country.isoCode, value: country.isoCode,
label: `${country.name} (+${country.phonecode})`, label: `${country.name} (+${country.phonecode})`,
preview: `+${country.phonecode}`, preview: `+${country.phonecode}`,
@@ -103,7 +103,7 @@
}); });
</script> </script>
{#snippet renderIcon(item: ComboboxItem)} {#snippet renderIcon(item: ComboboxOption)}
{#if countrycodeMap[item.value]?.flag} {#if countrycodeMap[item.value]?.flag}
{countrycodeMap[item.value].flag} {countrycodeMap[item.value].flag}
{/if} {/if}
@@ -133,6 +133,7 @@
onvalidate={(e) => { onvalidate={(e) => {
countriesValid = e.detail.valid; countriesValid = e.detail.valid;
}} }}
invalidMessage={null}
/> />
</div> </div>

View File

@@ -20,7 +20,7 @@
<script lang="ts"> <script lang="ts">
import type { ClassValue } from 'svelte/elements'; import type { ClassValue } from 'svelte/elements';
import Combobox, { type ComboboxItem } from './Combobox.svelte'; import Combobox, { type ComboboxOption } from './Combobox.svelte';
interface Props { interface Props {
label?: string; label?: string;
@@ -64,7 +64,7 @@
return a.timeZone.localeCompare(b.timeZone); return a.timeZone.localeCompare(b.timeZone);
}); });
const options: ComboboxItem[] = sortedTimeZones.map((timeZone) => { const options: ComboboxOption[] = sortedTimeZones.map((timeZone) => {
const infotext = [...new Set([timeZone.short, timeZone.offset])] const infotext = [...new Set([timeZone.short, timeZone.offset])]
.filter((item) => item !== undefined) .filter((item) => item !== undefined)
.join(' · '); .join(' · ');
@@ -82,10 +82,10 @@
acc[option.value] = option; acc[option.value] = option;
return acc; return acc;
}, },
{} as Record<string, ComboboxItem> {} as Record<string, ComboboxOption>
); );
let timezone = $state<ComboboxItem | undefined>( let timezone = $state<ComboboxOption | undefined>(
optionsMap[Intl.DateTimeFormat().resolvedOptions().timeZone] optionsMap[Intl.DateTimeFormat().resolvedOptions().timeZone]
); );
@@ -106,6 +106,6 @@
class={classValue} class={classValue}
/> />
{#snippet timezoneLabel(item: ComboboxItem)} {#snippet timezoneLabel(item: ComboboxOption)}
{@html wbr(item.label ?? 'Missing label')} {@html wbr(item.label ?? 'Missing label')}
{/snippet} {/snippet}

View File

@@ -2,7 +2,7 @@
export { default as Button } from './Button.svelte'; export { default as Button } from './Button.svelte';
export { default as CenterBox } from './CenterBox.svelte'; export { default as CenterBox } from './CenterBox.svelte';
export { default as Checkbox, type CheckboxState } from './Checkbox.svelte'; export { default as Checkbox, type CheckboxState } from './Checkbox.svelte';
export { default as Combobox, type ComboboxItem } from './Combobox.svelte'; export { default as Combobox, type ComboboxOption } from './Combobox.svelte';
export { default as DateInput } from './DateInput.svelte'; export { default as DateInput } from './DateInput.svelte';
export { default as Dialog } from './Dialog.svelte'; export { default as Dialog } from './Dialog.svelte';
export { default as FramelessButton } from './FramelessButton.svelte'; export { default as FramelessButton } from './FramelessButton.svelte';