From 6f298a4df48b1e403090777127dead48cdb52079 Mon Sep 17 00:00:00 2001 From: marzban-dev Date: Fri, 21 Feb 2025 19:58:20 +0330 Subject: [PATCH] Updated --- .../global/product-detail/QuantityCounter.vue | 67 +++++++++++-------- 1 file changed, 38 insertions(+), 29 deletions(-) diff --git a/frontend/components/global/product-detail/QuantityCounter.vue b/frontend/components/global/product-detail/QuantityCounter.vue index eab8fbb..388d0d1 100644 --- a/frontend/components/global/product-detail/QuantityCounter.vue +++ b/frontend/components/global/product-detail/QuantityCounter.vue @@ -10,49 +10,58 @@ type Props = { // props const props = defineProps(); -const { modelValue } = toRefs(props); +const { modelValue, max } = toRefs(props); + +// state + +const timer = ref(null); // emit const emit = defineEmits(["update:modelValue"]); -// state +// computed -const currentQuantity = ref(modelValue.value); +const currentQuantity = computed({ + get: () => modelValue.value ?? 0, + set: (value: number) => { + if (timer.value) clearTimeout(timer.value); + timer.value = setTimeout(() => { + emit("update:modelValue", value); + }, 50); + } +}); // methods const onInput = (e: any) => { - currentQuantity.value = Number(e.target.value); + const value = Number(e.target.value); + if (value > 0 && value <= max.value) { + currentQuantity.value = value; + } else { + currentQuantity.value = 1; + } }; -// watch - -watch(() => currentQuantity.value, (newValue) => { - emit("update:modelValue", newValue); -}); - \ No newline at end of file