Skip to content

Commit 33ab71e

Browse files
committed
radio
1 parent c9d696b commit 33ab71e

3 files changed

Lines changed: 21 additions & 22 deletions

File tree

pages/html/handleRadio.html

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,11 @@ <h1>handleRadio Demo</h1>
2929
<hr />
3030

3131
<button id="btn-dark">Select Dark via Code</button>
32-
<button id="btn-unbind">Unbind (Stop Listening)</button>
32+
| | |
3333
<button id="btn-bind">Bind (Start Listening)</button>
34-
<span>status: <span id="status">idle</span></span>
34+
<button id="btn-unbind">Unbind (Stop Listening)</button>
35+
| | |
36+
<span id="status">idle</span>
3537

3638
<hr />
3739

@@ -51,19 +53,21 @@ <h3>Tool.list output:</h3>
5153
initState: "system",
5254
initTrigger: true,
5355
autoBind: false, // this will tell not to bind automatically
54-
onChange: (value, tool) => {
56+
onChange: (value, t) => {
5557
display.innerText = value;
5658
console.log("Changed to:", value);
57-
58-
// Update the list output to show the live state
59-
listOutput.innerText = JSON.stringify(
60-
tool.list.map((i) => ({ value: i.value, checked: i.el.checked })),
61-
null,
62-
2
63-
);
59+
updateListOutput(t);
6460
},
6561
});
6662

63+
function updateListOutput(t = tool) {
64+
listOutput.innerText = JSON.stringify(
65+
t.list.map((i) => ({ value: i.value, checked: i.checked })),
66+
null,
67+
2
68+
);
69+
}
70+
6771
document.getElementById("btn-dark").addEventListener("click", () => {
6872
tool.checkByValue("dark");
6973
// Note: tool.checkByValue just sets checked = true,
@@ -74,12 +78,7 @@ <h3>Tool.list output:</h3>
7478
// Let's see if we should trigger onChange manually or if the user expects it.
7579
// handleRadio.js doesn't seem to have a way to trigger onChange programmatically besides initTrigger.
7680

77-
// Re-render list to show change
78-
listOutput.innerText = JSON.stringify(
79-
tool.list.map((i) => ({ value: i.value, checked: i.el.checked })),
80-
null,
81-
2
82-
);
81+
updateListOutput();
8382
// Since checkByValue doesn't trigger click, we manually update display here for the demo
8483
display.innerText = "dark (set via code)";
8584
});
@@ -90,6 +89,7 @@ <h3>Tool.list output:</h3>
9089
};
9190

9291
updateStatus();
92+
updateListOutput();
9393

9494
document.getElementById("btn-unbind").addEventListener("click", () => {
9595
tool.unbind();

pages/html/handleRadio.js

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -93,15 +93,13 @@ export default function handleRadio(opt) {
9393
getStatus: () => {
9494
return isBound;
9595
},
96-
list: [...delegateParent.querySelectorAll(s)].reduce((acc, el) => {
97-
acc.push({
96+
get list() {
97+
return [...delegateParent.querySelectorAll(s)].map((el) => ({
9898
el,
9999
checked: el.checked,
100100
value: el.value,
101-
});
102-
103-
return acc;
104-
}, []),
101+
}));
102+
},
105103
checkByValue: (value) => {
106104
const selector = selectorOne(name, value);
107105

pages/html/index.template.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,7 @@ <h2>input[type="color"]</h2>
169169

170170
<div class="cards">
171171
<h2>input[type="radio"]</h2>
172+
<p>See also <a href="handleRadio.html">handleRadio.js example</a></p>
172173

173174
<div class="radios">
174175
<label> <input type="radio" name="mode" value="one" /> one </label>

0 commit comments

Comments
 (0)