Skip to content

Commit

Permalink
feat(docs): checkbox indeterminate example (#236)
Browse files Browse the repository at this point in the history
  • Loading branch information
Margar1ta authored and pimenovoleg committed Sep 27, 2019
1 parent fb885b8 commit 074e97a
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1 +1 @@
/** No CSS for this example */
/** No CSS for this example **/
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
<mc-checkbox [checked]="true" [indeterminate]="true"> Indeterminate</mc-checkbox>
<div class="mc-body">
<mc-checkbox [checked]="parentChecked" [indeterminate]="parentIndeterminate" (change)="toggleChecked()" > All fruits</mc-checkbox>
<p *ngFor="let fruit of fruits; let i = index"><mc-checkbox [checked]="fruit.checked" (change)="updateCheckboxes(i)" >{{fruit.name}}</mc-checkbox>
</div>

Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import { Component } from '@angular/core';
import { ChangeDetectorRef, Component } from '@angular/core';


interface ICheckbox {
name: string;
checked: boolean;
}


/**
Expand All @@ -9,4 +15,46 @@ import { Component } from '@angular/core';
templateUrl: 'checkbox-indeterminate-example.html',
styleUrls: ['checkbox-indeterminate-example.css']
})
export class CheckboxIndeterminateExample {}
export class CheckboxIndeterminateExample {
parentIndeterminate = true;
parentChecked = true;

fruits: ICheckbox[] = [
{ name: 'Apples', checked: true },
{ name: 'Bananas', checked: false },
{ name: 'Grapes', checked: false }
];

constructor(private ref: ChangeDetectorRef) {}

updateCheckboxes(index: number) {
this.toggleFruitChecked(index);
this.updateIndeterminate();
this.ref.detectChanges();
}

toggleFruitChecked(index: number) {
this.fruits[index].checked = !this.fruits[index].checked;
}

toggleChecked() {
this.parentChecked = !this.parentChecked;
for (const fruit of this.fruits) {
fruit.checked = this.parentChecked;
}
this.parentIndeterminate = false;
this.ref.detectChanges();
}

updateIndeterminate() {
let checked: number = 0;
let unchecked: number = 0;
const length = this.fruits.length;
this.fruits.forEach((fruit) => {
fruit.checked ? checked++ : unchecked++;
});
this.parentIndeterminate = (checked !== length && unchecked !== length);
this.parentChecked = this.parentIndeterminate || length === checked;
}
}

0 comments on commit 074e97a

Please sign in to comment.