From e3dba6d72ebdaa6f975e76e6268e5480c83d7ae0 Mon Sep 17 00:00:00 2001 From: kinoppyd Date: Fri, 17 May 2024 14:30:37 +0900 Subject: [PATCH] Switch tab when tab date is today --- .../controllers/schedule_table_controller.js | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/app/javascript/controllers/schedule_table_controller.js b/app/javascript/controllers/schedule_table_controller.js index 40d16768..a58be64f 100644 --- a/app/javascript/controllers/schedule_table_controller.js +++ b/app/javascript/controllers/schedule_table_controller.js @@ -11,9 +11,19 @@ export default class extends Controller { return; } + if (id === '') { - tables[0].classList.remove('hidden'); - buttons[0].classList.add('tab-btn-active'); + const current = new Date(); + const today = `${current.getFullYear()}-${("0"+(current.getMonth() + 1)).slice(-2)}-${current.getDate()}`; + var index = [...buttons].findIndex((button) => button.value.toString() === today ); + + if (index === -1) { + index = 0 + } + + tables[index].classList.remove('hidden'); + buttons[index].classList.add('tab-btn-active'); + return; }