2021-07-10 00:45:44 +00:00
|
|
|
import { LitElement, html } from 'lit';
|
2021-07-12 14:48:38 +00:00
|
|
|
import { customElement, property, query } from 'lit/decorators.js';
|
2021-09-29 12:40:26 +00:00
|
|
|
import { classMap } from 'lit/directives/class-map.js';
|
2021-07-10 00:45:44 +00:00
|
|
|
import styles from './radio-group.styles';
|
2022-01-16 05:47:14 +00:00
|
|
|
import type SlRadio from '~/components/radio/radio';
|
2021-04-09 12:15:33 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @since 2.0
|
|
|
|
* @status stable
|
|
|
|
*
|
2021-06-25 20:25:46 +00:00
|
|
|
* @slot - The default slot where radio controls are placed.
|
|
|
|
* @slot label - The radio group label. Required for proper accessibility. Alternatively, you can use the label prop.
|
2021-04-09 12:15:33 +00:00
|
|
|
*
|
2021-06-25 20:25:46 +00:00
|
|
|
* @csspart base - The component's base wrapper.
|
|
|
|
* @csspart label - The radio group label.
|
2021-04-09 12:15:33 +00:00
|
|
|
*/
|
|
|
|
@customElement('sl-radio-group')
|
|
|
|
export default class SlRadioGroup extends LitElement {
|
2021-07-10 00:45:44 +00:00
|
|
|
static styles = styles;
|
2021-04-09 12:15:33 +00:00
|
|
|
|
2021-07-12 14:48:38 +00:00
|
|
|
@query('slot:not([name])') defaultSlot: HTMLSlotElement;
|
|
|
|
|
2021-04-09 12:15:33 +00:00
|
|
|
/** The radio group label. Required for proper accessibility. Alternatively, you can use the label slot. */
|
2021-07-01 00:04:46 +00:00
|
|
|
@property() label = '';
|
2021-04-09 12:15:33 +00:00
|
|
|
|
2021-08-27 13:00:01 +00:00
|
|
|
/** Shows the fieldset and legend that surrounds the radio group. */
|
|
|
|
@property({ type: Boolean, attribute: 'fieldset' }) fieldset = false;
|
2021-04-09 12:15:33 +00:00
|
|
|
|
2021-07-12 14:48:38 +00:00
|
|
|
handleFocusIn() {
|
2021-08-10 21:24:52 +00:00
|
|
|
// When tabbing into the fieldset, make sure it lands on the checked radio
|
|
|
|
requestAnimationFrame(() => {
|
2022-01-16 05:47:14 +00:00
|
|
|
const checkedRadio = [...(this.defaultSlot.assignedElements({ flatten: true }) as SlRadio[])].find(
|
|
|
|
el => el.tagName.toLowerCase() === 'sl-radio' && el.checked
|
|
|
|
);
|
2021-07-12 14:48:38 +00:00
|
|
|
|
2022-01-16 05:47:14 +00:00
|
|
|
checkedRadio?.focus();
|
2021-08-10 21:24:52 +00:00
|
|
|
});
|
2021-07-12 14:48:38 +00:00
|
|
|
}
|
|
|
|
|
2021-04-09 12:15:33 +00:00
|
|
|
render() {
|
|
|
|
return html`
|
|
|
|
<fieldset
|
|
|
|
part="base"
|
|
|
|
class=${classMap({
|
|
|
|
'radio-group': true,
|
2021-08-27 13:00:01 +00:00
|
|
|
'radio-group--has-fieldset': this.fieldset
|
2021-04-09 12:15:33 +00:00
|
|
|
})}
|
|
|
|
role="radiogroup"
|
2021-07-12 14:48:38 +00:00
|
|
|
@focusin=${this.handleFocusIn}
|
2021-04-09 12:15:33 +00:00
|
|
|
>
|
|
|
|
<legend part="label" class="radio-group__label">
|
|
|
|
<slot name="label">${this.label}</slot>
|
|
|
|
</legend>
|
|
|
|
<slot></slot>
|
|
|
|
</fieldset>
|
|
|
|
`;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
declare global {
|
|
|
|
interface HTMLElementTagNameMap {
|
|
|
|
'sl-radio-group': SlRadioGroup;
|
|
|
|
}
|
|
|
|
}
|