Skip to content

Commit 457cb36

Browse files
committed
chore: tab navigation implemented
1 parent dec283f commit 457cb36

File tree

1 file changed

+46
-44
lines changed

1 file changed

+46
-44
lines changed

elements/pf-radio/pf-radio.ts

Lines changed: 46 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,28 @@ export class PfRadio extends LitElement {
1919

2020
@property({
2121
type: Boolean,
22-
// attribute: 'inline-filter',
22+
attribute: 'checked',
2323
converter: {
2424
fromAttribute: value => value === 'true',
2525
},
2626
reflect: true,
27-
})
28-
checked = false;
27+
}) checked = false;
28+
29+
@property({
30+
type: Boolean,
31+
attribute: 'disabled',
32+
converter: {
33+
fromAttribute: value => value === 'true',
34+
},
35+
reflect: true,
36+
}) disabled = false;
37+
38+
@property({ attribute: 'name', reflect: true }) name = 'radio-test';
39+
@property({ attribute: 'label', reflect: true }) label?: string;
40+
@property({ attribute: 'value', reflect: true }) value = '';
41+
@property({ attribute: 'id', reflect: true }) id = '';
42+
@property({ attribute: 'tabindex', reflect: true }) tabIndex = -1;
2943

30-
@property({ reflect: true }) name = 'radio-test';
31-
@property({ reflect: true }) label?: string;
32-
@property({ reflect: true }) value = '';
33-
@property({ reflect: true }) id = '';
3444

3545
constructor() {
3646
super();
@@ -51,59 +61,49 @@ export class PfRadio extends LitElement {
5161
radioGroup.forEach(radio => {
5262
const element: HTMLElement = radio as HTMLElement;
5363
element?.removeAttribute('checked');
64+
element.tabIndex = -1;
5465
});
5566
this.checked = true;
67+
this.tabIndex = 0;
5668
}
5769
}
5870
}
5971

6072
#onKeyPress = (event: KeyboardEvent) => {
6173
const root: Node = this.getRootNode();
6274
let radioGroup: NodeListOf<PfRadio>;
75+
let isRadioChecked = false;
6376
if (root instanceof Document || root instanceof ShadowRoot) {
6477
radioGroup = root.querySelectorAll('pf-radio');
65-
if (!event.shiftKey && event.key === 'Tab') {
78+
if (event.key === 'Tab') {
6679
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-
// }
80+
radio.tabIndex = -1;
7681
if (radio.checked === true) {
77-
input.tabIndex = 0;
78-
} else if (index === 0) {
79-
input.tabIndex = 0;
80-
} else {
81-
input.tabIndex = -1;
82+
radio.tabIndex = 0;
83+
isRadioChecked = true;
8284
}
8385
});
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;
86+
if (!isRadioChecked) {
87+
if (event.key === 'Tab') {
88+
radioGroup.forEach((radio, index) => {
89+
radio.tabIndex = -1;
90+
if ( event.shiftKey ) {
91+
if (index === (radioGroup.length - 1)) {
92+
radio.tabIndex = 0;
93+
} else {
94+
radio.tabIndex = -1;
95+
}
96+
}
97+
if (!event.shiftKey) {
98+
if (index === 0) {
99+
radio.tabIndex = 0;
100+
} else {
101+
radio.tabIndex = -1;
102+
}
103+
}
104+
});
105105
}
106-
});
106+
}
107107
}
108108
}
109109
};
@@ -121,6 +121,7 @@ export class PfRadio extends LitElement {
121121
const element: HTMLElement = radio as HTMLElement;
122122
element?.removeAttribute('checked');
123123
this.checked = false;
124+
radio.tabIndex = 0;
124125
if (radioGroup[index] === event.target ) {
125126
if (event.key === 'ArrowDown' || event.key === 'ArrowRight') {
126127
if ((radioGroup.length - 1) === index) {
@@ -174,6 +175,7 @@ export class PfRadio extends LitElement {
174175
id=${this.id}
175176
.name=${this.name}
176177
type='radio'
178+
tabindex=${this.tabIndex}
177179
.checked='${this.checked}'
178180
/>
179181
`;

0 commit comments

Comments
 (0)