2023-02-24 05:32:18 +08:00
|
|
|
<script lang="ts">
|
|
|
|
import { fly } from "svelte/transition";
|
|
|
|
import { createEventDispatcher } from "svelte";
|
|
|
|
export let value: string | Array<string> | undefined = undefined;
|
|
|
|
export let filtered: Array<string>;
|
|
|
|
export let showOptions: boolean = false;
|
|
|
|
export let activeOption: string;
|
|
|
|
export let disabled: boolean = false;
|
|
|
|
|
2023-03-21 13:57:49 +08:00
|
|
|
let distance_from_top: number;
|
|
|
|
let distance_from_bottom: number;
|
|
|
|
let input_height: number;
|
|
|
|
let refElement: HTMLDivElement;
|
|
|
|
let top: string | null, bottom: string | null, max_height: number;
|
|
|
|
$: {
|
|
|
|
if (showOptions && refElement) {
|
|
|
|
distance_from_top = refElement.getBoundingClientRect().top;
|
|
|
|
distance_from_bottom =
|
|
|
|
window.innerHeight - refElement.getBoundingClientRect().bottom;
|
|
|
|
input_height =
|
|
|
|
refElement.parentElement?.getBoundingClientRect().height || 0;
|
|
|
|
}
|
|
|
|
if (distance_from_bottom > distance_from_top) {
|
|
|
|
top = `${input_height}px`;
|
|
|
|
max_height = distance_from_bottom;
|
|
|
|
bottom = null;
|
|
|
|
} else {
|
|
|
|
bottom = `${input_height}px`;
|
|
|
|
max_height = distance_from_top - input_height;
|
|
|
|
top = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-24 05:32:18 +08:00
|
|
|
const dispatch = createEventDispatcher();
|
2023-03-30 06:10:43 +08:00
|
|
|
|
|
|
|
function isSelected(choice: string) {
|
|
|
|
let arr = Array.isArray(value) ? value : [value];
|
|
|
|
return arr.includes(choice);
|
|
|
|
}
|
2023-02-24 05:32:18 +08:00
|
|
|
</script>
|
|
|
|
|
2023-03-21 13:57:49 +08:00
|
|
|
<div class="reference" bind:this={refElement} />
|
2023-02-24 05:32:18 +08:00
|
|
|
{#if showOptions && !disabled}
|
|
|
|
<ul
|
|
|
|
class="options"
|
|
|
|
aria-expanded={showOptions}
|
|
|
|
transition:fly={{ duration: 200, y: 5 }}
|
|
|
|
on:mousedown|preventDefault={(e) => dispatch("change", e)}
|
2023-03-21 13:57:49 +08:00
|
|
|
style:top
|
|
|
|
style:bottom
|
|
|
|
style:max-height={`calc(${max_height}px - var(--window-padding))`}
|
2023-02-24 05:32:18 +08:00
|
|
|
>
|
|
|
|
{#each filtered as choice}
|
|
|
|
<li
|
|
|
|
class="item"
|
|
|
|
role="button"
|
2023-03-30 06:10:43 +08:00
|
|
|
class:selected={isSelected(choice)}
|
2023-02-24 05:32:18 +08:00
|
|
|
class:active={activeOption === choice}
|
|
|
|
class:bg-gray-100={activeOption === choice}
|
|
|
|
class:dark:bg-gray-600={activeOption === choice}
|
|
|
|
data-value={choice}
|
|
|
|
aria-label={choice}
|
|
|
|
>
|
2023-03-30 06:10:43 +08:00
|
|
|
<span class:hide={!isSelected(choice)} class="inner-item pr-1">
|
2023-02-24 05:32:18 +08:00
|
|
|
✓
|
|
|
|
</span>
|
|
|
|
{choice}
|
|
|
|
</li>
|
|
|
|
{/each}
|
|
|
|
</ul>
|
|
|
|
{/if}
|
|
|
|
|
|
|
|
<style>
|
|
|
|
.options {
|
2023-03-21 13:57:49 +08:00
|
|
|
--window-padding: var(--size-8);
|
2023-02-24 05:32:18 +08:00
|
|
|
position: absolute;
|
|
|
|
z-index: var(--layer-5);
|
|
|
|
margin-left: 0;
|
|
|
|
box-shadow: var(--shadow-drop-lg);
|
2023-03-07 04:52:31 +08:00
|
|
|
border-radius: var(--container-radius);
|
2023-03-18 08:20:55 +08:00
|
|
|
background: var(--background-fill-primary);
|
2023-02-24 05:32:18 +08:00
|
|
|
width: var(--size-full);
|
|
|
|
overflow: auto;
|
2023-03-07 04:52:31 +08:00
|
|
|
color: var(--body-text-color);
|
2023-02-24 05:32:18 +08:00
|
|
|
list-style: none;
|
|
|
|
}
|
|
|
|
|
|
|
|
.item {
|
|
|
|
display: flex;
|
|
|
|
cursor: pointer;
|
|
|
|
padding: var(--size-2);
|
|
|
|
}
|
|
|
|
|
|
|
|
.item:hover,
|
|
|
|
.active {
|
2023-03-18 08:20:55 +08:00
|
|
|
background: var(--background-fill-secondary);
|
2023-02-24 05:32:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
.inner-item {
|
|
|
|
padding-right: var(--size-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
.hide {
|
|
|
|
visibility: hidden;
|
|
|
|
}
|
|
|
|
</style>
|