fix svelecte event handling for svelte 5

This commit is contained in:
Justin Edmund 2025-12-13 21:24:31 -08:00
parent 0c973785d1
commit 70df592890
2 changed files with 11 additions and 13 deletions

View file

@ -75,8 +75,9 @@
} }
} }
function handleInput(event: CustomEvent) { function handleInput(event: Event) {
const query = (event.detail as { inputValue?: string })?.inputValue ?? '' const target = event.target as HTMLInputElement | null
const query = target?.value ?? ''
// Debounce the search // Debounce the search
if (searchTimeout) { if (searchTimeout) {
@ -88,9 +89,7 @@
}, 300) }, 300)
} }
function handleChange(event: CustomEvent) { function handleChange(selected: CharacterOption | null) {
// Svelecte emits the selected option directly in event.detail
const selected = event.detail as CharacterOption | null
const newValue = selected?.id || null const newValue = selected?.id || null
value = newValue value = newValue
onValueChange?.(newValue) onValueChange?.(newValue)
@ -106,7 +105,8 @@
}) })
</script> </script>
<div class={typeaheadClasses}> <!-- svelte-ignore a11y_no_static_element_interactions -->
<div class={typeaheadClasses} oninput={handleInput}>
<Svelecte <Svelecte
{options} {options}
value={displayValue} value={displayValue}
@ -116,8 +116,7 @@
{placeholder} {placeholder}
{disabled} {disabled}
{clearable} {clearable}
on:input={handleInput} onChange={handleChange}
on:change={handleChange}
keepSelectionInList={false} keepSelectionInList={false}
/> />
{#if isLoading} {#if isLoading}

View file

@ -1,7 +1,7 @@
<!-- Typeahead Component (Svelecte wrapper) --> <!-- Typeahead Component (Svelecte wrapper) -->
<svelte:options runes={true} /> <svelte:options runes={true} />
<script lang="ts" generics="T"> <script lang="ts" generics="T extends object">
import Svelecte from 'svelecte'; import Svelecte from 'svelecte';
import { Label } from 'bits-ui'; import { Label } from 'bits-ui';
@ -83,8 +83,7 @@
.join(' ') .join(' ')
); );
function handleChange(event: CustomEvent<{ detail: T | T[] | null }>) { function handleChange(newValue: T | T[] | null) {
const newValue = event.detail;
value = newValue; value = newValue;
onValueChange?.(newValue); onValueChange?.(newValue);
} }
@ -114,7 +113,7 @@
{placeholder} {placeholder}
{disabled} {disabled}
{clearable} {clearable}
on:change={handleChange} onChange={handleChange}
/> />
</div> </div>
@ -136,7 +135,7 @@
{placeholder} {placeholder}
{disabled} {disabled}
{clearable} {clearable}
on:change={handleChange} onChange={handleChange}
/> />
</div> </div>
{/if} {/if}