@@ -22,12 +22,6 @@ import {MdRipple} from '../../ripple/ripple.js';
2222// Disable warning for classMap with destructuring
2323// tslint:disable:quoted-properties-on-dictionary
2424
25- /** The default value for a continuous slider. */
26- const DEFAULT_VALUE = 50 ;
27- /** The default start value for a range slider. */
28- const DEFAULT_VALUE_START = 25 ;
29- /** The default end value for a range slider. */
30- const DEFAULT_VALUE_END = 75 ;
3125
3226/**
3327 * Slider component.
@@ -61,19 +55,17 @@ export class Slider extends LitElement {
6155 /**
6256 * The slider value displayed when range is false.
6357 */
64- @property ( { type : Number } ) value = DEFAULT_VALUE ;
58+ @property ( { type : Number } ) value ?: number ;
6559
6660 /**
6761 * The slider start value displayed when range is true.
6862 */
69- @property ( { type : Number , attribute : 'value-start' } )
70- valueStart = DEFAULT_VALUE_START ;
63+ @property ( { type : Number , attribute : 'value-start' } ) valueStart ?: number ;
7164
7265 /**
7366 * The slider end value displayed when range is true.
7467 */
75- @property ( { type : Number , attribute : 'value-end' } )
76- valueEnd = DEFAULT_VALUE_END ;
68+ @property ( { type : Number , attribute : 'value-end' } ) valueEnd ?: number ;
7769
7870 /**
7971 * An optional label for the slider's value displayed when range is
@@ -195,8 +187,8 @@ export class Slider extends LitElement {
195187 @state ( ) private startOnTop = false ;
196188 @state ( ) private handlesOverlapping = false ;
197189
198- @state ( ) private renderValueStart = 0 ;
199- @state ( ) private renderValueEnd = 0 ;
190+ @state ( ) private renderValueStart ?: number ;
191+ @state ( ) private renderValueEnd ?: number ;
200192
201193 // used in synthetic events generated to control ripple hover state.
202194 private ripplePointerId = 1 ;
@@ -229,12 +221,12 @@ export class Slider extends LitElement {
229221 protected override willUpdate ( changed : PropertyValues ) {
230222 this . renderValueStart = changed . has ( 'valueStart' ) ?
231223 this . valueStart :
232- ( this . inputStart ?. valueAsNumber ?? 0 ) ;
224+ this . inputStart ?. valueAsNumber ;
233225 const endValueChanged =
234226 ( changed . has ( 'valueEnd' ) && this . range ) || changed . has ( 'value' ) ;
235227 this . renderValueEnd = endValueChanged ?
236228 ( this . range ? this . valueEnd : this . value ) :
237- ( this . inputEnd ?. valueAsNumber ?? 0 ) ;
229+ this . inputEnd ?. valueAsNumber ;
238230 // manually handle ripple hover state since the handle is pointer events
239231 // none.
240232 if ( changed . get ( 'handleStartHover' ) !== undefined ) {
@@ -269,6 +261,26 @@ export class Slider extends LitElement {
269261 this . renderValueStart = this . inputStart ! . valueAsNumber ;
270262 }
271263 this . renderValueEnd = this . inputEnd ! . valueAsNumber ;
264+ // update values if they are unset
265+ // when using a range, default to equi-distant between
266+ // min - valueStart - valueEnd - max
267+ if ( this . range ) {
268+ const segment = ( this . max - this . min ) / 3 ;
269+ if ( this . valueStart === undefined ) {
270+ this . inputStart ! . valueAsNumber = this . min + segment ;
271+ // read actual value from input
272+ const v = this . inputStart ! . valueAsNumber ;
273+ this . valueStart = this . renderValueStart = v ;
274+ }
275+ if ( this . valueEnd === undefined ) {
276+ this . inputEnd ! . valueAsNumber = this . min + 2 * segment ;
277+ // read actual value from input
278+ const v = this . inputEnd ! . valueAsNumber ;
279+ this . valueEnd = this . renderValueEnd = v ;
280+ }
281+ } else {
282+ this . value ??= this . renderValueEnd ;
283+ }
272284 if ( changed . has ( 'range' ) || changed . has ( 'renderValueStart' ) ||
273285 changed . has ( 'renderValueEnd' ) || this . isUpdatePending ) {
274286 this . handlesOverlapping = isOverlapping ( this . handleStart , this . handleEnd ) ;
@@ -281,9 +293,10 @@ export class Slider extends LitElement {
281293 protected override render ( ) {
282294 const step = this . step === 0 ? 1 : this . step ;
283295 const range = Math . max ( this . max - this . min , step ) ;
284- const startFraction =
285- this . range ? ( ( this . renderValueStart - this . min ) / range ) : 0 ;
286- const endFraction = ( this . renderValueEnd - this . min ) / range ;
296+ const startFraction = this . range ?
297+ ( ( ( this . renderValueStart ?? this . min ) - this . min ) / range ) :
298+ 0 ;
299+ const endFraction = ( ( this . renderValueEnd ?? this . min ) - this . min ) / range ;
287300 const containerStyles = {
288301 // for clipping inputs and active track.
289302 '--slider-start-fraction' : String ( startFraction ) ,
@@ -375,11 +388,8 @@ export class Slider extends LitElement {
375388 </ div > ` ;
376389 }
377390
378- private renderInput ( { start, value, label} : {
379- start : boolean ,
380- value : number ,
381- label : string ,
382- } ) {
391+ private renderInput ( { start, value, label} :
392+ { start : boolean ; value ?: number ; label : string ; } ) {
383393 const name = start ? `start` : `end` ;
384394 // when ranged, ensure announcement includes value info.
385395 // Needed for closure conformance
@@ -619,27 +629,27 @@ export class Slider extends LitElement {
619629 /** @private */
620630 formResetCallback ( ) {
621631 if ( this . range ) {
622- this . valueStart =
623- Number ( this . getAttribute ( 'value-start' ) ?? DEFAULT_VALUE_START ) ;
624- this . valueEnd =
625- Number ( this . getAttribute ( 'value-end' ) ?? DEFAULT_VALUE_END ) ;
632+ const valueStart = this . getAttribute ( 'value-start' ) ;
633+ this . valueStart = valueStart !== null ? Number ( valueStart ) : undefined ;
634+ const valueEnd = this . getAttribute ( 'value-end' ) ;
635+ this . valueEnd = valueEnd !== null ? Number ( valueEnd ) : undefined ;
626636 return ;
627637 }
628-
629- this . value = Number ( this . getAttribute ( ' value' ) ?? DEFAULT_VALUE ) ;
638+ const value = this . getAttribute ( 'value' ) ;
639+ this . value = value !== null ? Number ( value ) : undefined ;
630640 }
631641
632642 /** @private */
633643 formStateRestoreCallback ( state : string | Array < [ string , string ] > | null ) {
634644 if ( Array . isArray ( state ) ) {
635645 const [ [ , valueStart ] , [ , valueEnd ] ] = state ;
636- this . valueStart = Number ( valueStart ?? DEFAULT_VALUE_START ) ;
637- this . valueEnd = Number ( valueEnd ?? DEFAULT_VALUE_START ) ;
646+ this . valueStart = Number ( valueStart ) ;
647+ this . valueEnd = Number ( valueEnd ) ;
638648 this . range = true ;
639649 return ;
640650 }
641651
642- this . value = Number ( state ?? DEFAULT_VALUE ) ;
652+ this . value = Number ( state ) ;
643653 this . range = false ;
644654 }
645655}
0 commit comments