2021-04-15 15:59:52 +02:00
|
|
|
<!--
|
|
|
|
Copyright: Ankitects Pty Ltd and contributors
|
|
|
|
License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
|
|
|
|
-->
|
2021-10-26 00:43:02 +02:00
|
|
|
<script lang="ts">
|
2022-12-01 10:24:26 +01:00
|
|
|
import { getContext } from "svelte";
|
2022-12-07 06:31:37 +01:00
|
|
|
import type { Writable } from "svelte/store";
|
2021-03-31 15:40:11 +02:00
|
|
|
|
2022-12-01 10:24:26 +01:00
|
|
|
import { selectKey } from "./context-keys";
|
2022-09-27 04:16:45 +02:00
|
|
|
import DropdownItem from "./DropdownItem.svelte";
|
|
|
|
|
|
|
|
export let disabled = false;
|
2022-12-01 10:24:26 +01:00
|
|
|
export let value: number;
|
2022-09-27 04:16:45 +02:00
|
|
|
|
|
|
|
let element: HTMLButtonElement;
|
2021-10-31 00:29:22 +02:00
|
|
|
|
2022-12-01 10:24:26 +01:00
|
|
|
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;
|
|
|
|
}
|
2022-09-27 04:16:45 +02:00
|
|
|
}
|
2021-10-31 00:29:22 +02:00
|
|
|
}
|
2022-12-01 10:24:26 +01:00
|
|
|
|
2022-12-07 06:31:37 +01:00
|
|
|
const selectContext: Writable<{ value: number; setValue: Function }> =
|
|
|
|
getContext(selectKey);
|
|
|
|
const setValue = $selectContext.setValue;
|
2022-09-27 04:16:45 +02:00
|
|
|
</script>
|
|
|
|
|
2022-12-01 10:24:26 +01:00
|
|
|
<DropdownItem
|
|
|
|
{disabled}
|
2022-12-07 06:31:37 +01:00
|
|
|
active={value == $selectContext.value}
|
2022-12-01 10:24:26 +01:00
|
|
|
on:click={() => setValue(value)}
|
|
|
|
on:keydown={handleKey}
|
|
|
|
bind:buttonRef={element}
|
|
|
|
tabbable
|
|
|
|
>
|
2022-09-27 04:16:45 +02:00
|
|
|
<slot />
|
|
|
|
</DropdownItem>
|