Skip to content

Commit f90baaa

Browse files
committed
fixup! refactor(mdc-prototypes): do not mark components dirty in input setters
Proposal to just not support programmatic input updates
1 parent 1d4c09a commit f90baaa

File tree

4 files changed

+28
-93
lines changed

4 files changed

+28
-93
lines changed

src/material-experimental/mdc-checkbox/checkbox.spec.ts

Lines changed: 24 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import {dispatchFakeEvent} from '@angular/cdk/testing';
2-
import {ChangeDetectionStrategy, Component, DebugElement, Type, ViewChild} from '@angular/core';
2+
import {ChangeDetectionStrategy, Component, DebugElement, Type} from '@angular/core';
33
import {
44
ComponentFixture,
55
fakeAsync,
@@ -68,6 +68,29 @@ describe('MatCheckbox', () => {
6868
expect(inputElement.checked).toBe(false);
6969
}));
7070

71+
72+
it('should toggle checkbox ripple disabledness correctly', fakeAsync(() => {
73+
const rippleSelector = '.mat-ripple-element:not(.mat-checkbox-persistent-ripple)';
74+
const labelElement = checkboxNativeElement.querySelector('label') as HTMLLabelElement;
75+
76+
testComponent.isDisabled = true;
77+
fixture.detectChanges();
78+
dispatchFakeEvent(labelElement, 'mousedown');
79+
dispatchFakeEvent(labelElement, 'mouseup');
80+
labelElement.click();
81+
expect(checkboxNativeElement.querySelectorAll(rippleSelector).length).toBe(0);
82+
83+
flush();
84+
testComponent.isDisabled = false;
85+
fixture.detectChanges();
86+
dispatchFakeEvent(labelElement, 'mousedown');
87+
dispatchFakeEvent(labelElement, 'mouseup');
88+
labelElement.click();
89+
expect(checkboxNativeElement.querySelectorAll(rippleSelector).length).toBe(1);
90+
91+
flush();
92+
}));
93+
7194
it('should add and remove indeterminate state', fakeAsync(() => {
7295
expect(inputElement.checked).toBe(false);
7396
expect(inputElement.indeterminate).toBe(false);
@@ -689,64 +712,6 @@ describe('MatCheckbox', () => {
689712
}));
690713
});
691714

692-
describe('using ViewChild', () => {
693-
let checkboxDebugElement: DebugElement;
694-
let checkboxNativeElement: HTMLElement;
695-
let testComponent: CheckboxUsingViewChild;
696-
697-
beforeEach(() => {
698-
fixture = createComponent(CheckboxUsingViewChild);
699-
fixture.detectChanges();
700-
701-
checkboxDebugElement = fixture.debugElement.query(By.directive(MatCheckbox));
702-
checkboxNativeElement = checkboxDebugElement.nativeElement;
703-
testComponent = fixture.debugElement.componentInstance;
704-
});
705-
706-
it('should toggle checkbox disabledness correctly', fakeAsync(() => {
707-
const checkboxInstance = checkboxDebugElement.componentInstance;
708-
const inputElement = <HTMLInputElement>checkboxNativeElement.querySelector('input');
709-
expect(checkboxInstance.disabled).toBe(false);
710-
expect(inputElement.tabIndex).toBe(0);
711-
expect(inputElement.disabled).toBe(false);
712-
713-
testComponent.isDisabled = true;
714-
fixture.detectChanges();
715-
716-
expect(checkboxInstance.disabled).toBe(true);
717-
expect(inputElement.disabled).toBe(true);
718-
719-
testComponent.isDisabled = false;
720-
fixture.detectChanges();
721-
722-
expect(checkboxInstance.disabled).toBe(false);
723-
expect(inputElement.tabIndex).toBe(0);
724-
expect(inputElement.disabled).toBe(false);
725-
}));
726-
727-
it('should toggle checkbox ripple disabledness correctly', fakeAsync(() => {
728-
const rippleSelector = '.mat-ripple-element:not(.mat-checkbox-persistent-ripple)';
729-
const labelElement = checkboxNativeElement.querySelector('label') as HTMLLabelElement;
730-
731-
testComponent.isDisabled = true;
732-
fixture.detectChanges();
733-
dispatchFakeEvent(labelElement, 'mousedown');
734-
dispatchFakeEvent(labelElement, 'mouseup');
735-
labelElement.click();
736-
expect(checkboxNativeElement.querySelectorAll(rippleSelector).length).toBe(0);
737-
738-
flush();
739-
testComponent.isDisabled = false;
740-
fixture.detectChanges();
741-
dispatchFakeEvent(labelElement, 'mousedown');
742-
dispatchFakeEvent(labelElement, 'mouseup');
743-
labelElement.click();
744-
expect(checkboxNativeElement.querySelectorAll(rippleSelector).length).toBe(1);
745-
746-
flush();
747-
}));
748-
});
749-
750715
describe('with multiple checkboxes', () => {
751716
beforeEach(() => {
752717
fixture = createComponent(MultipleCheckboxes);
@@ -1034,21 +999,6 @@ class CheckboxWithTabIndex {
1034999
isDisabled: boolean = false;
10351000
}
10361001

1037-
1038-
/** Simple test component that accesses MatCheckbox using ViewChild. */
1039-
@Component({
1040-
template: `
1041-
<mat-checkbox></mat-checkbox>`,
1042-
})
1043-
class CheckboxUsingViewChild {
1044-
@ViewChild(MatCheckbox, {static: false}) checkbox: MatCheckbox;
1045-
1046-
set isDisabled(value: boolean) {
1047-
this.checkbox.disabled = value;
1048-
this.checkbox.markForCheck();
1049-
}
1050-
}
1051-
10521002
/** Simple test component with an aria-label set. */
10531003
@Component({template: `<mat-checkbox aria-label="Super effective"></mat-checkbox>`})
10541004
class CheckboxWithAriaLabel {

src/material-experimental/mdc-checkbox/checkbox.ts

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -302,14 +302,6 @@ export class MatCheckbox implements AfterViewInit, OnDestroy, ControlValueAccess
302302
this.checked = !this.checked;
303303
}
304304

305-
/**
306-
* Marks the component for check. This can be called after inputs have been updated
307-
* programmatically and the component should be updated to reflect the changes.
308-
*/
309-
markForCheck() {
310-
this._changeDetectorRef.markForCheck();
311-
}
312-
313305
/** Triggers the checkbox ripple. */
314306
_activateRipple() {
315307
if (!this.disabled && !this.disableRipple && this._animationMode != 'NoopAnimations') {

src/material-experimental/mdc-slide-toggle/slide-toggle.spec.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -487,8 +487,7 @@ describe('MatSlideToggle with forms', () => {
487487
// The control should start off with being untouched.
488488
expect(slideToggleModel.touched).toBe(false);
489489

490-
slideToggle.checked = true;
491-
slideToggle.markForCheck();
490+
testComponent.isChecked = true;
492491
fixture.detectChanges();
493492

494493
expect(slideToggleModel.touched).toBe(false);
@@ -728,10 +727,12 @@ class SlideToggleWithForm {
728727
}
729728

730729
@Component({
731-
template: `<mat-slide-toggle [(ngModel)]="modelValue" [disabled]="isDisabled"></mat-slide-toggle>`
730+
template: `<mat-slide-toggle [(ngModel)]="modelValue" [disabled]="isDisabled"
731+
[checked]="isChecked"></mat-slide-toggle>`
732732
})
733733
class SlideToggleWithModel {
734734
modelValue = false;
735+
isChecked = false;
735736
isDisabled = false;
736737
}
737738

src/material-experimental/mdc-slide-toggle/slide-toggle.ts

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -294,14 +294,6 @@ export class MatSlideToggle implements ControlValueAccessor, AfterViewInit, OnDe
294294
this._onChange(this.checked);
295295
}
296296

297-
/**
298-
* Marks the component for check. This can be called after inputs have been updated
299-
* programmatically and the component should be updated to reflect the changes.
300-
*/
301-
markForCheck() {
302-
this._changeDetectorRef.markForCheck();
303-
}
304-
305297
/** Handles blur events on the native input. */
306298
_onBlur() {
307299
// When a focused element becomes disabled, the browser *immediately* fires a blur event.

0 commit comments

Comments
 (0)