44 lines
1.1 KiB
JavaScript
44 lines
1.1 KiB
JavaScript
export function useNumericKeypad(initialValue = '1') {
|
|
const { ref } = Vue;
|
|
const quantity = ref(initialValue);
|
|
|
|
const appendDigit = (digit) => {
|
|
if (digit === '.' && quantity.value.includes('.')) return;
|
|
if (quantity.value === '0' && digit !== '.') {
|
|
quantity.value = digit;
|
|
} else {
|
|
quantity.value += digit;
|
|
}
|
|
};
|
|
|
|
const deleteDigit = () => {
|
|
quantity.value = quantity.value.length > 1 ? quantity.value.slice(0, -1) : '0';
|
|
};
|
|
|
|
const clearQuantity = () => {
|
|
quantity.value = '0';
|
|
};
|
|
|
|
const setQuantity = (value) => {
|
|
quantity.value = String(value);
|
|
};
|
|
|
|
const increment = (amount = 1) => {
|
|
quantity.value = String(Math.max(1, parseFloat(quantity.value) + amount));
|
|
};
|
|
|
|
const decrement = (amount = 1) => {
|
|
quantity.value = String(Math.max(1, parseFloat(quantity.value) - amount));
|
|
};
|
|
|
|
return {
|
|
quantity,
|
|
appendDigit,
|
|
deleteDigit,
|
|
clearQuantity,
|
|
setQuantity,
|
|
increment,
|
|
decrement
|
|
};
|
|
}
|