Skip to content

Commit

Permalink
fix(material-experimental/mdc-slider): fix a few null pointer exceptions
Browse files Browse the repository at this point in the history
Fixes a few null pointer errors if:
1. `setDisabledState` is called too early. This can't happen at the moment, but it showed up when trying to fix something in `@angular/forms` and it may become an issue in the future.
2. The component is destroyed before the initialization is complete. This showed up while investigating the first error.
  • Loading branch information
crisbeto committed Nov 7, 2021
1 parent be9abca commit fa1146d
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
9 changes: 8 additions & 1 deletion src/material-experimental/mdc-slider/slider.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,13 @@ describe('MDC-based MatSlider', () => {
}

describe('standard slider', () => {
let fixture: ComponentFixture<StandardSlider>;
let sliderInstance: MatSlider;
let inputInstance: MatSliderThumb;

beforeEach(
waitForAsync(() => {
const fixture = createComponent(StandardSlider);
fixture = createComponent(StandardSlider);
fixture.detectChanges();
const sliderDebugElement = fixture.debugElement.query(By.directive(MatSlider));
sliderInstance = sliderDebugElement.componentInstance;
Expand Down Expand Up @@ -104,6 +105,12 @@ describe('MDC-based MatSlider', () => {
expect(inputInstance.value).toBe(10);
sliderInstance._elementRef.nativeElement.style.marginLeft = 'initial';
});

it('should not throw if destroyed before initialization is complete', () => {
fixture.destroy();
fixture = TestBed.createComponent(StandardSlider);
expect(() => fixture.destroy()).not.toThrow();
});
});

describe('standard range slider', () => {
Expand Down
10 changes: 6 additions & 4 deletions src/material-experimental/mdc-slider/slider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -841,7 +841,7 @@ export class MatSlider

/** Sets the disabled state based on the disabled state of the inputs (ControlValueAccessor). */
_updateDisabled(): void {
const disabled = this._inputs.some(input => input._disabled);
const disabled = this._inputs ? this._inputs.some(input => input._disabled) : false;
this._setDisabled(disabled);
}

Expand Down Expand Up @@ -1152,7 +1152,9 @@ class SliderAdapter implements MDCSliderAdapter {
evtType: K,
handler: SpecificEventListener<K>,
): void => {
this._delegate._getThumbElement(thumbPosition).removeEventListener(evtType, handler);
if (this._delegate._initialized) {
this._delegate._getThumbElement(thumbPosition).removeEventListener(evtType, handler);
}
};
registerInputEventHandler = <K extends EventType>(
thumbPosition: Thumb,
Expand All @@ -1161,7 +1163,7 @@ class SliderAdapter implements MDCSliderAdapter {
): void => {
if (evtType === 'change') {
this._saveChangeEventHandler(thumbPosition, handler as SpecificEventListener<EventType>);
} else {
} else if (this._delegate._initialized) {
this._delegate._getInputElement(thumbPosition).addEventListener(evtType, handler);
}
};
Expand All @@ -1172,7 +1174,7 @@ class SliderAdapter implements MDCSliderAdapter {
): void => {
if (evtType === 'change') {
this._globalEventSubscriptions.unsubscribe();
} else {
} else if (this._delegate._initialized) {
this._delegate._getInputElement(thumbPosition).removeEventListener(evtType, handler);
}
};
Expand Down

0 comments on commit fa1146d

Please sign in to comment.