UNPKG

5.5 kBJavaScriptView Raw
1import {
2 l as l2
3} from "./chunk.R5Z37LZL.js";
4import {
5 defaultValue
6} from "./chunk.XM2NSF2I.js";
7import {
8 FormSubmitController
9} from "./chunk.67FQMIF5.js";
10import {
11 checkbox_styles_default
12} from "./chunk.EGNHYZMK.js";
13import {
14 o
15} from "./chunk.IAELDRGJ.js";
16import {
17 l
18} from "./chunk.7MO772SN.js";
19import {
20 watch
21} from "./chunk.W6MGCO4G.js";
22import {
23 emit
24} from "./chunk.UY5AQKHP.js";
25import {
26 e,
27 i,
28 n,
29 t
30} from "./chunk.VKNZYXSO.js";
31import {
32 $,
33 s
34} from "./chunk.WWAD5WF4.js";
35import {
36 __decorateClass
37} from "./chunk.K2NRSETB.js";
38
39// src/components/checkbox/checkbox.ts
40var SlCheckbox = class extends s {
41 constructor() {
42 super(...arguments);
43 this.formSubmitController = new FormSubmitController(this, {
44 value: (control) => control.checked ? control.value || "on" : void 0,
45 defaultValue: (control) => control.defaultChecked,
46 setValue: (control, checked) => control.checked = checked
47 });
48 this.hasFocus = false;
49 this.disabled = false;
50 this.required = false;
51 this.checked = false;
52 this.indeterminate = false;
53 this.invalid = false;
54 this.defaultChecked = false;
55 }
56 firstUpdated() {
57 this.invalid = !this.input.checkValidity();
58 }
59 click() {
60 this.input.click();
61 }
62 focus(options) {
63 this.input.focus(options);
64 }
65 blur() {
66 this.input.blur();
67 }
68 reportValidity() {
69 return this.input.reportValidity();
70 }
71 setCustomValidity(message) {
72 this.input.setCustomValidity(message);
73 this.invalid = !this.input.checkValidity();
74 }
75 handleClick() {
76 this.checked = !this.checked;
77 this.indeterminate = false;
78 emit(this, "sl-change");
79 }
80 handleBlur() {
81 this.hasFocus = false;
82 emit(this, "sl-blur");
83 }
84 handleDisabledChange() {
85 this.input.disabled = this.disabled;
86 this.invalid = !this.input.checkValidity();
87 }
88 handleFocus() {
89 this.hasFocus = true;
90 emit(this, "sl-focus");
91 }
92 handleStateChange() {
93 this.invalid = !this.input.checkValidity();
94 }
95 render() {
96 return $`
97 <label
98 part="base"
99 class=${o({
100 checkbox: true,
101 "checkbox--checked": this.checked,
102 "checkbox--disabled": this.disabled,
103 "checkbox--focused": this.hasFocus,
104 "checkbox--indeterminate": this.indeterminate
105 })}
106 >
107 <input
108 class="checkbox__input"
109 type="checkbox"
110 name=${l(this.name)}
111 value=${l(this.value)}
112 .indeterminate=${l2(this.indeterminate)}
113 .checked=${l2(this.checked)}
114 .disabled=${this.disabled}
115 .required=${this.required}
116 aria-checked=${this.checked ? "true" : "false"}
117 @click=${this.handleClick}
118 @blur=${this.handleBlur}
119 @focus=${this.handleFocus}
120 />
121
122 <span part="control" class="checkbox__control">
123 ${this.checked ? $`
124 <svg part="checked-icon" class="checkbox__icon" viewBox="0 0 16 16">
125 <g stroke="none" stroke-width="1" fill="none" fill-rule="evenodd" stroke-linecap="round">
126 <g stroke="currentColor" stroke-width="2">
127 <g transform="translate(3.428571, 3.428571)">
128 <path d="M0,5.71428571 L3.42857143,9.14285714"></path>
129 <path d="M9.14285714,0 L3.42857143,9.14285714"></path>
130 </g>
131 </g>
132 </g>
133 </svg>
134 ` : ""}
135 ${!this.checked && this.indeterminate ? $`
136 <svg part="indeterminate-icon" class="checkbox__icon" viewBox="0 0 16 16">
137 <g stroke="none" stroke-width="1" fill="none" fill-rule="evenodd" stroke-linecap="round">
138 <g stroke="currentColor" stroke-width="2">
139 <g transform="translate(2.285714, 6.857143)">
140 <path d="M10.2857143,1.14285714 L1.14285714,1.14285714"></path>
141 </g>
142 </g>
143 </g>
144 </svg>
145 ` : ""}
146 </span>
147
148 <span part="label" class="checkbox__label">
149 <slot></slot>
150 </span>
151 </label>
152 `;
153 }
154};
155SlCheckbox.styles = checkbox_styles_default;
156__decorateClass([
157 i('input[type="checkbox"]')
158], SlCheckbox.prototype, "input", 2);
159__decorateClass([
160 t()
161], SlCheckbox.prototype, "hasFocus", 2);
162__decorateClass([
163 e()
164], SlCheckbox.prototype, "name", 2);
165__decorateClass([
166 e()
167], SlCheckbox.prototype, "value", 2);
168__decorateClass([
169 e({ type: Boolean, reflect: true })
170], SlCheckbox.prototype, "disabled", 2);
171__decorateClass([
172 e({ type: Boolean, reflect: true })
173], SlCheckbox.prototype, "required", 2);
174__decorateClass([
175 e({ type: Boolean, reflect: true })
176], SlCheckbox.prototype, "checked", 2);
177__decorateClass([
178 e({ type: Boolean, reflect: true })
179], SlCheckbox.prototype, "indeterminate", 2);
180__decorateClass([
181 e({ type: Boolean, reflect: true })
182], SlCheckbox.prototype, "invalid", 2);
183__decorateClass([
184 defaultValue("checked")
185], SlCheckbox.prototype, "defaultChecked", 2);
186__decorateClass([
187 watch("disabled", { waitUntilFirstUpdate: true })
188], SlCheckbox.prototype, "handleDisabledChange", 1);
189__decorateClass([
190 watch("checked", { waitUntilFirstUpdate: true }),
191 watch("indeterminate", { waitUntilFirstUpdate: true })
192], SlCheckbox.prototype, "handleStateChange", 1);
193SlCheckbox = __decorateClass([
194 n("sl-checkbox")
195], SlCheckbox);
196
197export {
198 SlCheckbox
199};