mirror of
https://github.com/wassname/slider.git
synced 2026-09-10 12:38:09 +08:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
7525de1f1a | ||
|
|
856bdef4ea | ||
|
|
bb41b82b59 | ||
|
|
a428cd3335 | ||
|
|
c021cadcbb | ||
|
|
4a59745a23 | ||
|
|
3e78e9ac4a | ||
|
|
82179e613a | ||
|
|
0a6fb5a570 | ||
|
|
53aa61faf2 | ||
|
|
9655483bf5 | ||
|
|
faa4f384a3 | ||
|
|
18ddbb796c | ||
|
|
051fef6603 | ||
|
|
b325daef48 | ||
|
|
db47c3d27d | ||
|
|
f20014a9d4 | ||
|
|
6074d5fc43 | ||
|
|
34754440ed | ||
|
|
fac5a1b9f3 | ||
|
|
1b8e27e613 | ||
|
|
e438757a64 | ||
|
|
aad5fef20c | ||
|
|
1568019a59 | ||
|
|
abdb297c16 | ||
|
|
67b76953c6 | ||
|
|
b6d8f389ef | ||
|
|
d7e6b9d9be | ||
|
|
67a33126ec | ||
|
|
1a8331bcf1 |
+3
-3
@@ -29,8 +29,8 @@ script:
|
|||||||
env:
|
env:
|
||||||
matrix:
|
matrix:
|
||||||
- TEST_TYPE=lint
|
- TEST_TYPE=lint
|
||||||
- TEST_TYPE=browser-test
|
- TEST_TYPE=test
|
||||||
- TEST_TYPE=browser-test-cover
|
- TEST_TYPE=coverage
|
||||||
- TEST_TYPE=saucelabs
|
- TEST_TYPE=saucelabs
|
||||||
global:
|
global:
|
||||||
- secure: S1VwbaPzLnSH/IUT/wlJulxAX5VHRIDmSt53h/ycHcZsszUpWcLCJRQAe0fTVB2dAx5MdBbSZ+o+tr3tRwVB5TRAYm0oTCsYAkOZaWOB28RuUQtdGt3wf9xxTG1UiPiaLLUW3waX9zAaf3yqKBcJGf1op0RD8dksxbCFw/7xVbU=
|
- secure: S1VwbaPzLnSH/IUT/wlJulxAX5VHRIDmSt53h/ycHcZsszUpWcLCJRQAe0fTVB2dAx5MdBbSZ+o+tr3tRwVB5TRAYm0oTCsYAkOZaWOB28RuUQtdGt3wf9xxTG1UiPiaLLUW3waX9zAaf3yqKBcJGf1op0RD8dksxbCFw/7xVbU=
|
||||||
@@ -39,4 +39,4 @@ env:
|
|||||||
|
|
||||||
matrix:
|
matrix:
|
||||||
allow_failures:
|
allow_failures:
|
||||||
- env: "TEST_TYPE=saucelabs"
|
- env: "TEST_TYPE=saucelabs"
|
||||||
|
|||||||
@@ -1,6 +1,11 @@
|
|||||||
# History
|
# History
|
||||||
----
|
----
|
||||||
|
|
||||||
|
## 5.0.0 / 2016-09-12
|
||||||
|
|
||||||
|
[#147](https://github.com/react-component/slider/issues/147) fix style conflicts with rc-tooltip [@benjycui](https://github.com/benjycui)
|
||||||
|
[#145](https://github.com/react-component/slider/pull/145) fix `onChange` will be triggered while mousemove [@Fuzzyma](https://github.com/Fuzzyma)
|
||||||
|
|
||||||
## 4.0.0 / 2016-08-12
|
## 4.0.0 / 2016-08-12
|
||||||
|
|
||||||
[#133](https://github.com/react-component/slider/pull/133) support multi-range ([@sosz](https://github.com/sosz))
|
[#133](https://github.com/react-component/slider/pull/133) support multi-range ([@sosz](https://github.com/sosz))
|
||||||
|
|||||||
@@ -56,8 +56,8 @@ require('rc-slider/assets/index.css');
|
|||||||
|
|
||||||
var React = require('react');
|
var React = require('react');
|
||||||
var ReactDOM = require('react-dom');
|
var ReactDOM = require('react-dom');
|
||||||
var Rcslider = require('rc-slider');
|
var Slider = require('rc-slider');
|
||||||
ReactDOM.render(<Rcslider />, container);
|
ReactDOM.render(<Slider />, container);
|
||||||
```
|
```
|
||||||
|
|
||||||
## API
|
## API
|
||||||
@@ -168,7 +168,7 @@ ReactDOM.render(<Rcslider />, container);
|
|||||||
<td>tipFormatter</td>
|
<td>tipFormatter</td>
|
||||||
<td>function or `null`</td>
|
<td>function or `null`</td>
|
||||||
<td></td>
|
<td></td>
|
||||||
<td>Format the value of the tooltip if it shows. If `null` the tooltip will always be hidden.</td>
|
<td>Format the value of the tooltip if it shows. If `null` the tooltip will always be hidden. When given a function, the first argument will be the value and the second will be the index of the slider handle.</td>
|
||||||
</tr>
|
</tr>
|
||||||
<tr>
|
<tr>
|
||||||
<td>dots</td>
|
<td>dots</td>
|
||||||
|
|||||||
+22
-14
@@ -23,20 +23,25 @@
|
|||||||
|
|
||||||
.@{prefixClass} {
|
.@{prefixClass} {
|
||||||
position: relative;
|
position: relative;
|
||||||
height: 4px;
|
height: 14px;
|
||||||
|
padding: 5px 0;
|
||||||
width: 100%;
|
width: 100%;
|
||||||
border-radius: @border-radius-base;
|
border-radius: @border-radius-base;
|
||||||
background-color: #e9e9e9;
|
|
||||||
|
|
||||||
.borderBox();
|
.borderBox();
|
||||||
|
|
||||||
|
&-rail {
|
||||||
|
position: absolute;
|
||||||
|
width: 100%;
|
||||||
|
background-color: #e9e9e9;
|
||||||
|
height: 4px;
|
||||||
|
}
|
||||||
|
|
||||||
&-track {
|
&-track {
|
||||||
position: absolute;
|
position: absolute;
|
||||||
left: 0;
|
left: 0;
|
||||||
height: 4px;
|
height: 4px;
|
||||||
border-radius: @border-radius-base;
|
border-radius: @border-radius-base;
|
||||||
background-color: tint(@primary-color, 60%);
|
background-color: tint(@primary-color, 60%);
|
||||||
z-index: 1;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
&-handle {
|
&-handle {
|
||||||
@@ -49,7 +54,6 @@
|
|||||||
border-radius: 50%;
|
border-radius: 50%;
|
||||||
border: solid 2px tint(@primary-color, 50%);
|
border: solid 2px tint(@primary-color, 50%);
|
||||||
background-color: #fff;
|
background-color: #fff;
|
||||||
z-index: 2;
|
|
||||||
|
|
||||||
&:hover {
|
&:hover {
|
||||||
border-color: tint(@primary-color, 20%);
|
border-color: tint(@primary-color, 20%);
|
||||||
@@ -64,11 +68,10 @@
|
|||||||
|
|
||||||
&-mark {
|
&-mark {
|
||||||
position: absolute;
|
position: absolute;
|
||||||
top: 10px;
|
top: 18px;
|
||||||
left: 0;
|
left: 0;
|
||||||
width: 100%;
|
width: 100%;
|
||||||
font-size: 12px;
|
font-size: 12px;
|
||||||
z-index: 3;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
&-mark-text {
|
&-mark-text {
|
||||||
@@ -89,7 +92,6 @@
|
|||||||
width: 100%;
|
width: 100%;
|
||||||
height: 4px;
|
height: 4px;
|
||||||
background: transparent;
|
background: transparent;
|
||||||
z-index: 1;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
&-dot {
|
&-dot {
|
||||||
@@ -134,23 +136,30 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
.@{prefixClass}-vertical {
|
.@{prefixClass}-vertical {
|
||||||
width: 4px;
|
width: 14px;
|
||||||
height: 100%;
|
height: 100%;
|
||||||
|
padding: 0 5px;
|
||||||
|
|
||||||
.@{prefixClass} {
|
.@{prefixClass} {
|
||||||
|
&-rail {
|
||||||
|
height: 100%;
|
||||||
|
width: 4px;
|
||||||
|
}
|
||||||
|
|
||||||
&-track {
|
&-track {
|
||||||
|
left: 5px;
|
||||||
bottom: 0;
|
bottom: 0;
|
||||||
width: 4px;
|
width: 4px;
|
||||||
}
|
}
|
||||||
|
|
||||||
&-handle {
|
&-handle {
|
||||||
position: absolute;
|
|
||||||
margin-left: -5px;
|
margin-left: -5px;
|
||||||
margin-bottom: -7px;
|
margin-bottom: -7px;
|
||||||
}
|
}
|
||||||
|
|
||||||
&-mark {
|
&-mark {
|
||||||
top: 0;
|
top: 0;
|
||||||
left: 10px;
|
left: 18px;
|
||||||
height: 100%;
|
height: 100%;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -232,11 +241,10 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
.rc-tooltip {
|
.@{prefixClass}-tooltip {
|
||||||
position: absolute;
|
position: absolute;
|
||||||
left: -9999px;
|
left: -9999px;
|
||||||
top: -9999px;
|
top: -9999px;
|
||||||
z-index: 4;
|
|
||||||
visibility: visible;
|
visibility: visible;
|
||||||
|
|
||||||
.borderBox();
|
.borderBox();
|
||||||
@@ -278,4 +286,4 @@
|
|||||||
border-width: @tooltip-arrow-width @tooltip-arrow-width 0;
|
border-width: @tooltip-arrow-width @tooltip-arrow-width 0;
|
||||||
border-top-color: @tooltip-arrow-color;
|
border-top-color: @tooltip-arrow-color;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+16
-13
@@ -16,19 +16,21 @@ const handleStyle = {
|
|||||||
background: '#fff',
|
background: '#fff',
|
||||||
fontSize: '14px',
|
fontSize: '14px',
|
||||||
textAlign: 'center',
|
textAlign: 'center',
|
||||||
zIndex: 3,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const CustomHandle = props => {
|
const CustomHandle = React.createClass({
|
||||||
const style = Object.assign({ left: props.offset + '%' }, handleStyle);
|
propTypes: {
|
||||||
return (
|
value: React.PropTypes.any,
|
||||||
<div style={style}>val: {props.value}</div>
|
offset: React.PropTypes.number,
|
||||||
);
|
},
|
||||||
};
|
render() {
|
||||||
CustomHandle.propTypes = {
|
const props = this.props;
|
||||||
value: React.PropTypes.any,
|
const style = Object.assign({ left: `${props.offset}%` }, handleStyle);
|
||||||
offset: React.PropTypes.number,
|
return (
|
||||||
};
|
<div style={style}>val: {props.value}</div>
|
||||||
|
);
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
ReactDOM.render(
|
ReactDOM.render(
|
||||||
<div>
|
<div>
|
||||||
@@ -40,5 +42,6 @@ ReactDOM.render(
|
|||||||
<p>Slider with custom handle</p>
|
<p>Slider with custom handle</p>
|
||||||
<Slider min={0} max={20} defaultValue={3} handle={<CustomHandle />} />
|
<Slider min={0} max={20} defaultValue={3} handle={<CustomHandle />} />
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>,
|
||||||
, document.getElementById('__react-content'));
|
document.getElementById('__react-content')
|
||||||
|
);
|
||||||
|
|||||||
+3
-1
@@ -81,7 +81,9 @@ const DynamicBounds = React.createClass({
|
|||||||
<label>Max: </label>
|
<label>Max: </label>
|
||||||
<input type="number" value={this.state.max} onChange={this.onMaxChange} />
|
<input type="number" value={this.state.max} onChange={this.onMaxChange} />
|
||||||
<br /><br />
|
<br /><br />
|
||||||
<Slider range defaultValue={[20, 50]} 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>
|
</div>
|
||||||
);
|
);
|
||||||
},
|
},
|
||||||
|
|||||||
+12
-4
@@ -13,7 +13,7 @@ function log(value) {
|
|||||||
|
|
||||||
|
|
||||||
function percentFormatter(v) {
|
function percentFormatter(v) {
|
||||||
return v + ' %';
|
return `${v} %`;
|
||||||
}
|
}
|
||||||
|
|
||||||
const CustomizedSlider = React.createClass({
|
const CustomizedSlider = React.createClass({
|
||||||
@@ -32,7 +32,11 @@ const CustomizedSlider = React.createClass({
|
|||||||
console.log(value);
|
console.log(value);
|
||||||
},
|
},
|
||||||
render() {
|
render() {
|
||||||
return <Slider value={this.state.value} onChange={this.onSliderChange} onAfterChange={this.onAfterChange} />;
|
return (
|
||||||
|
<Slider value={this.state.value}
|
||||||
|
onChange={this.onSliderChange} onAfterChange={this.onAfterChange}
|
||||||
|
/>
|
||||||
|
);
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -65,7 +69,9 @@ const DynamicBounds = React.createClass({
|
|||||||
<label>Max: </label>
|
<label>Max: </label>
|
||||||
<input type="number" value={this.state.max} onChange={this.onMaxChange} />
|
<input type="number" value={this.state.max} onChange={this.onMaxChange} />
|
||||||
<br /><br />
|
<br /><br />
|
||||||
<Slider defaultValue={50} 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>
|
</div>
|
||||||
);
|
);
|
||||||
},
|
},
|
||||||
@@ -87,7 +93,9 @@ ReactDOM.render(
|
|||||||
</div>
|
</div>
|
||||||
<div style={style}>
|
<div style={style}>
|
||||||
<p>Basic Slider with `tipFormatter`</p>
|
<p>Basic Slider with `tipFormatter`</p>
|
||||||
<Slider tipFormatter={percentFormatter} tipTransitionName="rc-slider-tooltip-zoom-down" onChange={log} />
|
<Slider tipFormatter={percentFormatter}
|
||||||
|
tipTransitionName="rc-slider-tooltip-zoom-down" onChange={log}
|
||||||
|
/>
|
||||||
</div>
|
</div>
|
||||||
<div style={style}>
|
<div style={style}>
|
||||||
<p>Basic Slider without tooltip</p>
|
<p>Basic Slider without tooltip</p>
|
||||||
|
|||||||
+3
-1
@@ -49,7 +49,9 @@ ReactDOM.render(
|
|||||||
</div>
|
</div>
|
||||||
<div style={style}>
|
<div style={style}>
|
||||||
<p>Range with marks and steps</p>
|
<p>Range with marks and steps</p>
|
||||||
<Slider vertical min={-10} range marks={marks} step={10} onChange={log} defaultValue={[20, 40]} />
|
<Slider vertical min={-10} range marks={marks} step={10}
|
||||||
|
onChange={log} defaultValue={[20, 40]}
|
||||||
|
/>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
, document.getElementById('__react-content'));
|
, document.getElementById('__react-content'));
|
||||||
|
|||||||
+6
-2
@@ -39,7 +39,9 @@ const CustomizedRange = React.createClass({
|
|||||||
render() {
|
render() {
|
||||||
return (
|
return (
|
||||||
<div style={style}>
|
<div style={style}>
|
||||||
<Slider range vertical allowCross={false} value={this.state.value} onChange={this.onSliderChange} />
|
<Slider range vertical allowCross={false} value={this.state.value}
|
||||||
|
onChange={this.onSliderChange}
|
||||||
|
/>
|
||||||
<label>LowerBound: </label>
|
<label>LowerBound: </label>
|
||||||
<input type="number" value={this.state.lowerBound} onChange={this.onLowerBoundChange} />
|
<input type="number" value={this.state.lowerBound} onChange={this.onLowerBoundChange} />
|
||||||
<br />
|
<br />
|
||||||
@@ -75,7 +77,9 @@ const DynamicBounds = React.createClass({
|
|||||||
render() {
|
render() {
|
||||||
return (
|
return (
|
||||||
<div style={style}>
|
<div style={style}>
|
||||||
<Slider range vertical defaultValue={[20, 50]} min={this.state.min} max={this.state.max} onChange={this.onSliderChange} />
|
<Slider range vertical defaultValue={[20, 50]} min={this.state.min} max={this.state.max}
|
||||||
|
onChange={this.onSliderChange}
|
||||||
|
/>
|
||||||
<label>Min: </label>
|
<label>Min: </label>
|
||||||
<input type="number" value={this.state.min} onChange={this.onMinChange} />
|
<input type="number" value={this.state.min} onChange={this.onMinChange} />
|
||||||
<br />
|
<br />
|
||||||
|
|||||||
@@ -14,7 +14,7 @@ function log(value) {
|
|||||||
|
|
||||||
|
|
||||||
function percentFormatter(v) {
|
function percentFormatter(v) {
|
||||||
return v + ' %';
|
return `${v} %`;
|
||||||
}
|
}
|
||||||
|
|
||||||
const CustomizedSlider = React.createClass({
|
const CustomizedSlider = React.createClass({
|
||||||
@@ -58,7 +58,9 @@ const DynamicBounds = React.createClass({
|
|||||||
return (
|
return (
|
||||||
<div style={style}>
|
<div style={style}>
|
||||||
<p>Slider with dynamic `min` `max`</p>
|
<p>Slider with dynamic `min` `max`</p>
|
||||||
<Slider vertical defaultValue={50} min={this.state.min} max={this.state.max} onChange={this.onSliderChange} />
|
<Slider vertical defaultValue={50} min={this.state.min} max={this.state.max}
|
||||||
|
onChange={this.onSliderChange}
|
||||||
|
/>
|
||||||
<label>Min: </label>
|
<label>Min: </label>
|
||||||
<input type="number" value={this.state.min} onChange={this.onMinChange} />
|
<input type="number" value={this.state.min} onChange={this.onMinChange} />
|
||||||
<br />
|
<br />
|
||||||
@@ -85,7 +87,9 @@ ReactDOM.render(
|
|||||||
</div>
|
</div>
|
||||||
<div style={style}>
|
<div style={style}>
|
||||||
<p>Basic Slider with `tipFormatter`</p>
|
<p>Basic Slider with `tipFormatter`</p>
|
||||||
<Slider vertical tipFormatter={percentFormatter} tipTransitionName="rc-slider-tooltip-zoom-down" onChange={log} />
|
<Slider vertical tipFormatter={percentFormatter}
|
||||||
|
tipTransitionName="rc-slider-tooltip-zoom-down" onChange={log}
|
||||||
|
/>
|
||||||
</div>
|
</div>
|
||||||
<div style={style}>
|
<div style={style}>
|
||||||
<p>Basic Slider without tooltip</p>
|
<p>Basic Slider without tooltip</p>
|
||||||
|
|||||||
+6
-2
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "rc-slider",
|
"name": "rc-slider",
|
||||||
"version": "4.0.0",
|
"version": "5.4.0",
|
||||||
"description": "slider ui component for react",
|
"description": "slider ui component for react",
|
||||||
"keywords": [
|
"keywords": [
|
||||||
"react",
|
"react",
|
||||||
@@ -19,10 +19,14 @@
|
|||||||
},
|
},
|
||||||
"files": [
|
"files": [
|
||||||
"assets/*.css",
|
"assets/*.css",
|
||||||
"lib"
|
"lib",
|
||||||
|
"dist"
|
||||||
],
|
],
|
||||||
"licenses": "MIT",
|
"licenses": "MIT",
|
||||||
"main": "./lib/index.js",
|
"main": "./lib/index.js",
|
||||||
|
"entry": {
|
||||||
|
"rc-slider": ["./assets/index.less", "./src/index.js"]
|
||||||
|
},
|
||||||
"style": "./assets/index.css",
|
"style": "./assets/index.css",
|
||||||
"config": {
|
"config": {
|
||||||
"port": 8005
|
"port": 8005
|
||||||
|
|||||||
+13
-9
@@ -10,21 +10,22 @@ export default class Handle extends React.Component {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
showTooltip() {
|
|
||||||
this.setState({
|
|
||||||
isTooltipVisible: true,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
hideTooltip() {
|
hideTooltip() {
|
||||||
this.setState({
|
this.setState({
|
||||||
isTooltipVisible: false,
|
isTooltipVisible: false,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
showTooltip() {
|
||||||
|
this.setState({
|
||||||
|
isTooltipVisible: true,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
render() {
|
render() {
|
||||||
const {
|
const {
|
||||||
prefixCls,
|
prefixCls,
|
||||||
|
tooltipPrefixCls,
|
||||||
className,
|
className,
|
||||||
tipTransitionName,
|
tipTransitionName,
|
||||||
tipFormatter,
|
tipFormatter,
|
||||||
@@ -33,9 +34,10 @@ export default class Handle extends React.Component {
|
|||||||
value,
|
value,
|
||||||
dragging,
|
dragging,
|
||||||
noTip,
|
noTip,
|
||||||
|
index,
|
||||||
} = this.props;
|
} = this.props;
|
||||||
|
|
||||||
const style = vertical ? { bottom: offset + '%' } : { left: offset + '%' };
|
const style = vertical ? { bottom: `${offset}%` } : { left: `${offset}%` };
|
||||||
const handle = (
|
const handle = (
|
||||||
<div className={className} style={style}
|
<div className={className} style={style}
|
||||||
onMouseUp={this.showTooltip.bind(this)}
|
onMouseUp={this.showTooltip.bind(this)}
|
||||||
@@ -51,10 +53,10 @@ export default class Handle extends React.Component {
|
|||||||
const isTooltipVisible = dragging || this.state.isTooltipVisible;
|
const isTooltipVisible = dragging || this.state.isTooltipVisible;
|
||||||
return (
|
return (
|
||||||
<Tooltip
|
<Tooltip
|
||||||
prefixCls={prefixCls.replace('slider', 'tooltip')}
|
prefixCls={tooltipPrefixCls || `${prefixCls}-tooltip`}
|
||||||
placement="top"
|
placement="top"
|
||||||
visible={isTooltipVisible}
|
visible={isTooltipVisible}
|
||||||
overlay={<span>{tipFormatter(value)}</span>}
|
overlay={<span>{tipFormatter(value, index)}</span>}
|
||||||
delay={0}
|
delay={0}
|
||||||
transitionName={tipTransitionName}
|
transitionName={tipTransitionName}
|
||||||
>
|
>
|
||||||
@@ -66,6 +68,7 @@ export default class Handle extends React.Component {
|
|||||||
|
|
||||||
Handle.propTypes = {
|
Handle.propTypes = {
|
||||||
prefixCls: React.PropTypes.string,
|
prefixCls: React.PropTypes.string,
|
||||||
|
tooltipPrefixCls: React.PropTypes.string,
|
||||||
className: React.PropTypes.string,
|
className: React.PropTypes.string,
|
||||||
vertical: React.PropTypes.bool,
|
vertical: React.PropTypes.bool,
|
||||||
offset: React.PropTypes.number,
|
offset: React.PropTypes.number,
|
||||||
@@ -74,4 +77,5 @@ Handle.propTypes = {
|
|||||||
value: React.PropTypes.number,
|
value: React.PropTypes.number,
|
||||||
dragging: React.PropTypes.bool,
|
dragging: React.PropTypes.bool,
|
||||||
noTip: React.PropTypes.bool,
|
noTip: React.PropTypes.bool,
|
||||||
|
index: React.PropTypes.number,
|
||||||
};
|
};
|
||||||
|
|||||||
+7
-7
@@ -12,20 +12,20 @@ const Marks = ({ className, vertical, marks, included, upperBound, lowerBound, m
|
|||||||
const isActived = (!included && point === upperBound) ||
|
const isActived = (!included && point === upperBound) ||
|
||||||
(included && point <= upperBound && point >= lowerBound);
|
(included && point <= upperBound && point >= lowerBound);
|
||||||
const markClassName = classNames({
|
const markClassName = classNames({
|
||||||
[className + '-text']: true,
|
[`${className}-text`]: true,
|
||||||
[className + '-text-active']: isActived,
|
[`${className}-text-active`]: isActived,
|
||||||
});
|
});
|
||||||
|
|
||||||
const bottomStyle = {
|
const bottomStyle = {
|
||||||
// height: markWidth + '%',
|
// height: markWidth + '%',
|
||||||
marginBottom: '-200' + '%',
|
marginBottom: '-50%',
|
||||||
bottom: (point - min) / range * 100 + '%',
|
bottom: `${(point - min) / range * 100}%`,
|
||||||
};
|
};
|
||||||
|
|
||||||
const leftStyle = {
|
const leftStyle = {
|
||||||
width: markWidth + '%',
|
width: `${markWidth}%`,
|
||||||
marginLeft: -markWidth / 2 + '%',
|
marginLeft: `${-markWidth / 2}%`,
|
||||||
left: (point - min) / range * 100 + '%',
|
left: `${(point - min) / range * 100}%`,
|
||||||
};
|
};
|
||||||
|
|
||||||
const style = vertical ? bottomStyle : leftStyle;
|
const style = vertical ? bottomStyle : leftStyle;
|
||||||
|
|||||||
+232
-176
@@ -1,3 +1,4 @@
|
|||||||
|
import { findDOMNode } from 'react-dom';
|
||||||
import React, { cloneElement } from 'react';
|
import React, { cloneElement } from 'react';
|
||||||
import addEventListener from 'rc-util/lib/Dom/addEventListener';
|
import addEventListener from 'rc-util/lib/Dom/addEventListener';
|
||||||
import classNames from 'classnames';
|
import classNames from 'classnames';
|
||||||
@@ -5,6 +6,7 @@ import Track from './Track';
|
|||||||
import DefaultHandle from './Handle';
|
import DefaultHandle from './Handle';
|
||||||
import Steps from './Steps';
|
import Steps from './Steps';
|
||||||
import Marks from './Marks';
|
import Marks from './Marks';
|
||||||
|
import warning from 'warning';
|
||||||
|
|
||||||
function noop() {
|
function noop() {
|
||||||
}
|
}
|
||||||
@@ -21,6 +23,13 @@ function getMousePosition(vertical, e) {
|
|||||||
return vertical ? e.clientY : e.pageX;
|
return vertical ? e.clientY : e.pageX;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function getHandleCenterPosition(vertical, handle) {
|
||||||
|
const coords = handle.getBoundingClientRect();
|
||||||
|
return vertical ?
|
||||||
|
coords.top + (coords.height * 0.5) :
|
||||||
|
coords.left + (coords.width * 0.5);
|
||||||
|
}
|
||||||
|
|
||||||
function pauseEvent(e) {
|
function pauseEvent(e) {
|
||||||
e.stopPropagation();
|
e.stopPropagation();
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
@@ -30,7 +39,7 @@ class Slider extends React.Component {
|
|||||||
constructor(props) {
|
constructor(props) {
|
||||||
super(props);
|
super(props);
|
||||||
|
|
||||||
const { range, min, max } = props;
|
const { range, min, max, step } = props;
|
||||||
const initialValue = range ? Array.apply(null, Array(range + 1)).map(() => min) : min;
|
const initialValue = range ? Array.apply(null, Array(range + 1)).map(() => min) : min;
|
||||||
const defaultValue = ('defaultValue' in props ? props.defaultValue : initialValue);
|
const defaultValue = ('defaultValue' in props ? props.defaultValue : initialValue);
|
||||||
const value = (props.value !== undefined ? props.value : defaultValue);
|
const value = (props.value !== undefined ? props.value : defaultValue);
|
||||||
@@ -44,6 +53,17 @@ class Slider extends React.Component {
|
|||||||
recent = bounds.length - 1;
|
recent = bounds.length - 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (process.env.NODE_ENV !== 'production' &&
|
||||||
|
step && Math.floor(step) === step &&
|
||||||
|
(max - min) % step !== 0) {
|
||||||
|
warning(
|
||||||
|
false,
|
||||||
|
'Slider[max] - Slider[min] (%s) should be a multiple of Slider[step] (%s)',
|
||||||
|
max - min,
|
||||||
|
step
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
this.state = {
|
this.state = {
|
||||||
handle: null,
|
handle: null,
|
||||||
recent,
|
recent,
|
||||||
@@ -81,7 +101,7 @@ class Slider extends React.Component {
|
|||||||
const isNotControlled = !('value' in props);
|
const isNotControlled = !('value' in props);
|
||||||
if (isNotControlled) {
|
if (isNotControlled) {
|
||||||
this.setState(state);
|
this.setState(state);
|
||||||
} else if (state.handle) {
|
} else if (state.handle !== undefined) {
|
||||||
this.setState({ handle: state.handle });
|
this.setState({ handle: state.handle });
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -90,19 +110,25 @@ class Slider extends React.Component {
|
|||||||
props.onChange(changedValue);
|
props.onChange(changedValue);
|
||||||
}
|
}
|
||||||
|
|
||||||
onMouseMove(e) {
|
onMouseDown(e) {
|
||||||
const position = getMousePosition(this.props.vertical, e);
|
if (e.button !== 0) { return; }
|
||||||
this.onMove(e, position);
|
|
||||||
|
let position = getMousePosition(this.props.vertical, e);
|
||||||
|
if (!this.isEventFromHandle(e)) {
|
||||||
|
this.dragOffset = 0;
|
||||||
|
} else {
|
||||||
|
const handlePosition = getHandleCenterPosition(this.props.vertical, e.target);
|
||||||
|
this.dragOffset = position - handlePosition;
|
||||||
|
position = handlePosition;
|
||||||
|
}
|
||||||
|
this.onStart(position);
|
||||||
|
this.addDocumentEvents('mouse');
|
||||||
|
pauseEvent(e);
|
||||||
}
|
}
|
||||||
|
|
||||||
onTouchMove(e) {
|
onMouseMove(e) {
|
||||||
if (isNotTouchEvent(e)) {
|
const position = getMousePosition(this.props.vertical, e);
|
||||||
this.end('touch');
|
this.onMove(e, position - this.dragOffset);
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const position = getTouchPosition(this.props.vertical, e);
|
|
||||||
this.onMove(e, position);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
onMove(e, position) {
|
onMove(e, position) {
|
||||||
@@ -115,7 +141,7 @@ class Slider extends React.Component {
|
|||||||
const diffValue = diffPosition / this.getSliderLength() * (props.max - props.min);
|
const diffValue = diffPosition / this.getSliderLength() * (props.max - props.min);
|
||||||
|
|
||||||
const value = this.trimAlignValue(this.startValue + diffValue);
|
const value = this.trimAlignValue(this.startValue + diffValue);
|
||||||
const oldValue = state[state.handle];
|
const oldValue = state.bounds[state.handle];
|
||||||
if (value === oldValue) return;
|
if (value === oldValue) return;
|
||||||
|
|
||||||
const nextBounds = [...state.bounds];
|
const nextBounds = [...state.bounds];
|
||||||
@@ -134,23 +160,6 @@ class Slider extends React.Component {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
onTouchStart(e) {
|
|
||||||
if (isNotTouchEvent(e)) return;
|
|
||||||
|
|
||||||
const position = getTouchPosition(this.props.vertical, e);
|
|
||||||
this.onStart(position);
|
|
||||||
this.addDocumentEvents('touch');
|
|
||||||
pauseEvent(e);
|
|
||||||
}
|
|
||||||
|
|
||||||
onMouseDown(e) {
|
|
||||||
if (e.button !== 0) { return; }
|
|
||||||
const position = getMousePosition(this.props.vertical, e);
|
|
||||||
this.onStart(position);
|
|
||||||
this.addDocumentEvents('mouse');
|
|
||||||
pauseEvent(e);
|
|
||||||
}
|
|
||||||
|
|
||||||
onStart(position) {
|
onStart(position) {
|
||||||
const props = this.props;
|
const props = this.props;
|
||||||
props.onBeforeChange(this.getValue());
|
props.onBeforeChange(this.getValue());
|
||||||
@@ -196,34 +205,30 @@ class Slider extends React.Component {
|
|||||||
this.onChange({ bounds: nextBounds });
|
this.onChange({ bounds: nextBounds });
|
||||||
}
|
}
|
||||||
|
|
||||||
getValue() {
|
onTouchMove(e) {
|
||||||
const { bounds } = this.state;
|
if (isNotTouchEvent(e)) {
|
||||||
return this.props.range ? bounds : bounds[1];
|
this.end('touch');
|
||||||
}
|
return;
|
||||||
|
|
||||||
getSliderLength() {
|
|
||||||
const slider = this.refs.slider;
|
|
||||||
if (!slider) {
|
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return this.props.vertical ? slider.clientHeight : slider.clientWidth;
|
const position = getTouchPosition(this.props.vertical, e);
|
||||||
|
this.onMove(e, position - this.dragOffset);
|
||||||
}
|
}
|
||||||
|
|
||||||
getSliderStart() {
|
onTouchStart(e) {
|
||||||
const slider = this.refs.slider;
|
if (isNotTouchEvent(e)) return;
|
||||||
const rect = slider.getBoundingClientRect();
|
|
||||||
|
|
||||||
return this.props.vertical ? rect.top : rect.left;
|
let position = getTouchPosition(this.props.vertical, e);
|
||||||
}
|
if (!this.isEventFromHandle(e)) {
|
||||||
|
this.dragOffset = 0;
|
||||||
getPrecision(step) {
|
} else {
|
||||||
const stepString = step.toString();
|
const handlePosition = getHandleCenterPosition(this.props.vertical, e.target);
|
||||||
let precision = 0;
|
this.dragOffset = position - handlePosition;
|
||||||
if (stepString.indexOf('.') >= 0) {
|
position = handlePosition;
|
||||||
precision = stepString.length - stepString.indexOf('.') - 1;
|
|
||||||
}
|
}
|
||||||
return precision;
|
this.onStart(position);
|
||||||
|
this.addDocumentEvents('touch');
|
||||||
|
pauseEvent(e);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -247,10 +252,155 @@ class Slider extends React.Component {
|
|||||||
return this._getPointsCache.points;
|
return this._getPointsCache.points;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
getPrecision(step) {
|
||||||
|
const stepString = step.toString();
|
||||||
|
let precision = 0;
|
||||||
|
if (stepString.indexOf('.') >= 0) {
|
||||||
|
precision = stepString.length - stepString.indexOf('.') - 1;
|
||||||
|
}
|
||||||
|
return precision;
|
||||||
|
}
|
||||||
|
|
||||||
|
getSliderLength() {
|
||||||
|
const slider = this.refs.slider;
|
||||||
|
if (!slider) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
return this.props.vertical ? slider.clientHeight : slider.clientWidth;
|
||||||
|
}
|
||||||
|
|
||||||
|
getSliderStart() {
|
||||||
|
const slider = this.refs.slider;
|
||||||
|
const rect = slider.getBoundingClientRect();
|
||||||
|
|
||||||
|
return this.props.vertical ? rect.top : rect.left;
|
||||||
|
}
|
||||||
|
|
||||||
|
getValue() {
|
||||||
|
const { bounds } = this.state;
|
||||||
|
return this.props.range ? bounds : bounds[1];
|
||||||
|
}
|
||||||
|
|
||||||
|
addDocumentEvents(type) {
|
||||||
|
if (type === 'touch') {
|
||||||
|
// just work for chrome iOS Safari and Android Browser
|
||||||
|
this.onTouchMoveListener =
|
||||||
|
addEventListener(document, 'touchmove', this.onTouchMove.bind(this));
|
||||||
|
this.onTouchUpListener =
|
||||||
|
addEventListener(document, 'touchend', this.end.bind(this, 'touch'));
|
||||||
|
} else if (type === 'mouse') {
|
||||||
|
this.onMouseMoveListener =
|
||||||
|
addEventListener(document, 'mousemove', this.onMouseMove.bind(this));
|
||||||
|
this.onMouseUpListener =
|
||||||
|
addEventListener(document, 'mouseup', this.end.bind(this, 'mouse'));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
calcOffset(value) {
|
||||||
|
const { min, max } = this.props;
|
||||||
|
const ratio = (value - min) / (max - min);
|
||||||
|
return ratio * 100;
|
||||||
|
}
|
||||||
|
|
||||||
|
calcValue(offset) {
|
||||||
|
const { vertical, min, max } = this.props;
|
||||||
|
const ratio = Math.abs(offset / this.getSliderLength());
|
||||||
|
const value = vertical ? (1 - ratio) * (max - min) + min : ratio * (max - min) + min;
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
|
calcValueByPos(position) {
|
||||||
|
const pixelOffset = position - this.getSliderStart();
|
||||||
|
const nextValue = this.trimAlignValue(this.calcValue(pixelOffset));
|
||||||
|
return nextValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
end(type) {
|
||||||
|
this.removeEvents(type);
|
||||||
|
this.props.onAfterChange(this.getValue());
|
||||||
|
this.setState({ handle: null });
|
||||||
|
}
|
||||||
|
|
||||||
|
isEventFromHandle(e) {
|
||||||
|
return this.state.bounds.some((x, i) => (
|
||||||
|
this.refs[`handle-${i}`] &&
|
||||||
|
e.target === findDOMNode(this.refs[`handle-${i}`])
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
isValueOutOfBounds(value, props) {
|
isValueOutOfBounds(value, props) {
|
||||||
return value < props.min || value > props.max;
|
return value < props.min || value > props.max;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pushHandle(bounds, handle, direction, amount) {
|
||||||
|
const originalValue = bounds[handle];
|
||||||
|
let currentValue = bounds[handle];
|
||||||
|
while (direction * (currentValue - originalValue) < amount) {
|
||||||
|
if (!this.pushHandleOnePoint(bounds, handle, direction)) {
|
||||||
|
// can't push handle enough to create the needed `amount` gap, so we
|
||||||
|
// revert its position to the original value
|
||||||
|
bounds[handle] = originalValue;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
currentValue = bounds[handle];
|
||||||
|
}
|
||||||
|
// the handle was pushed enough to create the needed `amount` gap
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
pushHandleOnePoint(bounds, handle, direction) {
|
||||||
|
const points = this.getPoints();
|
||||||
|
const pointIndex = points.indexOf(bounds[handle]);
|
||||||
|
const nextPointIndex = pointIndex + direction;
|
||||||
|
if (nextPointIndex >= points.length || nextPointIndex < 0) {
|
||||||
|
// reached the minimum or maximum available point, can't push anymore
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
const nextHandle = handle + direction;
|
||||||
|
const nextValue = points[nextPointIndex];
|
||||||
|
const { pushable: threshold } = this.props;
|
||||||
|
const diffToNext = direction * (bounds[nextHandle] - nextValue);
|
||||||
|
if (!this.pushHandle(bounds, nextHandle, direction, threshold - diffToNext)) {
|
||||||
|
// couldn't push next handle, so we won't push this one either
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
// push the handle
|
||||||
|
bounds[handle] = nextValue;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
pushSurroundingHandles(bounds, handle, originalValue) {
|
||||||
|
const { pushable: threshold } = this.props;
|
||||||
|
const value = bounds[handle];
|
||||||
|
|
||||||
|
let direction = 0;
|
||||||
|
if (bounds[handle + 1] - value < threshold) {
|
||||||
|
direction = +1;
|
||||||
|
} else if (value - bounds[handle - 1] < threshold) {
|
||||||
|
direction = -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (direction === 0) { return; }
|
||||||
|
|
||||||
|
const nextHandle = handle + direction;
|
||||||
|
const diffToNext = direction * (bounds[nextHandle] - value);
|
||||||
|
if (!this.pushHandle(bounds, nextHandle, direction, threshold - diffToNext)) {
|
||||||
|
// revert to original value if pushing is impossible
|
||||||
|
bounds[handle] = originalValue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
removeEvents(type) {
|
||||||
|
if (type === 'touch') {
|
||||||
|
this.onTouchMoveListener.remove();
|
||||||
|
this.onTouchUpListener.remove();
|
||||||
|
} else if (type === 'mouse') {
|
||||||
|
this.onMouseMoveListener.remove();
|
||||||
|
this.onMouseUpListener.remove();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
trimAlignValue(v, nextProps) {
|
trimAlignValue(v, nextProps) {
|
||||||
const state = this.state || {};
|
const state = this.state || {};
|
||||||
const { handle, bounds } = state;
|
const { handle, bounds } = state;
|
||||||
@@ -284,110 +434,6 @@ class Slider extends React.Component {
|
|||||||
return step !== null ? parseFloat(closestPoint.toFixed(this.getPrecision(step))) : closestPoint;
|
return step !== null ? parseFloat(closestPoint.toFixed(this.getPrecision(step))) : closestPoint;
|
||||||
}
|
}
|
||||||
|
|
||||||
pushHandleOnePoint(bounds, handle, direction) {
|
|
||||||
const points = this.getPoints();
|
|
||||||
const pointIndex = points.indexOf(bounds[handle]);
|
|
||||||
const nextPointIndex = pointIndex + direction;
|
|
||||||
if (nextPointIndex >= points.length || nextPointIndex < 0) {
|
|
||||||
// reached the minimum or maximum available point, can't push anymore
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
const nextHandle = handle + direction;
|
|
||||||
const nextValue = points[nextPointIndex];
|
|
||||||
const { pushable: threshold } = this.props;
|
|
||||||
const diffToNext = direction * (bounds[nextHandle] - nextValue);
|
|
||||||
if (!this.pushHandle(bounds, nextHandle, direction, threshold - diffToNext)) {
|
|
||||||
// couldn't push next handle, so we won't push this one either
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
// push the handle
|
|
||||||
bounds[handle] = nextValue;
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
pushHandle(bounds, handle, direction, amount) {
|
|
||||||
const originalValue = bounds[handle];
|
|
||||||
let currentValue = bounds[handle];
|
|
||||||
while (direction * (currentValue - originalValue) < amount) {
|
|
||||||
if (!this.pushHandleOnePoint(bounds, handle, direction)) {
|
|
||||||
// can't push handle enough to create the needed `amount` gap, so we
|
|
||||||
// revert its position to the original value
|
|
||||||
bounds[handle] = originalValue;
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
currentValue = bounds[handle];
|
|
||||||
}
|
|
||||||
// the handle was pushed enough to create the needed `amount` gap
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
pushSurroundingHandles(bounds, handle, originalValue) {
|
|
||||||
const { pushable: threshold } = this.props;
|
|
||||||
const value = bounds[handle];
|
|
||||||
|
|
||||||
let direction = 0;
|
|
||||||
if (bounds[handle + 1] - value < threshold) {
|
|
||||||
direction = +1;
|
|
||||||
} else if (value - bounds[handle - 1] < threshold) {
|
|
||||||
direction = -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (direction === 0) { return; }
|
|
||||||
|
|
||||||
const nextHandle = handle + direction;
|
|
||||||
const diffToNext = direction * (bounds[nextHandle] - value);
|
|
||||||
if (!this.pushHandle(bounds, nextHandle, direction, threshold - diffToNext)) {
|
|
||||||
// revert to original value if pushing is impossible
|
|
||||||
bounds[handle] = originalValue;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
calcOffset(value) {
|
|
||||||
const { min, max } = this.props;
|
|
||||||
const ratio = (value - min) / (max - min);
|
|
||||||
return ratio * 100;
|
|
||||||
}
|
|
||||||
|
|
||||||
calcValue(offset) {
|
|
||||||
const { vertical, min, max } = this.props;
|
|
||||||
const ratio = Math.abs(offset / this.getSliderLength());
|
|
||||||
const value = vertical ? (1 - ratio) * (max - min) + min : ratio * (max - min) + min;
|
|
||||||
return value;
|
|
||||||
}
|
|
||||||
|
|
||||||
calcValueByPos(position) {
|
|
||||||
const pixelOffset = position - this.getSliderStart();
|
|
||||||
const nextValue = this.trimAlignValue(this.calcValue(pixelOffset));
|
|
||||||
return nextValue;
|
|
||||||
}
|
|
||||||
|
|
||||||
addDocumentEvents(type) {
|
|
||||||
if (type === 'touch') {
|
|
||||||
// just work for chrome iOS Safari and Android Browser
|
|
||||||
this.onTouchMoveListener = addEventListener(document, 'touchmove', this.onTouchMove.bind(this));
|
|
||||||
this.onTouchUpListener = addEventListener(document, 'touchend', this.end.bind(this, 'touch'));
|
|
||||||
} else if (type === 'mouse') {
|
|
||||||
this.onMouseMoveListener = addEventListener(document, 'mousemove', this.onMouseMove.bind(this));
|
|
||||||
this.onMouseUpListener = addEventListener(document, 'mouseup', this.end.bind(this, 'mouse'));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
removeEvents(type) {
|
|
||||||
if (type === 'touch') {
|
|
||||||
this.onTouchMoveListener.remove();
|
|
||||||
this.onTouchUpListener.remove();
|
|
||||||
} else if (type === 'mouse') {
|
|
||||||
this.onMouseMoveListener.remove();
|
|
||||||
this.onMouseUpListener.remove();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
end(type) {
|
|
||||||
this.removeEvents(type);
|
|
||||||
this.props.onAfterChange(this.getValue());
|
|
||||||
this.setState({ handle: null });
|
|
||||||
}
|
|
||||||
|
|
||||||
render() {
|
render() {
|
||||||
const {
|
const {
|
||||||
handle,
|
handle,
|
||||||
@@ -396,6 +442,7 @@ class Slider extends React.Component {
|
|||||||
const {
|
const {
|
||||||
className,
|
className,
|
||||||
prefixCls,
|
prefixCls,
|
||||||
|
tooltipPrefixCls,
|
||||||
disabled,
|
disabled,
|
||||||
vertical,
|
vertical,
|
||||||
dots,
|
dots,
|
||||||
@@ -413,7 +460,7 @@ class Slider extends React.Component {
|
|||||||
|
|
||||||
const offsets = bounds.map(v => this.calcOffset(v));
|
const offsets = bounds.map(v => this.calcOffset(v));
|
||||||
|
|
||||||
const handleClassName = prefixCls + '-handle';
|
const handleClassName = `${prefixCls}-handle`;
|
||||||
|
|
||||||
const handlesClassNames = bounds.map((v, i) => classNames({
|
const handlesClassNames = bounds.map((v, i) => classNames({
|
||||||
[handleClassName]: true,
|
[handleClassName]: true,
|
||||||
@@ -426,6 +473,7 @@ class Slider extends React.Component {
|
|||||||
|
|
||||||
const commonHandleProps = {
|
const commonHandleProps = {
|
||||||
prefixCls,
|
prefixCls,
|
||||||
|
tooltipPrefixCls,
|
||||||
noTip: isNoTip,
|
noTip: isNoTip,
|
||||||
tipTransitionName,
|
tipTransitionName,
|
||||||
tipFormatter,
|
tipFormatter,
|
||||||
@@ -438,7 +486,9 @@ class Slider extends React.Component {
|
|||||||
value: v,
|
value: v,
|
||||||
offset: offsets[i],
|
offset: offsets[i],
|
||||||
dragging: handle === i,
|
dragging: handle === i,
|
||||||
|
index: i,
|
||||||
key: i,
|
key: i,
|
||||||
|
ref: `handle-${i}`,
|
||||||
}));
|
}));
|
||||||
if (!range) { handles.shift(); }
|
if (!range) { handles.shift(); }
|
||||||
|
|
||||||
@@ -451,33 +501,38 @@ class Slider extends React.Component {
|
|||||||
[`${prefixCls}-track-${i}`]: true,
|
[`${prefixCls}-track-${i}`]: true,
|
||||||
});
|
});
|
||||||
tracks.push(
|
tracks.push(
|
||||||
<Track className={trackClassName} vertical={vertical} included={isIncluded}
|
<Track className={trackClassName} vertical={vertical} included={isIncluded}
|
||||||
offset={offsets[i - 1]} length={offsets[i] - offsets[i - 1]} key={i} />
|
offset={offsets[i - 1]} length={offsets[i] - offsets[i - 1]} key={i}
|
||||||
|
/>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
const sliderClassName = classNames({
|
const sliderClassName = classNames({
|
||||||
[prefixCls]: true,
|
[prefixCls]: true,
|
||||||
[prefixCls + '-disabled']: disabled,
|
[`${prefixCls}-with-marks`]: Object.keys(marks).length,
|
||||||
|
[`${prefixCls}-disabled`]: disabled,
|
||||||
|
[`${prefixCls}-vertical`]: this.props.vertical,
|
||||||
[className]: !!className,
|
[className]: !!className,
|
||||||
[prefixCls + '-vertical']: this.props.vertical,
|
|
||||||
});
|
});
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div ref="slider" className={sliderClassName}
|
<div ref="slider" className={sliderClassName}
|
||||||
onTouchStart={disabled ? noop : this.onTouchStart.bind(this)}
|
onTouchStart={disabled ? noop : this.onTouchStart.bind(this)}
|
||||||
onMouseDown={disabled ? noop : this.onMouseDown.bind(this)}
|
onMouseDown={disabled ? noop : this.onMouseDown.bind(this)}
|
||||||
>
|
>
|
||||||
{handles}
|
<div className={`${prefixCls}-rail`} />
|
||||||
{tracks}
|
{tracks}
|
||||||
<Steps prefixCls={prefixCls} vertical = {vertical} marks={marks} dots={dots} step={step}
|
<Steps prefixCls={prefixCls} vertical = {vertical} marks={marks} dots={dots} step={step}
|
||||||
included={isIncluded} lowerBound={bounds[0]}
|
included={isIncluded} lowerBound={bounds[0]}
|
||||||
upperBound={bounds[bounds.length - 1]} max={max} min={min} />
|
upperBound={bounds[bounds.length - 1]} max={max} min={min}
|
||||||
<Marks className={prefixCls + '-mark'} vertical = {vertical} marks={marks}
|
/>
|
||||||
included={isIncluded} lowerBound={bounds[0]}
|
{handles}
|
||||||
upperBound={bounds[bounds.length - 1]} max={max} min={min} />
|
<Marks className={`${prefixCls}-mark`} vertical = {vertical} marks={marks}
|
||||||
{children}
|
included={isIncluded} lowerBound={bounds[0]}
|
||||||
</div>
|
upperBound={bounds[bounds.length - 1]} max={max} min={min}
|
||||||
|
/>
|
||||||
|
{children}
|
||||||
|
</div>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -498,6 +553,7 @@ Slider.propTypes = {
|
|||||||
included: React.PropTypes.bool,
|
included: React.PropTypes.bool,
|
||||||
className: React.PropTypes.string,
|
className: React.PropTypes.string,
|
||||||
prefixCls: React.PropTypes.string,
|
prefixCls: React.PropTypes.string,
|
||||||
|
tooltipPrefixCls: React.PropTypes.string,
|
||||||
disabled: React.PropTypes.bool,
|
disabled: React.PropTypes.bool,
|
||||||
children: React.PropTypes.any,
|
children: React.PropTypes.any,
|
||||||
onBeforeChange: React.PropTypes.func,
|
onBeforeChange: React.PropTypes.func,
|
||||||
|
|||||||
+8
-5
@@ -3,7 +3,10 @@ import classNames from 'classnames';
|
|||||||
import warning from 'warning';
|
import warning from 'warning';
|
||||||
|
|
||||||
function calcPoints(vertical, marks, dots, step, min, max) {
|
function calcPoints(vertical, marks, dots, step, min, max) {
|
||||||
warning(dots ? step > 0 : true, '`Slider[step]` should be a positive number in order to make Slider[dots] work.');
|
warning(
|
||||||
|
dots ? step > 0 : true,
|
||||||
|
'`Slider[step]` should be a positive number in order to make Slider[dots] work.'
|
||||||
|
);
|
||||||
const points = Object.keys(marks).map(parseFloat);
|
const points = Object.keys(marks).map(parseFloat);
|
||||||
if (dots) {
|
if (dots) {
|
||||||
for (let i = min; i <= max; i = i + step) {
|
for (let i = min; i <= max; i = i + step) {
|
||||||
@@ -18,20 +21,20 @@ const Steps = ({ prefixCls, vertical, marks, dots, step, included,
|
|||||||
lowerBound, upperBound, max, min }) => {
|
lowerBound, upperBound, max, min }) => {
|
||||||
const range = max - min;
|
const range = max - min;
|
||||||
const elements = calcPoints(vertical, marks, dots, step, min, max).map((point) => {
|
const elements = calcPoints(vertical, marks, dots, step, min, max).map((point) => {
|
||||||
const offset = Math.abs(point - min) / range * 100 + '%';
|
const offset = `${Math.abs(point - min) / range * 100}%`;
|
||||||
const style = vertical ? { bottom: offset } : { left: offset };
|
const style = vertical ? { bottom: offset } : { left: offset };
|
||||||
|
|
||||||
const isActived = (!included && point === upperBound) ||
|
const isActived = (!included && point === upperBound) ||
|
||||||
(included && point <= upperBound && point >= lowerBound);
|
(included && point <= upperBound && point >= lowerBound);
|
||||||
const pointClassName = classNames({
|
const pointClassName = classNames({
|
||||||
[prefixCls + '-dot']: true,
|
[`${prefixCls}-dot`]: true,
|
||||||
[prefixCls + '-dot-active']: isActived,
|
[`${prefixCls}-dot-active`]: isActived,
|
||||||
});
|
});
|
||||||
|
|
||||||
return <span className={pointClassName} style={style} key={point} />;
|
return <span className={pointClassName} style={style} key={point} />;
|
||||||
});
|
});
|
||||||
|
|
||||||
return <div className={prefixCls + '-step'}>{elements}</div>;
|
return <div className={`${prefixCls}-step`}>{elements}</div>;
|
||||||
};
|
};
|
||||||
|
|
||||||
export default Steps;
|
export default Steps;
|
||||||
|
|||||||
+4
-4
@@ -5,11 +5,11 @@ const Track = ({ className, included, vertical, offset, length }) => {
|
|||||||
visibility: included ? 'visible' : 'hidden',
|
visibility: included ? 'visible' : 'hidden',
|
||||||
};
|
};
|
||||||
if (vertical) {
|
if (vertical) {
|
||||||
style.bottom = offset + '%';
|
style.bottom = `${offset}%`;
|
||||||
style.height = length + '%';
|
style.height = `${length}%`;
|
||||||
} else {
|
} else {
|
||||||
style.left = offset + '%';
|
style.left = `${offset}%`;
|
||||||
style.width = length + '%';
|
style.width = `${length}%`;
|
||||||
}
|
}
|
||||||
return <div className={className} style={style} />;
|
return <div className={className} style={style} />;
|
||||||
};
|
};
|
||||||
|
|||||||
+134
@@ -1,9 +1,22 @@
|
|||||||
|
/* eslint-disable max-len */
|
||||||
const expect = require('expect.js');
|
const expect = require('expect.js');
|
||||||
const React = require('react');
|
const React = require('react');
|
||||||
const ReactDOM = require('react-dom');
|
const ReactDOM = require('react-dom');
|
||||||
const ReactTestUtils = require('react-addons-test-utils');
|
const ReactTestUtils = require('react-addons-test-utils');
|
||||||
const Slider = require('..');
|
const Slider = require('..');
|
||||||
|
|
||||||
|
function createSliderWrapperComponent() {
|
||||||
|
return class SliderWrapper extends React.Component {
|
||||||
|
render() {
|
||||||
|
return (
|
||||||
|
<div style={{ position: `absolute`, width: `100px`, height: `10px` }}>
|
||||||
|
<Slider ref="slider"/>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
require('../assets/index.less');
|
require('../assets/index.less');
|
||||||
|
|
||||||
describe('rc-slider', function test() {
|
describe('rc-slider', function test() {
|
||||||
@@ -206,4 +219,125 @@ describe('rc-slider', function test() {
|
|||||||
const slider = ReactDOM.render(<Slider vertical />, div);
|
const slider = ReactDOM.render(<Slider vertical />, div);
|
||||||
expect(ReactTestUtils.scryRenderedDOMComponentsWithClass(slider, 'rc-slider-vertical').length).to.be(1);
|
expect(ReactTestUtils.scryRenderedDOMComponentsWithClass(slider, 'rc-slider-vertical').length).to.be(1);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should not call onChange when value is the same', () => {
|
||||||
|
const values = [];
|
||||||
|
const handler = (e) => {
|
||||||
|
values.push(e);
|
||||||
|
};
|
||||||
|
|
||||||
|
ReactDOM.render(<Slider onChange={handler}/>, div);
|
||||||
|
const handle = div.querySelector('.rc-slider-handle');
|
||||||
|
|
||||||
|
const down = document.createEvent('MouseEvent');
|
||||||
|
down.initEvent('mousedown', true, true);
|
||||||
|
|
||||||
|
const move = document.createEvent('MouseEvent');
|
||||||
|
move.initEvent('mousemove', true, true);
|
||||||
|
|
||||||
|
const up = document.createEvent('MouseEvent');
|
||||||
|
up.initEvent('mouseup', true, true);
|
||||||
|
|
||||||
|
handle.dispatchEvent(down);
|
||||||
|
handle.dispatchEvent(move);
|
||||||
|
handle.dispatchEvent(up);
|
||||||
|
|
||||||
|
expect(values.length).to.be(0);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should set `dragOffset` to correct value when the left handle is clicked off-center', () => {
|
||||||
|
const slider = ReactDOM.render(<Slider />, div);
|
||||||
|
const leftHandle = ReactTestUtils.scryRenderedDOMComponentsWithClass(slider, 'rc-slider-handle')[0];
|
||||||
|
slider.onMouseDown({
|
||||||
|
type: 'mousedown',
|
||||||
|
target: leftHandle,
|
||||||
|
pageX: 5, button: 0,
|
||||||
|
stopPropagation() {},
|
||||||
|
preventDefault() {},
|
||||||
|
});
|
||||||
|
expect(slider.dragOffset).to.be(5);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should respect `dragOffset` while dragging the handle via MouseEvents', () => {
|
||||||
|
const SliderWrapper = createSliderWrapperComponent();
|
||||||
|
const slider = ReactDOM.render(<SliderWrapper/>, div).refs.slider;
|
||||||
|
const leftHandle = ReactTestUtils.scryRenderedDOMComponentsWithClass(slider, 'rc-slider-handle')[0];
|
||||||
|
slider.onMouseDown({
|
||||||
|
type: 'mousedown',
|
||||||
|
target: leftHandle,
|
||||||
|
pageX: 5, button: 0,
|
||||||
|
stopPropagation() {},
|
||||||
|
preventDefault() {},
|
||||||
|
});
|
||||||
|
expect(slider.dragOffset).to.be(5);
|
||||||
|
slider.onMouseMove({
|
||||||
|
type: 'mousemove',
|
||||||
|
target: leftHandle,
|
||||||
|
pageX: 14, button: 0,
|
||||||
|
stopPropagation() {},
|
||||||
|
preventDefault() {},
|
||||||
|
});
|
||||||
|
expect(slider.getValue()).to.be(9);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should set `dragOffset` to 0 when the MouseEvent target isn\'t a handle', () => {
|
||||||
|
const slider = ReactDOM.render(<Slider />, div);
|
||||||
|
const sliderTrack = ReactTestUtils.scryRenderedDOMComponentsWithClass(slider, 'rc-slider-track')[0];
|
||||||
|
slider.onMouseDown({
|
||||||
|
type: 'mousedown',
|
||||||
|
target: sliderTrack,
|
||||||
|
pageX: 5, button: 0,
|
||||||
|
stopPropagation() {},
|
||||||
|
preventDefault() {},
|
||||||
|
});
|
||||||
|
expect(slider.dragOffset).to.be(0);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should set `dragOffset` to correct value when the left handle is touched off-center', () => {
|
||||||
|
const slider = ReactDOM.render(<Slider />, div);
|
||||||
|
const leftHandle = ReactTestUtils.scryRenderedDOMComponentsWithClass(slider, 'rc-slider-handle')[0];
|
||||||
|
slider.onTouchStart({
|
||||||
|
type: 'touchstart',
|
||||||
|
target: leftHandle,
|
||||||
|
touches: [{ pageX: 5 }],
|
||||||
|
stopPropagation() {},
|
||||||
|
preventDefault() {},
|
||||||
|
});
|
||||||
|
expect(slider.dragOffset).to.be(5);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should respect `dragOffset` while dragging the handle via TouchEvents', () => {
|
||||||
|
const SliderWrapper = createSliderWrapperComponent();
|
||||||
|
const slider = ReactDOM.render(<SliderWrapper/>, div).refs.slider;
|
||||||
|
const leftHandle = ReactTestUtils.scryRenderedDOMComponentsWithClass(slider, 'rc-slider-handle')[0];
|
||||||
|
slider.onTouchStart({
|
||||||
|
type: 'touchstart',
|
||||||
|
target: leftHandle,
|
||||||
|
touches: [{ pageX: 5 }],
|
||||||
|
stopPropagation() {},
|
||||||
|
preventDefault() {},
|
||||||
|
});
|
||||||
|
expect(slider.dragOffset).to.be(5);
|
||||||
|
slider.onTouchMove({
|
||||||
|
type: 'touchmove',
|
||||||
|
target: leftHandle,
|
||||||
|
touches: [{ pageX: 14 }],
|
||||||
|
stopPropagation() {},
|
||||||
|
preventDefault() {},
|
||||||
|
});
|
||||||
|
expect(slider.getValue()).to.be(9);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should set `dragOffset` to 0 when the TouchEvent target isn\'t a handle', () => {
|
||||||
|
const slider = ReactDOM.render(<Slider />, div);
|
||||||
|
const sliderTrack = ReactTestUtils.scryRenderedDOMComponentsWithClass(slider, 'rc-slider-track')[0];
|
||||||
|
slider.onTouchStart({
|
||||||
|
type: 'touchstart',
|
||||||
|
target: sliderTrack,
|
||||||
|
touches: [{ pageX: 5 }],
|
||||||
|
stopPropagation() {},
|
||||||
|
preventDefault() {},
|
||||||
|
});
|
||||||
|
expect(slider.dragOffset).to.be(0);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user