cleanup: use context api for select menus and options
This commit is contained in:
1 parent
ee08787afc
commit
dd9abe5345
20 files changed
+271
-336
No files matched your search
@@ -0,0 +1,40 @@
|
||||
<script lang="ts" generics="T">
|
||||
import { getContext, onMount, type Snippet } from 'svelte'
|
||||
import type { HTMLAttributes, HTMLOptionAttributes } from 'svelte/elements'
|
||||
import type { IconSource } from 'svelte-hero-icons'
|
||||
|
||||
interface Props extends Omit<HTMLOptionAttributes, 'prefix'> {
|
||||
children: Snippet
|
||||
icon?: IconSource
|
||||
}
|
||||
let { children, icon, ...rest }: Props = $props()
|
||||
|
||||
let optionElement = $state<HTMLOptionElement>()
|
||||
let option: any = $derived({
|
||||
value: optionElement?.value ?? '',
|
||||
label: optionElement?.innerText?.trim() ?? '',
|
||||
icon: icon,
|
||||
disabled: false,
|
||||
})
|
||||
|
||||
interface SelectContext {
|
||||
options: {
|
||||
value: string
|
||||
label: string
|
||||
icon?: IconSource
|
||||
disabled: boolean
|
||||
}[]
|
||||
}
|
||||
|
||||
const context = getContext<SelectContext>('select')
|
||||
|
||||
$effect(() => {
|
||||
if (!context.options.find((v) => v == option)) {
|
||||
context.options.push(option)
|
||||
}
|
||||
})
|
||||
</script>
|
||||
|
||||
<option {...rest} bind:this={optionElement}>
|
||||
{@render children()}
|
||||
</option>
|
||||
Reference in new issue
Block a user