CommonInputSearch.vue 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. <!-- Copyright (C) 2012-2023 Zammad Foundation, https://zammad-foundation.org/ -->
  2. <script setup lang="ts">
  3. import { useVModel } from '@vueuse/core'
  4. import { shallowRef } 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 = shallowRef<HTMLInputElement>()
  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="absolute shrink-0 text-gray ltr:left-2 rtl:right-2"
  44. size="base"
  45. name="mobile-search"
  46. decorative
  47. />
  48. <input
  49. ref="filterInput"
  50. v-model="filter"
  51. v-bind="$attrs"
  52. :placeholder="i18n.t(placeholder)"
  53. class="h-12 w-full grow rounded-xl bg-gray-500 px-9 text-base placeholder:text-gray 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. type="text"
  59. role="searchbox"
  60. />
  61. <div class="absolute flex shrink-0 items-center ltr:right-2 rtl:left-2">
  62. <slot name="controls" />
  63. <CommonIcon
  64. v-if="filter && filter.length"
  65. :aria-label="i18n.t('Clear Search')"
  66. class="text-gray"
  67. size="base"
  68. name="mobile-close-small"
  69. @click.stop="clearFilter"
  70. />
  71. </div>
  72. </div>
  73. </template>