1 | import { Component, ChangeDetectionStrategy, ViewEncapsulation, forwardRef, Renderer2, ChangeDetectorRef, ViewChild, HostBinding, Input, NgModule } from '@angular/core';
|
2 | import { NG_VALUE_ACCESSOR } from '@angular/forms';
|
3 | import { trigger, state, style, transition, animate } from '@angular/animations';
|
4 | import { CommonModule } from '@angular/common';
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 | class CheckboxComponent {
|
11 | |
12 |
|
13 |
|
14 |
|
15 | constructor(_renderer, _changeDetectorRef) {
|
16 | this._renderer = _renderer;
|
17 | this._changeDetectorRef = _changeDetectorRef;
|
18 | |
19 |
|
20 |
|
21 | this.name = '';
|
22 | this.tabIndex = 0;
|
23 | }
|
24 | |
25 |
|
26 |
|
27 | get isFocused() {
|
28 | return this._focused;
|
29 | }
|
30 | |
31 |
|
32 |
|
33 | get checked() {
|
34 | return this.input.nativeElement.checked;
|
35 | }
|
36 | |
37 |
|
38 |
|
39 |
|
40 | set checked(checked) {
|
41 | this._renderer.setProperty(this.input.nativeElement, 'checked', checked);
|
42 | }
|
43 |
|
44 | |
45 |
|
46 |
|
47 | inputFocus() {
|
48 | this._focused = true;
|
49 | }
|
50 | |
51 |
|
52 |
|
53 | inputBlur() {
|
54 | this._focused = false;
|
55 | if (this._onTouchedCb) {
|
56 | this._onTouchedCb();
|
57 | }
|
58 | }
|
59 | |
60 |
|
61 |
|
62 |
|
63 | writeValue(value) {
|
64 | this.checked = !!value;
|
65 | this._changeDetectorRef.markForCheck();
|
66 | }
|
67 |
|
68 | |
69 |
|
70 |
|
71 |
|
72 | registerOnChange(fn) {
|
73 | this._onChangeCb = fn;
|
74 | }
|
75 | |
76 |
|
77 |
|
78 |
|
79 | registerOnTouched(fn) {
|
80 | this._onTouchedCb = fn;
|
81 | }
|
82 | |
83 |
|
84 |
|
85 |
|
86 | inputChange(event) {
|
87 | const { checked } = ( (event.target));
|
88 | if (this._onChangeCb) {
|
89 | this._onChangeCb(checked);
|
90 | }
|
91 | }
|
92 | |
93 |
|
94 |
|
95 | focus() {
|
96 | if (this.input && this.input.nativeElement) {
|
97 | this.input.nativeElement.focus();
|
98 | }
|
99 | }
|
100 | }
|
101 | CheckboxComponent.decorators = [
|
102 | { type: Component, args: [{
|
103 | selector: 'bm-checkbox',
|
104 | template: "<label class=\"bm-checkbox__label-container\">\n <input\n #input\n type=\"checkbox\"\n class=\"bm-visual-hidden\"\n [checked]=\"checked\"\n [required]=\"required\"\n [disabled]=\"disabled\"\n [name]=\"name\"\n [tabIndex]=\"tabIndex\"\n [readonly]=\"readonly\"\n [attr.aria-label]=\"ariaLabel\"\n [attr.aria-labelledby]=\"ariaLabelledby\"\n (change)=\"inputChange($event)\"\n (focus)=\"inputFocus()\"\n (blur)=\"inputBlur()\"\n />\n <div class=\"bm-checkbox__container\">\n <svg\n *ngIf=\"checked\"\n [@fadeInOut]=\"'in'\"\n class=\"bm-checkbox__tick\"\n xml:space=\"preserve\"\n version=\"1.1\"\n viewBox=\"0 0 24 24\"\n xmlns=\"http://www.w3.org/2000/svg\"\n >\n <path\n class=\"bm-checkbox__tick-path\"\n d=\"M4.1,12.7 9,17.6 20.3,6.3\"\n fill=\"none\"\n stroke=\"white\"\n ></path>\n </svg>\n </div>\n <span class=\"bm-checkbox__label\"><ng-content></ng-content></span>\n</label>\n",
|
105 | changeDetection: ChangeDetectionStrategy.OnPush,
|
106 | encapsulation: ViewEncapsulation.None,
|
107 | providers: [
|
108 | {
|
109 | provide: NG_VALUE_ACCESSOR,
|
110 |
|
111 | useExisting: forwardRef(( |
112 |
|
113 |
|
114 | () => CheckboxComponent)),
|
115 | multi: true,
|
116 | },
|
117 | ],
|
118 | animations: [
|
119 | trigger('fadeInOut', [
|
120 | state('in', style({ opacity: 1 })),
|
121 | transition('void => *', [
|
122 | style({
|
123 | opacity: 0,
|
124 | }),
|
125 | animate('0.2s ease-in'),
|
126 | ]),
|
127 | transition('* => void', [
|
128 | animate('0.2s ease-out', style({
|
129 | opacity: 0,
|
130 | })),
|
131 | ]),
|
132 | ]),
|
133 | ],
|
134 | styles: [".bm-checkbox--focused .bm-checkbox-container{border-color:var(--bm-checkbox-path-color,#4399fd)}.bm-checkbox__container{border:1px solid var(--secondary-color,#bcbcbc);border-radius:3px;display:inline-block;width:1em;height:1em;position:relative;vertical-align:middle}.bm-checkbox__tick{position:absolute}.bm-checkbox__tick-path{stroke:var(--bm-checkbox-path-color,#4399fd);stroke-width:5px;vertical-align:middle}"]
|
135 | }] }
|
136 | ];
|
137 |
|
138 | CheckboxComponent.ctorParameters = () => [
|
139 | { type: Renderer2 },
|
140 | { type: ChangeDetectorRef }
|
141 | ];
|
142 | CheckboxComponent.propDecorators = {
|
143 | input: [{ type: ViewChild, args: ['input', { static: true },] }],
|
144 | isFocused: [{ type: HostBinding, args: ['class.bm-checkbox--focused',] }],
|
145 | name: [{ type: Input }],
|
146 | tabIndex: [{ type: Input }],
|
147 | required: [{ type: Input }],
|
148 | disabled: [{ type: Input }],
|
149 | readonly: [{ type: Input }],
|
150 | ariaLabel: [{ type: Input, args: ['aria-label',] }],
|
151 | ariaLabelledby: [{ type: Input, args: ['aria-labelledby',] }],
|
152 | checked: [{ type: Input }]
|
153 | };
|
154 |
|
155 |
|
156 |
|
157 |
|
158 |
|
159 | class CheckboxComponentModule {
|
160 | }
|
161 | CheckboxComponentModule.decorators = [
|
162 | { type: NgModule, args: [{
|
163 | imports: [CommonModule],
|
164 | declarations: [CheckboxComponent],
|
165 | exports: [CheckboxComponent],
|
166 | },] }
|
167 | ];
|
168 |
|
169 | export { CheckboxComponent, CheckboxComponentModule };
|
170 |
|