-
Notifications
You must be signed in to change notification settings - Fork 80
fix(radio-button-group): no longer focus first radio button on label click and adds setFocus
method.
#7050
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-group): no longer focus first radio button on label click and adds setFocus
method.
#7050
Changes from all commits
3585474
0d52082
f70b1a1
61fdbea
b15ff52
2753186
4b5ca37
082cb9c
1740178
c389f18
1357821
cffff6d
88cd7ce
f7f25d7
55c2018
955b751
0510a88
2a0cc42
184e2a9
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 |
---|---|---|
|
@@ -6,24 +6,30 @@ import { | |
h, | ||
Host, | ||
Listen, | ||
Method, | ||
Prop, | ||
State, | ||
VNode, | ||
Watch | ||
} from "@stencil/core"; | ||
import { createObserver } from "../../utils/observers"; | ||
import { Layout, Scale } from "../interfaces"; | ||
import { | ||
componentLoaded, | ||
LoadableComponent, | ||
setComponentLoaded, | ||
setUpLoadableComponent | ||
} from "../../utils/loadable"; | ||
|
||
/** | ||
* @slot - A slot for adding `calcite-radio-button`s. | ||
*/ | ||
@Component({ | ||
tag: "calcite-radio-button-group", | ||
styleUrl: "radio-button-group.scss", | ||
shadow: { | ||
delegatesFocus: true | ||
} | ||
shadow: true | ||
}) | ||
export class RadioButtonGroup { | ||
export class RadioButtonGroup implements LoadableComponent { | ||
//-------------------------------------------------------------------------- | ||
// | ||
// Element | ||
|
@@ -91,6 +97,8 @@ export class RadioButtonGroup { | |
|
||
mutationObserver = createObserver("mutation", () => this.passPropsToRadioButtons()); | ||
|
||
@State() radioButtons: HTMLCalciteRadioButtonElement[] = []; | ||
|
||
//-------------------------------------------------------------------------- | ||
// | ||
// Lifecycle | ||
|
@@ -102,6 +110,14 @@ export class RadioButtonGroup { | |
this.mutationObserver?.observe(this.el, { childList: true, subtree: true }); | ||
} | ||
|
||
componentWillLoad(): void { | ||
setUpLoadableComponent(this); | ||
} | ||
|
||
componentDidLoad(): void { | ||
setComponentLoaded(this); | ||
} | ||
|
||
disconnectedCallback(): void { | ||
this.mutationObserver?.disconnect(); | ||
} | ||
|
@@ -113,10 +129,11 @@ export class RadioButtonGroup { | |
//-------------------------------------------------------------------------- | ||
|
||
private passPropsToRadioButtons = (): void => { | ||
const radioButtons = this.el.querySelectorAll("calcite-radio-button"); | ||
this.selectedItem = Array.from(radioButtons).find((radioButton) => radioButton.checked) || null; | ||
if (radioButtons.length > 0) { | ||
radioButtons.forEach((radioButton) => { | ||
this.radioButtons = Array.from(this.el.querySelectorAll("calcite-radio-button")); | ||
this.selectedItem = | ||
Array.from(this.radioButtons).find((radioButton) => radioButton.checked) || null; | ||
if (this.radioButtons.length > 0) { | ||
this.radioButtons.forEach((radioButton) => { | ||
radioButton.disabled = this.disabled || radioButton.disabled; | ||
radioButton.hidden = this.hidden; | ||
radioButton.name = this.name; | ||
|
@@ -126,6 +143,10 @@ export class RadioButtonGroup { | |
} | ||
}; | ||
|
||
private getFocusableRadioButton(): HTMLCalciteRadioButtonElement | null { | ||
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. Can you use array.find instead of the while loop? return this.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. The other option would to be to use the |
||
return this.radioButtons.find((radiobutton) => !radiobutton.disabled) ?? null; | ||
} | ||
|
||
//-------------------------------------------------------------------------- | ||
// | ||
// Events | ||
|
@@ -137,6 +158,25 @@ export class RadioButtonGroup { | |
*/ | ||
@Event({ cancelable: false }) calciteRadioButtonGroupChange: EventEmitter<void>; | ||
|
||
//-------------------------------------------------------------------------- | ||
// | ||
// Public Method | ||
// | ||
//-------------------------------------------------------------------------- | ||
|
||
/** Sets focus on the fist focusable `calcite-radio-button` element in the component. */ | ||
@Method() | ||
async setFocus(): Promise<void> { | ||
await componentLoaded(this); | ||
if (this.selectedItem && !this.selectedItem.disabled) { | ||
this.selectedItem.setFocus(); | ||
return; | ||
} | ||
if (this.radioButtons.length > 0) { | ||
this.getFocusableRadioButton()?.setFocus(); | ||
} | ||
} | ||
|
||
//-------------------------------------------------------------------------- | ||
// | ||
// Event Listeners | ||
|
Uh oh!
There was an error while loading. Please reload this page.