diff --git a/src/lib/components/select/Option.svelte b/src/lib/components/select/Option.svelte index 44a53934..54704fbf 100644 --- a/src/lib/components/select/Option.svelte +++ b/src/lib/components/select/Option.svelte @@ -10,12 +10,35 @@ import { forwardEventsBuilder, useActions, type ActionArray } from '../../actions'; export let use: ActionArray = []; import { exclude } from '../../utils/exclude'; + import type { SelectOption } from '$lib/types/select-option'; const forwardEvents = forwardEventsBuilder(get_current_component()); - export let option: string; + export let option: SelectOption; - const value: Writable = getContext('select-value'); - const handleSelect: (option: string) => void = getContext('select-handleSelect'); + const value: Writable = getContext('select-value'); + const optionLabel: string = getContext('select-option-label'); + const optionValue: string = getContext('select-option-value'); + const handleSelect: (option: SelectOption) => void = getContext('select-handleSelect'); + const multiple: boolean = getContext('select-multiple'); + + let optionIsSelected = false; + + $: { + if ($value && multiple && Array.isArray($value)) { + const isSelected = $value.find((v) => v[optionValue] === option[optionValue]); + if (isSelected) { + optionIsSelected = true; + } else { + optionIsSelected = false; + } + } else if ($value && !Array.isArray($value)) { + if ($value[optionValue] === option[optionValue]) { + optionIsSelected = true; + } else { + optionIsSelected = false; + } + } + } const defaultClass = 'group text-light-content dark:text-dark-content cursor-pointer select-none p-0.5 w-full'; @@ -25,18 +48,18 @@