@@ -30,16 +30,19 @@ export class PfRadio extends LitElement {
3030 @property ( { reflect : true } ) name = 'radio-test' ;
3131 @property ( { reflect : true } ) label ?: string ;
3232 @property ( { reflect : true } ) value = '' ;
33+ @property ( { reflect : true } ) id = '' ;
3334
3435 constructor ( ) {
3536 super ( ) ;
3637 }
3738
3839 connectedCallback ( ) : void {
3940 super . connectedCallback ( ) ;
41+ this . addEventListener ( 'keydown' , this . #onKeydown) ;
42+ document . addEventListener ( 'keydown' , this . #onKeyPress) ;
4043 }
4144
42- #onRadioButtonClick( event : Event ) {
45+ #onRadioButtonClick( ) {
4346 if ( ! this . checked ) {
4447 const root : Node = this . getRootNode ( ) ;
4548 let radioGroup : NodeListOf < PfRadio > ;
@@ -54,12 +57,121 @@ export class PfRadio extends LitElement {
5457 }
5558 }
5659
60+ #onKeyPress = ( event : KeyboardEvent ) => {
61+ const root : Node = this . getRootNode ( ) ;
62+ let radioGroup : NodeListOf < PfRadio > ;
63+ if ( root instanceof Document || root instanceof ShadowRoot ) {
64+ radioGroup = root . querySelectorAll ( 'pf-radio' ) ;
65+ if ( ! event . shiftKey && event . key === 'Tab' ) {
66+ radioGroup . forEach ( ( radio , index ) => {
67+ const input = radio . shadowRoot ?. querySelector ( 'input' ) as HTMLInputElement ;
68+ // input.tabIndex = -1;
69+ // if(radio.id === this.shadowRoot?.activeElement?.id){
70+ // //input.tabIndex = -1;
71+ // //event.preventDefault();
72+ // //root.focusOut()
73+ // }else{
74+ // //input.tabIndex = 0;
75+ // }
76+ if ( radio . checked === true ) {
77+ input . tabIndex = 0 ;
78+ } else if ( index === 0 ) {
79+ input . tabIndex = 0 ;
80+ } else {
81+ input . tabIndex = - 1 ;
82+ }
83+ } ) ;
84+ }
85+
86+ if ( event . shiftKey && event . key === 'Tab' ) {
87+ radioGroup . forEach ( ( radio , index ) => {
88+ const input = radio . shadowRoot ?. querySelector ( 'input' ) as HTMLInputElement ;
89+ // input.tabIndex = 0;
90+ // input.tabIndex = 0;
91+ // if(radio.id === this.shadowRoot?.activeElement?.id){
92+ // input.tabIndex = 0;
93+ // //event.preventDefault();
94+ // //root.focusOut()
95+ // }else{
96+ // //input.tabIndex = 0;
97+ // }
98+ if ( radio . checked === true ) {
99+ input . tabIndex = 0 ;
100+ input . focus ( ) ;
101+ } else if ( index === ( radioGroup . length - 1 ) ) {
102+ input . tabIndex = 0 ;
103+ } else {
104+ input . tabIndex = - 1 ;
105+ }
106+ } ) ;
107+ }
108+ }
109+ } ;
110+
111+ #onKeydown = ( event : KeyboardEvent ) => {
112+ if ( event . key === 'ArrowDown'
113+ || event . key === 'ArrowRight'
114+ || event . key === 'ArrowUp'
115+ || event . key === 'ArrowLeft' ) {
116+ const root : Node = this . getRootNode ( ) ;
117+ let radioGroup : NodeListOf < PfRadio > ;
118+ if ( root instanceof Document || root instanceof ShadowRoot ) {
119+ radioGroup = root . querySelectorAll ( 'pf-radio' ) ;
120+ radioGroup . forEach ( ( radio , index ) => {
121+ const element : HTMLElement = radio as HTMLElement ;
122+ element ?. removeAttribute ( 'checked' ) ;
123+ this . checked = false ;
124+ if ( radioGroup [ index ] === event . target ) {
125+ if ( event . key === 'ArrowDown' || event . key === 'ArrowRight' ) {
126+ if ( ( radioGroup . length - 1 ) === index ) {
127+ radioGroup [ 0 ] . focus ( ) ;
128+ radioGroup [ 0 ] . checked = true ;
129+ } else {
130+ radioGroup [ index + 1 ] . focus ( ) ;
131+ radioGroup [ index + 1 ] . checked = true ;
132+ }
133+ } else if ( event . key === 'ArrowUp' || event . key === 'ArrowLeft' ) {
134+ if ( index === 0 ) {
135+ radioGroup [ radioGroup . length - 1 ] . focus ( ) ;
136+ radioGroup [ radioGroup . length - 1 ] . checked = true ;
137+ } else {
138+ radioGroup [ index - 1 ] . focus ( ) ;
139+ radioGroup [ index - 1 ] . checked = true ;
140+ }
141+ }
142+ }
143+ } ) ;
144+ }
145+ }
146+ } ;
147+
148+
149+ // #onKeydown1 = (event: KeyboardEvent) => {
150+ // switch (event.key) {
151+ // case "ArrowDown":
152+ // //this.#onArrowDown(event);
153+ // // Do something for "down arrow" key press.
154+ // break;
155+ // case "ArrowUp":
156+ // // Do something for "up arrow" key press.
157+ // break;
158+ // case "ArrowLeft":
159+ // // Do something for "left arrow" key press.
160+ // break;
161+ // case "ArrowRight":
162+ // // Do something for "right arrow" key press.
163+ // break;
164+ // default:
165+ // return; // Quit when this doesn't handle the key event.
166+ // }
167+ // };
168+
57169 render ( ) : TemplateResult < 1 > {
58170 return html `
59171 < label for ='input '> ${ this . label } </ label >
60172 < input
61- @click =${ ( e : Event ) => this . #onRadioButtonClick( e ) }
62- id ='input'
173+ @click =${ this . #onRadioButtonClick}
174+ id =${ this . id }
63175 .name=${ this . name }
64176 type='radio'
65177 .checked='${ this . checked } '
0 commit comments