-
Notifications
You must be signed in to change notification settings - Fork 6.8k
/
Copy pathcheckbox-demo.ts
140 lines (127 loc) · 3.63 KB
/
checkbox-demo.ts
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.dev/license
*/
import {ChangeDetectionStrategy, Component, Directive} from '@angular/core';
import {FormsModule, ReactiveFormsModule} from '@angular/forms';
import {MAT_CHECKBOX_DEFAULT_OPTIONS, MatCheckboxModule} from '@angular/material/checkbox';
import {MatPseudoCheckboxModule, ThemePalette} from '@angular/material/core';
import {MatInputModule} from '@angular/material/input';
import {MatSelectModule} from '@angular/material/select';
import {MatTooltip} from '@angular/material/tooltip';
export interface Task {
name: string;
completed: boolean;
subtasks?: Task[];
}
@Directive({
selector: '[clickActionNoop]',
providers: [{provide: MAT_CHECKBOX_DEFAULT_OPTIONS, useValue: {clickAction: 'noop'}}],
})
export class ClickActionNoop {}
@Directive({
selector: '[clickActionCheck]',
providers: [{provide: MAT_CHECKBOX_DEFAULT_OPTIONS, useValue: {clickAction: 'check'}}],
})
export class ClickActionCheck {}
@Component({
selector: 'mat-checkbox-demo-nested-checklist',
styles: `
li {
margin-bottom: 4px;
}
`,
templateUrl: 'nested-checklist.html',
imports: [MatCheckboxModule, FormsModule],
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class MatCheckboxDemoNestedChecklist {
tasks: Task[] = [
{
name: 'Reminders',
completed: false,
subtasks: [
{name: 'Cook Dinner', completed: false},
{name: 'Read the Material Design Spec', completed: false},
{name: 'Upgrade Application to Angular', completed: false},
],
},
{
name: 'Groceries',
completed: false,
subtasks: [
{name: 'Organic Eggs', completed: false},
{name: 'Protein Powder', completed: false},
{name: 'Almond Meal Flour', completed: false},
],
},
];
allComplete(task: Task): boolean {
const subtasks = task.subtasks;
return task.completed || (subtasks != null && subtasks.every(t => t.completed));
}
someComplete(tasks: Task[] | undefined | null): boolean {
if (tasks == null) {
return false;
}
const numComplete = tasks.filter(t => t.completed).length;
return numComplete > 0 && numComplete < tasks.length;
}
setAllCompleted(tasks: Task[] | undefined | null, completed: boolean): void {
if (tasks == null) {
return;
}
tasks.forEach(t => (t.completed = completed));
}
}
@Component({
selector: 'checkbox-demo',
templateUrl: 'checkbox-demo.html',
styleUrl: 'checkbox-demo.css',
imports: [
FormsModule,
MatCheckboxModule,
MatInputModule,
MatSelectModule,
MatPseudoCheckboxModule,
ReactiveFormsModule,
MatCheckboxDemoNestedChecklist,
ClickActionNoop,
ClickActionCheck,
MatTooltip,
],
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class CheckboxDemo {
isIndeterminate = false;
isChecked = false;
isDisabled = false;
isDisabledInteractive = false;
labelPosition: 'before' | 'after' = 'after';
useAlternativeColor = false;
demoRequired = false;
demoLabelAfter = false;
demoChecked = false;
demoDisabled = false;
demoIndeterminate = false;
demoLabel: string;
demoLabelledBy: string;
demoId: string;
demoName: string;
demoValue: string;
demoColor: ThemePalette = 'primary';
demoDisableRipple = false;
demoHideLabel = false;
printResult() {
if (this.isIndeterminate) {
return 'Maybe!';
}
return this.isChecked ? 'Yes!' : 'No!';
}
checkboxColor() {
return this.useAlternativeColor ? 'primary' : 'accent';
}
}