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