Combobox: allow null value
This commit is contained in:
@@ -29,10 +29,11 @@
|
||||
};
|
||||
|
||||
/** returns option label, falling back to value or 'Undefined Option' if no option provided */
|
||||
const getLabel = (opt: ComboboxOption | undefined): string =>
|
||||
const getLabel = (opt: ComboboxOption | undefined | null): string =>
|
||||
opt ? (opt.label ?? opt.value) : 'Undefined Option';
|
||||
/** returns option preview, falling back to getLabel if missing */
|
||||
const getPreview = (opt: ComboboxOption | undefined): string => opt?.preview ?? getLabel(opt);
|
||||
const getPreview = (opt: ComboboxOption | undefined | null): string =>
|
||||
opt?.preview ?? getLabel(opt);
|
||||
</script>
|
||||
|
||||
<script lang="ts">
|
||||
@@ -78,7 +79,7 @@
|
||||
stateless?: boolean;
|
||||
|
||||
/** Bindable value of the combobox, the currently selected option */
|
||||
value?: ComboboxOption;
|
||||
value?: ComboboxOption | null;
|
||||
/** Array of ComboboxOptions for the picker */
|
||||
options: ComboboxOption[];
|
||||
/**
|
||||
@@ -271,7 +272,7 @@
|
||||
});
|
||||
|
||||
/** currently highlighted option, updated by keyboard navigation or defaults to first item */
|
||||
let highlighted = $derived.by((): ComboboxOption | undefined => {
|
||||
let highlighted = $derived.by((): ComboboxOption | undefined | null => {
|
||||
if (!searching) return undefined; // otherwise, the first item is highlighted on first open
|
||||
if (filteredItems.length === 0) return undefined;
|
||||
if (value !== undefined && filteredItems.find((v) => v.value === value?.value)) return value;
|
||||
|
||||
Reference in New Issue
Block a user