UNPKG

@synergy-design-system/components

Version:
270 lines (264 loc) 8.22 kB
import { checkbox_styles_default } from "./chunk.VOUZJK5T.js"; import { defaultValue } from "./chunk.3JDUKQRN.js"; import { form_control_styles_default } from "./chunk.34WYIU4C.js"; import { FormControlController } from "./chunk.YDQ424MR.js"; import { HasSlotController } from "./chunk.CHFWLQN5.js"; import { SynIcon } from "./chunk.KODS2CCZ.js"; import { enableDefaultSettings } from "./chunk.WYMKPLYK.js"; import { watch } from "./chunk.UR2E7Y2Q.js"; import { component_styles_default } from "./chunk.2NT3B5WJ.js"; import { SynergyElement } from "./chunk.L2IYPRPF.js"; import { __decorateClass } from "./chunk.VK2FVWOF.js"; // src/components/checkbox/checkbox.component.ts import { classMap } from "lit/directives/class-map.js"; import { html } from "lit"; import { ifDefined } from "lit/directives/if-defined.js"; import { live } from "lit/directives/live.js"; import { property, query } from "lit/decorators.js"; var SynCheckbox = class extends SynergyElement { constructor() { super(...arguments); this.formControlController = new FormControlController(this, { value: (control) => control.checked ? control.value || "on" : void 0, defaultValue: (control) => control.defaultChecked, setValue: (control, checked) => control.checked = checked }); this.hasSlotController = new HasSlotController(this, "help-text"); this.title = ""; this.name = ""; this.size = "medium"; this.disabled = false; this.readonly = false; this.checked = false; this.indeterminate = false; this.defaultChecked = false; this.form = ""; this.required = false; this.helpText = ""; } /** Gets the validity state object */ get validity() { return this.input.validity; } /** Gets the validation message */ get validationMessage() { return this.input.validationMessage; } firstUpdated() { this.formControlController.updateValidity(); } handleClick(e) { if (this.readonly) { e.preventDefault(); return; } this.checked = !this.checked; this.indeterminate = false; this.emit("syn-change"); } handleBlur() { this.emit("syn-blur"); } handleInput() { this.emit("syn-input"); } handleInvalid(event) { this.formControlController.setValidity(false); this.formControlController.emitInvalidEvent(event); } handleFocus() { this.emit("syn-focus"); } handleDisabledChange() { this.formControlController.setValidity(this.disabled); } handleStateChange() { this.input.checked = this.checked; this.input.indeterminate = this.indeterminate; this.formControlController.updateValidity(); } /** Simulates a click on the checkbox. */ click() { this.input.click(); } /** Sets focus on the checkbox. */ focus(options) { this.input.focus(options); } /** Removes focus from the checkbox. */ blur() { this.input.blur(); } /** Checks for validity but does not show a validation message. Returns `true` when valid and `false` when invalid. */ checkValidity() { return this.input.checkValidity(); } /** 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() { return this.input.reportValidity(); } /** * Sets a custom validation message. The value provided will be shown to the user when the form is submitted. To clear * the custom validation message, call this method with an empty string. */ setCustomValidity(message) { this.input.setCustomValidity(message); this.formControlController.updateValidity(); } render() { const hasHelpTextSlot = this.hasSlotController.test("help-text"); const hasHelpText = this.helpText ? true : !!hasHelpTextSlot; return html` <div 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--has-help-text": hasHelpText })} > <label part="base" class=${classMap({ checkbox: true, "checkbox--checked": this.checked, "checkbox--disabled": this.disabled, "checkbox--readonly": this.readonly, "checkbox--indeterminate": this.indeterminate, "checkbox--small": this.size === "small", "checkbox--medium": this.size === "medium", "checkbox--large": this.size === "large" })} > <input class="checkbox__input" type="checkbox" title=${this.title} name=${this.name} value=${ifDefined(this.value)} .indeterminate=${live(this.indeterminate)} .checked=${live(this.checked)} .disabled=${this.disabled} .readOnly=${this.readonly} .required=${this.required} aria-checked=${this.checked ? "true" : "false"} aria-describedby="help-text" @click=${this.handleClick} @input=${this.handleInput} @invalid=${this.handleInvalid} @blur=${this.handleBlur} @focus=${this.handleFocus} /> <span part="control${this.checked ? " control--checked" : ""}${this.indeterminate ? " control--indeterminate" : ""}" class="checkbox__control" > ${this.checked ? html` <syn-icon part="checked-icon" class="checkbox__checked-icon" library="system" name="check"></syn-icon> ` : ""} ${!this.checked && this.indeterminate ? html` <syn-icon part="indeterminate-icon" class="checkbox__indeterminate-icon" library="system" name="indeterminate" ></syn-icon> ` : ""} </span> <div part="label" class="checkbox__label"> <slot></slot> </div> </label> <div aria-hidden=${hasHelpText ? "false" : "true"} class="form-control__help-text" id="help-text" part="form-control-help-text" > <slot name="help-text">${this.helpText}</slot> </div> </div> `; } }; SynCheckbox.styles = [component_styles_default, form_control_styles_default, checkbox_styles_default]; SynCheckbox.dependencies = { "syn-icon": SynIcon }; __decorateClass([ query('input[type="checkbox"]') ], SynCheckbox.prototype, "input", 2); __decorateClass([ property({ reflect: true }) ], SynCheckbox.prototype, "title", 2); __decorateClass([ property() ], SynCheckbox.prototype, "name", 2); __decorateClass([ property() ], SynCheckbox.prototype, "value", 2); __decorateClass([ property({ reflect: true }) ], SynCheckbox.prototype, "size", 2); __decorateClass([ property({ type: Boolean, reflect: true }) ], SynCheckbox.prototype, "disabled", 2); __decorateClass([ property({ type: Boolean, reflect: true }) ], SynCheckbox.prototype, "readonly", 2); __decorateClass([ property({ type: Boolean, reflect: true }) ], SynCheckbox.prototype, "checked", 2); __decorateClass([ property({ type: Boolean, reflect: true }) ], SynCheckbox.prototype, "indeterminate", 2); __decorateClass([ defaultValue("checked") ], SynCheckbox.prototype, "defaultChecked", 2); __decorateClass([ property({ reflect: true }) ], SynCheckbox.prototype, "form", 2); __decorateClass([ property({ type: Boolean, reflect: true }) ], SynCheckbox.prototype, "required", 2); __decorateClass([ property({ attribute: "help-text" }) ], SynCheckbox.prototype, "helpText", 2); __decorateClass([ watch("disabled", { waitUntilFirstUpdate: true }) ], SynCheckbox.prototype, "handleDisabledChange", 1); __decorateClass([ watch(["checked", "indeterminate"], { waitUntilFirstUpdate: true }) ], SynCheckbox.prototype, "handleStateChange", 1); SynCheckbox = __decorateClass([ enableDefaultSettings("SynCheckbox") ], SynCheckbox); export { SynCheckbox }; //# sourceMappingURL=chunk.PHEANWZJ.js.map