File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 44 < meta charset ="UTF-8 " />
55 < meta name ="viewport " content ="width=device-width, initial-scale=1.0 " />
66 < title > handleRadio Demo</ title >
7+ < style >
8+ # status {
9+ background-color : red;
10+ color : white;
11+ padding : 2px 5px ;
12+ border-radius : 3px ;
13+ }
14+ </ style >
715 </ head >
816 < body >
917 < h1 > handleRadio Demo</ h1 >
@@ -76,14 +84,21 @@ <h3>Tool.list output:</h3>
7684 display . innerText = "dark (set via code)" ;
7785 } ) ;
7886
87+ const statusEl = document . getElementById ( "status" ) ;
88+ const updateStatus = ( ) => {
89+ statusEl . innerText = tool . getStatus ( ) ? "bound" : "unbound" ;
90+ } ;
91+
92+ updateStatus ( ) ;
93+
7994 document . getElementById ( "btn-unbind" ) . addEventListener ( "click" , ( ) => {
8095 tool . unbind ( ) ;
81- alert ( "Unbound! Clicking radios won't update the display anymore." ) ;
96+ updateStatus ( ) ;
8297 } ) ;
8398
8499 document . getElementById ( "btn-bind" ) . addEventListener ( "click" , ( ) => {
85100 tool . bind ( ) ;
86- alert ( "Bound! Clicking radios will now update the display." ) ;
101+ updateStatus ( ) ;
87102 } ) ;
88103 </ script >
89104
Original file line number Diff line number Diff line change @@ -31,6 +31,8 @@ export default function handleRadio(opt) {
3131 onChange = ( ) => { } ;
3232 }
3333
34+ let isBound = false ;
35+
3436 if ( ! delegateParent ) {
3537 delegateParent = document . body ;
3638 }
@@ -75,16 +77,21 @@ export default function handleRadio(opt) {
7577
7678 if ( autoBind ) {
7779 delegateParent . addEventListener ( "click" , delegateFn ) ;
80+ isBound = true ;
7881 }
7982
8083 tool = {
8184 unbind : ( ) => {
8285 delegateParent . removeEventListener ( "click" , delegateFn ) ;
86+ isBound = false ;
8387 } ,
8488 bind : ( ) => {
85- delegateParent . removeEventListener ( "click" , delegateFn ) ; // Prevent duplicates
86-
89+ delegateParent . removeEventListener ( "click" , delegateFn ) ;
8790 delegateParent . addEventListener ( "click" , delegateFn ) ;
91+ isBound = true ;
92+ } ,
93+ getStatus : ( ) => {
94+ return isBound ;
8895 } ,
8996 list : [ ...delegateParent . querySelectorAll ( s ) ] . reduce ( ( acc , el ) => {
9097 acc . push ( {
You can’t perform that action at this time.
0 commit comments