toggle group: improve ref binding
This commit is contained in:
@@ -26,12 +26,7 @@
|
||||
}: Props = $props();
|
||||
|
||||
const id = $derived(generateIdentifier('toggle-group', name));
|
||||
|
||||
const extended: { detail: Option; ref?: ToggleSelect }[] = $derived.by(() => {
|
||||
return options.map((opt) => ({
|
||||
detail: opt
|
||||
}));
|
||||
});
|
||||
const refs: ToggleSelect[] = $state([]);
|
||||
|
||||
let inputElement: HTMLInputElement | undefined = $state<HTMLInputElement>();
|
||||
let valid: boolean = $state(true);
|
||||
@@ -68,18 +63,18 @@
|
||||
/>
|
||||
{/if}
|
||||
|
||||
{#each extended as opt, i (getValue(opt.detail))}
|
||||
{#each options as opt, i (getValue(opt))}
|
||||
<ToggleSelect
|
||||
bind:this={extended[i].ref}
|
||||
ontoggle={makeSelectedHandler(getValue(opt.detail))}
|
||||
bind:this={refs[i]}
|
||||
ontoggle={makeSelectedHandler(getValue(opt))}
|
||||
onkeydown={(e) => {
|
||||
if (e.key === 'ArrowRight' || e.key === 'ArrowDown') {
|
||||
if (i < extended.length - 1) {
|
||||
extended[i + 1].ref?.focus();
|
||||
if (i < options.length - 1) {
|
||||
refs[i + 1]?.focus();
|
||||
}
|
||||
} else if (e.key === 'ArrowLeft' || e.key === 'ArrowUp') {
|
||||
if (i > 0) {
|
||||
extended[i - 1].ref?.focus();
|
||||
refs[i - 1]?.focus();
|
||||
}
|
||||
} else {
|
||||
return;
|
||||
@@ -87,7 +82,7 @@
|
||||
e.preventDefault();
|
||||
}}
|
||||
>
|
||||
<span class={[typeof opt === 'string' && 'capitalize']}>{getLabel(opt.detail)}</span>
|
||||
<span class={[typeof opt === 'string' && 'capitalize']}>{getLabel(opt)}</span>
|
||||
</ToggleSelect>
|
||||
{/each}
|
||||
</div>
|
||||
|
||||
Reference in New Issue
Block a user