koel/resources/assets/js/components/ui/SearchForm.vue

73 lines
1.9 KiB
Vue
Raw Normal View History

2022-04-15 14:24:30 +00:00
<template>
2024-04-04 22:20:42 +00:00
<form
id="searchForm"
class="text-k-text-secondary flex items-stretch border overflow-hidden gap-2 pl-4 pr-0 py-0 rounded-md
border-solid border-transparent bg-black/20 focus-within:border-white/20 focus-within:bg-black/50
transition-[border,_background-color] duration-200 ease-in-out"
2024-04-23 21:01:27 +00:00
role="search"
2024-04-04 22:20:42 +00:00
@submit.prevent="onSubmit"
>
<span class="hidden md:flex opacity-70 items-center">
2023-11-10 13:16:06 +00:00
<Icon :icon="faSearch" />
</span>
2024-04-04 22:20:42 +00:00
<TextInput
2022-04-24 20:23:20 +00:00
ref="input"
v-model="q"
2022-04-15 14:24:30 +00:00
:class="{ dirty: q }"
:placeholder="placeholder"
2022-04-15 14:24:30 +00:00
autocorrect="false"
2024-04-24 20:25:39 +00:00
class="w-full rounded-none h-[36px] !bg-transparent !text-k-text-primary !placeholder:text-white/50
2024-05-19 05:49:42 +00:00
focus-visible:outline-0 !px-2"
2022-04-24 20:23:20 +00:00
name="q"
required
2022-04-15 14:24:30 +00:00
spellcheck="false"
2022-04-24 20:23:20 +00:00
type="search"
2022-10-13 15:18:47 +00:00
@focus="maybeGoToSearchScreen"
2022-04-24 20:23:20 +00:00
@input="onInput"
2024-04-04 22:20:42 +00:00
/>
2024-04-23 21:01:27 +00:00
<button class="block md:hidden py-0 px-4 bg-white/5 rounded-none" title="Search" type="submit">
2023-11-10 13:16:06 +00:00
<Icon :icon="faSearch" />
2022-10-13 15:18:47 +00:00
</button>
</form>
2022-04-15 14:24:30 +00:00
</template>
2022-04-15 17:00:08 +00:00
<script lang="ts" setup>
2022-10-13 15:18:47 +00:00
import isMobile from 'ismobilejs'
import { faSearch } from '@fortawesome/free-solid-svg-icons'
2022-04-15 17:00:08 +00:00
import { ref } from 'vue'
2022-04-15 14:24:30 +00:00
import { debounce } from 'lodash'
2022-11-18 18:44:20 +00:00
import { eventBus } from '@/utils'
import { useRouter } from '@/composables'
2024-04-04 22:20:42 +00:00
import TextInput from '@/components/ui/form/TextInput.vue'
2022-10-13 15:18:47 +00:00
const placeholder = isMobile.any ? 'Search' : 'Press F to search'
2022-11-18 18:44:20 +00:00
const { go } = useRouter()
2022-04-15 14:24:30 +00:00
2024-04-04 22:20:42 +00:00
const input = ref<InstanceType<typeof TextInput>>()
2022-04-15 17:00:08 +00:00
const q = ref('')
2022-04-15 14:24:30 +00:00
let onInput = () => {
2022-04-15 17:00:08 +00:00
const _q = q.value.trim()
_q && eventBus.emit('SEARCH_KEYWORDS_CHANGED', _q)
}
if (process.env.NODE_ENV !== 'test') {
onInput = debounce(onInput, 500)
}
2022-04-15 14:24:30 +00:00
2022-10-13 15:18:47 +00:00
const onSubmit = () => {
eventBus.emit('TOGGLE_SIDEBAR')
2022-11-18 18:44:20 +00:00
go('search')
2022-10-13 15:18:47 +00:00
}
2022-11-18 18:44:20 +00:00
const maybeGoToSearchScreen = () => isMobile.any || go('search')
2022-04-15 14:24:30 +00:00
eventBus.on('FOCUS_SEARCH_FIELD', () => {
2024-04-04 22:20:42 +00:00
input.value?.el?.focus()
input.value?.el?.select()
2022-04-15 14:24:30 +00:00
})
</script>