-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheckbox.tsx
More file actions
78 lines (66 loc) · 1.7 KB
/
checkbox.tsx
File metadata and controls
78 lines (66 loc) · 1.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import {
Component,
Event,
type EventEmitter,
h,
Prop,
State,
} from "@stencil/core";
import checkIcon from "@tabler/icons/outline/check.svg";
@Component({
tag: "scout-checkbox",
styleUrl: "checkbox.css",
scoped: true,
})
export class ScoutCheckbox {
@Prop() checked: boolean = false;
@Prop() disabled: boolean = false;
/**
* Use this prop if you need to connect your checkbox with another element describing its use, other than the property label.
*/
@Prop() ariaLabelledby: string;
@Prop() label: string;
@Prop() value: string;
@Prop() name: string;
@State() ariaId: string;
@Event() scoutChecked: EventEmitter<{
checked: boolean;
element: HTMLInputElement;
}>;
/**
* Internal event used for form field association.
*/
@Event() _fieldId: EventEmitter<string>;
componentWillLoad(): Promise<void> | void {
this.ariaId = `_${Math.random().toString(36).substring(2, 9)}`;
this._fieldId.emit(this.ariaId);
}
onChange(event: Event) {
const checkbox = event.target as HTMLInputElement;
this.scoutChecked.emit({
checked: checkbox.checked,
element: checkbox,
});
}
render() {
const Tag = this.label?.length ? "label" : "div";
return (
<Tag>
<input
id={this.ariaId}
type="checkbox"
value={this.value}
name={this.name}
class="checkbox"
style={{ "--icon-checkbox": `url(${checkIcon})` }}
aria-labelledby={this.ariaLabelledby}
aria-disabled={this.disabled}
disabled={this.disabled}
checked={this.checked}
onChange={(event) => this.onChange(event)}
/>
{this.label}
</Tag>
);
}
}