Skip to content

Commit c4eb418

Browse files
committed
fix(material-experimental/mdc-slider): init step on thumb inputs (#21971)
* fix(material-experimental/mdc-slider): init step on thumb inputs
1 parent 4aaa4d3 commit c4eb418

File tree

1 file changed

+22
-16
lines changed
  • src/material-experimental/mdc-slider

1 file changed

+22
-16
lines changed

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

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ export class MatSliderThumb implements AfterViewInit {
8080
/** The current value of this slider input. */
8181
@Input()
8282
get value(): number {
83-
return coerceNumberProperty(this._elementRef.nativeElement.getAttribute('value'));
83+
return coerceNumberProperty(this._hostElement.getAttribute('value'));
8484
}
8585
set value(v: number) {
8686
const value = coerceNumberProperty(v);
@@ -91,7 +91,7 @@ export class MatSliderThumb implements AfterViewInit {
9191
this._slider._setValue(value, this._thumbPosition);
9292
} else {
9393
// Setup for the MDC foundation.
94-
this._elementRef.nativeElement.setAttribute('value', `${value}`);
94+
this._hostElement.setAttribute('value', `${value}`);
9595
}
9696
}
9797

@@ -114,36 +114,41 @@ export class MatSliderThumb implements AfterViewInit {
114114
? Thumb.START
115115
: Thumb.END;
116116

117+
/** The injected document if available or fallback to the global document reference. */
117118
private _document: Document;
118119

120+
/** The host native HTML input element. */
121+
_hostElement: HTMLInputElement;
122+
119123
constructor(
120124
@Inject(DOCUMENT) document: any,
121125
private readonly _slider: MatSlider,
122-
readonly _elementRef: ElementRef<HTMLInputElement>,
126+
private readonly _elementRef: ElementRef<HTMLInputElement>,
123127
) {
124128
this._document = document;
129+
this._hostElement = _elementRef.nativeElement;
125130
// By calling this in the constructor we guarantee that the sibling sliders initial value by
126131
// has already been set by the time we reach ngAfterViewInit().
127132
this._initializeInputValueAttribute();
128133
}
129134

130135
ngAfterViewInit() {
131-
this._initializeInputMinMax();
136+
this._initializeInputState();
132137
this._initializeInputValueProperty();
133138

134139
// Setup for the MDC foundation.
135140
if (this._slider.disabled) {
136-
this._elementRef.nativeElement.disabled = true;
141+
this._hostElement.disabled = true;
137142
}
138143
}
139144

140145
/** Returns true if this slider input currently has focus. */
141146
_isFocused(): boolean {
142-
return this._document.activeElement === this._elementRef.nativeElement;
147+
return this._document.activeElement === this._hostElement;
143148
}
144149

145150
/**
146-
* Sets the min and max properties on the slider thumb input.
151+
* Sets the min, max, and step properties on the slider thumb input.
147152
*
148153
* Must be called AFTER the sibling slider thumb input is guaranteed to have had its value
149154
* attribute value set. For a range slider, the min and max of the slider thumb input depends on
@@ -155,15 +160,16 @@ export class MatSliderThumb implements AfterViewInit {
155160
* instead be capped at either the default min or max.
156161
*
157162
*/
158-
private _initializeInputMinMax(): void {
159-
const min = this._elementRef.nativeElement.hasAttribute('matSliderEndThumb')
163+
private _initializeInputState(): void {
164+
const min = this._hostElement.hasAttribute('matSliderEndThumb')
160165
? this._slider._getInput(Thumb.START).value
161166
: this._slider.min;
162-
const max = this._elementRef.nativeElement.hasAttribute('matSliderStartThumb')
167+
const max = this._hostElement.hasAttribute('matSliderStartThumb')
163168
? this._slider._getInput(Thumb.END).value
164169
: this._slider.max;
165-
this._elementRef.nativeElement.min = `${min}`;
166-
this._elementRef.nativeElement.max = `${max}`;
170+
this._hostElement.min = `${min}`;
171+
this._hostElement.max = `${max}`;
172+
this._hostElement.step = `${this._slider.step}`;
167173
}
168174

169175
/**
@@ -175,7 +181,7 @@ export class MatSliderThumb implements AfterViewInit {
175181
* instead be capped at either the default min or max.
176182
*/
177183
private _initializeInputValueProperty(): void {
178-
this._elementRef.nativeElement.value = `${this.value}`;
184+
this._hostElement.value = `${this.value}`;
179185
}
180186

181187
/**
@@ -186,8 +192,8 @@ export class MatSliderThumb implements AfterViewInit {
186192
*/
187193
private _initializeInputValueAttribute(): void {
188194
// Only set the default value if an initial value has not already been provided.
189-
if (!this._elementRef.nativeElement.hasAttribute('value')) {
190-
this.value = this._elementRef.nativeElement.hasAttribute('matSliderEndThumb')
195+
if (!this._hostElement.hasAttribute('value')) {
196+
this.value = this._hostElement.hasAttribute('matSliderEndThumb')
191197
? this._slider.max
192198
: this._slider.min;
193199
}
@@ -369,7 +375,7 @@ export class MatSlider implements AfterViewInit, OnDestroy {
369375

370376
/** Gets the slider thumb HTML input element of the given thumb position. */
371377
_getInputElement(thumbPosition: Thumb): HTMLInputElement {
372-
return this._getInput(thumbPosition)._elementRef.nativeElement;
378+
return this._getInput(thumbPosition)._hostElement;
373379
}
374380

375381
/** Gets the slider thumb HTML element of the given thumb position. */

0 commit comments

Comments
 (0)