Skip to content

Commit

Permalink
fix radio group tabbing
Browse files Browse the repository at this point in the history
  • Loading branch information
claviska committed Jul 12, 2021
1 parent a48eef2 commit b98e1f6
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
1 change: 1 addition & 0 deletions docs/resources/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ This release improves how component dependencies are imported. If you've been ch

- Added "Reflects" column to the properties table
- Dependencies are now automatically imported for all components
- Fixed a bug where tabbing into `sl-radio-group` would not always focus the checked radio
- Improved base path utility logic

## 2.0.0-beta.46
Expand Down
17 changes: 16 additions & 1 deletion src/components/radio-group/radio-group.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { LitElement, html } from 'lit';
import { customElement, property } from 'lit/decorators.js';
import { customElement, property, query } from 'lit/decorators.js';
import { classMap } from 'lit-html/directives/class-map';
import type SlRadio from '../radio/radio';
import styles from './radio-group.styles';

/**
Expand All @@ -17,12 +18,25 @@ import styles from './radio-group.styles';
export default class SlRadioGroup extends LitElement {
static styles = styles;

@query('slot:not([name])') defaultSlot: HTMLSlotElement;

/** The radio group label. Required for proper accessibility. Alternatively, you can use the label slot. */
@property() label = '';

/** Hides the fieldset and legend that surrounds the radio group. The label will still be read by screen readers. */
@property({ type: Boolean, attribute: 'no-fieldset' }) noFieldset = false;

handleFocusIn() {
// When focusing into the fieldset, make sure it lands on the checked radio
const checkedRadio = [...this.defaultSlot.assignedElements({ flatten: true })].find(
el => el.tagName.toLowerCase() === 'sl-radio' && (el as SlRadio).checked
) as SlRadio;

if (checkedRadio) {
checkedRadio.focus();
}
}

render() {
return html`
<fieldset
Expand All @@ -32,6 +46,7 @@ export default class SlRadioGroup extends LitElement {
'radio-group--no-fieldset': this.noFieldset
})}
role="radiogroup"
@focusin=${this.handleFocusIn}
>
<legend part="label" class="radio-group__label">
<slot name="label">${this.label}</slot>
Expand Down

0 comments on commit b98e1f6

Please sign in to comment.