anki/ts/components/SelectOption.svelte
Matthias Metelka e059aab184
Improve Select component and add it back to Change Notetype screen (#2239)
* Do not include oldIdx in Select change event

I included it due to confusion about the variable names in the Change Notetype components.

* Remove redundant on:change listener from NotetypeSelector

* Use Select component in Change Notetype MapperRow (again)

* Remove redundant --cols and --col-size definitions

Bootstrap divides rows into columns of equal width by default.

* Add highlight to active DropdownItem

* Remove bootstrap dropdown item styling

* Fix JS error on dropdown accept action

cause: When closing the dropdown, buttonRef was removed before the callback in setTimeout was run.
2022-12-07 15:31:37 +10:00

58 lines
1.6 KiB
Svelte

<!--
Copyright: Ankitects Pty Ltd and contributors
License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
-->
<script lang="ts">
import { getContext } from "svelte";
import type { Writable } from "svelte/store";
import { selectKey } from "./context-keys";
import DropdownItem from "./DropdownItem.svelte";
export let disabled = false;
export let value: number;
let element: HTMLButtonElement;
function handleKey(e: KeyboardEvent) {
/* Arrow key navigation */
switch (e.code) {
case "ArrowUp": {
const prevSibling = element?.previousElementSibling as HTMLElement;
if (prevSibling) {
prevSibling.focus();
} else {
// close popover
document.body.click();
}
break;
}
case "ArrowDown": {
const nextSibling = element?.nextElementSibling as HTMLElement;
if (nextSibling) {
nextSibling.focus();
} else {
// close popover
document.body.click();
}
break;
}
}
}
const selectContext: Writable<{ value: number; setValue: Function }> =
getContext(selectKey);
const setValue = $selectContext.setValue;
</script>
<DropdownItem
{disabled}
active={value == $selectContext.value}
on:click={() => setValue(value)}
on:keydown={handleKey}
bind:buttonRef={element}
tabbable
>
<slot />
</DropdownItem>