CommonInputSearch.vue 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <!-- Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/ -->
  2. <script setup lang="ts">
  3. import { useVModel } from '@vueuse/core'
  4. import { computed, shallowRef } 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 CommonInputSearchEmits {
  13. (e: 'update:modelValue', filter: string): void
  14. }
  15. export interface CommonInputSearchExpose {
  16. focus(): void
  17. }
  18. const props = withDefaults(defineProps<CommonInputSearchProps>(), {
  19. placeholder: __('Search…'),
  20. })
  21. const emit = defineEmits<CommonInputSearchEmits>()
  22. const filter = useVModel(props, 'modelValue', emit)
  23. const filterInput = shallowRef<HTMLInputElement>()
  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. </script>
  49. <script lang="ts">
  50. export default {
  51. inheritAttrs: false,
  52. }
  53. </script>
  54. <template>
  55. <div
  56. class="inline-flex grow items-center justify-start gap-1"
  57. :class="wrapperClass"
  58. >
  59. <CommonIcon
  60. class="shrink-0 fill-stone-200 dark:fill-neutral-500"
  61. size="tiny"
  62. name="search"
  63. decorative
  64. />
  65. <div class="relative inline-flex grow overflow-clip">
  66. <div class="grow">
  67. <input
  68. ref="filterInput"
  69. v-model="filter"
  70. v-bind="$attrs"
  71. :placeholder="i18n.t(placeholder)"
  72. :aria-label="$t('Search…')"
  73. class="w-full min-w-16 text-black outline-none dark:text-white"
  74. :class="{
  75. 'bg-blue-200 dark:bg-gray-700': !alternativeBackground,
  76. 'bg-white dark:bg-gray-500': alternativeBackground,
  77. }"
  78. type="text"
  79. role="searchbox"
  80. @keydown.right="maybeAcceptSuggestion"
  81. @keydown.end="maybeAcceptSuggestion"
  82. @keydown.tab="maybeAcceptSuggestion"
  83. />
  84. </div>
  85. <div
  86. v-if="suggestionVisiblePart?.length"
  87. class="pointer-events-none absolute top-0 flex whitespace-pre"
  88. data-test-id="suggestion"
  89. >
  90. <span class="invisible">{{ filter }}</span>
  91. <span class="text-stone-200 dark:text-neutral-500">{{
  92. suggestionVisiblePart
  93. }}</span>
  94. </div>
  95. </div>
  96. <div class="flex shrink-0 items-center gap-1">
  97. <slot name="controls" />
  98. <CommonIcon
  99. 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"
  100. :class="{
  101. invisible: !filter?.length,
  102. }"
  103. :aria-label="i18n.t('Clear Search')"
  104. :aria-hidden="!filter?.length ? 'true' : undefined"
  105. name="backspace2"
  106. size="tiny"
  107. role="button"
  108. :tabindex="!filter?.length ? '-1' : '0'"
  109. @click.stop="clearFilter()"
  110. @keypress.space.prevent.stop="clearFilter()"
  111. />
  112. </div>
  113. </div>
  114. </template>