Skip to content

Commit d161d8d

Browse files
committed
bug #2401 [LiveComponent] Fix checkbox/radio value matching (smnandre)
This PR was merged into the 2.x branch. Discussion ---------- [LiveComponent] Fix checkbox/radio value matching | Q | A | ------------- | --- | Bug fix? | yes | New feature? | no | Issues | Fix #... | License | MIT It seems we let [some changes](b406997#diff-9e1dc5b957786f187f0ba493e27e3324bf16cac1129765adacfc2b26b41b6adbR82) 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. Commits ------- 9c4c0af [LiveComponent] Fix checkbox/radio value matching
2 parents de5b5b1 + 9c4c0af commit d161d8d

File tree

2 files changed

+18
-34
lines changed

2 files changed

+18
-34
lines changed

src/LiveComponent/assets/dist/live_controller.js

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -250,26 +250,18 @@ function setValueOnElement(element, value) {
250250
return;
251251
}
252252
if (element.type === 'radio') {
253-
element.checked = element.value === value;
253+
element.checked = element.value == value;
254254
return;
255255
}
256256
if (element.type === 'checkbox') {
257257
if (Array.isArray(value)) {
258-
let valueFound = false;
259-
value.forEach((val) => {
260-
if (val === element.value) {
261-
valueFound = true;
262-
}
263-
});
264-
element.checked = valueFound;
258+
element.checked = value.some((val) => val == element.value);
259+
}
260+
else if (element.hasAttribute('value')) {
261+
element.checked = element.value == value;
265262
}
266263
else {
267-
if (element.hasAttribute('value')) {
268-
element.checked = element.value === value;
269-
}
270-
else {
271-
element.checked = value;
272-
}
264+
element.checked = value;
273265
}
274266
return;
275267
}

src/LiveComponent/assets/src/dom_utils.ts

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -79,34 +79,26 @@ export function setValueOnElement(element: HTMLElement, value: any): void {
7979
}
8080

8181
if (element.type === 'radio') {
82-
element.checked = element.value === value;
82+
// biome-ignore lint/suspicious/noDoubleEquals: need fuzzy matching
83+
element.checked = element.value == value;
8384

8485
return;
8586
}
8687

8788
if (element.type === 'checkbox') {
8889
if (Array.isArray(value)) {
89-
// I'm purposely not using Array.includes here because it's
90-
// strict, and because of Numeric/String mis-casting, I
91-
// want the "includes" to be "fuzzy".
92-
let valueFound = false;
93-
value.forEach((val) => {
94-
if (val === element.value) {
95-
valueFound = true;
96-
}
97-
});
98-
99-
element.checked = valueFound;
90+
// Because of Numeric/String mis-casting,
91+
// we want the "includes" to be "fuzzy".
92+
// biome-ignore lint/suspicious/noDoubleEquals: need fuzzy matching
93+
element.checked = value.some((val) => val == element.value);
94+
} else if (element.hasAttribute('value')) {
95+
// if the checkbox has a value="", then check if it matches
96+
// biome-ignore lint/suspicious/noDoubleEquals: need fuzzy matching
97+
element.checked = element.value == value;
10098
} else {
101-
if (element.hasAttribute('value')) {
102-
// if the checkbox has a value="", then check if it matches
103-
element.checked = element.value === value;
104-
} else {
105-
// no value, treat it like a boolean
106-
element.checked = value;
107-
}
99+
// no value, treat it like a boolean
100+
element.checked = value;
108101
}
109-
110102
return;
111103
}
112104
}

0 commit comments

Comments
 (0)