Compare commits

...
9 Commits
5 changed files with 202 additions and 195 deletions
+3 -3
View File
@@ -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>
+9 -6
View File
@@ -18,16 +18,19 @@ const handleStyle = {
textAlign: 'center', textAlign: 'center',
}; };
const CustomHandle = props => { const CustomHandle = React.createClass({
propTypes: {
value: React.PropTypes.any,
offset: React.PropTypes.number,
},
render() {
const props = this.props;
const style = Object.assign({ left: `${props.offset}%` }, handleStyle); const style = Object.assign({ left: `${props.offset}%` }, handleStyle);
return ( return (
<div style={style}>val: {props.value}</div> <div style={style}>val: {props.value}</div>
); );
}; },
CustomHandle.propTypes = { });
value: React.PropTypes.any,
offset: React.PropTypes.number,
};
ReactDOM.render( ReactDOM.render(
<div> <div>
+2 -2
View File
@@ -1,6 +1,6 @@
{ {
"name": "rc-slider", "name": "rc-slider",
"version": "5.3.0", "version": "5.4.0",
"description": "slider ui component for react", "description": "slider ui component for react",
"keywords": [ "keywords": [
"react", "react",
@@ -25,7 +25,7 @@
"licenses": "MIT", "licenses": "MIT",
"main": "./lib/index.js", "main": "./lib/index.js",
"entry": { "entry": {
"rc-slider": ["./src/index.js", "./assets/index.less"] "rc-slider": ["./assets/index.less", "./src/index.js"]
}, },
"style": "./assets/index.css", "style": "./assets/index.css",
"config": { "config": {
+9 -7
View File
@@ -10,18 +10,18 @@ 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,
@@ -34,6 +34,7 @@ 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}%` };
@@ -55,7 +56,7 @@ export default class Handle extends React.Component {
prefixCls={tooltipPrefixCls || `${prefixCls}-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}
> >
@@ -76,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,
}; };
+175 -173
View File
@@ -110,21 +110,27 @@ class Slider extends React.Component {
props.onChange(changedValue); props.onChange(changedValue);
} }
onMouseDown(e) {
if (e.button !== 0) { return; }
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);
}
onMouseMove(e) { onMouseMove(e) {
const position = getMousePosition(this.props.vertical, e); const position = getMousePosition(this.props.vertical, e);
this.onMove(e, position - this.dragOffset); this.onMove(e, position - this.dragOffset);
} }
onTouchMove(e) {
if (isNotTouchEvent(e)) {
this.end('touch');
return;
}
const position = getTouchPosition(this.props.vertical, e);
this.onMove(e, position - this.dragOffset);
}
onMove(e, position) { onMove(e, position) {
pauseEvent(e); pauseEvent(e);
const props = this.props; const props = this.props;
@@ -154,38 +160,6 @@ class Slider extends React.Component {
}); });
} }
onTouchStart(e) {
if (isNotTouchEvent(e)) return;
let position = getTouchPosition(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('touch');
pauseEvent(e);
}
onMouseDown(e) {
if (e.button !== 0) { return; }
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);
}
onStart(position) { onStart(position) {
const props = this.props; const props = this.props;
props.onBeforeChange(this.getValue()); props.onBeforeChange(this.getValue());
@@ -231,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 position = getTouchPosition(this.props.vertical, e);
const slider = this.refs.slider; this.onMove(e, position - this.dragOffset);
if (!slider) {
return 0;
} }
return this.props.vertical ? slider.clientHeight : slider.clientWidth; onTouchStart(e) {
} if (isNotTouchEvent(e)) return;
getSliderStart() { let position = getTouchPosition(this.props.vertical, e);
const slider = this.refs.slider; if (!this.isEventFromHandle(e)) {
const rect = slider.getBoundingClientRect(); this.dragOffset = 0;
} else {
return this.props.vertical ? rect.top : rect.left; const handlePosition = getHandleCenterPosition(this.props.vertical, e.target);
this.dragOffset = position - handlePosition;
position = handlePosition;
} }
this.onStart(position);
getPrecision(step) { this.addDocumentEvents('touch');
const stepString = step.toString(); pauseEvent(e);
let precision = 0;
if (stepString.indexOf('.') >= 0) {
precision = stepString.length - stepString.indexOf('.') - 1;
}
return precision;
} }
/** /**
@@ -282,6 +252,76 @@ 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) { isEventFromHandle(e) {
return this.state.bounds.some((x, i) => ( return this.state.bounds.some((x, i) => (
this.refs[`handle-${i}`] && this.refs[`handle-${i}`] &&
@@ -293,6 +333,74 @@ class Slider extends React.Component {
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;
@@ -326,114 +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,
@@ -486,6 +486,7 @@ 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}`, ref: `handle-${i}`,
})); }));
@@ -508,9 +509,10 @@ class Slider extends React.Component {
const sliderClassName = classNames({ const sliderClassName = classNames({
[prefixCls]: true, [prefixCls]: true,
[`${prefixCls}-with-marks`]: Object.keys(marks).length,
[`${prefixCls}-disabled`]: disabled, [`${prefixCls}-disabled`]: disabled,
[className]: !!className,
[`${prefixCls}-vertical`]: this.props.vertical, [`${prefixCls}-vertical`]: this.props.vertical,
[className]: !!className,
}); });
return ( return (