234 lines
6.3 KiB
JavaScript
234 lines
6.3 KiB
JavaScript
|
import {
|
||
|
checkbox_styles_default
|
||
|
} from "./chunk.VVWLP2KZ.js";
|
||
|
import {
|
||
|
l
|
||
|
} from "./chunk.MB643KRE.js";
|
||
|
import {
|
||
|
defaultValue
|
||
|
} from "./chunk.RQ7JZ4R7.js";
|
||
|
import {
|
||
|
FormControlController
|
||
|
} from "./chunk.NMS6LWK4.js";
|
||
|
import {
|
||
|
o
|
||
|
} from "./chunk.2URMUHDY.js";
|
||
|
import {
|
||
|
e
|
||
|
} from "./chunk.UZVKBFXH.js";
|
||
|
import {
|
||
|
SlIcon
|
||
|
} from "./chunk.UZYAV5H6.js";
|
||
|
import {
|
||
|
watch
|
||
|
} from "./chunk.FA5RT4K4.js";
|
||
|
import {
|
||
|
ShoelaceElement,
|
||
|
e as e2,
|
||
|
n,
|
||
|
r
|
||
|
} from "./chunk.SEXBCYCU.js";
|
||
|
import {
|
||
|
x
|
||
|
} from "./chunk.CXZZ2LVK.js";
|
||
|
import {
|
||
|
__decorateClass
|
||
|
} from "./chunk.KIILAQWQ.js";
|
||
|
|
||
|
// src/components/checkbox/checkbox.component.ts
|
||
|
var SlCheckbox = class extends ShoelaceElement {
|
||
|
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.hasFocus = false;
|
||
|
this.title = "";
|
||
|
this.name = "";
|
||
|
this.size = "medium";
|
||
|
this.disabled = false;
|
||
|
this.checked = false;
|
||
|
this.indeterminate = false;
|
||
|
this.defaultChecked = false;
|
||
|
this.form = "";
|
||
|
this.required = false;
|
||
|
}
|
||
|
/** 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() {
|
||
|
this.checked = !this.checked;
|
||
|
this.indeterminate = false;
|
||
|
this.emit("sl-change");
|
||
|
}
|
||
|
handleBlur() {
|
||
|
this.hasFocus = false;
|
||
|
this.emit("sl-blur");
|
||
|
}
|
||
|
handleInput() {
|
||
|
this.emit("sl-input");
|
||
|
}
|
||
|
handleInvalid(event) {
|
||
|
this.formControlController.setValidity(false);
|
||
|
this.formControlController.emitInvalidEvent(event);
|
||
|
}
|
||
|
handleFocus() {
|
||
|
this.hasFocus = true;
|
||
|
this.emit("sl-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() {
|
||
|
return x`
|
||
|
<label
|
||
|
part="base"
|
||
|
class=${e({
|
||
|
checkbox: true,
|
||
|
"checkbox--checked": this.checked,
|
||
|
"checkbox--disabled": this.disabled,
|
||
|
"checkbox--focused": this.hasFocus,
|
||
|
"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=${o(this.value)}
|
||
|
.indeterminate=${l(this.indeterminate)}
|
||
|
.checked=${l(this.checked)}
|
||
|
.disabled=${this.disabled}
|
||
|
.required=${this.required}
|
||
|
aria-checked=${this.checked ? "true" : "false"}
|
||
|
@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 ? x`
|
||
|
<sl-icon part="checked-icon" class="checkbox__checked-icon" library="system" name="check"></sl-icon>
|
||
|
` : ""}
|
||
|
${!this.checked && this.indeterminate ? x`
|
||
|
<sl-icon
|
||
|
part="indeterminate-icon"
|
||
|
class="checkbox__indeterminate-icon"
|
||
|
library="system"
|
||
|
name="indeterminate"
|
||
|
></sl-icon>
|
||
|
` : ""}
|
||
|
</span>
|
||
|
|
||
|
<div part="label" class="checkbox__label">
|
||
|
<slot></slot>
|
||
|
</div>
|
||
|
</label>
|
||
|
`;
|
||
|
}
|
||
|
};
|
||
|
SlCheckbox.styles = checkbox_styles_default;
|
||
|
SlCheckbox.dependencies = { "sl-icon": SlIcon };
|
||
|
__decorateClass([
|
||
|
e2('input[type="checkbox"]')
|
||
|
], SlCheckbox.prototype, "input", 2);
|
||
|
__decorateClass([
|
||
|
r()
|
||
|
], SlCheckbox.prototype, "hasFocus", 2);
|
||
|
__decorateClass([
|
||
|
n()
|
||
|
], SlCheckbox.prototype, "title", 2);
|
||
|
__decorateClass([
|
||
|
n()
|
||
|
], SlCheckbox.prototype, "name", 2);
|
||
|
__decorateClass([
|
||
|
n()
|
||
|
], SlCheckbox.prototype, "value", 2);
|
||
|
__decorateClass([
|
||
|
n({ reflect: true })
|
||
|
], SlCheckbox.prototype, "size", 2);
|
||
|
__decorateClass([
|
||
|
n({ type: Boolean, reflect: true })
|
||
|
], SlCheckbox.prototype, "disabled", 2);
|
||
|
__decorateClass([
|
||
|
n({ type: Boolean, reflect: true })
|
||
|
], SlCheckbox.prototype, "checked", 2);
|
||
|
__decorateClass([
|
||
|
n({ type: Boolean, reflect: true })
|
||
|
], SlCheckbox.prototype, "indeterminate", 2);
|
||
|
__decorateClass([
|
||
|
defaultValue("checked")
|
||
|
], SlCheckbox.prototype, "defaultChecked", 2);
|
||
|
__decorateClass([
|
||
|
n({ reflect: true })
|
||
|
], SlCheckbox.prototype, "form", 2);
|
||
|
__decorateClass([
|
||
|
n({ type: Boolean, reflect: true })
|
||
|
], SlCheckbox.prototype, "required", 2);
|
||
|
__decorateClass([
|
||
|
watch("disabled", { waitUntilFirstUpdate: true })
|
||
|
], SlCheckbox.prototype, "handleDisabledChange", 1);
|
||
|
__decorateClass([
|
||
|
watch(["checked", "indeterminate"], { waitUntilFirstUpdate: true })
|
||
|
], SlCheckbox.prototype, "handleStateChange", 1);
|
||
|
|
||
|
export {
|
||
|
SlCheckbox
|
||
|
};
|