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

125 wiersze
3.5 KiB
TypeScript
Czysty Zwykły widok Historia

import { html, LitElement } 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';
import type SlRadio from '~/components/radio/radio';
2022-03-14 21:47:02 +00:00
import { emit } from '~/internal/event';
2021-07-10 00:45:44 +00:00
import styles from './radio-group.styles';
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
*
2022-03-09 20:54:18 +00:00
* @csspart base - The component's internal wrapper.
2021-06-25 20:25:46 +00:00
* @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
2022-03-14 21:47:02 +00:00
connectedCallback() {
super.connectedCallback();
this.setAttribute('role', 'radiogroup');
}
getAllRadios() {
return this.defaultSlot
.assignedElements({ flatten: true })
.filter(el => el.tagName.toLowerCase() === 'sl-radio') as SlRadio[];
}
handleRadioClick(event: MouseEvent) {
const target = event.target as HTMLElement;
const checkedRadio = target.closest('sl-radio');
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;
emit(radios[index], 'sl-change');
event.preventDefault();
}
}
handleSlotChange() {
const radios = this.getAllRadios();
const checkedRadio = radios.find(radio => radio.checked);
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() {
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-14 21:47:02 +00:00
<slot
@click=${this.handleRadioClick}
@keydown=${this.handleKeyDown}
@slotchange=${this.handleSlotChange}
></slot>
2021-04-09 12:15:33 +00:00
</fieldset>
`;
}
}
declare global {
interface HTMLElementTagNameMap {
'sl-radio-group': SlRadioGroup;
}
}