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

83 lines
1.5 KiB
Vue
Raw Normal View History

2022-04-15 14:24:30 +00:00
<template>
<div id="searchForm" class="side search" role="search">
2022-04-15 14:24:30 +00:00
<input
2022-04-24 20:23:20 +00:00
ref="input"
v-model="q"
2022-04-15 14:24:30 +00:00
:class="{ dirty: q }"
autocorrect="false"
2022-04-24 20:23:20 +00:00
name="q"
2022-04-15 14:24:30 +00:00
placeholder="Press F to search"
spellcheck="false"
2022-04-24 20:23:20 +00:00
type="search"
@focus="goToSearchScreen"
@input="onInput"
2022-04-15 14:24:30 +00:00
>
</div>
</template>
2022-04-15 17:00:08 +00:00
<script lang="ts" setup>
import { ref } from 'vue'
2022-04-15 14:24:30 +00:00
import { debounce } from 'lodash'
import { eventBus, requireInjection } from '@/utils'
import { RouterKey } from '@/symbols'
const router = requireInjection(RouterKey)
2022-04-15 14:24:30 +00:00
2022-04-24 20:23:20 +00:00
const input = ref<HTMLInputElement>()
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
const goToSearchScreen = () => router.go('search')
2022-04-15 14:24:30 +00:00
2022-04-15 17:00:08 +00:00
eventBus.on({
FOCUS_SEARCH_FIELD: () => {
2022-04-24 20:23:20 +00:00
input.value?.focus()
input.value?.select()
2022-04-15 14:24:30 +00:00
}
})
</script>
<style lang="scss">
#searchForm {
@include vertical-center();
flex: 0 0 256px;
order: -1;
input[type="search"] {
width: 218px;
margin-top: 0;
}
2022-04-15 17:00:08 +00:00
@media only screen and (max-width: 667px) {
z-index: 100;
2022-04-15 14:24:30 +00:00
position: absolute;
left: 0;
background: var(--color-bg-primary);
width: 100%;
padding: 12px;
top: var(--header-height);
border-bottom: 1px solid rgba(255, 255, 255, .1);
2022-04-15 14:24:30 +00:00
input[type="search"] {
width: 100%;
}
}
.desktop & {
justify-content: flex-end;
input[type="search"] {
width: 160px;
}
}
}
</style>