munch-ease-frontend/src/components/meals/PersonList.vue

171 lines
4.4 KiB
Vue
Raw Normal View History

2024-05-04 04:22:37 +00:00
<template>
<span class="person-list">
<span v-for="person in people" :key="person.id">
<button class="person-circle remove-person" @click="$emit('remove-person', person)" >{{ person.name }}</button>
</span>
<span>
<button v-if="!isAddingPerson" class="person-circle add-person" @click="isAddingPerson = true">+</button>
2024-05-04 08:00:50 +00:00
<input v-else v-model="searchName" ref="searchNameInput" @keyup.enter="addPerson" @keyup.esc="isAddingPerson = false" @blur="isAddingPerson = false" />
<ul class="person-droplist" ref="persondroplist" v-if="isAddingPerson && searchResults.length">
2024-05-04 04:22:37 +00:00
<li v-for="person in searchResults" :key="person.id">
2024-05-04 08:00:50 +00:00
<button class="person-circle add-person" @mousedown="addPerson(person)">{{ person.name }}</button>
2024-05-04 04:22:37 +00:00
</li>
</ul>
</span>
</span>
</template>
<style scoped>
.person-list {
display: inline-block;
text-align: center;
min-height: 50px;
padding: 0;
margin: 0;
}
/* Remove all the button styling */
.person-circle {
background: none;
border: none;
padding: 0;
margin: 0;
}
/* Show the initials of the person in a circle */
.person-circle {
display: inline-block;
width: 50px;
height: 50px;
line-height: 50px;
text-align: center;
border-radius: 50%;
background: #eee;
}
/* On hover, prompt the user to click to remove the person */
/* change opacity of circle, and use css to add a large cross over the circle */
.remove-person:hover {
cursor: pointer;
position: relative;
background-color: lightcoral;
}
/* Add a cross to the circle */
.remove-person:hover::before, .remove-person:hover::after {
pointer-events: none;
content: "X";
color: white;
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
}
.add-person {
background-color: lightgreen;
}
.add-person:hover {
cursor: pointer;
font-weight: bolder;
color: white;
background-color: green;
}
.person-droplist {
position: absolute;
background-color: white;
border: 1px solid #ccc;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
list-style-type: none;
padding: 0;
margin: 0;
max-height: 40vh;
overflow-y: scroll;
2024-05-04 08:00:50 +00:00
z-index: 1000;
2024-05-04 04:22:37 +00:00
}
.person-droplist li {
padding: 8px;
cursor: pointer;
display: inline;
padding: 1ex 1em;
}
</style>
<script>
2024-05-04 08:00:50 +00:00
import { ref } from 'vue';
2024-05-04 04:22:37 +00:00
import data from '@/data.js'
export default {
name: 'PersonList',
props: {
people: {
type: Array,
default: () => []
}
},
data() {
return {
isAddingPerson: false,
searchName: '',
searchResults: [],
}
},
2024-05-04 08:00:50 +00:00
setup() {
const searchNameInput = ref(null);
const persondroplist = ref(null);
return { searchNameInput, persondroplist };
},
2024-05-04 04:22:37 +00:00
watch: {
searchName: async function() {
2024-05-04 08:00:50 +00:00
await this.updateSearchResults()
},
searchNameInput: async function() {
this.searchNameInput?.focus();
await this.updateSearchResults()
},
persondroplist: function() {
if (this.persondroplist && this.searchNameInput)
{
// Align the droplist to the input field & its size
const inputRect = this.searchNameInput.getBoundingClientRect();
this.persondroplist.style.left = `${inputRect.left}px`;
this.persondroplist.style.top = `${inputRect.bottom}px`;
this.persondroplist.style.width = `${inputRect.width}px`;
}
2024-05-04 04:22:37 +00:00
}
},
methods: {
2024-05-04 08:00:50 +00:00
async updateSearchResults() {
const results = await data.searchPerson(this.searchName);
// Exclude people already in the list
const idSet = new Set(this.people.map(p => p.id));
this.searchResults = results.filter(p => !idSet.has(p.id));
},
2024-05-04 04:22:37 +00:00
addPerson(person) {
2024-05-20 12:16:39 +00:00
if (!person?.id >= 0 && this.searchResults.length > 0)
2024-05-04 04:22:37 +00:00
{
person = this.searchResults[0];
}
2024-05-20 12:16:39 +00:00
if (person?.id >= 0 && !this.people.find(p => p.id === person.id))
2024-05-04 04:22:37 +00:00
{
this.$emit('add-person', person);
}
2024-05-04 08:00:50 +00:00
2024-05-04 04:22:37 +00:00
this.searchName = '';
this.searchResults = [];
this.isAddingPerson = false;
},
removePerson(person) {
this.$emit('remove-person', person);
},
}
}
</script>