parsley/app/javascript/components/RecipeEditIngredientItem.vue

109 lines
3.2 KiB
Vue
Raw Normal View History

2018-04-01 21:43:23 -05:00
<template>
2018-05-01 10:55:57 -05:00
<div class="columns is-mobile edit-ingredient-item">
2018-04-01 21:43:23 -05:00
<div class="column">
<div class="columns is-multiline is-mobile">
<div class="column is-half-mobile is-2-tablet">
<span class="label is-small-mobile" v-if="showLabels">Quantity</span>
<input class="input is-small-mobile" type="text" v-model="ingredient.quantity">
</div>
<div class="column is-half-mobile is-3-tablet">
<span class="label is-small-mobile" v-if="showLabels">Units</span>
<input class="input is-small-mobile" type="text" v-model="ingredient.units">
</div>
<div class="column is-half-mobile is-3-tablet">
<span class="label is-small-mobile" v-if="showLabels">Name</span>
<app-autocomplete
:inputClass="{'is-small-mobile': true, 'is-success': ingredient.ingredient_id !== null}"
ref="autocomplete"
v-model="ingredient.name"
:minLength="2"
valueAttribute="name"
labelAttribute="name"
placeholder=""
2018-04-01 22:32:13 -05:00
@inputClick="nameClick"
2018-04-01 21:43:23 -05:00
@optionSelected="searchItemSelected"
:onGetOptions="updateSearchItems"
>
</app-autocomplete>
</div>
<div class="column is-half-mobile is-4-tablet">
<span class="label is-small-mobile" v-if="showLabels">Preparation</span>
<input class="input is-small-mobile" type="text" v-model="ingredient.preparation">
</div>
</div>
</div>
<div class="column is-narrow">
<span class="label is-small-mobile" v-if="showLabels">&nbsp;</span>
2018-09-11 22:56:26 -05:00
<button type="button" class="button is-danger is-small" @click="deleteFood(ingredient)">
2018-05-01 10:55:57 -05:00
<app-icon icon="x" size="md"></app-icon>
2018-04-03 18:31:20 -05:00
</button>
2018-04-01 21:43:23 -05:00
</div>
</div>
</template>
2024-10-02 14:34:50 -05:00
<script setup>
2018-04-01 21:43:23 -05:00
2024-10-02 14:34:50 -05:00
import { useTemplateRef, watch } from "vue";
2018-04-01 21:43:23 -05:00
import api from "../lib/Api";
2024-10-02 14:34:50 -05:00
const emit = defineEmits(["deleteFood"]);
const props = defineProps({
ingredient: {
required: true,
type: Object
2018-04-01 21:43:23 -05:00
},
2024-10-02 14:34:50 -05:00
showLabels: {
required: false,
type: Boolean,
default: false
}
});
2018-04-01 21:43:23 -05:00
2024-10-02 14:34:50 -05:00
const autocompleteElement = useTemplateRef("autocomplete");
2018-04-01 21:43:23 -05:00
2024-10-02 16:20:07 -05:00
watch(props.ingredient, (val) => {
2024-10-02 14:34:50 -05:00
if (props.ingredient.ingredient && props.ingredient.ingredient.name !== val) {
props.ingredient.ingredient_id = null;
props.ingredient.ingredient = null;
}
});
function deleteFood(ingredient) {
emit("deleteFood", ingredient);
}
function updateSearchItems(text) {
return api.getSearchIngredients(text);
}
function searchItemSelected(ingredient) {
props.ingredient.ingredient_id = ingredient.id;
props.ingredient.ingredient = ingredient;
props.ingredient.name = ingredient.name;
}
2018-04-01 21:43:23 -05:00
2024-10-02 14:34:50 -05:00
function nameClick() {
if (props.ingredient.ingredient_id === null && props.ingredient.name !== null && props.ingredient.name.length > 2) {
autocompleteElement.updateOptions(props.ingredient.name);
2018-04-01 21:43:23 -05:00
}
}
</script>
<style lang="scss" scoped>
2018-05-01 10:55:57 -05:00
2024-09-29 09:44:40 -05:00
@use "bulma/sass/utilities" as bulma;
2018-05-01 10:55:57 -05:00
.edit-ingredient-item {
2024-09-29 09:44:40 -05:00
border-bottom: solid 1px bulma.$grey-light;
2018-05-01 10:55:57 -05:00
margin-bottom: 1.25rem;
&:last-child {
border-bottom: none;
}
}
2018-04-01 21:43:23 -05:00
</style>