ldc-legaulois/resources/ts/bo/components/ListItemComponent.vue
2024-04-04 12:34:11 +02:00

217 lines
5.8 KiB
Vue

<template>
<div
:class="`list-item-component list-item-${inputName}`"
ref="listItem"
>
<table
v-if="itemList.length > 0"
class="table text-center table-sm table-hover table-bordered m-0"
>
<thead>
<tr>
<th
v-for="(column, columnIndex) of itemColumns"
:key="columnIndex"
scope="col"
>
{{ column[1] }}
</th>
<th
scope="col"
v-if="displayBtnRemove"
class="table-action"
>
<!-- Laisser vide -->
</th>
</tr>
</thead>
<tbody class="table-group-divider">
<tr
v-for="(item, itemIndex) of itemList"
:key="itemIndex"
>
<td
class="align-bottom"
v-for="(column, columnIndex) of itemColumns"
:key="columnIndex"
>
<p class="text-danger m-0">
{{
parseValidationInput(
`${inputName}.${String(itemIndex)}.${column[0]}`,
allErrors
)
}}
</p>
<input
class="form-control form-control-sm"
:name="`${inputName}[${String(itemIndex)}][${column[0]}]`"
:type="itemType[columnIndex]"
v-model="item[column[0] as keyof typeof item]"
:placeholder="inputPlaceholder[columnIndex]"
:min="itemType[columnIndex] == 'number' ? '0' : undefined"
>
</td>
<td
v-if="displayBtnRemove"
class="align-bottom"
>
<button
@click.prevent="removeLine(itemIndex)"
class="btn btn-sm btn-danger"
:title="__('Supprimer la ligne')"
data-bs-toggle="tooltip"
>
<i class="fa-solid fa-trash" />
</button>
</td>
</tr>
</tbody>
</table>
<div v-else>
<p>{{ __("Aucun item renseigné(s).") }}</p>
</div>
<div
v-if="displayBtnAdd"
class="text-center mt-1"
>
<button
@click.prevent="addLine"
class="btn btn-sm btn-primary"
type="button"
:title="__('Ajouter une ligne')"
data-bs-toggle="tooltip"
>
<i class="fa-solid fa-plus" />
</button>
</div>
</div>
</template>
<script lang="ts">
import { defineComponent } from "vue";
import parseLaravelErrorMessages from "../../modules/parseLaravelErrorMessages";
import trans from "../../modules/trans";
import { Tooltips } from "./../../modules/bsTooltips";
export default defineComponent({
mixins: [trans, parseLaravelErrorMessages],
data(): {
itemList: Array<Object>;
itemColumns: Array<string[]>;
itemLimit: Array<number>;
inputName: string;
itemType: Array<string>;
inputPlaceholder: Array<string>;
allErrors: Record<string, Array<string>>;
displayBtnAdd: boolean;
displayBtnRemove: boolean;
tooltips: Tooltips|null;
} {
return {
itemList: [{}],
itemColumns: [[""]],
itemLimit: [0, 0],
inputName: "",
itemType: [""],
inputPlaceholder: [""],
allErrors: {},
displayBtnAdd: true,
displayBtnRemove: true,
tooltips: null,
};
},
mounted() {
const json = String(this.$attrs.json ?? "{}");
const data = JSON.parse(json);
this.itemList = data.itemList;
this.itemColumns = data.itemColumns;
this.itemType = data.itemType;
this.itemLimit = data.itemLimit;
this.inputName = data.inputName;
this.inputPlaceholder = data.inputPlaceholder;
this.allErrors = data.errors ?? {};
this.setDefaultLines();
this.checkLengthList();
this.$nextTick(() => {
this.tooltips = Tooltips.make({
type: "dom",
elements: (this.$refs.listItem as HTMLDivElement).querySelectorAll("[data-bs-toggle=\"tooltip\"]")
});
});
},
methods: {
/**
* Set default line(s)
* according to the minimum limit defined.
*/
setDefaultLines() {
if (this.itemList.length < 1) {
for (let index = 0; index < this.itemLimit[0]; index++) {
this.addLine();
}
}
},
/**
* Add a new line to the <table>,
* add a new element to the array,
* show remove line button.
*/
addLine() {
this.itemList.push({});
if (this.displayBtnRemove == false) {
this.displayBtnRemove = true;
}
this.checkLengthList();
setTimeout(() => {
this.tooltips?.closeBootstrapTooltip();
this.tooltips = Tooltips.make({
type: "dom",
elements: (this.$refs.listItem as HTMLDivElement)
.querySelectorAll("[data-bs-toggle=\"tooltip\"]")
});
}, 500);
},
/**
* Remove a specific line from the <table>,
* remove a specific element to the array,
* show adding line button.
*/
removeLine(itemIndex: number) {
this.itemList.splice(itemIndex, 1);
if (this.displayBtnAdd == false) {
this.displayBtnAdd = true;
}
this.checkLengthList();
setTimeout(() => {
this.tooltips?.closeBootstrapTooltip();
this.tooltips = Tooltips.make({
type: "dom",
elements: (this.$refs.listItem as HTMLDivElement)
.querySelectorAll("[data-bs-toggle=\"tooltip\"]")
});
}, 500);
},
/**
* Check the length of the list,
* update variables accordingly.
*/
checkLengthList() {
/** Length minimum */
if (this.itemList.length <= this.itemLimit[0]) {
this.displayBtnRemove = false;
}
/** Length maximum */
if (this.itemList.length >= this.itemLimit[1]) {
this.displayBtnAdd = false;
}
},
},
});
</script>
<style lang="scss">
.list-item-component .table-action {
width: 2rem;
}
</style>