CommonInputSearch.vue 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <!-- Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/ -->
  2. <script setup lang="ts">
  3. import { useVModel } from '@vueuse/core'
  4. import { useTemplateRef, computed } from 'vue'
  5. export interface CommonInputSearchProps {
  6. modelValue?: string
  7. wrapperClass?: string
  8. placeholder?: string
  9. suggestion?: string
  10. alternativeBackground?: boolean
  11. }
  12. export interface CommonInputSearchExpose {
  13. focus(): void
  14. }
  15. const props = withDefaults(defineProps<CommonInputSearchProps>(), {
  16. placeholder: __('Search…'),
  17. })
  18. const emit = defineEmits<{
  19. 'update:modelValue': [filter: string]
  20. keydown: [event: KeyboardEvent]
  21. }>()
  22. const filter = useVModel(props, 'modelValue', emit)
  23. const filterInput = useTemplateRef('filter-input')
  24. const focus = () => {
  25. filterInput.value?.focus()
  26. }
  27. defineExpose({ focus })
  28. const clearFilter = () => {
  29. filter.value = ''
  30. focus()
  31. }
  32. const suggestionVisiblePart = computed(() =>
  33. props.suggestion?.slice(filter.value?.length),
  34. )
  35. const maybeAcceptSuggestion = (event: Event) => {
  36. if (
  37. !props.suggestion ||
  38. !filter.value ||
  39. !filterInput.value ||
  40. !filterInput.value.selectionStart ||
  41. filter.value.length >= props.suggestion.length ||
  42. filterInput.value.selectionStart < filter.value.length
  43. )
  44. return
  45. event.preventDefault()
  46. filter.value = props.suggestion
  47. }
  48. const onKeydown = (event: KeyboardEvent) => {
  49. emit('keydown', event)
  50. }
  51. </script>
  52. <script lang="ts">
  53. export default {
  54. inheritAttrs: false,
  55. }
  56. </script>
  57. <template>
  58. <div
  59. class="inline-flex grow items-center justify-start gap-1"
  60. :class="wrapperClass"
  61. >
  62. <CommonIcon
  63. class="shrink-0 fill-stone-200 dark:fill-neutral-500"
  64. size="tiny"
  65. name="search"
  66. decorative
  67. />
  68. <div class="relative inline-flex grow overflow-clip">
  69. <div class="grow">
  70. <input
  71. ref="filter-input"
  72. v-model="filter"
  73. v-bind="$attrs"
  74. :placeholder="i18n.t(placeholder)"
  75. :aria-label="$t('Search…')"
  76. class="w-full min-w-16 text-black outline-none dark:text-white"
  77. :class="{
  78. 'bg-blue-200 dark:bg-gray-700': !alternativeBackground,
  79. 'bg-neutral-50 dark:bg-gray-500': alternativeBackground,
  80. }"
  81. type="text"
  82. role="searchbox"
  83. @keydown.right="maybeAcceptSuggestion"
  84. @keydown.end="maybeAcceptSuggestion"
  85. @keydown.tab="maybeAcceptSuggestion"
  86. @keydown="onKeydown"
  87. />
  88. </div>
  89. <div
  90. v-if="suggestionVisiblePart?.length"
  91. class="pointer-events-none absolute top-0 flex whitespace-pre"
  92. data-test-id="suggestion"
  93. >
  94. <span class="invisible">{{ filter }}</span>
  95. <span class="text-stone-200 dark:text-neutral-500">{{
  96. suggestionVisiblePart
  97. }}</span>
  98. </div>
  99. </div>
  100. <div class="flex shrink-0 items-center gap-1">
  101. <slot name="controls" />
  102. <CommonIcon
  103. class="fill-stone-200 hover:fill-black focus-visible:rounded-sm focus-visible:outline focus-visible:outline-1 focus-visible:outline-offset-1 focus-visible:outline-blue-800 dark:fill-neutral-500 dark:hover:fill-white"
  104. :class="{
  105. invisible: !filter?.length,
  106. }"
  107. :aria-label="i18n.t('Clear Search')"
  108. :aria-hidden="!filter?.length ? 'true' : undefined"
  109. name="backspace2"
  110. size="tiny"
  111. role="button"
  112. :tabindex="!filter?.length ? '-1' : '0'"
  113. @click.stop="clearFilter()"
  114. @keypress.space.prevent.stop="clearFilter()"
  115. />
  116. </div>
  117. </div>
  118. </template>