fix: select menus dont update on language change
This commit is contained in:
1 file changed
+15
-6
@@ -1,4 +1,5 @@
|
|||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
|
import { locale } from '$lib/i18n/translations'
|
||||||
import { getContext, type Snippet } from 'svelte'
|
import { getContext, type Snippet } from 'svelte'
|
||||||
import type { IconSource } from 'svelte-hero-icons'
|
import type { IconSource } from 'svelte-hero-icons'
|
||||||
import type { HTMLOptionAttributes } from 'svelte/elements'
|
import type { HTMLOptionAttributes } from 'svelte/elements'
|
||||||
@@ -12,7 +13,7 @@
|
|||||||
let optionElement = $state<HTMLOptionElement>()
|
let optionElement = $state<HTMLOptionElement>()
|
||||||
let option = $derived({
|
let option = $derived({
|
||||||
value: optionElement?.value ?? '',
|
value: optionElement?.value ?? '',
|
||||||
label: optionElement?.innerText?.trim() ?? '',
|
label: optionElement?.innerText ?? '',
|
||||||
icon: icon,
|
icon: icon,
|
||||||
disabled: optionElement?.disabled,
|
disabled: optionElement?.disabled,
|
||||||
isLabel: optionElement?.getAttribute('data-label') == 'true',
|
isLabel: optionElement?.getAttribute('data-label') == 'true',
|
||||||
@@ -31,12 +32,20 @@
|
|||||||
const context = getContext<SelectContext>('select')
|
const context = getContext<SelectContext>('select')
|
||||||
|
|
||||||
$effect(() => {
|
$effect(() => {
|
||||||
if (!context.options.find(v => v == option)) {
|
if ($locale || !context.options.find(v => v == option)) {
|
||||||
context.options.push(option)
|
const index = context.options.findIndex(i => i.value == option.value)
|
||||||
|
|
||||||
|
if (index != -1) {
|
||||||
|
context.options.splice(index, 1, option)
|
||||||
|
} else {
|
||||||
|
context.options.push(option)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<option {...rest} bind:this={optionElement}>
|
{#key $locale}
|
||||||
{@render children()}
|
<option {...rest} bind:this={optionElement}>
|
||||||
</option>
|
{@render children()}
|
||||||
|
</option>
|
||||||
|
{/key}
|
||||||
Reference in new issue
Block a user