CommonInputSearch.vue 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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 } from 'vue'
  5. export interface CommonInputSearchProps {
  6. modelValue?: string
  7. noBorder?: boolean
  8. wrapperClass?: string
  9. placeholder?: string
  10. }
  11. export interface CommonInputSearchEmits {
  12. (e: 'update:modelValue', filter: string): void
  13. }
  14. export interface CommonInputSearchExpose {
  15. focus(): void
  16. }
  17. const props = withDefaults(defineProps<CommonInputSearchProps>(), {
  18. placeholder: __('Search…'),
  19. })
  20. const emit = defineEmits<CommonInputSearchEmits>()
  21. const filter = useVModel(props, 'modelValue', emit)
  22. const filterInput = useTemplateRef('filter-input')
  23. const focus = () => {
  24. filterInput.value?.focus()
  25. }
  26. defineExpose({ focus })
  27. const clearFilter = () => {
  28. filter.value = ''
  29. focus()
  30. }
  31. </script>
  32. <script lang="ts">
  33. export default {
  34. inheritAttrs: false,
  35. }
  36. </script>
  37. <template>
  38. <div
  39. class="relative flex w-full items-center self-stretch"
  40. :class="wrapperClass"
  41. >
  42. <CommonIcon
  43. class="text-gray absolute shrink-0 ltr:left-2 rtl:right-2"
  44. size="base"
  45. name="search"
  46. decorative
  47. />
  48. <input
  49. ref="filter-input"
  50. v-model="filter"
  51. v-bind="$attrs"
  52. :placeholder="i18n.t(placeholder)"
  53. class="placeholder:text-gray h-12 w-full grow rounded-xl bg-gray-500 px-9 text-base focus:shadow-none focus:outline-none focus:ring-0"
  54. :class="{
  55. 'focus:border-white focus:ring-0': !noBorder,
  56. 'focus:border-transparent': noBorder,
  57. }"
  58. :aria-label="$t('Search…')"
  59. type="text"
  60. role="searchbox"
  61. />
  62. <div class="absolute flex shrink-0 items-center ltr:right-2 rtl:left-2">
  63. <slot name="controls" />
  64. <CommonIcon
  65. v-if="filter && filter.length"
  66. :aria-label="i18n.t('Clear Search')"
  67. class="text-gray"
  68. size="base"
  69. name="input-cancel"
  70. @click.stop="clearFilter"
  71. />
  72. </div>
  73. </div>
  74. </template>