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

139 lines
3.7 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-08-26 16:24:08 +07:00
import { DeleteButton, EditButton, SaveButton, UndoButton } from '../button';
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-08-09 15:06:41 +07:00
// import useProductServiceStore from '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
lazy-rules="ondemand"
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>
2024-08-26 16:24:08 +07:00
<EditButton
2024-06-21 11:06:35 +00:00
v-if="!item.isEdit"
2024-08-26 16:24:08 +07:00
icon-only
id="btn-edit-work-name"
2024-08-26 16:24:08 +07:00
:disabled="isWorkNameEdit()"
2024-06-21 11:06:35 +00:00
@click="item.isEdit = true"
2024-08-26 16:24:08 +07:00
/>
<SaveButton
v-else
2024-08-26 16:24:08 +07:00
icon-only
id="btn-save-work-name"
2024-06-21 11:06:35 +00:00
@click="
() => {
$emit('edit', item.id, { name: item.name }),
(item.isEdit = false);
}
"
2024-08-26 16:24:08 +07:00
/>
<DeleteButton
2024-06-21 11:06:35 +00:00
v-if="!item.isEdit"
2024-08-26 16:24:08 +07:00
icon-only
id="btn-delete-work-name"
2024-08-26 16:24:08 +07:00
:disabled="isWorkNameEdit()"
2024-06-21 11:06:35 +00:00
@click="$emit('delete', item.id)"
2024-08-26 16:24:08 +07:00
/>
<UndoButton
v-else
2024-08-26 16:24:08 +07:00
icon-only
id="btn-undo-work-name"
2024-06-21 11:06:35 +00:00
@click="assignClone"
2024-08-26 16:24:08 +07:00
/>
</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-08-26 16:24:08 +07:00
{{ $t('productService.service.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>