-
-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f48b6f0
commit 19f76c2
Showing
4 changed files
with
181 additions
and
86 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
87 changes: 87 additions & 0 deletions
87
packages/svelte-ux/src/lib/components/_SelectListOptions.svelte
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
<script lang='ts'> | ||
import MenuItem from './MenuItem.svelte'; | ||
import Logger from '../utils/logger'; | ||
import { getComponentTheme } from './theme'; | ||
import { cls } from '../utils/styles'; | ||
const logger = new Logger('SelectListOptions'); | ||
export let optionText: (option: any) => string; | ||
export let optionValue: (option: any) => any; | ||
export let selectIndex: (index: number) => any; | ||
export let selectOption: (x: any) => any; | ||
export let onKeyDown: (x: KeyboardEvent) => void; | ||
export let onKeyPress: (x: KeyboardEvent) => void; | ||
export let open: boolean; | ||
export let loading: boolean; | ||
export let filteredOptions: any[]; | ||
export let value: any = undefined; | ||
export let selected: any = undefined; | ||
export let highlightIndex: number; | ||
export let searchText: string; | ||
export let classes: { | ||
root?: string; | ||
option?: string; | ||
selected?: string; | ||
group?: string; | ||
empty?: string; | ||
} = {}; | ||
const theme = getComponentTheme('SelectField'); | ||
export let menuOptionsEl: HTMLDivElement; | ||
</script> | ||
|
||
<div | ||
role='listbox' | ||
tabindex='-1' | ||
aria-expanded={open ? 'true' : 'false'} | ||
class={cls('_SelectListOptions options group p-1 focus:outline-none', theme.options, classes.root)} | ||
class:opacity-50={loading} | ||
bind:this={menuOptionsEl} | ||
on:click|stopPropagation={(e) => { | ||
logger.debug('options container clicked'); | ||
|
||
if (e.target instanceof HTMLElement) { | ||
// Find slot parent of click target option, fallback to `e.target` if slot is not overridden | ||
// Use `.options > ` in case slot is nested (ex. GraphQLSelect with slot) | ||
const slotEl = e.target.closest('.options > [slot=option]') ?? e.target; | ||
// Find the index of the clicked on element (ignoring group headers) | ||
const optionIndex = slotEl | ||
? [...menuOptionsEl.children] | ||
.filter((el) => !el.classList.contains('group-header')) | ||
.indexOf(slotEl) | ||
: -1; | ||
logger.debug({ slotEl, optionIndex }); | ||
// ignore clicks on group options | ||
if (optionIndex !== -1) { | ||
selectIndex(optionIndex); | ||
} | ||
} | ||
}} | ||
on:keydown={onKeyDown} | ||
on:keypress={onKeyPress} | ||
> | ||
{#each filteredOptions ?? [] as option, index (optionValue(option))} | ||
{@const previousOption = filteredOptions[index - 1]} | ||
{#if option.group && option.group !== previousOption?.group} | ||
<div | ||
class={cls( | ||
'group-header text-xs leading-8 tracking-widest text-black/50 px-2', | ||
theme.group, | ||
classes.group | ||
)} | ||
> | ||
{option.group} | ||
</div> | ||
{/if} | ||
|
||
<slot name="option" {option} {index}></slot> | ||
{:else} | ||
<slot name="empty" {loading} {searchText}></slot> | ||
{/each} | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters