Skip to content

Commit

Permalink
fix(material-experimental/mdc-slider): throw error when thumb is miss…
Browse files Browse the repository at this point in the history
…ing (#24061)

Adds a proper error when the MDC slider is missing a thumb. Currently we eventually hit a runtime error which isn't easy to debug.

Fixes #24057.
  • Loading branch information
crisbeto committed Dec 7, 2021
1 parent a52da04 commit 749edd8
Showing 1 changed file with 18 additions and 14 deletions.
32 changes: 18 additions & 14 deletions src/material-experimental/mdc-slider/slider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -706,6 +706,7 @@ export class MatSlider

ngAfterViewInit() {
if (typeof ngDevMode === 'undefined' || ngDevMode) {
_validateThumbs(this._isRange(), this._getThumb(Thumb.START), this._getThumb(Thumb.END));
_validateInputs(
this._isRange(),
this._getInputElement(Thumb.START),
Expand Down Expand Up @@ -1202,25 +1203,28 @@ class SliderAdapter implements MDCSliderAdapter {
};
}

/**
* Ensures that there is not an invalid configuration for the slider thumb inputs.
*/
/** Ensures that there is not an invalid configuration for the slider thumb inputs. */
function _validateInputs(
isRange: boolean,
startInputElement: HTMLInputElement,
endInputElement: HTMLInputElement,
): void {
if (isRange) {
if (!startInputElement.hasAttribute('matSliderStartThumb')) {
_throwInvalidInputConfigurationError();
}
if (!endInputElement.hasAttribute('matSliderEndThumb')) {
_throwInvalidInputConfigurationError();
}
} else {
if (!endInputElement.hasAttribute('matSliderThumb')) {
_throwInvalidInputConfigurationError();
}
const startValid = !isRange || startInputElement.hasAttribute('matSliderStartThumb');
const endValid = endInputElement.hasAttribute(isRange ? 'matSliderEndThumb' : 'matSliderThumb');

if (!startValid || !endValid) {
_throwInvalidInputConfigurationError();
}
}

/** Validates that the slider has the correct set of thumbs. */
function _validateThumbs(
isRange: boolean,
start: MatSliderVisualThumb | undefined,
end: MatSliderVisualThumb | undefined,
): void {
if (!end && (!isRange || !start)) {
_throwInvalidInputConfigurationError();
}
}

Expand Down

0 comments on commit 749edd8

Please sign in to comment.