From 9c4c0aff2631598e79ea19910e1af28c88a9027c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20Andr=C3=A9?= Date: Thu, 21 Nov 2024 00:29:19 +0100 Subject: [PATCH] [LiveComponent] Fix checkbox/radio value matching It seems we let some changes appear using linter/formater.. and this time the fuzzy comparaison was intended. This PR revert those changes to bring back some flexibility and better value matching. --- .../assets/dist/live_controller.js | 20 ++++-------- src/LiveComponent/assets/src/dom_utils.ts | 32 +++++++------------ 2 files changed, 18 insertions(+), 34 deletions(-) diff --git a/src/LiveComponent/assets/dist/live_controller.js b/src/LiveComponent/assets/dist/live_controller.js index 94c9f8dd0ca..1b9f7568a9d 100644 --- a/src/LiveComponent/assets/dist/live_controller.js +++ b/src/LiveComponent/assets/dist/live_controller.js @@ -250,26 +250,18 @@ function setValueOnElement(element, value) { return; } if (element.type === 'radio') { - element.checked = element.value === value; + element.checked = element.value == value; return; } if (element.type === 'checkbox') { if (Array.isArray(value)) { - let valueFound = false; - value.forEach((val) => { - if (val === element.value) { - valueFound = true; - } - }); - element.checked = valueFound; + element.checked = value.some((val) => val == element.value); + } + else if (element.hasAttribute('value')) { + element.checked = element.value == value; } else { - if (element.hasAttribute('value')) { - element.checked = element.value === value; - } - else { - element.checked = value; - } + element.checked = value; } return; } diff --git a/src/LiveComponent/assets/src/dom_utils.ts b/src/LiveComponent/assets/src/dom_utils.ts index 7454cf28c3d..d096f6d14e5 100644 --- a/src/LiveComponent/assets/src/dom_utils.ts +++ b/src/LiveComponent/assets/src/dom_utils.ts @@ -79,34 +79,26 @@ export function setValueOnElement(element: HTMLElement, value: any): void { } if (element.type === 'radio') { - element.checked = element.value === value; + // biome-ignore lint/suspicious/noDoubleEquals: need fuzzy matching + element.checked = element.value == value; return; } if (element.type === 'checkbox') { if (Array.isArray(value)) { - // I'm purposely not using Array.includes here because it's - // strict, and because of Numeric/String mis-casting, I - // want the "includes" to be "fuzzy". - let valueFound = false; - value.forEach((val) => { - if (val === element.value) { - valueFound = true; - } - }); - - element.checked = valueFound; + // Because of Numeric/String mis-casting, + // we want the "includes" to be "fuzzy". + // biome-ignore lint/suspicious/noDoubleEquals: need fuzzy matching + element.checked = value.some((val) => val == element.value); + } else if (element.hasAttribute('value')) { + // if the checkbox has a value="", then check if it matches + // biome-ignore lint/suspicious/noDoubleEquals: need fuzzy matching + element.checked = element.value == value; } else { - if (element.hasAttribute('value')) { - // if the checkbox has a value="", then check if it matches - element.checked = element.value === value; - } else { - // no value, treat it like a boolean - element.checked = value; - } + // no value, treat it like a boolean + element.checked = value; } - return; } }