import {classMap} from "lit/directives/class-map.js"; import {type CSSResultGroup, html, type PropertyValues, unsafeCSS} from 'lit'; import { customErrorValidityState, FormControlController, validValidityState, valueMissingValidityState } from "../../internal/form"; import {HasSlotController} from "../../internal/slot"; import {property, query, state} from 'lit/decorators.js'; import {watch} from '../../internal/watch'; import ZincElement from '../../internal/zinc-element'; import type {ZincFormControl} from '../../internal/zinc-element'; import type ZnRadio from "../radio"; import styles from './radio-group.scss'; /** * @summary Short summary of the component's intended use. * @documentation https://zinc.style/components/radio-group * @status experimental * @since 1.0 * * @dependency zn-example * * @event zn-event-name - Emitted as an example. * * @slot - The default slot. * @slot example - An example slot. * * @csspart base - The component's base wrapper. * * @cssproperty --example - An example CSS custom property. */ export default class ZnRadioGroup extends ZincElement implements ZincFormControl { static styles: CSSResultGroup = unsafeCSS(styles); protected readonly formControlController = new FormControlController(this); private readonly hasSlotController = new HasSlotController(this, 'help-text', 'label'); private customValidityMessage = ''; private validationTimeout: number; @query('slot:not([name])') defaultSlot: HTMLSlotElement; @query('.radio-group__validation-input') validationInput: HTMLInputElement; @state() private errorMessage = ''; @state() defaultValue = ''; /** * The radio group's label. Required for proper accessibility. If you need to display HTML, use the `label` slot * instead. */ @property() label = ''; /** Text that appears in a tooltip next to the label. If you need to display HTML in the tooltip, use the `label-tooltip` slot instead. */ @property({attribute: 'label-tooltip'}) labelTooltip = ''; /** The radio groups' 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 = ''; /** The current value of the radio group, submitted as a name/value pair with form data. */ @property({reflect: true}) value = ''; /** The radio group's size. This size will be applied to all child radios */ @property({reflect: true}) size: 'small' | 'medium' | 'large' = 'medium'; /** The checkbox group's orientation. Changes the group's layout from the default (vertical) to horizontal. */ @property({type: Boolean, reflect: true}) horizontal = false; /** The checkbox group's style. Changes the group's style from the default (plain) style to the 'contained' style. This style will be applied to all child checkboxes. */ @property({type: Boolean, reflect: true}) contained = false; /** * By default, form controls are associated with the nearest containing `
` element. This attribute allows you * to place the form control outside a form and associate it with the form that has this `id`. The form must be in * the same document or shadow root for this to work. */ @property({reflect: true}) form = ''; /** Ensures a child radio is checked before allowing the containing form to submit. */ @property({type: Boolean, reflect: true}) required = false; get validity() { const isRequiredAndEmpty = this.required && !this.value; const hasCustomValidityMessage = this.customValidityMessage !== ''; if (hasCustomValidityMessage) { return customErrorValidityState; } if (isRequiredAndEmpty) { return valueMissingValidityState; } return validValidityState; } /** Gets the validation message */ get validationMessage() { const isRequiredAndEmpty = this.required && !this.value; const hasCustomValidityMessage = this.customValidityMessage !== ''; if (hasCustomValidityMessage) { return this.customValidityMessage; } if (isRequiredAndEmpty) { return this.validationInput.validationMessage; } return ''; } connectedCallback() { super.connectedCallback(); this.defaultValue = this.value; } protected firstUpdated(_changedProperties: PropertyValues) { super.firstUpdated(_changedProperties); this.formControlController.updateValidity(); } private getAllRadios() { return [...this.querySelectorAll('zn-radio')]; } private handleRadioClick(event: MouseEvent) { const target = (event.target as HTMLElement).closest('zn-radio')!; const radios = this.getAllRadios(); if (!target || target.disabled) { return; } this.value = target.value; radios.forEach(radio => (radio.checked = radio === target)); } 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; 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; }); this.value = radios[index].value; radios[index].checked = true; radios[index].shadowRoot!.querySelector('button')!.focus(); event.preventDefault(); } private handleLabelClick() { this.focus(); } private handleInvalid(event: Event) { this.formControlController.setValidity(false); this.formControlController.emitInvalidEvent(event); } private async syncRadioElements() { const radios = this.getAllRadios(); await Promise.all( // Sync the checked state and size radios.map(async (radio: ZnRadio) => { await radio.updateComplete; radio.checked = radio.value === this.value; radio.size = this.size; radio.horizontal = this.horizontal; radio.contained = this.contained; }) ); if (radios.length > 0 && !radios.some(radio => radio.checked)) { radios[0].setAttribute('tabindex', '0'); } } private syncRadios() { if (customElements.get('zn-radio')) { this.syncRadioElements(); return; } if (customElements.get('zn-radio')) { this.syncRadioElements(); } else { customElements.whenDefined('zn-radio').then(() => this.syncRadios()); } } private updateCheckedRadio() { const radios = this.getAllRadios(); radios.forEach(radio => (radio.checked = radio.value === this.value)); this.formControlController.setValidity(this.validity?.valid); } @watch('size', {waitUntilFirstUpdate: true}) handleSizeChange() { this.syncRadios(); } @watch('value') handleValueChange() { if (this.hasUpdated) { this.updateCheckedRadio(); } } /** Checks for validity but does not show a validation message. Returns `true` when valid and `false` when invalid. */ checkValidity() { const isRequiredAndEmpty = this.required && !this.value; const hasCustomValidityMessage = this.customValidityMessage !== ''; if (isRequiredAndEmpty || hasCustomValidityMessage) { this.formControlController.emitInvalidEvent(); return false; } return true; } /** Gets the associated form, if one exists. */ getForm(): HTMLFormElement | null { return this.formControlController.getForm(); } /** Checks for validity and shows the browser's validation message if the control is invalid. */ reportValidity(): boolean { const isValid = this.validity?.valid; this.errorMessage = this.customValidityMessage || isValid ? '' : this.validationInput.validationMessage; this.formControlController.setValidity(isValid); this.validationInput.hidden = true; clearTimeout(this.validationTimeout); if (!isValid) { // Show the browser's constraint validation message this.validationInput.hidden = false; this.validationInput.reportValidity(); this.validationTimeout = setTimeout(() => (this.validationInput.hidden = true), 10000) as unknown as number; } return isValid; } /** Sets a custom validation message. Pass an empty string to restore validity. */ setCustomValidity(message = '') { this.customValidityMessage = message; this.errorMessage = message; this.validationInput.setCustomValidity(message); this.formControlController.updateValidity(); } public focus(options?: FocusOptions) { const radios = this.getAllRadios(); const checked = radios.find(radio => radio.checked); const firstEnabledRadio = radios.find(radio => !radio.disabled); const radioToFocus = checked || firstEnabledRadio; // Call focus for the checked radio // If no radio is checked, focus the first one that is not disabled if (radioToFocus) { radioToFocus.focus(options); } } render() { const hasLabelSlot = this.hasSlotController.test('label'); const hasLabelTooltipSlot = this.hasSlotController.test('label-tooltip'); const hasHelpTextSlot = this.hasSlotController.test('help-text'); const hasLabel = this.label ? true : !!hasLabelSlot; const hasLabelTooltip = this.labelTooltip ? true : hasLabelTooltipSlot; const hasHelpText = this.helpText ? true : !!hasHelpTextSlot; const defaultSlot = html` `; return html`
${this.errorMessage}
${defaultSlot}
${this.helpText}
`; } }