Compare commits

..
19 Commits
Author SHA1 Message Date
Benjy Cui 7525de1f1a bump 5.4.0 2016-12-06 09:46:03 +08:00
Ray Zane 856bdef4ea feat: Make the second argument to tipFormatter the index of the handle (#167) 2016-12-06 09:41:21 +08:00
Igor Goldvekht bb41b82b59 docs: update Usage section to use accurate component name (#182) 2016-12-01 16:38:33 +08:00
Benjy Cui a428cd3335 style: sort component 2016-11-22 10:40:02 +08:00
Benjy Cui c021cadcbb bump 5.3.5 2016-11-21 15:56:05 +08:00
Benjy Cui 4a59745a23 chore: expose className to identify Slider with marks 2016-11-21 15:55:24 +08:00
Benjy Cui 3e78e9ac4a docs: fix warning in example, close: #173 2016-11-21 15:46:06 +08:00
Benjy Cui 82179e613a bump 5.3.4 2016-11-16 10:14:58 +08:00
Benjy Cui 0a6fb5a570 bump 5.3.3 2016-11-15 14:29:53 +08:00
Benjy Cui 53aa61faf2 feat: provide dist files 2016-11-15 14:04:15 +08:00
Benjy Cui 9655483bf5 bump 5.2.1 2016-10-31 11:51:44 +08:00
Benjy Cui faa4f384a3 fix: handle could be 0, close: #163 2016-10-31 11:50:49 +08:00
Benjy Cui 18ddbb796c bump 5.2.0 2016-10-31 11:24:55 +08:00
Benjy Cui 051fef6603 feat: make clickable area larger, ref: #160 2016-10-31 11:23:41 +08:00
Benjy Cui b325daef48 bump 5.1.3 2016-10-21 16:44:58 +08:00
Benjy Cui db47c3d27d chore: improve UX, close: #160 2016-10-21 16:44:32 +08:00
Benjy Cui f20014a9d4 bump 5.1.2 2016-10-14 15:10:14 +08:00
Paul Taylor 6074d5fc43 fix: Account for position difference between handle and event origin when dragging (#146)
* Adjust handle drag position to account for the difference between handle origin and mouse or touch event origin.

* Add slider dragOffset tests.
2016-10-14 15:07:51 +08:00
Benjy Cui 34754440ed docs: add change log for 5.0.0, close: #149 2016-10-14 14:45:53 +08:00
9 changed files with 359 additions and 190 deletions
+5
View File
@@ -1,6 +1,11 @@
# 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
[#133](https://github.com/react-component/slider/pull/133) support multi-range ([@sosz](https://github.com/sosz))
+3 -3
View File
@@ -56,8 +56,8 @@ require('rc-slider/assets/index.css');
var React = require('react');
var ReactDOM = require('react-dom');
var Rcslider = require('rc-slider');
ReactDOM.render(<Rcslider />, container);
var Slider = require('rc-slider');
ReactDOM.render(<Slider />, container);
```
## API
@@ -168,7 +168,7 @@ ReactDOM.render(<Rcslider />, container);
<td>tipFormatter</td>
<td>function or `null`</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>
<td>dots</td>
+20 -7
View File
@@ -23,13 +23,19 @@
.@{prefixClass} {
position: relative;
height: 4px;
height: 14px;
padding: 5px 0;
width: 100%;
border-radius: @border-radius-base;
background-color: #e9e9e9;
.borderBox();
&-rail {
position: absolute;
width: 100%;
background-color: #e9e9e9;
height: 4px;
}
&-track {
position: absolute;
left: 0;
@@ -62,7 +68,7 @@
&-mark {
position: absolute;
top: 10px;
top: 18px;
left: 0;
width: 100%;
font-size: 12px;
@@ -130,23 +136,30 @@
}
.@{prefixClass}-vertical {
width: 4px;
width: 14px;
height: 100%;
padding: 0 5px;
.@{prefixClass} {
&-rail {
height: 100%;
width: 4px;
}
&-track {
left: 5px;
bottom: 0;
width: 4px;
}
&-handle {
position: absolute;
margin-left: -5px;
margin-bottom: -7px;
}
&-mark {
top: 0;
left: 10px;
left: 18px;
height: 100%;
}
+13 -10
View File
@@ -18,16 +18,19 @@ const handleStyle = {
textAlign: 'center',
};
const CustomHandle = props => {
const style = Object.assign({ left: `${props.offset}%` }, handleStyle);
return (
<div style={style}>val: {props.value}</div>
);
};
CustomHandle.propTypes = {
value: React.PropTypes.any,
offset: React.PropTypes.number,
};
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);
return (
<div style={style}>val: {props.value}</div>
);
},
});
ReactDOM.render(
<div>
+6 -2
View File
@@ -1,6 +1,6 @@
{
"name": "rc-slider",
"version": "5.1.1",
"version": "5.4.0",
"description": "slider ui component for react",
"keywords": [
"react",
@@ -19,10 +19,14 @@
},
"files": [
"assets/*.css",
"lib"
"lib",
"dist"
],
"licenses": "MIT",
"main": "./lib/index.js",
"entry": {
"rc-slider": ["./assets/index.less", "./src/index.js"]
},
"style": "./assets/index.css",
"config": {
"port": 8005
+9 -7
View File
@@ -10,18 +10,18 @@ export default class Handle extends React.Component {
};
}
showTooltip() {
this.setState({
isTooltipVisible: true,
});
}
hideTooltip() {
this.setState({
isTooltipVisible: false,
});
}
showTooltip() {
this.setState({
isTooltipVisible: true,
});
}
render() {
const {
prefixCls,
@@ -34,6 +34,7 @@ export default class Handle extends React.Component {
value,
dragging,
noTip,
index,
} = this.props;
const style = vertical ? { bottom: `${offset}%` } : { left: `${offset}%` };
@@ -55,7 +56,7 @@ export default class Handle extends React.Component {
prefixCls={tooltipPrefixCls || `${prefixCls}-tooltip`}
placement="top"
visible={isTooltipVisible}
overlay={<span>{tipFormatter(value)}</span>}
overlay={<span>{tipFormatter(value, index)}</span>}
delay={0}
transitionName={tipTransitionName}
>
@@ -76,4 +77,5 @@ Handle.propTypes = {
value: React.PropTypes.number,
dragging: React.PropTypes.bool,
noTip: React.PropTypes.bool,
index: React.PropTypes.number,
};
+1 -1
View File
@@ -18,7 +18,7 @@ const Marks = ({ className, vertical, marks, included, upperBound, lowerBound, m
const bottomStyle = {
// height: markWidth + '%',
marginBottom: '-200%',
marginBottom: '-50%',
bottom: `${(point - min) / range * 100}%`,
};
+194 -160
View File
@@ -1,3 +1,4 @@
import { findDOMNode } from 'react-dom';
import React, { cloneElement } from 'react';
import addEventListener from 'rc-util/lib/Dom/addEventListener';
import classNames from 'classnames';
@@ -22,6 +23,13 @@ function getMousePosition(vertical, e) {
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) {
e.stopPropagation();
e.preventDefault();
@@ -93,7 +101,7 @@ class Slider extends React.Component {
const isNotControlled = !('value' in props);
if (isNotControlled) {
this.setState(state);
} else if (state.handle) {
} else if (state.handle !== undefined) {
this.setState({ handle: state.handle });
}
@@ -102,19 +110,25 @@ class Slider extends React.Component {
props.onChange(changedValue);
}
onMouseMove(e) {
const position = getMousePosition(this.props.vertical, e);
this.onMove(e, position);
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);
}
onTouchMove(e) {
if (isNotTouchEvent(e)) {
this.end('touch');
return;
}
const position = getTouchPosition(this.props.vertical, e);
this.onMove(e, position);
onMouseMove(e) {
const position = getMousePosition(this.props.vertical, e);
this.onMove(e, position - this.dragOffset);
}
onMove(e, position) {
@@ -146,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) {
const props = this.props;
props.onBeforeChange(this.getValue());
@@ -208,34 +205,30 @@ class Slider extends React.Component {
this.onChange({ bounds: nextBounds });
}
getValue() {
const { bounds } = this.state;
return this.props.range ? bounds : bounds[1];
}
getSliderLength() {
const slider = this.refs.slider;
if (!slider) {
return 0;
onTouchMove(e) {
if (isNotTouchEvent(e)) {
this.end('touch');
return;
}
return this.props.vertical ? slider.clientHeight : slider.clientWidth;
const position = getTouchPosition(this.props.vertical, e);
this.onMove(e, position - this.dragOffset);
}
getSliderStart() {
const slider = this.refs.slider;
const rect = slider.getBoundingClientRect();
onTouchStart(e) {
if (isNotTouchEvent(e)) return;
return this.props.vertical ? rect.top : rect.left;
}
getPrecision(step) {
const stepString = step.toString();
let precision = 0;
if (stepString.indexOf('.') >= 0) {
precision = stepString.length - stepString.indexOf('.') - 1;
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;
}
return precision;
this.onStart(position);
this.addDocumentEvents('touch');
pauseEvent(e);
}
/**
@@ -259,10 +252,155 @@ class Slider extends React.Component {
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) {
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) {
const state = this.state || {};
const { handle, bounds } = state;
@@ -296,114 +434,6 @@ class Slider extends React.Component {
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() {
const {
handle,
@@ -456,7 +486,9 @@ class Slider extends React.Component {
value: v,
offset: offsets[i],
dragging: handle === i,
index: i,
key: i,
ref: `handle-${i}`,
}));
if (!range) { handles.shift(); }
@@ -477,9 +509,10 @@ class Slider extends React.Component {
const sliderClassName = classNames({
[prefixCls]: true,
[`${prefixCls}-with-marks`]: Object.keys(marks).length,
[`${prefixCls}-disabled`]: disabled,
[className]: !!className,
[`${prefixCls}-vertical`]: this.props.vertical,
[className]: !!className,
});
return (
@@ -487,6 +520,7 @@ class Slider extends React.Component {
onTouchStart={disabled ? noop : this.onTouchStart.bind(this)}
onMouseDown={disabled ? noop : this.onMouseDown.bind(this)}
>
<div className={`${prefixCls}-rail`} />
{tracks}
<Steps prefixCls={prefixCls} vertical = {vertical} marks={marks} dots={dots} step={step}
included={isIncluded} lowerBound={bounds[0]}
+108
View File
@@ -5,6 +5,18 @@ const ReactDOM = require('react-dom');
const ReactTestUtils = require('react-addons-test-utils');
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');
describe('rc-slider', function test() {
@@ -232,4 +244,100 @@ describe('rc-slider', function test() {
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);
});
});