shoelace/src/components/radio-group/radio-group.ts

135 wiersze
4.0 KiB
TypeScript
Czysty Zwykły widok Historia

import { html, LitElement } from 'lit';
2022-03-15 21:42:59 +00:00
import { customElement, property, query, state } from 'lit/decorators.js';
2021-09-29 12:40:26 +00:00
import { classMap } from 'lit/directives/class-map.js';
2022-03-24 11:48:03 +00:00
import '~/components/button-group/button-group';
import type SlRadio from '~/components/radio/radio';
2021-07-10 00:45:44 +00:00
import styles from './radio-group.styles';
2021-04-09 12:15:33 +00:00
2022-03-15 21:42:59 +00:00
const RADIO_CHILDREN = ['sl-radio', 'sl-radio-button'];
2021-04-09 12:15:33 +00:00
/**
* @since 2.0
* @status stable
*
2022-03-15 21:42:59 +00:00
* @dependency sl-button-group
*
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
*
2022-03-09 20:54:18 +00:00
* @csspart base - The component's internal wrapper.
2022-03-15 21:42:59 +00:00
* @csspart label - The radio group's label.
* @csspart button-group - The button group that wraps radio buttons.
* @csspart button-group__base - The button group's `base` part.
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;
2022-03-15 21:42:59 +00:00
@state() hasButtonGroup = false;
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
2022-03-14 21:47:02 +00:00
connectedCallback() {
super.connectedCallback();
this.setAttribute('role', 'radiogroup');
}
getAllRadios() {
2022-03-15 21:42:59 +00:00
return [...this.querySelectorAll(RADIO_CHILDREN.join(','))].filter(el =>
RADIO_CHILDREN.includes(el.tagName.toLowerCase())
) as SlRadio[];
2022-03-14 21:47:02 +00:00
}
handleRadioClick(event: MouseEvent) {
const target = event.target as HTMLElement;
2022-03-15 21:42:59 +00:00
const checkedRadio = target.closest(RADIO_CHILDREN.map(selector => `${selector}:not([disabled])`).join(','));
2022-03-14 21:47:02 +00:00
if (checkedRadio) {
const radios = this.getAllRadios();
radios.forEach(radio => {
radio.checked = radio === checkedRadio;
radio.input.tabIndex = radio === checkedRadio ? 0 : -1;
});
}
}
handleKeyDown(event: KeyboardEvent) {
if (['ArrowUp', 'ArrowDown', 'ArrowLeft', 'ArrowRight'].includes(event.key)) {
const radios = this.getAllRadios().filter(radio => !radio.disabled);
const checkedRadio = radios.find(radio => radio.checked) ?? radios[0];
const incr = ['ArrowUp', 'ArrowLeft'].includes(event.key) ? -1 : 1;
let index = radios.indexOf(checkedRadio) + incr;
if (index < 0) {
index = radios.length - 1;
}
if (index > radios.length - 1) {
index = 0;
}
2021-07-12 14:48:38 +00:00
2022-03-14 21:47:02 +00:00
this.getAllRadios().forEach(radio => {
radio.checked = false;
radio.input.tabIndex = -1;
});
radios[index].focus();
radios[index].checked = true;
radios[index].input.tabIndex = 0;
event.preventDefault();
}
}
handleSlotChange() {
const radios = this.getAllRadios();
const checkedRadio = radios.find(radio => radio.checked);
2022-03-15 21:42:59 +00:00
this.hasButtonGroup = !!radios.find(radio => radio.tagName.toLowerCase() === 'sl-radio-button');
2022-03-14 21:47:02 +00:00
radios.forEach(radio => {
radio.setAttribute('role', 'radio');
radio.input.tabIndex = -1;
2021-08-10 21:24:52 +00:00
});
2022-03-14 21:47:02 +00:00
if (checkedRadio) {
checkedRadio.input.tabIndex = 0;
} else if (radios.length > 0) {
radios[0].input.tabIndex = 0;
}
2021-07-12 14:48:38 +00:00
}
2021-04-09 12:15:33 +00:00
render() {
2022-03-15 21:42:59 +00:00
const defaultSlot = html`
<slot @click=${this.handleRadioClick} @keydown=${this.handleKeyDown} @slotchange=${this.handleSlotChange}></slot>
`;
2021-04-09 12:15:33 +00:00
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
})}
>
<legend part="label" class="radio-group__label">
<slot name="label">${this.label}</slot>
</legend>
2022-03-15 21:42:59 +00:00
${this.hasButtonGroup
? html`<sl-button-group part="button-group">${defaultSlot}</sl-button-group>`
: defaultSlot}
2021-04-09 12:15:33 +00:00
</fieldset>
`;
}
}
declare global {
interface HTMLElementTagNameMap {
'sl-radio-group': SlRadioGroup;
}
}