@@ -44,18 +44,17 @@ export class PfRadio extends LitElement {
4444 /** Radio groups: instances.get(groupName).forEach(pfRadio => { ... }) */
4545 private static instances = new Map < string , Set < PfRadio > > ( ) ;
4646 private static radioInstances = new Map < Node , Map < string , Set < PfRadio > > > ( ) ;
47-
4847 private static selected = new Map < Node , Map < string , PfRadio > > ( ) ;
4948
5049 static {
51- globalThis . addEventListener ( 'keydown' , e => {
50+ globalThis . addEventListener ( 'keydown' , ( e : KeyboardEvent ) => {
5251 switch ( e . key ) {
5352 case 'Tab' :
5453 this . radioInstances . forEach ( ( radioGroup , parentNode ) => {
5554 radioGroup . forEach ( ( radioSet , groupName ) => {
5655 const selectedNode = this . selected . get ( parentNode ) ;
5756 const selected = selectedNode ?. get ( groupName ) ;
58- [ ...radioSet ] . forEach ( ( radio , i , radios ) => {
57+ [ ...radioSet ] . forEach ( ( radio : PfRadio , i : number , radios : PfRadio [ ] ) => {
5958 // the radio group has a selected element
6059 // it should be the only focusable member of the group
6160 radio . focusable = false ;
@@ -87,21 +86,19 @@ export class PfRadio extends LitElement {
8786 let radioGroup : NodeListOf < PfRadio > ;
8887 if ( root instanceof Document || root instanceof ShadowRoot ) {
8988 radioGroup = root . querySelectorAll ( 'pf-radio' ) ;
90- // let radioGroupArray: any[] = [];
9189 radioGroup . forEach ( ( radio : PfRadio ) => {
92- if ( radio . parentNode === this . parentNode && radio . name === this . name ) {
93- // radioGroupArray.push(radio);
94- let map = PfRadio . radioInstances . get ( this . parentNode as HTMLElement ) ;
95- if ( ! map ) {
96- map = new Map < string , Set < PfRadio > > ( ) ;
97- PfRadio . radioInstances . set ( this . parentNode as HTMLElement , map ) ;
90+ if ( radio . parentNode && radio . parentNode === this . parentNode && radio . name === this . name ) {
91+ let radioGroupMap = PfRadio . radioInstances . get ( radio . parentNode ) ;
92+ if ( ! radioGroupMap ) {
93+ radioGroupMap = new Map < string , Set < PfRadio > > ( ) ;
94+ PfRadio . radioInstances . set ( radio . parentNode , radioGroupMap ) ;
9895 }
99- let set = map . get ( this . name ) ;
100- if ( ! set ) {
101- set = new Set < PfRadio > ( ) ;
102- map . set ( this . name , set ) ;
96+ let radioSet : Set < PfRadio > | undefined = radioGroupMap . get ( this . name ) ;
97+ if ( ! radioSet ) {
98+ radioSet = new Set < PfRadio > ( ) ;
99+ radioGroupMap . set ( this . name , radioSet ) ;
103100 }
104- set . add ( radio ) ;
101+ radioSet . add ( radio ) ;
105102 }
106103 } ) ;
107104 }
@@ -132,6 +129,12 @@ export class PfRadio extends LitElement {
132129
133130 disconnectedCallback ( ) : void {
134131 PfRadio . instances . get ( this . name ) ?. delete ( this ) ;
132+ if ( this . parentNode ) {
133+ const parentNode = PfRadio . radioInstances . get ( this . parentNode ) ;
134+ if ( parentNode ) {
135+ PfRadio . radioInstances . delete ( this . parentNode ) ;
136+ }
137+ }
135138 super . disconnectedCallback ( ) ;
136139 }
137140
@@ -140,13 +143,13 @@ export class PfRadio extends LitElement {
140143 PfRadio . radioInstances . forEach ( ( radioGroup , parentNode ) => {
141144 if ( parentNode === this . parentNode ) {
142145 radioGroup . forEach ( ( radioSet , groupName ) => {
143- if ( groupName === this . name ) {
146+ if ( this . parentNode && groupName === this . name ) {
144147 [ ...radioSet ] . forEach ( radio => {
145148 radio . checked = false ;
146149 } ) ;
147150 this . checked = true ;
148151 this . dispatchEvent ( new PfRadioChangeEvent ( event , this . value ) ) ;
149- this . #updateSelected( this . parentNode as HTMLElement , this , this . name ) ;
152+ this . #updateSelected( this . parentNode , this , this . name ) ;
150153 }
151154 } ) ;
152155 }
@@ -174,7 +177,7 @@ export class PfRadio extends LitElement {
174177 if ( groupName === this . name ) {
175178 this . checked = false ;
176179 [ ...radioSet ] . forEach ( ( radio : PfRadio , index : number , radios : PfRadio [ ] ) => {
177- if ( radio === event . target ) {
180+ if ( this . parentNode && radio === event . target ) {
178181 const isArrowDownOrRight : boolean =
179182 [ 'ArrowDown' , 'ArrowRight' ] . includes ( event . key ) ;
180183 const isArrowUpOrLeft : boolean = [ 'ArrowUp' , 'ArrowLeft' ] . includes ( event . key ) ;
@@ -189,8 +192,7 @@ export class PfRadio extends LitElement {
189192 // consider the api of this event.
190193 // do we add the group to it? do we fire from every element on every change?
191194 this . dispatchEvent ( new PfRadioChangeEvent ( event , radios [ nextIndex ] . value ) ) ;
192- this . #updateSelected( this . parentNode as HTMLElement ,
193- radios [ nextIndex ] , radios [ nextIndex ] . name ) ;
195+ this . #updateSelected( this . parentNode , radios [ nextIndex ] , radios [ nextIndex ] . name ) ;
194196 }
195197 } ) ;
196198 }
0 commit comments