123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256 |
- <!-- Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/ -->
- <script setup lang="ts">
- import { computed, nextTick, ref, toRef, watch } from 'vue'
- import { useElementBounding, useWindowSize } from '@vueuse/core'
- import { escapeRegExp } from 'lodash-es'
- import { i18n } from '#shared/i18n.ts'
- import { useTrapTab } from '#shared/composables/useTrapTab.ts'
- import CommonInputSearch from '#desktop/components/CommonInputSearch/CommonInputSearch.vue'
- import CommonSelect from '#desktop/components/CommonSelect/CommonSelect.vue'
- import type { CommonSelectInstance } from '#desktop/components/CommonSelect/types.ts'
- import { useFormBlock } from '#shared/form/useFormBlock.ts'
- import useValue from '#shared/components/Form/composables/useValue.ts'
- import useSelectOptions from '#shared/composables/useSelectOptions.ts'
- import useSelectPreselect from '#shared/composables/useSelectPreselect.ts'
- import type { SelectContext } from '#shared/components/Form/fields/FieldSelect/types.ts'
- interface Props {
- context: SelectContext
- }
- const props = defineProps<Props>()
- const contextReactive = toRef(props, 'context')
- const { hasValue, valueContainer, currentValue, clearValue } =
- useValue(contextReactive)
- const {
- sortedOptions,
- selectOption,
- getSelectedOption,
- getSelectedOptionIcon,
- getSelectedOptionLabel,
- setupMissingOrDisabledOptionHandling,
- } = useSelectOptions(toRef(props.context, 'options'), contextReactive)
- const input = ref<HTMLDivElement>()
- const outputElement = ref<HTMLOutputElement>()
- const filter = ref('')
- const filterInput = ref<HTMLInputElement>()
- const select = ref<CommonSelectInstance>()
- const { activateTabTrap, deactivateTabTrap } = useTrapTab(input, true)
- const clearFilter = () => {
- filter.value = ''
- }
- watch(() => contextReactive.value.noFiltering, clearFilter)
- const deaccent = (s: string) =>
- s.normalize('NFD').replace(/[\u0300-\u036f]/g, '')
- const filteredOptions = computed(() => {
- // Trim and de-accent search keywords and compile them as a case-insensitive regex.
- // Make sure to escape special regex characters!
- const filterRegex = new RegExp(
- escapeRegExp(deaccent(filter.value.trim())),
- 'i',
- )
- // Search across options via their de-accented labels.
- return sortedOptions.value.filter((option) =>
- filterRegex.test(deaccent(option.label || String(option.value))),
- )
- })
- const inputElementBounds = useElementBounding(input)
- const windowSize = useWindowSize()
- const isBelowHalfScreen = computed(() => {
- return inputElementBounds.y.value > windowSize.height.value / 2
- })
- const openSelectDropdown = () => {
- if (select.value?.isOpen || props.context.disabled) return
- select.value?.openDropdown(inputElementBounds, windowSize.height)
- requestAnimationFrame(() => {
- activateTabTrap()
- if (props.context.noFiltering) outputElement.value?.focus()
- else filterInput.value?.focus()
- })
- }
- const openOrMoveFocusToDropdown = (lastOption = false) => {
- if (!select.value?.isOpen) {
- openSelectDropdown()
- return
- }
- deactivateTabTrap()
- nextTick(() => {
- requestAnimationFrame(() => {
- select.value?.moveFocusToDropdown(lastOption)
- })
- })
- }
- const onCloseDropdown = () => {
- clearFilter()
- deactivateTabTrap()
- }
- useFormBlock(contextReactive, openSelectDropdown)
- useSelectPreselect(sortedOptions, contextReactive)
- setupMissingOrDisabledOptionHandling()
- </script>
- <template>
- <div
- ref="input"
- :class="[
- context.classes.input,
- `flex h-auto min-h-10 bg-blue-200 dark:bg-gray-700 hover:outline hover:outline-1 hover:outline-offset-1 hover:outline-blue-600 dark:hover:outline-blue-900 has-[output:focus,input:focus]:outline has-[output:focus,input:focus]:outline-1 has-[output:focus,input:focus]:outline-offset-1 has-[output:focus,input:focus]:outline-blue-800 dark:has-[output:focus,input:focus]:outline-blue-800`,
- {
- 'rounded-lg': !select?.isOpen,
- 'rounded-t-lg': select?.isOpen && !isBelowHalfScreen,
- 'rounded-b-lg': select?.isOpen && isBelowHalfScreen,
- },
- ]"
- data-test-id="field-select"
- >
- <CommonSelect
- ref="select"
- #default="{ state: expanded, close: closeDropdown }"
- :model-value="currentValue"
- :options="filteredOptions"
- :multiple="context.multiple"
- :owner="context.id"
- no-options-label-translation
- no-close
- passive
- @select="selectOption"
- @close="onCloseDropdown"
- >
- <output
- :id="context.id"
- ref="outputElement"
- role="combobox"
- aria-controls="common-select"
- aria-owns="common-select"
- aria-haspopup="menu"
- :aria-expanded="expanded"
- :name="context.node.name"
- class="px-2.5 py-2 flex grow gap-2.5 items-center text-black dark:text-white focus:outline-none formkit-disabled:pointer-events-none"
- :aria-labelledby="`label-${context.id}`"
- :aria-disabled="context.disabled"
- :data-multiple="context.multiple"
- :tabindex="
- context.disabled || (expanded && !context.noFiltering) ? '-1' : '0'
- "
- v-bind="{
- ...context.attrs,
- onBlur: undefined,
- }"
- @keydown.escape.prevent="closeDropdown()"
- @keypress.enter.prevent="openSelectDropdown()"
- @keydown.down.prevent="openOrMoveFocusToDropdown()"
- @keydown.up.prevent="openOrMoveFocusToDropdown(true)"
- @keypress.space.prevent="openSelectDropdown()"
- @blur="context.handlers.blur"
- >
- <div
- v-if="hasValue && context.multiple"
- class="flex flex-wrap gap-1.5"
- role="list"
- >
- <div
- v-for="selectedValue in valueContainer"
- :key="selectedValue"
- class="flex items-center gap-1.5"
- role="listitem"
- >
- <div
- class="inline-flex items-center px-1.5 py-0.5 gap-1 rounded bg-white dark:bg-gray-200 text-black dark:text-white text-xs"
- >
- <CommonIcon
- v-if="getSelectedOptionIcon(selectedValue)"
- :name="getSelectedOptionIcon(selectedValue)"
- class="fill-gray-100 dark:fill-neutral-400"
- size="xs"
- decorative
- />
- {{
- getSelectedOptionLabel(selectedValue) ||
- i18n.t('%s (unknown)', selectedValue)
- }}
- <CommonIcon
- :aria-label="i18n.t('Unselect Option')"
- class="fill-stone-200 dark:fill-neutral-500 focus-visible:outline focus-visible:rounded-sm focus-visible:outline-1 focus-visible:outline-offset-1 focus-visible:outline-blue-800"
- name="x-lg"
- size="xs"
- role="button"
- tabindex="0"
- @click.stop="selectOption(getSelectedOption(selectedValue))"
- @keypress.enter.prevent.stop="
- selectOption(getSelectedOption(selectedValue))
- "
- @keypress.space.prevent.stop="
- selectOption(getSelectedOption(selectedValue))
- "
- />
- </div>
- </div>
- </div>
- <CommonInputSearch
- v-if="expanded && !context.noFiltering"
- ref="filterInput"
- v-model="filter"
- @keypress.space.stop
- />
- <div v-else class="flex grow flex-wrap gap-1" role="list">
- <div
- v-if="hasValue && !context.multiple"
- class="flex items-center gap-1.5 text-sm"
- role="listitem"
- >
- <CommonIcon
- v-if="getSelectedOptionIcon(currentValue)"
- :name="getSelectedOptionIcon(currentValue)"
- class="fill-gray-100 dark:fill-neutral-400"
- size="tiny"
- decorative
- />
- {{
- getSelectedOptionLabel(currentValue) ||
- i18n.t('%s (unknown)', currentValue)
- }}
- </div>
- </div>
- <CommonIcon
- v-if="context.clearable && hasValue && !context.disabled"
- :aria-label="i18n.t('Clear Selection')"
- class="shrink-0 fill-stone-200 dark:fill-neutral-500 focus-visible:outline focus-visible:rounded-sm focus-visible:outline-1 focus-visible:outline-offset-1 focus-visible:outline-blue-800"
- name="x-lg"
- size="xs"
- role="button"
- tabindex="0"
- @click.stop="clearValue()"
- @keypress.enter.prevent.stop="clearValue()"
- @keypress.space.prevent.stop="clearValue()"
- />
- <CommonIcon
- class="shrink-0 fill-stone-200 dark:fill-neutral-500"
- name="chevron-down"
- size="xs"
- decorative
- />
- </output>
- </CommonSelect>
- </div>
- </template>
|