munch-ease-frontend/src/components/recipes/RecipeSearchBox.vue

99 lines
2.3 KiB
Vue
Raw Normal View History

2024-01-13 23:09:15 +00:00
<template>
2024-01-14 01:46:45 +00:00
<div class="recipe-search-box" @focusout="recipes = []">
<input type="text" v-model="searchTerm" @keyup.enter="search" @keyup.exit="clear" @focusin="search"
:placeholder="placeholder" />
2024-01-14 01:46:45 +00:00
<ul v-if="recipes?.length" class="dropdown">
<li class="recipe" v-for="recipe in recipes" :key="recipe.id" @mousedown="selectRecipe(recipe)">
<recipe-card :recipe="recipe" />
2024-01-13 23:09:15 +00:00
</li>
</ul>
</div>
</template>
<script>
2025-10-18 01:36:55 +00:00
import { searchRecipes } from '@/api/recipes'
2024-01-14 01:46:45 +00:00
import RecipeCard from './RecipeCard.vue';
2024-01-13 23:09:15 +00:00
export default {
2024-01-14 01:46:45 +00:00
name: 'RecipeSearchBox',
components: { RecipeCard },
props: {
placeholder: { type: String, default: 'Add a recipe...' }
},
2024-01-13 23:09:15 +00:00
data() {
return {
searchTerm: '',
2024-01-14 01:46:45 +00:00
recipes: [],
timeouts: [],
2024-01-13 23:09:15 +00:00
}
},
watch: {
searchTerm() {
const searchTerm = this.searchTerm;
2024-01-14 01:46:45 +00:00
if (searchTerm) {
this.timeouts.push(setTimeout(() => {
if (searchTerm === this.searchTerm) {
this.search();
}
}, 200));
}
2024-01-13 23:09:15 +00:00
}
},
methods: {
2024-01-17 09:17:22 +00:00
async search() {
2025-10-18 01:36:55 +00:00
this.recipes = await searchRecipes(this.searchTerm) ?? this.recipes;
2024-01-13 23:09:15 +00:00
},
selectRecipe(recipe) {
this.$emit('select-recipe', recipe);
this.searchTerm = '';
this.recipes = [];
}
}
}
</script>
<style scoped>
2024-01-14 01:46:45 +00:00
.recipe-search-box {
position: relative;
}
2024-01-13 23:09:15 +00:00
2024-01-14 01:46:45 +00:00
.recipe-search-box input {
2024-10-15 22:32:44 +00:00
width: calc(100% - 2em);
2024-01-14 01:46:45 +00:00
padding: 8px;
border: 1px solid #ccc;
border-radius: 4px;
font-size: 16px;
outline: none;
2024-01-13 23:09:15 +00:00
}
2024-01-14 01:46:45 +00:00
.recipe-search-box .dropdown {
position: absolute;
top: 100%;
width: 100%;
margin: auto;
background-color: #fff;
border: 1px solid #ccc;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
z-index: 1000;
2024-05-04 08:00:50 +00:00
2024-01-13 23:09:15 +00:00
list-style-type: none;
2024-01-14 01:46:45 +00:00
margin: 0;
padding: 0;
max-height: 40vh;
overflow-y: scroll;
2024-01-13 23:09:15 +00:00
}
2024-01-14 01:46:45 +00:00
.recipe-search-box .dropdown li {
padding: 8px;
cursor: pointer;
border-bottom: 1px solid #ccc;
2024-01-13 23:09:15 +00:00
}
2024-01-14 01:46:45 +00:00
.recipe-search-box .dropdown li:last-child {
border-bottom: none;
2024-01-13 23:09:15 +00:00
}
2024-01-14 01:46:45 +00:00
.recipe-search-box .dropdown li:hover {
background-color: #eee;
2024-01-13 23:09:15 +00:00
}
</style>