diff --git a/lib/components/SDropdownSection.vue b/lib/components/SDropdownSection.vue index 6d6c701e..d285caa0 100644 --- a/lib/components/SDropdownSection.vue +++ b/lib/components/SDropdownSection.vue @@ -21,6 +21,9 @@ defineProps<{ :search="section.search" :selected="section.selected" :options="section.options" + :query="section.query" + :active="section.active" + :option-id-prefix="section.optionIdPrefix" :on-click="section.onClick" /> options: MaybeRef + active?: DropdownSectionFilterSelectedValue + optionIdPrefix?: string onClick?(value: any): void }>() @@ -30,29 +33,35 @@ const { t } = useTrans({ const input = ref(null) const query = ref('') -const enabledOptions = computed(() => { +const enabledOptions = smartComputed(() => { return unref(props.options).filter((o) => !o.disabled) }) const fuse = computed(() => { - return new Fuse(unref(enabledOptions), { keys: ['label'] }) + return new Fuse(enabledOptions.value, { keys: ['label'] }) }) const filteredOptions = computed(() => { - return !props.search || !query.value - ? unref(enabledOptions) + return props.search !== true || !query.value + ? enabledOptions.value : fuse.value.search(query.value).map((r) => r.item) }) onMounted(() => { - input.value?.focus() + if (props.search === true) { + input.value?.focus() + } }) -function isActive(value: any) { +function isSelected(value: any) { const selected = unref(props.selected) return Array.isArray(selected) ? selected.includes(value) : selected === value } +function isActive(value: any) { + return props.active === value +} + function focusPrev(event: any) { event.target.parentNode.previousElementSibling?.firstElementChild?.focus() } @@ -65,25 +74,32 @@ function handleClick(option: DropdownSectionFilterOption, value: any) { option.onClick && option.onClick(value) props.onClick && props.onClick(value) } + +function getOptionId(index: number) { + return props.optionIdPrefix ? `${props.optionIdPrefix}-${index}` : undefined +}