-
Notifications
You must be signed in to change notification settings - Fork 77
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix(radio-button): focuses first focusable radio-button element in group. #7152
Changes from 10 commits
7e51b41
1900ca3
1e2c62d
4912760
5356c56
4ac4434
9beb381
a943ebe
62ff46a
82dadee
4926b92
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ import { | |
Element, | ||
Event, | ||
EventEmitter, | ||
forceUpdate, | ||
h, | ||
Host, | ||
Listen, | ||
|
@@ -73,6 +74,11 @@ export class RadioButton | |
/** When `true`, interaction is prevented and the component is displayed with lower opacity. */ | ||
@Prop({ reflect: true }) disabled = false; | ||
|
||
@Watch("disabled") | ||
disabledChanged(): void { | ||
this.updateTabIndexOfOtherRadioButtonsInGroup(); | ||
} | ||
|
||
/** | ||
* The focused state of the component. | ||
* | ||
|
@@ -94,6 +100,11 @@ export class RadioButton | |
/** When `true`, the component is not displayed and is not focusable or checkable. */ | ||
@Prop({ reflect: true }) hidden = false; | ||
|
||
@Watch("hidden") | ||
hiddenChanged(): void { | ||
this.updateTabIndexOfOtherRadioButtonsInGroup(); | ||
} | ||
|
||
/** | ||
* The hovered state of the component. | ||
* | ||
|
@@ -184,9 +195,11 @@ export class RadioButton | |
) as HTMLCalciteRadioButtonElement[]; | ||
}; | ||
|
||
isDefaultSelectable = (): boolean => { | ||
isFocusable = (): boolean => { | ||
const radioButtons = this.queryButtons(); | ||
return !radioButtons.some((radioButton) => radioButton.checked) && radioButtons[0] === this.el; | ||
const firstFocusable = radioButtons.find((radiobutton) => !radiobutton.disabled); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
const checked = radioButtons.find((radiobutton) => radiobutton.checked); | ||
return firstFocusable === this.el && !checked; | ||
}; | ||
|
||
check = (): void => { | ||
|
@@ -291,11 +304,25 @@ export class RadioButton | |
}); | ||
} | ||
|
||
private updateTabIndexOfOtherRadioButtonsInGroup(): void { | ||
const radioButtons = this.queryButtons(); | ||
const otherFocusableRadioButtons = radioButtons.filter( | ||
(radioButton) => radioButton.guid !== this.guid && !radioButton.disabled | ||
); | ||
otherFocusableRadioButtons.forEach((radiobutton) => { | ||
forceUpdate(radiobutton); | ||
eriklharper marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}); | ||
} | ||
eriklharper marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
private getTabIndex(): number | undefined { | ||
if (this.disabled) { | ||
return undefined; | ||
} | ||
return this.checked || this.isDefaultSelectable() ? 0 : -1; | ||
if (this.checked || this.isFocusable()) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No love for the previous ternary? 😭 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oops, there was some extra code in |
||
return 0; | ||
} else { | ||
return -1; | ||
} | ||
} | ||
|
||
//-------------------------------------------------------------------------- | ||
|
@@ -446,6 +473,7 @@ export class RadioButton | |
connectInteractive(this); | ||
connectLabel(this); | ||
connectForm(this); | ||
this.updateTabIndexOfOtherRadioButtonsInGroup(); | ||
} | ||
|
||
componentWillLoad(): void { | ||
|
@@ -464,6 +492,7 @@ export class RadioButton | |
disconnectInteractive(this); | ||
disconnectLabel(this); | ||
disconnectForm(this); | ||
this.updateTabIndexOfOtherRadioButtonsInGroup(); | ||
} | ||
|
||
componentDidRender(): void { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sidebar: we should revisit custom
hidden
props since it's a reflected prop onHTMLElement
.