Compare commits

..
15 Commits
Author SHA1 Message Date
Benjy Cui f850dab6c4 bump 3.3.1 2015-12-21 09:59:02 +08:00
Benjy Cui f3c1d4bd3d fix: the key of dots should be unique #64 2015-12-21 09:57:30 +08:00
Benjy Cui 4d795b01cc chore: update unit test 2015-12-18 17:30:34 +08:00
Benjy Cui 959007ed30 bump 3.3.0 2015-12-18 10:11:50 +08:00
Benjy Cui 079469b765 refactor: the default value of tipFormatter 2015-12-18 10:10:52 +08:00
Benjy Cui ddb2bf670b Merge pull request #61 from frank-weindel/hide-the-tip
If tipFormatter is null, don't show the tip
2015-12-18 09:18:41 +08:00
Frank Weindel 4c57f3ecca Refactor isNoTip expression 2015-12-17 10:49:29 -05:00
Frank Weindel 24f3b755b2 Add example for hiding the tooltip 2015-12-17 10:48:54 -05:00
Frank Weindel 867e252631 Add documentation on how to hide the tip 2015-12-16 16:42:13 -05:00
Frank Weindel 39de81dfa9 If tipFormatter is null, don't show the tip 2015-12-16 16:38:15 -05:00
Benjy Cui 978d877323 bump 3.2.0 2015-12-16 09:30:46 +08:00
Boris Serdiuk a1ae3a8374 Merge pull request #60 from react-component/fix-rerender
fix: crash when rerender #57
2015-12-15 13:53:31 +03:00
Benjy Cui 15f568283c fix: crash when rerender #57 2015-12-15 11:32:41 +08:00
Benjy Cui e85d325542 Merge pull request #59 from react-component/feat-allowCross
feat: add `allowCross` property #56
2015-12-15 09:06:09 +08:00
Benjy Cui 395fa6c201 feat: add allowCross property #56 2015-12-12 08:45:28 +08:00
9 changed files with 150 additions and 62 deletions
+8 -2
View File
@@ -106,6 +106,12 @@ ReactDOM.render(<Rcslider />, container);
<td>false</td>
<td>Determines the type of slider. If range is `true`, two handles will be rendered in order to select a range.</td>
</tr>
<tr>
<td>allowCross</td>
<td>boolean</td>
<td>true</td>
<td>When `range` is `true`, `allowCross` could be set as `true` to allow those two handles cross.</td>
</tr>
<tr>
<td>defaultValue</td>
<td>number or [number, number]</td>
@@ -138,9 +144,9 @@ ReactDOM.render(<Rcslider />, container);
</tr>
<tr>
<td>tipFormatter</td>
<td>function</td>
<td>function or `null`</td>
<td></td>
<td>Format the value of the tooltip if it shows.</td>
<td>Format the value of the tooltip if it shows. If `null` the tooltip will always be hidden.</td>
</tr>
<tr>
<td>dots</td>
+2 -1
View File
@@ -10,6 +10,7 @@ const marks = {
0: '0°C',
26: '26°C',
37: '37°C',
50: '50°C',
100: '100°C',
};
@@ -25,7 +26,7 @@ ReactDOM.render(
</div>
<div style={style}>
<p>Slider with marks and steps</p>
<Slider min={-10} marks={marks} step={10} onChange={log} defaultValue={20} />
<Slider dots min={-10} marks={marks} step={10} onChange={log} defaultValue={20} />
</div>
<div style={style}>
+23 -3
View File
@@ -1,3 +1,4 @@
/* eslint react/no-multi-comp: 0 */
require('rc-slider/assets/index.less');
const React = require('react');
@@ -22,6 +23,21 @@ const CustomizedRange = React.createClass({
value: value,
});
},
render: function() {
return <Slider range value={this.state.value} onChange={this.onSliderChange} />;
},
});
const DynamicBounds = React.createClass({
getInitialState() {
return {
min: 0,
max: 100,
};
},
onSliderChange: function(value) {
log(value);
},
onMinChange: function(e) {
this.setState({
min: +e.target.value || 0,
@@ -41,7 +57,7 @@ const CustomizedRange = React.createClass({
<label>Max: </label>
<input type="number" value={this.state.max} onChange={this.onMaxChange} />
<br /><br />
<Slider range value={this.state.value} min={this.state.min} max={this.state.max} onChange={this.onSliderChange} />
<Slider range defaultValue={[20, 50]} min={this.state.min} max={this.state.max} onChange={this.onSliderChange} />
</div>
);
},
@@ -50,8 +66,8 @@ const CustomizedRange = React.createClass({
ReactDOM.render(
<div>
<div style={style}>
<p>Basic Range</p>
<Slider range defaultValue={[0, 20]} onChange={log} />
<p>Basic Range`allowCross=false`</p>
<Slider range allowCross={false} defaultValue={[0, 20]} onChange={log} />
</div>
<div style={style}>
<p>Basic Range`step=20` </p>
@@ -69,5 +85,9 @@ ReactDOM.render(
<p>Customized Range</p>
<CustomizedRange />
</div>
<div style={style}>
<p>Range with dynamic `max` `min`</p>
<DynamicBounds />
</div>
</div>
, document.getElementById('__react-content'));
+25 -3
View File
@@ -1,3 +1,4 @@
/* eslint react/no-multi-comp: 0 */
require('rc-slider/assets/index.less');
const React = require('react');
@@ -19,8 +20,6 @@ const CustomizedSlider = React.createClass({
getInitialState: function() {
return {
value: 50,
min: 0,
max: 100,
};
},
onSliderChange: function(value) {
@@ -29,6 +28,21 @@ const CustomizedSlider = React.createClass({
value: value,
});
},
render: function() {
return <Slider value={this.state.value} onChange={this.onSliderChange} />;
},
});
const DynamicBounds = React.createClass({
getInitialState: function() {
return {
min: 0,
max: 100,
};
},
onSliderChange: function(value) {
log(value);
},
onMinChange: function(e) {
this.setState({
min: +e.target.value || 0,
@@ -48,7 +62,7 @@ const CustomizedSlider = React.createClass({
<label>Max: </label>
<input type="number" value={this.state.max} onChange={this.onMaxChange} />
<br /><br />
<Slider value={this.state.value} min={this.state.min} max={this.state.max} onChange={this.onSliderChange} />
<Slider defaultValue={50} min={this.state.min} max={this.state.max} onChange={this.onSliderChange} />
</div>
);
},
@@ -72,6 +86,10 @@ ReactDOM.render(
<p>Basic Slider with `tipFormatter`</p>
<Slider tipFormatter={percentFormatter} tipTransitionName="rc-slider-tooltip-zoom-down" onChange={log} />
</div>
<div style={style}>
<p>Basic Slider without tooltip</p>
<Slider tipFormatter={null} onChange={log} />
</div>
<div style={style}>
<p>Controlled Slider</p>
<Slider value={50} />
@@ -80,5 +98,9 @@ ReactDOM.render(
<p>Customized Slider</p>
<CustomizedSlider />
</div>
<div style={style}>
<p>Slider with dynamic `min` `max`</p>
<DynamicBounds />
</div>
</div>
, document.getElementById('__react-content'));
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "rc-slider",
"version": "3.1.4",
"version": "3.3.1",
"description": "slider ui component for react",
"keywords": [
"react",
+1
View File
@@ -5,6 +5,7 @@ function calcPoints(marks, dots, step, min, max) {
const points = Object.keys(marks).map(parseFloat);
if (dots) {
for (let i = min; i <= max; i = i + step) {
if (points.indexOf(i) >= 0) continue;
points.push(i);
}
}
+1 -1
View File
@@ -42,7 +42,7 @@ export default class Handle extends React.Component {
prefixCls={className.replace('slider-handle', 'tooltip')}
placement="top"
visible={isTooltipVisible}
overlay={<span>{tipFormatter ? tipFormatter(value) : value}</span>}
overlay={<span>{tipFormatter(value)}</span>}
delay={0}
transitionName={tipTransitionName}>
{handle}
+38 -16
View File
@@ -71,7 +71,7 @@ class Slider extends React.Component {
const {lowerBound, upperBound} = this.state;
if (nextProps.range) {
const value = nextProps.value;
const value = nextProps.value || [lowerBound, upperBound];
const nextUpperBound = this.trimAlignValue(value[1], nextProps);
const nextLowerBound = this.trimAlignValue(value[0], nextProps);
if (nextLowerBound === lowerBound && nextUpperBound === upperBound) return;
@@ -85,7 +85,8 @@ class Slider extends React.Component {
this.props.onChange([nextLowerBound, nextUpperBound]);
}
} else {
const nextValue = this.trimAlignValue(nextProps.value, nextProps);
const value = 'value' in nextProps ? nextProps.value : upperBound;
const nextValue = this.trimAlignValue(value, nextProps);
if (nextValue === upperBound && lowerBound === nextProps.min) return;
this.setState({
@@ -98,19 +99,16 @@ class Slider extends React.Component {
}
}
onChange(handle, value) {
onChange(state) {
const props = this.props;
const isNotControlled = !('value' in props);
if (isNotControlled) {
this.setState({[handle]: value});
this.setState(state);
} else if (state.handle) {
this.setState({handle: state.handle});
}
const state = this.state;
const data = {
upperBound: state.upperBound,
lowerBound: state.lowerBound,
};
data[handle] = value;
const data = objectAssign({}, this.state, state);
const changedValue = props.range ? [data.lowerBound, data.upperBound] : data.upperBound;
props.onChange(changedValue);
}
@@ -142,7 +140,26 @@ class Slider extends React.Component {
const oldValue = state[state.handle];
if (value === oldValue) return;
this.onChange(state.handle, value);
if (props.allowCross && value < state.lowerBound && state.handle === 'upperBound') {
this.onChange({
handle: 'lowerBound',
lowerBound: value,
upperBound: this.state.lowerBound,
});
return;
}
if (props.allowCross && value > state.upperBound && state.handle === 'lowerBound') {
this.onChange({
handle: 'upperBound',
upperBound: value,
lowerBound: this.state.upperBound,
});
return;
}
this.onChange({
[state.handle]: value,
});
}
onTouchStart(e) {
@@ -197,7 +214,9 @@ class Slider extends React.Component {
const oldValue = state[valueNeedChanging];
if (value === oldValue) return;
this.onChange(valueNeedChanging, value);
this.onChange({
[valueNeedChanging]: value,
});
}
getValue() {
@@ -238,7 +257,7 @@ class Slider extends React.Component {
trimAlignValue(v, nextProps) {
const state = this.state || {};
const {handle, lowerBound, upperBound} = state;
const {marks, step, min, max} = objectAssign({}, this.props, nextProps || {});
const {marks, step, min, max, allowCross} = objectAssign({}, this.props, nextProps || {});
let val = v;
if (val <= min) {
@@ -247,10 +266,10 @@ class Slider extends React.Component {
if (val >= max) {
val = max;
}
if (handle === 'upperBound' && val <= lowerBound) {
if (!allowCross && handle === 'upperBound' && val <= lowerBound) {
val = lowerBound;
}
if (handle === 'lowerBound' && val >= upperBound) {
if (!allowCross && handle === 'lowerBound' && val >= upperBound) {
val = upperBound;
}
@@ -320,7 +339,7 @@ class Slider extends React.Component {
const lowerOffset = this.calcOffset(lowerBound);
const handleClassName = prefixCls + '-handle';
const isNoTip = (step === null) && !tipFormatter;
const isNoTip = (step === null) || (tipFormatter === null);
const upper = (<Handle className={handleClassName}
noTip={isNoTip} tipTransitionName={tipTransitionName} tipFormatter={tipFormatter}
@@ -384,6 +403,7 @@ Slider.propTypes = {
tipFormatter: React.PropTypes.func,
dots: React.PropTypes.bool,
range: React.PropTypes.bool,
allowCross: React.PropTypes.bool,
};
Slider.defaultProps = {
@@ -397,10 +417,12 @@ Slider.defaultProps = {
onBeforeChange: noop,
onChange: noop,
onAfterChange: noop,
tipFormatter: value => value,
included: true,
disabled: false,
dots: false,
range: false,
allowCross: true,
};
export default Slider;
+51 -35
View File
@@ -22,28 +22,36 @@ describe('rc-slider', function() {
expect(ReactTestUtils.scryRenderedDOMComponentsWithClass(slider, 'rc-slider-track').length).to.be(1);
});
it('should render a Slider with correct value and CSS style', () => {
it('should render a Slider with default value correctly', () => {
const sliderWithDefaultValue = ReactDOM.render(<Slider defaultValue={50} />, div);
expect(sliderWithDefaultValue.state.upperBound).to.be(50);
expect(ReactTestUtils
.scryRenderedDOMComponentsWithClass(sliderWithDefaultValue, 'rc-slider-handle')[0]
.style.cssText)
.to.be('left: 50%; ');
expect(ReactTestUtils
.scryRenderedDOMComponentsWithClass(sliderWithDefaultValue, 'rc-slider-track')[0]
.style.cssText)
.to.be('left: 0%; width: 50%; visibility: visible; ');
.to.match(/left: 50%;/);
const trackStyle = ReactTestUtils
.scryRenderedDOMComponentsWithClass(sliderWithDefaultValue, 'rc-slider-track')[0]
.style.cssText;
expect(trackStyle).to.match(/left: 0%;/);
expect(trackStyle).to.match(/width: 50%;/);
expect(trackStyle).to.match(/visibility: visible;/);
});
it('should render a Slider with value corrently', () => {
const sliderWithValue = ReactDOM.render(<Slider value={50} />, div);
expect(sliderWithValue.state.upperBound).to.be(50);
expect(ReactTestUtils
.scryRenderedDOMComponentsWithClass(sliderWithDefaultValue, 'rc-slider-handle')[0]
.scryRenderedDOMComponentsWithClass(sliderWithValue, 'rc-slider-handle')[0]
.style.cssText)
.to.be('left: 50%; ');
expect(ReactTestUtils
.scryRenderedDOMComponentsWithClass(sliderWithDefaultValue, 'rc-slider-track')[0]
.style.cssText)
.to.be('left: 0%; width: 50%; visibility: visible; ');
.to.match(/left: 50%;/);
const trackStyle = ReactTestUtils
.scryRenderedDOMComponentsWithClass(sliderWithValue, 'rc-slider-track')[0]
.style.cssText;
expect(trackStyle).to.match(/left: 0%;/);
expect(trackStyle).to.match(/width: 50%;/);
expect(trackStyle).to.match(/visibility: visible;/);
});
it('should render a Range with correct DOM structure', () => {
@@ -53,38 +61,46 @@ describe('rc-slider', function() {
expect(ReactTestUtils.scryRenderedDOMComponentsWithClass(range, 'rc-slider-track').length).to.be(1);
});
it('should render a Range with correct value and CSS style', () => {
const sliderWithDefaultValue = ReactDOM.render(<Slider range defaultValue={[0, 50]} />, div);
expect(sliderWithDefaultValue.state.lowerBound).to.be(0);
expect(sliderWithDefaultValue.state.upperBound).to.be(50);
it('should render a Range with default value correctly', () => {
const rangeWithDefaultValue = ReactDOM.render(<Slider range defaultValue={[0, 50]} />, div);
expect(rangeWithDefaultValue.state.lowerBound).to.be(0);
expect(rangeWithDefaultValue.state.upperBound).to.be(50);
expect(ReactTestUtils
.scryRenderedDOMComponentsWithClass(sliderWithDefaultValue, 'rc-slider-handle')[0]
.scryRenderedDOMComponentsWithClass(rangeWithDefaultValue, 'rc-slider-handle')[0]
.style.cssText)
.to.be('left: 50%; ');
.to.match(/left: 50%;/);
expect(ReactTestUtils
.scryRenderedDOMComponentsWithClass(sliderWithDefaultValue, 'rc-slider-handle')[1]
.scryRenderedDOMComponentsWithClass(rangeWithDefaultValue, 'rc-slider-handle')[1]
.style.cssText)
.to.be('left: 0%; ');
expect(ReactTestUtils
.scryRenderedDOMComponentsWithClass(sliderWithDefaultValue, 'rc-slider-track')[0]
.style.cssText)
.to.be('left: 0%; width: 50%; visibility: visible; ');
.to.match(/left: 0%;/);
const sliderWithValue = ReactDOM.render(<Slider range value={[50, 100]} />, div);
expect(sliderWithValue.state.lowerBound).to.be(50);
expect(sliderWithValue.state.upperBound).to.be(100);
const trackStyle = ReactTestUtils
.scryRenderedDOMComponentsWithClass(rangeWithDefaultValue, 'rc-slider-track')[0]
.style.cssText;
expect(trackStyle).to.match(/left: 0%;/);
expect(trackStyle).to.match(/width: 50%;/);
expect(trackStyle).to.match(/visibility: visible;/);
});
it('should render a Range with value correctly', () => {
const rangeWithValue = ReactDOM.render(<Slider range value={[50, 100]} />, div);
expect(rangeWithValue.state.lowerBound).to.be(50);
expect(rangeWithValue.state.upperBound).to.be(100);
expect(ReactTestUtils
.scryRenderedDOMComponentsWithClass(sliderWithDefaultValue, 'rc-slider-handle')[0]
.scryRenderedDOMComponentsWithClass(rangeWithValue, 'rc-slider-handle')[0]
.style.cssText)
.to.be('left: 100%; ');
.to.match(/left: 100%;/);
expect(ReactTestUtils
.scryRenderedDOMComponentsWithClass(sliderWithDefaultValue, 'rc-slider-handle')[1]
.scryRenderedDOMComponentsWithClass(rangeWithValue, 'rc-slider-handle')[1]
.style.cssText)
.to.be('left: 50%; ');
expect(ReactTestUtils
.scryRenderedDOMComponentsWithClass(sliderWithDefaultValue, 'rc-slider-track')[0]
.style.cssText)
.to.be('width: 50%; visibility: visible; left: 50%; ');
.to.match(/left: 50%;/);
const trackStyle = ReactTestUtils
.scryRenderedDOMComponentsWithClass(rangeWithValue, 'rc-slider-track')[0]
.style.cssText;
expect(trackStyle).to.match(/left: 50%;/);
expect(trackStyle).to.match(/width: 50%;/);
expect(trackStyle).to.match(/visibility: visible;/);
});
it('should render dots correctly when `dots=true`', () => {