From 6c2045eaef7de47a916a78bbf4c0ef8f7b784d03 Mon Sep 17 00:00:00 2001 From: Ye Jun Wu Date: Fri, 16 Jul 2021 15:31:24 +0200 Subject: [PATCH] fix: format to max hours on blur instead of change --- components/records/weekly-timesheet-totals-row.vue | 2 +- helpers/timesheet.ts | 13 +++++-------- 2 files changed, 6 insertions(+), 9 deletions(-) diff --git a/components/records/weekly-timesheet-totals-row.vue b/components/records/weekly-timesheet-totals-row.vue index f2cc4c43..6e25ce9c 100644 --- a/components/records/weekly-timesheet-totals-row.vue +++ b/components/records/weekly-timesheet-totals-row.vue @@ -62,7 +62,7 @@ export default defineComponent({ .reduce((prevValue, value) => +prevValue + +value) }); - return total === 0 ? '0' : floatToTotalTimeString(total); + return floatToTotalTimeString(total); }); const weekTheoreticalTotal = computed(() => { diff --git a/helpers/timesheet.ts b/helpers/timesheet.ts index 4ff0ba34..3930d538 100644 --- a/helpers/timesheet.ts +++ b/helpers/timesheet.ts @@ -184,7 +184,7 @@ export function floatTo24TimeString(float: number) { n.setMinutes(Math.round(float * 60)); const result = n.toTimeString() - return result !== "Invalid Date" ? result.slice(0, 5) : ''; + return result !== "Invalid Date" ? result.slice(0, 5) : '0'; } export function floatToTotalTimeString(float: number) { @@ -193,7 +193,7 @@ export function floatToTotalTimeString(float: number) { ? +hoursMinutes[0] > 10 ? hoursMinutes[0] : `0${hoursMinutes[0]}` - : ''; + : '00'; const formattedMinutes = Math.round(parseFloat(`0.${hoursMinutes[1]}`) * 60); const minutes = @@ -225,16 +225,13 @@ export function timesheetFormatter(min: number, max: number) { formatter: (value: string, e: InputEvent) => { const formatted = value.replace(/[^0-9.,:]+|\.(?=.*\.)/g, ""); - if (formatted) { - const num = +value; + if (e.type === "blur") { + const numString = formatted.replace(",", "."); + const num = +numString; if (num < min) return `0`; if (num >= max) return `${max}:00`; - } - const numString = formatted.replace(",", "."); - - if (e.type === "blur") { if (numString.match(/^[0]+$/) || numString === '.') return "0"; if (!numString.match(/[:]/)) { return floatTo24TimeString(+numString);