it-tools/src/components/SearchBar.vue

65 lines
1.6 KiB
Vue
Raw Normal View History

2022-04-04 01:03:06 +02:00
<script lang="ts" setup>
2022-04-22 23:31:40 +02:00
import { SearchRound } from '@vicons/material';
2022-04-04 01:03:06 +02:00
import { computed, ref } from 'vue';
2022-04-22 23:31:40 +02:00
import { deburr } from 'lodash';
2022-04-04 01:03:06 +02:00
import { tools } from '@/tools';
import { useRouter } from 'vue-router';
2022-04-22 23:31:40 +02:00
const router = useRouter();
const queryString = ref('');
2022-04-04 01:03:06 +02:00
2022-04-22 23:31:40 +02:00
const cleanString = (s: string) => deburr(s.trim().toLowerCase());
2022-04-04 01:03:06 +02:00
const searchableTools = tools.map(({ name, description, keywords, path }) => ({
2022-04-22 23:31:40 +02:00
searchableText: [name, description, ...keywords].map(cleanString).join(' '),
path,
name,
}));
2022-04-04 01:03:06 +02:00
const options = computed(() => {
2022-04-22 23:31:40 +02:00
const query = cleanString(queryString.value);
2022-04-04 01:03:06 +02:00
2022-04-22 23:31:40 +02:00
return searchableTools
.filter(({ searchableText }) => searchableText.includes(query))
.map(({ name, path }) => ({ label: name, value: path }));
});
2022-04-04 01:03:06 +02:00
function onSelect(path: string) {
2022-04-22 23:31:40 +02:00
router.push(path);
queryString.value = '';
2022-04-04 01:03:06 +02:00
}
</script>
<template>
2022-04-15 23:10:47 +02:00
<div class="search-bar">
<n-auto-complete
v-model:value="queryString"
:options="options"
:input-props="{ autocomplete: 'disabled' }"
:on-select="onSelect"
>
<template #default="{ handleInput, handleBlur, handleFocus, value: slotValue }">
<n-input
round
clearable
placeholder="Search a tool..."
:value="slotValue"
@input="handleInput"
@focus="handleFocus"
@blur="handleBlur"
>
<template #prefix>
<n-icon :component="SearchRound" />
</template>
</n-input>
</template>
</n-auto-complete>
</div>
2022-04-04 01:03:06 +02:00
</template>
<style lang="less" scoped>
// ::v-deep(.n-input__border) {
// border: none;
// }
2022-04-22 23:31:40 +02:00
</style>