@@ -2,7 +2,7 @@ import React from 'react';
22import Slider from './Slider' ;
33import Track from './Track' ;
44import ValueTransformer from './ValueTransformer' ;
5- import { autobind , captialize , clamp , distanceTo , extend } from './util' ;
5+ import { autobind , captialize , clamp , distanceTo , extend , isEmpty , isNumber , omit } from './util' ;
66import { maxMinValuePropType } from './propTypes' ;
77import defaultClassNames from './defaultClassNames' ;
88
@@ -53,6 +53,7 @@ class InputRange extends React.Component {
5353
5454 // Initial state
5555 const state = {
56+ didChange : false ,
5657 percentages : {
5758 min : 0 ,
5859 max : 0 ,
@@ -69,7 +70,8 @@ class InputRange extends React.Component {
6970
7071 this . state = state ;
7172 this . valueTransformer = new ValueTransformer ( this ) ;
72- this . isMultiValue = this . props . hasOwnProperty ( 'values' ) ;
73+ this . isMultiValue = this . props . hasOwnProperty ( 'defaultValues' ) ||
74+ this . props . hasOwnProperty ( 'values' ) ;
7375
7476 // Auto-bind
7577 autobind ( [
@@ -85,24 +87,27 @@ class InputRange extends React.Component {
8587 }
8688
8789 componentWillReceiveProps ( nextProps ) {
88- this . setPositionsByProps ( nextProps ) ;
90+ const props = omit ( nextProps , [ 'defaultValue' , 'defaultValues' ] ) ;
91+
92+ this . setPositionsByProps ( props ) ;
8993 }
9094
9195 shouldComponentUpdate ( nextProps , nextState ) {
9296 const currentProps = this . props ;
9397 const currentState = this . state ;
94-
95- return (
98+ const shouldUpdate = (
9699 currentState . values . min !== nextState . values . min ||
97100 currentState . values . max !== nextState . values . max ||
98101 currentState . value !== nextState . value ||
99102 currentProps . minValue !== nextProps . minValue ||
100103 currentProps . maxValue !== nextProps . maxValue
101104 ) ;
105+
106+ return shouldUpdate ;
102107 }
103108
104109 componentDidUpdate ( ) {
105- if ( this . props . onChange ) {
110+ if ( this . props . onChange && this . state . didChange ) {
106111 let results = this . state . values . max ;
107112
108113 if ( this . isMultiValue ) {
@@ -111,6 +116,10 @@ class InputRange extends React.Component {
111116
112117 this . props . onChange ( this , results ) ;
113118 }
119+
120+ this . setState ( {
121+ didChange : true ,
122+ } ) ;
114123 }
115124
116125 // Getters / Setters
@@ -168,13 +177,21 @@ class InputRange extends React.Component {
168177 }
169178
170179 setPositionByValue ( slider , value ) {
180+ if ( ! isNumber ( value ) ) {
181+ return ;
182+ }
183+
171184 const validValue = clamp ( value , this . props . minValue , this . props . maxValue ) ;
172185 const position = this . valueTransformer . positionFromValue ( validValue ) ;
173186
174187 this . setPosition ( slider , position ) ;
175188 }
176189
177190 setPositionsByValues ( values ) {
191+ if ( ! values || ! isNumber ( values . min ) || ! isNumber ( values . max ) ) {
192+ return ;
193+ }
194+
178195 const validValues = {
179196 min : clamp ( values . min , this . props . minValue , this . props . maxValue ) ,
180197 max : clamp ( values . max , this . props . minValue , this . props . maxValue ) ,
@@ -190,9 +207,13 @@ class InputRange extends React.Component {
190207
191208 setPositionsByProps ( props ) {
192209 if ( this . isMultiValue ) {
193- this . setPositionsByValues ( props . values ) ;
210+ const values = ! isEmpty ( props . values ) ? props . values : props . defaultValues ;
211+
212+ this . setPositionsByValues ( values ) ;
194213 } else {
195- this . setPositionByValue ( this . refs . sliderMax , props . value ) ;
214+ const value = isNumber ( props . value ) ? props . value : props . defaultValue ;
215+
216+ this . setPositionByValue ( this . refs . sliderMax , value ) ;
196217 }
197218 }
198219
@@ -325,6 +346,8 @@ class InputRange extends React.Component {
325346InputRange . propTypes = {
326347 ariaLabelledby : React . PropTypes . string ,
327348 classNames : React . PropTypes . objectOf ( React . PropTypes . string ) ,
349+ defaultValue : maxMinValuePropType ,
350+ defaultValues : maxMinValuePropType ,
328351 maxValue : maxMinValuePropType ,
329352 minValue : maxMinValuePropType ,
330353 name : React . PropTypes . string ,
@@ -338,7 +361,6 @@ InputRange.defaultProps = {
338361 classNames : defaultClassNames ,
339362 minValue : 0 ,
340363 maxValue : 10 ,
341- value : 0 ,
342364 step : 1 ,
343365} ;
344366
0 commit comments