UNPKG

@synergy-design-system/components

Version:

This package provides the base of the Synergy Design System as native web components. It uses [lit](https://www.lit.dev) and parts of [shoelace](https://shoelace.style/). Synergy officially supports the latest two versions of all major browsers (as define

373 lines (367 loc) 12.1 kB
import { radio_group_styles_default } from "./chunk.K62NPKGN.js"; import { radio_group_custom_styles_default } from "./chunk.32LFHSM5.js"; import { SynButtonGroup } from "./chunk.SG6WH2YB.js"; import { form_control_custom_styles_default, form_control_styles_default } from "./chunk.G4URZQCL.js"; import { FormControlController, customErrorValidityState, validValidityState, valueMissingValidityState } from "./chunk.HP2LEQRU.js"; import { HasSlotController } from "./chunk.WVVQK5TE.js"; import { enableDefaultSettings } from "./chunk.E5UUNP6E.js"; import { watch } from "./chunk.BVZQ6QSY.js"; import { component_styles_default } from "./chunk.NLYVOJGK.js"; import { SynergyElement } from "./chunk.3AZFEB6D.js"; import { __decorateClass } from "./chunk.Z4XV3SMG.js"; // src/components/radio-group/radio-group.component.ts import { classMap } from "lit/directives/class-map.js"; import { html } from "lit"; import { property, query, state } from "lit/decorators.js"; var SynRadioGroup = class extends SynergyElement { constructor() { super(...arguments); this.formControlController = new FormControlController(this); this.hasSlotController = new HasSlotController(this, "help-text", "label"); this.customValidityMessage = ""; this.hasButtonGroup = false; this.errorMessage = ""; this.defaultValue = ""; this.label = ""; this.helpText = ""; this.name = "option"; this.value = ""; this.size = "medium"; this.form = ""; this.required = false; } /** Gets the validity state object */ get validity() { const isRequiredAndEmpty = this.required && !this.value; const hasCustomValidityMessage = this.customValidityMessage !== ""; if (hasCustomValidityMessage) { return customErrorValidityState; } else 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; } else if (isRequiredAndEmpty) { return this.validationInput.validationMessage; } return ""; } connectedCallback() { super.connectedCallback(); this.defaultValue = this.value; } firstUpdated() { this.formControlController.updateValidity(); } getAllRadios() { return [...this.querySelectorAll("syn-radio, syn-radio-button")]; } handleRadioClick(event) { const target = event.target.closest("syn-radio, syn-radio-button"); const radios = this.getAllRadios(); const oldValue = this.value; if (!target || target.disabled) { return; } this.value = target.value; radios.forEach((radio) => radio.checked = radio === target); if (this.value !== oldValue) { this.emit("syn-change"); this.emit("syn-input"); } } handleKeyDown(event) { var _a; if (!["ArrowUp", "ArrowDown", "ArrowLeft", "ArrowRight", " "].includes(event.key)) { return; } const radios = this.getAllRadios().filter((radio) => !radio.disabled); const checkedRadio = (_a = radios.find((radio) => radio.checked)) != null ? _a : 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.setAttribute("tabindex", "-1"); } }); this.value = radios[index].value; radios[index].checked = true; if (!this.hasButtonGroup) { radios[index].setAttribute("tabindex", "0"); radios[index].focus(); } else { radios[index].shadowRoot.querySelector("button").focus(); } if (this.value !== oldValue) { this.emit("syn-change"); this.emit("syn-input"); } event.preventDefault(); } handleLabelClick() { this.focus(); } handleInvalid(event) { this.formControlController.setValidity(false); this.formControlController.emitInvalidEvent(event); } async syncRadioElements() { var _a, _b; const radios = this.getAllRadios(); await Promise.all( // Sync the checked state and size radios.map(async (radio) => { await radio.updateComplete; radio.checked = radio.value === this.value; radio.size = this.size; }) ); this.hasButtonGroup = radios.some((radio) => radio.tagName.toLowerCase() === "syn-radio-button"); if (radios.length > 0 && !radios.some((radio) => radio.checked)) { if (this.hasButtonGroup) { const buttonRadio = (_a = radios[0].shadowRoot) == null ? void 0 : _a.querySelector("button"); if (buttonRadio) { buttonRadio.setAttribute("tabindex", "0"); } } else { radios[0].setAttribute("tabindex", "0"); } } if (this.hasButtonGroup) { const buttonGroup = (_b = this.shadowRoot) == null ? void 0 : _b.querySelector("syn-button-group"); if (buttonGroup) { buttonGroup.disableRole = true; } } } syncRadios() { if (customElements.get("syn-radio") && customElements.get("syn-radio-button")) { this.syncRadioElements(); return; } if (customElements.get("syn-radio")) { this.syncRadioElements(); } else { customElements.whenDefined("syn-radio").then(() => this.syncRadios()); } if (customElements.get("syn-radio-button")) { this.syncRadioElements(); } else { customElements.whenDefined("syn-radio-button").then(() => this.syncRadios()); } } updateCheckedRadio() { const radios = this.getAllRadios(); radios.forEach((radio) => radio.checked = radio.value === this.value); this.formControlController.setValidity(this.validity.valid); } handleSizeChange() { this.syncRadios(); } 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() { return this.formControlController.getForm(); } /** Checks for validity and shows the browser's validation message if the control is invalid. */ reportValidity() { 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) { this.validationInput.hidden = false; this.validationInput.reportValidity(); this.validationTimeout = setTimeout(() => this.validationInput.hidden = true, 1e4); } 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(); } /** Sets focus on the radio-group. */ focus(options) { const radios = this.getAllRadios(); const checked = radios.find((radio) => radio.checked); const firstEnabledRadio = radios.find((radio) => !radio.disabled); const radioToFocus = checked || firstEnabledRadio; if (radioToFocus) { radioToFocus.focus(options); } } 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` <slot @slotchange=${this.syncRadios} @click=${this.handleRadioClick} @keydown=${this.handleKeyDown}></slot> `; return html` <fieldset part="form-control" class=${classMap({ "form-control": true, "form-control--small": this.size === "small", "form-control--medium": this.size === "medium", "form-control--large": this.size === "large", "form-control--radio-group": true, "form-control--has-label": hasLabel, "form-control--has-help-text": hasHelpText })} role="radiogroup" aria-labelledby="label" aria-describedby="help-text" aria-errormessage="error-message" > <label part="form-control-label" id="label" class="form-control__label" aria-hidden=${hasLabel ? "false" : "true"} @click=${this.handleLabelClick} > <slot name="label">${this.label}</slot> </label> <div part="form-control-input" class="form-control-input"> <div class="visually-hidden"> <div id="error-message" aria-live="assertive">${this.errorMessage}</div> <label class="radio-group__validation"> <input type="text" class="radio-group__validation-input" ?required=${this.required} tabindex="-1" hidden @invalid=${this.handleInvalid} /> </label> </div> ${this.hasButtonGroup ? html` <syn-button-group part="button-group" exportparts="base:button-group__base" role="presentation"> ${defaultSlot} </syn-button-group> ` : defaultSlot} </div> <div part="form-control-help-text" id="help-text" class="form-control__help-text" aria-hidden=${hasHelpText ? "false" : "true"} > <slot name="help-text">${this.helpText}</slot> </div> </fieldset> `; } }; SynRadioGroup.styles = [component_styles_default, form_control_styles_default, radio_group_styles_default, form_control_custom_styles_default, radio_group_custom_styles_default]; SynRadioGroup.dependencies = { "syn-button-group": SynButtonGroup }; __decorateClass([ query("slot:not([name])") ], SynRadioGroup.prototype, "defaultSlot", 2); __decorateClass([ query(".radio-group__validation-input") ], SynRadioGroup.prototype, "validationInput", 2); __decorateClass([ state() ], SynRadioGroup.prototype, "hasButtonGroup", 2); __decorateClass([ state() ], SynRadioGroup.prototype, "errorMessage", 2); __decorateClass([ state() ], SynRadioGroup.prototype, "defaultValue", 2); __decorateClass([ property() ], SynRadioGroup.prototype, "label", 2); __decorateClass([ property({ attribute: "help-text" }) ], SynRadioGroup.prototype, "helpText", 2); __decorateClass([ property() ], SynRadioGroup.prototype, "name", 2); __decorateClass([ property({ reflect: true }) ], SynRadioGroup.prototype, "value", 2); __decorateClass([ property({ reflect: true }) ], SynRadioGroup.prototype, "size", 2); __decorateClass([ property({ reflect: true }) ], SynRadioGroup.prototype, "form", 2); __decorateClass([ property({ type: Boolean, reflect: true }) ], SynRadioGroup.prototype, "required", 2); __decorateClass([ watch("size", { waitUntilFirstUpdate: true }) ], SynRadioGroup.prototype, "handleSizeChange", 1); __decorateClass([ watch("value") ], SynRadioGroup.prototype, "handleValueChange", 1); SynRadioGroup = __decorateClass([ enableDefaultSettings("SynRadioGroup") ], SynRadioGroup); export { SynRadioGroup }; //# sourceMappingURL=chunk.5BL2CWFH.js.map