Compare commits

...
20 Commits
Author SHA1 Message Date
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
Benjy Cui fac5a1b9f3 bump 5.1.1 2016-09-12 11:33:18 +08:00
Benjy Cui 1b8e27e613 bump 5.1.0 2016-09-12 11:28:41 +08:00
Benjy Cui e438757a64 feat: support tooltipPrefixcls 2016-09-12 11:26:48 +08:00
Benjy Cui aad5fef20c bump 5.0.0 2016-09-12 10:57:20 +08:00
Ulrich-Matthias Schäfer 1568019a59 fix: onChange which fired on every mousmove, added test (#145) 2016-09-12 10:54:16 +08:00
Dylan Kirkby abdb297c16 chore: Add warning when range isn't a multiple of the step (#142) 2016-09-12 10:53:15 +08:00
Benjy Cui 67b76953c6 chore: update .travis.yml 2016-09-10 17:04:06 +08:00
Benjy Cui b6d8f389ef fix: should limit CSS scope, close: #147 2016-09-10 16:52:38 +08:00
Benjy Cui d7e6b9d9be bump 4.0.1 2016-08-16 10:01:30 +08:00
Benjy Cui 67a33126ec style: update code style to please lint 2016-08-16 09:59:14 +08:00
Derrick Pelletier 1a8331bcf1 refactor: Removed z-indexes #135 (#136) 2016-08-16 09:32:21 +08:00
16 changed files with 307 additions and 81 deletions
+3 -3
View File
@@ -29,8 +29,8 @@ script:
env:
matrix:
- TEST_TYPE=lint
- TEST_TYPE=browser-test
- TEST_TYPE=browser-test-cover
- TEST_TYPE=test
- TEST_TYPE=coverage
- TEST_TYPE=saucelabs
global:
- secure: S1VwbaPzLnSH/IUT/wlJulxAX5VHRIDmSt53h/ycHcZsszUpWcLCJRQAe0fTVB2dAx5MdBbSZ+o+tr3tRwVB5TRAYm0oTCsYAkOZaWOB28RuUQtdGt3wf9xxTG1UiPiaLLUW3waX9zAaf3yqKBcJGf1op0RD8dksxbCFw/7xVbU=
@@ -39,4 +39,4 @@ env:
matrix:
allow_failures:
- env: "TEST_TYPE=saucelabs"
- env: "TEST_TYPE=saucelabs"
+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))
+22 -14
View File
@@ -23,20 +23,25 @@
.@{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;
height: 4px;
border-radius: @border-radius-base;
background-color: tint(@primary-color, 60%);
z-index: 1;
}
&-handle {
@@ -49,7 +54,6 @@
border-radius: 50%;
border: solid 2px tint(@primary-color, 50%);
background-color: #fff;
z-index: 2;
&:hover {
border-color: tint(@primary-color, 20%);
@@ -64,11 +68,10 @@
&-mark {
position: absolute;
top: 10px;
top: 18px;
left: 0;
width: 100%;
font-size: 12px;
z-index: 3;
}
&-mark-text {
@@ -89,7 +92,6 @@
width: 100%;
height: 4px;
background: transparent;
z-index: 1;
}
&-dot {
@@ -134,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%;
}
@@ -232,11 +241,10 @@
}
}
.rc-tooltip {
.@{prefixClass}-tooltip {
position: absolute;
left: -9999px;
top: -9999px;
z-index: 4;
visibility: visible;
.borderBox();
@@ -278,4 +286,4 @@
border-width: @tooltip-arrow-width @tooltip-arrow-width 0;
border-top-color: @tooltip-arrow-color;
}
}
}
+4 -4
View File
@@ -16,11 +16,10 @@ const handleStyle = {
background: '#fff',
fontSize: '14px',
textAlign: 'center',
zIndex: 3,
};
const CustomHandle = props => {
const style = Object.assign({ left: props.offset + '%' }, handleStyle);
const style = Object.assign({ left: `${props.offset}%` }, handleStyle);
return (
<div style={style}>val: {props.value}</div>
);
@@ -40,5 +39,6 @@ ReactDOM.render(
<p>Slider with custom handle</p>
<Slider min={0} max={20} defaultValue={3} handle={<CustomHandle />} />
</div>
</div>
, document.getElementById('__react-content'));
</div>,
document.getElementById('__react-content')
);
+3 -1
View File
@@ -81,7 +81,9 @@ const DynamicBounds = React.createClass({
<label>Max: </label>
<input type="number" value={this.state.max} onChange={this.onMaxChange} />
<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>
);
},
+12 -4
View File
@@ -13,7 +13,7 @@ function log(value) {
function percentFormatter(v) {
return v + ' %';
return `${v} %`;
}
const CustomizedSlider = React.createClass({
@@ -32,7 +32,11 @@ const CustomizedSlider = React.createClass({
console.log(value);
},
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>
<input type="number" value={this.state.max} onChange={this.onMaxChange} />
<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>
);
},
@@ -87,7 +93,9 @@ ReactDOM.render(
</div>
<div style={style}>
<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 style={style}>
<p>Basic Slider without tooltip</p>
+3 -1
View File
@@ -49,7 +49,9 @@ ReactDOM.render(
</div>
<div style={style}>
<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>
, document.getElementById('__react-content'));
+6 -2
View File
@@ -39,7 +39,9 @@ const CustomizedRange = React.createClass({
render() {
return (
<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>
<input type="number" value={this.state.lowerBound} onChange={this.onLowerBoundChange} />
<br />
@@ -75,7 +77,9 @@ const DynamicBounds = React.createClass({
render() {
return (
<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>
<input type="number" value={this.state.min} onChange={this.onMinChange} />
<br />
+7 -3
View File
@@ -14,7 +14,7 @@ function log(value) {
function percentFormatter(v) {
return v + ' %';
return `${v} %`;
}
const CustomizedSlider = React.createClass({
@@ -58,7 +58,9 @@ const DynamicBounds = React.createClass({
return (
<div style={style}>
<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>
<input type="number" value={this.state.min} onChange={this.onMinChange} />
<br />
@@ -85,7 +87,9 @@ ReactDOM.render(
</div>
<div style={style}>
<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 style={style}>
<p>Basic Slider without tooltip</p>
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "rc-slider",
"version": "4.0.0",
"version": "5.2.1",
"description": "slider ui component for react",
"keywords": [
"react",
+4 -2
View File
@@ -25,6 +25,7 @@ export default class Handle extends React.Component {
render() {
const {
prefixCls,
tooltipPrefixCls,
className,
tipTransitionName,
tipFormatter,
@@ -35,7 +36,7 @@ export default class Handle extends React.Component {
noTip,
} = this.props;
const style = vertical ? { bottom: offset + '%' } : { left: offset + '%' };
const style = vertical ? { bottom: `${offset}%` } : { left: `${offset}%` };
const handle = (
<div className={className} style={style}
onMouseUp={this.showTooltip.bind(this)}
@@ -51,7 +52,7 @@ export default class Handle extends React.Component {
const isTooltipVisible = dragging || this.state.isTooltipVisible;
return (
<Tooltip
prefixCls={prefixCls.replace('slider', 'tooltip')}
prefixCls={tooltipPrefixCls || `${prefixCls}-tooltip`}
placement="top"
visible={isTooltipVisible}
overlay={<span>{tipFormatter(value)}</span>}
@@ -66,6 +67,7 @@ export default class Handle extends React.Component {
Handle.propTypes = {
prefixCls: React.PropTypes.string,
tooltipPrefixCls: React.PropTypes.string,
className: React.PropTypes.string,
vertical: React.PropTypes.bool,
offset: React.PropTypes.number,
+7 -7
View File
@@ -12,20 +12,20 @@ const Marks = ({ className, vertical, marks, included, upperBound, lowerBound, m
const isActived = (!included && point === upperBound) ||
(included && point <= upperBound && point >= lowerBound);
const markClassName = classNames({
[className + '-text']: true,
[className + '-text-active']: isActived,
[`${className}-text`]: true,
[`${className}-text-active`]: isActived,
});
const bottomStyle = {
// height: markWidth + '%',
marginBottom: '-200' + '%',
bottom: (point - min) / range * 100 + '%',
marginBottom: '-50%',
bottom: `${(point - min) / range * 100}%`,
};
const leftStyle = {
width: markWidth + '%',
marginLeft: -markWidth / 2 + '%',
left: (point - min) / range * 100 + '%',
width: `${markWidth}%`,
marginLeft: `${-markWidth / 2}%`,
left: `${(point - min) / range * 100}%`,
};
const style = vertical ? bottomStyle : leftStyle;
+84 -30
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';
@@ -5,6 +6,7 @@ import Track from './Track';
import DefaultHandle from './Handle';
import Steps from './Steps';
import Marks from './Marks';
import warning from 'warning';
function noop() {
}
@@ -21,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();
@@ -30,7 +39,7 @@ class Slider extends React.Component {
constructor(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 defaultValue = ('defaultValue' in props ? props.defaultValue : initialValue);
const value = (props.value !== undefined ? props.value : defaultValue);
@@ -44,6 +53,17 @@ class Slider extends React.Component {
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 = {
handle: null,
recent,
@@ -81,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 });
}
@@ -92,7 +112,7 @@ class Slider extends React.Component {
onMouseMove(e) {
const position = getMousePosition(this.props.vertical, e);
this.onMove(e, position);
this.onMove(e, position - this.dragOffset);
}
onTouchMove(e) {
@@ -102,7 +122,7 @@ class Slider extends React.Component {
}
const position = getTouchPosition(this.props.vertical, e);
this.onMove(e, position);
this.onMove(e, position - this.dragOffset);
}
onMove(e, position) {
@@ -115,7 +135,7 @@ class Slider extends React.Component {
const diffValue = diffPosition / this.getSliderLength() * (props.max - props.min);
const value = this.trimAlignValue(this.startValue + diffValue);
const oldValue = state[state.handle];
const oldValue = state.bounds[state.handle];
if (value === oldValue) return;
const nextBounds = [...state.bounds];
@@ -137,7 +157,14 @@ class Slider extends React.Component {
onTouchStart(e) {
if (isNotTouchEvent(e)) return;
const position = getTouchPosition(this.props.vertical, e);
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);
@@ -145,7 +172,15 @@ class Slider extends React.Component {
onMouseDown(e) {
if (e.button !== 0) { return; }
const position = getMousePosition(this.props.vertical, e);
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);
@@ -247,6 +282,13 @@ class Slider extends React.Component {
return this._getPointsCache.points;
}
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;
}
@@ -364,11 +406,15 @@ class Slider extends React.Component {
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'));
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'));
this.onMouseMoveListener =
addEventListener(document, 'mousemove', this.onMouseMove.bind(this));
this.onMouseUpListener =
addEventListener(document, 'mouseup', this.end.bind(this, 'mouse'));
}
}
@@ -396,6 +442,7 @@ class Slider extends React.Component {
const {
className,
prefixCls,
tooltipPrefixCls,
disabled,
vertical,
dots,
@@ -413,7 +460,7 @@ class Slider extends React.Component {
const offsets = bounds.map(v => this.calcOffset(v));
const handleClassName = prefixCls + '-handle';
const handleClassName = `${prefixCls}-handle`;
const handlesClassNames = bounds.map((v, i) => classNames({
[handleClassName]: true,
@@ -426,6 +473,7 @@ class Slider extends React.Component {
const commonHandleProps = {
prefixCls,
tooltipPrefixCls,
noTip: isNoTip,
tipTransitionName,
tipFormatter,
@@ -439,6 +487,7 @@ class Slider extends React.Component {
offset: offsets[i],
dragging: handle === i,
key: i,
ref: `handle-${i}`,
}));
if (!range) { handles.shift(); }
@@ -451,33 +500,37 @@ class Slider extends React.Component {
[`${prefixCls}-track-${i}`]: true,
});
tracks.push(
<Track className={trackClassName} vertical={vertical} included={isIncluded}
offset={offsets[i - 1]} length={offsets[i] - offsets[i - 1]} key={i} />
<Track className={trackClassName} vertical={vertical} included={isIncluded}
offset={offsets[i - 1]} length={offsets[i] - offsets[i - 1]} key={i}
/>
);
}
const sliderClassName = classNames({
[prefixCls]: true,
[prefixCls + '-disabled']: disabled,
[`${prefixCls}-disabled`]: disabled,
[className]: !!className,
[prefixCls + '-vertical']: this.props.vertical,
[`${prefixCls}-vertical`]: this.props.vertical,
});
return (
<div ref="slider" className={sliderClassName}
onTouchStart={disabled ? noop : this.onTouchStart.bind(this)}
onMouseDown={disabled ? noop : this.onMouseDown.bind(this)}
>
{handles}
{tracks}
<Steps prefixCls={prefixCls} vertical = {vertical} marks={marks} dots={dots} step={step}
included={isIncluded} lowerBound={bounds[0]}
upperBound={bounds[bounds.length - 1]} max={max} min={min} />
<Marks className={prefixCls + '-mark'} vertical = {vertical} marks={marks}
included={isIncluded} lowerBound={bounds[0]}
upperBound={bounds[bounds.length - 1]} max={max} min={min} />
{children}
</div>
<div ref="slider" className={sliderClassName}
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]}
upperBound={bounds[bounds.length - 1]} max={max} min={min}
/>
{handles}
<Marks className={`${prefixCls}-mark`} vertical = {vertical} marks={marks}
included={isIncluded} lowerBound={bounds[0]}
upperBound={bounds[bounds.length - 1]} max={max} min={min}
/>
{children}
</div>
);
}
}
@@ -498,6 +551,7 @@ Slider.propTypes = {
included: React.PropTypes.bool,
className: React.PropTypes.string,
prefixCls: React.PropTypes.string,
tooltipPrefixCls: React.PropTypes.string,
disabled: React.PropTypes.bool,
children: React.PropTypes.any,
onBeforeChange: React.PropTypes.func,
+8 -5
View File
@@ -3,7 +3,10 @@ import classNames from 'classnames';
import warning from 'warning';
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);
if (dots) {
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 }) => {
const range = max - min;
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 isActived = (!included && point === upperBound) ||
(included && point <= upperBound && point >= lowerBound);
const pointClassName = classNames({
[prefixCls + '-dot']: true,
[prefixCls + '-dot-active']: isActived,
[`${prefixCls}-dot`]: true,
[`${prefixCls}-dot-active`]: isActived,
});
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;
+4 -4
View File
@@ -5,11 +5,11 @@ const Track = ({ className, included, vertical, offset, length }) => {
visibility: included ? 'visible' : 'hidden',
};
if (vertical) {
style.bottom = offset + '%';
style.height = length + '%';
style.bottom = `${offset}%`;
style.height = `${length}%`;
} else {
style.left = offset + '%';
style.width = length + '%';
style.left = `${offset}%`;
style.width = `${length}%`;
}
return <div className={className} style={style} />;
};
+134
View File
@@ -1,9 +1,22 @@
/* eslint-disable max-len */
const expect = require('expect.js');
const React = require('react');
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() {
@@ -206,4 +219,125 @@ describe('rc-slider', function test() {
const slider = ReactDOM.render(<Slider vertical />, div);
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);
});
});