@@ -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 ( ) ;
0 commit comments