import { html } from 'lit'; import { customElement, property, query, state } from 'lit/decorators.js'; import { classMap } from 'lit/directives/class-map.js'; import { FormControlController } from '../../internal/form'; import ShoelaceElement from '../../internal/shoelace-element'; import { HasSlotController } from '../../internal/slot'; import { watch } from '../../internal/watch'; import '../button-group/button-group'; import styles from './radio-group.styles'; import type { ShoelaceFormControl } from '../../internal/shoelace-element'; import type SlRadioButton from '../radio-button/radio-button'; import type SlRadio from '../radio/radio'; import type { CSSResultGroup } from 'lit'; /** * @summary Radio groups are used to group multiple [radios](/components/radio) or [radio buttons](/components/radio-button) so they function as a single form control. * * @since 2.0 * @status stable * * @dependency sl-button-group * * @slot - The default slot where `` or `` elements are placed. * @slot label - The radio group's label. Required for proper accessibility. Alternatively, you can use the `label` * attribute. * * @event sl-change - Emitted when the radio group's selected value changes. * @event sl-input - Emitted when the radio group receives user input. * * @csspart form-control - The form control that wraps the label, input, and help text. * @csspart form-control-label - The label's wrapper. * @csspart form-control-input - The input's wrapper. * @csspart form-control-help-text - The help text's wrapper. * @csspart button-group - The button group that wraps radio buttons. * @csspart button-group__base - The button group's `base` part. */ @customElement('sl-radio-group') export default class SlRadioGroup extends ShoelaceElement implements ShoelaceFormControl { static styles: CSSResultGroup = styles; protected readonly formControlController = new FormControlController(this, { defaultValue: control => control.defaultValue }); private readonly hasSlotController = new HasSlotController(this, 'help-text', 'label'); @query('slot:not([name])') defaultSlot: HTMLSlotElement; @query('.radio-group__validation-input') input: HTMLInputElement; @state() private hasButtonGroup = false; @state() private errorMessage = ''; @state() private customErrorMessage = ''; @state() defaultValue = ''; /** * The radio group's label. Required for proper accessibility. If you need to display HTML, use the `label` slot * instead. */ @property() label = ''; /** The radio groups's help text. If you need to display HTML, use the `help-text` slot instead. */ @property({ attribute: 'help-text' }) helpText = ''; /** The name of the radio group, submitted as a name/value pair with form data. */ @property() name = 'option'; /** The current value of the radio group, submitted as a name/value pair with form data. */ @property({ reflect: true }) value = ''; /** Ensures a child radio is checked before allowing the containing form to submit. */ @property({ type: Boolean, reflect: true }) required = false; connectedCallback() { super.connectedCallback(); this.defaultValue = this.value; } firstUpdated() { this.formControlController.updateValidity(); } private getAllRadios() { return [...this.querySelectorAll('sl-radio, sl-radio-button')]; } private handleRadioClick(event: MouseEvent) { const target = event.target as SlRadio | SlRadioButton; const radios = this.getAllRadios(); const oldValue = this.value; if (target.disabled) { return; } this.value = target.value; radios.forEach(radio => (radio.checked = radio === target)); if (this.value !== oldValue) { this.emit('sl-change'); this.emit('sl-input'); } } private handleKeyDown(event: KeyboardEvent) { if (!['ArrowUp', 'ArrowDown', 'ArrowLeft', 'ArrowRight', ' '].includes(event.key)) { return; } const radios = this.getAllRadios().filter(radio => !radio.disabled); const checkedRadio = radios.find(radio => radio.checked) ?? radios[0]; const incr = event.key === ' ' ? 0 : ['ArrowUp', 'ArrowLeft'].includes(event.key) ? -1 : 1; const oldValue = this.value; let index = radios.indexOf(checkedRadio) + incr; if (index < 0) { index = radios.length - 1; } if (index > radios.length - 1) { index = 0; } this.getAllRadios().forEach(radio => { radio.checked = false; if (!this.hasButtonGroup) { radio.tabIndex = -1; } }); this.value = radios[index].value; radios[index].checked = true; if (!this.hasButtonGroup) { radios[index].tabIndex = 0; radios[index].focus(); } else { radios[index].shadowRoot!.querySelector('button')!.focus(); } if (this.value !== oldValue) { this.emit('sl-change'); this.emit('sl-input'); } event.preventDefault(); } private handleLabelClick() { const radios = this.getAllRadios(); const checked = radios.find(radio => radio.checked); const radioToFocus = checked || radios[0]; // Move focus to the checked radio (or the first one if none are checked) when clicking the label if (radioToFocus) { radioToFocus.focus(); } } private handleSlotChange() { const radios = this.getAllRadios(); radios.forEach(radio => (radio.checked = radio.value === this.value)); this.hasButtonGroup = radios.some(radio => radio.tagName.toLowerCase() === 'sl-radio-button'); if (!radios.some(radio => radio.checked)) { if (this.hasButtonGroup) { const buttonRadio = radios[0].shadowRoot!.querySelector('button')!; buttonRadio.tabIndex = 0; } else { radios[0].tabIndex = 0; } } if (this.hasButtonGroup) { const buttonGroup = this.shadowRoot?.querySelector('sl-button-group'); if (buttonGroup) { buttonGroup.disableRole = true; } } } private showNativeErrorMessage() { this.input.hidden = false; this.input.reportValidity(); setTimeout(() => (this.input.hidden = true), 10000); } private updateCheckedRadio() { const radios = this.getAllRadios(); radios.forEach(radio => (radio.checked = radio.value === this.value)); this.formControlController.setValidity(this.validity.valid); } @watch('value') handleValueChange() { if (this.hasUpdated) { this.updateCheckedRadio(); } } /** Checks for validity but does not show the browser's validation message. */ checkValidity() { return this.validity.valid; } /** Sets a custom validation message. If `message` is not empty, the field will be considered invalid. */ setCustomValidity(message = '') { this.customErrorMessage = message; this.errorMessage = message; if (!message) { this.formControlController.setValidity(true); } else { this.formControlController.setValidity(false); this.input.setCustomValidity(message); } } get validity(): ValidityState { const hasMissingData = !((this.value && this.required) || !this.required); const hasCustomError = this.customErrorMessage !== ''; return { badInput: false, customError: hasCustomError, patternMismatch: false, rangeOverflow: false, rangeUnderflow: false, stepMismatch: false, tooLong: false, tooShort: false, typeMismatch: false, valid: hasMissingData || hasCustomError ? false : true, valueMissing: !hasMissingData }; } /** Checks for validity and shows the browser's validation message if the control is invalid. */ reportValidity(): boolean { const validity = this.validity; this.errorMessage = this.customErrorMessage || validity.valid ? '' : this.input.validationMessage; this.formControlController.setValidity(validity.valid); if (!validity.valid) { this.showNativeErrorMessage(); } return validity.valid; } render() { const hasLabelSlot = this.hasSlotController.test('label'); const hasHelpTextSlot = this.hasSlotController.test('help-text'); const hasLabel = this.label ? true : !!hasLabelSlot; const hasHelpText = this.helpText ? true : !!hasHelpTextSlot; const defaultSlot = html` `; return html` ${this.label} ${this.errorMessage} ${this.hasButtonGroup ? html` ${defaultSlot} ` : defaultSlot} ${this.helpText} `; /* eslint-enable lit-a11y/click-events-have-key-events */ } } declare global { interface HTMLElementTagNameMap { 'sl-radio-group': SlRadioGroup; } }