jws-frontend/src/components/04_product-service/WorkNameManagement.vue

163 lines
4.2 KiB
Vue
Raw Normal View History

<script lang="ts" setup>
2024-06-21 11:06:35 +00:00
import { onMounted, ref, watch } from 'vue';
2024-06-27 05:04:30 +00:00
// import { useI18n } from 'vue-i18n';
// import { storeToRefs } from 'pinia';
2024-06-21 11:06:35 +00:00
2024-06-27 05:04:30 +00:00
// import useProductServiceStore from 'src/stores/product-service';
2024-06-21 11:06:35 +00:00
import NoData from '../NoData.vue';
2024-06-21 02:45:32 +00:00
2024-06-21 11:06:35 +00:00
const nameList = defineModel<{ id: string; name: string; isEdit: boolean }[]>(
'nameList',
{ required: true },
);
2024-06-21 02:45:32 +00:00
2024-06-21 11:06:35 +00:00
const cloneList = ref();
const isAdd = ref(false);
const inputName = ref<HTMLInputElement[]>([]);
2024-06-21 02:45:32 +00:00
2024-06-21 11:06:35 +00:00
function isWorkNameEdit() {
return cloneList.value.some((i: { isEdit: boolean }) => i.isEdit === true);
2024-06-21 02:45:32 +00:00
}
2024-06-21 11:06:35 +00:00
async function assignClone() {
cloneList.value = await JSON.parse(JSON.stringify(nameList.value));
2024-06-21 02:45:32 +00:00
}
2024-06-21 11:06:35 +00:00
defineExpose({
isWorkNameEdit,
});
defineEmits<{
(e: 'delete', id: string): void;
(e: 'edit', id: string, data: { name: string }): void;
(e: 'add', data: { name: string; productId: []; order: number }): void;
}>();
onMounted(async () => {
await assignClone();
});
watch(
() => nameList.value.length,
async () => {
await assignClone();
if (!isAdd.value) return;
cloneList.value[cloneList.value.length - 1].isEdit = true;
setTimeout(() => {
inputName.value[cloneList.value.length - 1].focus();
isAdd.value = false;
}, 150);
},
);
</script>
<template>
2024-06-21 11:06:35 +00:00
<div v-if="cloneList" class="full-width column no-wrap full-height">
2024-06-27 05:04:30 +00:00
<div class="bordered rounded surface-1 flex col column justify-between">
<q-list v-if="cloneList.length > 0" class="full-width col scroll">
2024-06-21 02:45:32 +00:00
<q-item
class="items-center row q-px-lg"
2024-06-21 11:06:35 +00:00
v-for="(item, index) in cloneList"
2024-06-21 02:45:32 +00:00
:key="index"
>
<q-input
2024-06-21 11:06:35 +00:00
ref="inputName"
2024-06-24 04:45:58 +00:00
:for="`input-work-name-${index}`"
dense
class="col q-mr-md"
2024-06-21 11:06:35 +00:00
v-model="item.name"
placeholder="ชื่องาน"
2024-06-21 11:06:35 +00:00
:borderless="!item.isEdit"
:readonly="!item.isEdit"
:outlined="item.isEdit"
></q-input>
<q-btn
2024-06-21 11:06:35 +00:00
v-if="!item.isEdit"
id="btn-edit-work-name"
icon="mdi-pencil-outline"
2024-06-21 11:06:35 +00:00
:disable="isWorkNameEdit()"
dense
flat
round
color="primary"
2024-06-21 11:06:35 +00:00
@click="item.isEdit = true"
>
2024-06-24 04:45:58 +00:00
<q-tooltip>{{ $t('edit') }}</q-tooltip>
</q-btn>
<q-btn
v-else
id="btn-edit-work-name"
icon="mdi-check"
dense
flat
round
color="primary"
2024-06-21 11:06:35 +00:00
@click="
() => {
$emit('edit', item.id, { name: item.name }),
(item.isEdit = false);
}
"
>
2024-06-24 04:45:58 +00:00
<q-tooltip>{{ $t('save') }}</q-tooltip>
</q-btn>
<q-btn
2024-06-21 11:06:35 +00:00
v-if="!item.isEdit"
id="btn-delete-work-name"
2024-06-21 11:06:35 +00:00
:disable="isWorkNameEdit()"
icon="mdi-trash-can-outline"
dense
flat
round
class="q-ml-md"
color="negative"
2024-06-21 11:06:35 +00:00
@click="$emit('delete', item.id)"
>
2024-06-24 04:45:58 +00:00
<q-tooltip>{{ $t('delete') }}</q-tooltip>
</q-btn>
<q-btn
v-else
id="btn-edit-work-name"
icon="mdi-undo"
dense
flat
round
class="q-ml-md"
color="negative"
2024-06-21 11:06:35 +00:00
@click="assignClone"
>
2024-06-24 04:45:58 +00:00
<q-tooltip>{{ $t('cancel') }}</q-tooltip>
</q-btn>
</q-item>
</q-list>
2024-06-27 05:04:30 +00:00
<div v-else class="flex col justify-center items-center">
<NoData />
</div>
<div class="bordered-t full-width">
2024-06-21 11:06:35 +00:00
<q-item
clickable
:disable="isWorkNameEdit()"
@click="
() => {
$emit('add', { name: '', productId: [], order: 1 }),
(isAdd = true);
}
"
>
<span class="q-px-lg flex items-center app-text-muted-2">
<q-icon name="mdi-plus" class="q-mr-md" />
2024-06-24 04:45:58 +00:00
{{ $t('addWork') }}
</span>
</q-item>
</div>
</div>
2024-06-27 05:04:30 +00:00
<!-- <div
v-else
class="bordered rounded surface-1 flex justify-center items-center col"
>
<NoData />
2024-06-27 05:04:30 +00:00
</div> -->
</div>
</template>