From 66fe246f5b65b5b486c01050ffe8140af8551745 Mon Sep 17 00:00:00 2001 From: winet Date: Thu, 7 Mar 2024 16:32:16 +1000 Subject: [PATCH] refactor tabs script --- app/blueprints/frontend/static/tabs.js | 29 +++++++++++++------------- 1 file changed, 14 insertions(+), 15 deletions(-) diff --git a/app/blueprints/frontend/static/tabs.js b/app/blueprints/frontend/static/tabs.js index dee11f5..4bdb78b 100644 --- a/app/blueprints/frontend/static/tabs.js +++ b/app/blueprints/frontend/static/tabs.js @@ -5,31 +5,30 @@ tabs_lists.forEach((tabs_list) => { tabs = [...tabs, ...tabs_list.querySelectorAll('[role="tab"]')]; }) +function setInputsState(section, disabled = false) { + section.querySelectorAll('input, textarea, button, select').forEach((el) => { + // console.log(el); + el.disabled = disabled; + }); +} + tabs.forEach((tab) => { tab.addEventListener('hide.bs.tab', event => { const id_content_hide = event.target.getAttribute('data-bs-target'); const id_content_show = event.relatedTarget.getAttribute('data-bs-target'); - console.log('==== Hide ===='); + // console.log('==== Hide ===='); const content_hide = document.querySelector(id_content_hide); - content_hide.querySelectorAll('input, textarea, button').forEach((el) => { - console.log(el); - el.disabled = true; - }); + setInputsState(content_hide, true); - console.log('==== Show ===='); + // console.log('==== Show ===='); const content_show = document.querySelector(id_content_show); - content_show.querySelectorAll('input, textarea, button').forEach((el) => { - console.log(el); - el.disabled = false; - }); + setInputsState(content_show); }); if (!tab.getAttribute('aria-selected')) { - const id_content_hide = tab.getAttribute('data-bs-target') - const content_hide = document.querySelector(`#${id_content_hide}`); - content_hide.querySelectorAll('input, textarea, button').forEach((el) => { - el.disabled = true; - }); + const id_content_hide = tab.getAttribute('data-bs-target'); + const content_hide = document.querySelector(id_content_hide); + setInputsState(content_hide, true); } }) \ No newline at end of file