Compare commits

...
37 Commits
Author SHA1 Message Date
Benjy Cui ee0dd09a4f fix: support decimal step 2015-11-23 16:00:21 +08:00
Benjy Cui a5de9b0dbc Merge pull request #48 from react-component/improve-performance
refactor: improve performance
2015-11-23 15:37:13 +08:00
Benjy Cui 37d32ae71c Merge pull request #49 from just-boris/master
style: reduce gaps between marks
2015-11-23 09:57:24 +08:00
just-boris f339db494b reduce gaps between marks 2015-11-23 00:17:29 +03:00
Benjy Cui c097b8f761 bump 3.1.1 2015-11-22 10:31:08 +08:00
Benjy Cui 10244cf01b refactor: improve the performance of Slider 2015-11-22 10:29:41 +08:00
Benjy Cui e8a86b91e2 Merge pull request #45 from react-component/feat-mark-as-step
Feat mark as step
2015-11-19 11:55:10 +08:00
Benjy Cui 05dafc0f10 bump 3.1.0 2015-11-19 11:43:25 +08:00
Benjy Cui 5dbde43da8 docs: update README.md 2015-11-19 11:43:07 +08:00
Benjy Cui 0a23a67f31 feat: allow user set step to null to make marks as steps 2015-11-19 11:42:04 +08:00
Benjy Cui 6f39212df5 Merge pull request #43 from react-component/fix-marks
fix: the key of marks could be negative
2015-11-18 18:37:46 +08:00
Benjy Cui a7c2319949 fix: the key of marks could be negative 2015-11-18 18:27:37 +08:00
Benjy Cui ee71955e14 Merge pull request #42 from react-component/3.0.0-beta
bump 3.0.0
2015-11-18 14:48:52 +08:00
Benjy Cui 8448e80212 bump 3.0.0 2015-11-18 14:38:52 +08:00
Benjy Cui 99e61001fc chore: update unit test 2015-11-18 14:32:56 +08:00
Benjy Cui 1546546040 refactor: remove useless code 2015-11-18 14:32:56 +08:00
Benjy Cui d4e2aad482 chore: update examples 2015-11-18 14:32:56 +08:00
Benjy Cui 7680347dfe feat: remove index/defaultIndex 2015-11-18 14:32:56 +08:00
Benjy Cui e2530ad2bf feat: Slider with marks should pass value(not index) to event handles 2015-11-18 14:32:56 +08:00
Benjy Cui 0566e7f098 fix: should trigger event after the value is changed 2015-11-18 14:32:56 +08:00
Benjy Cui 1e430ba694 fix: if there is no marks, step=1 should work 2015-11-18 14:32:56 +08:00
Benjy Cui f29894e690 chore: update tooltip className 2015-11-18 14:32:56 +08:00
Benjy Cui fd6c393f66 feat: Steps should not depend on marks 2015-11-18 14:32:56 +08:00
Benjy Cui e3ba1eee5b feat: marks should be an object 2015-11-18 14:32:56 +08:00
Benjy Cui 8dc66dd2a4 chore: remove isIncluded 2015-11-18 14:32:56 +08:00
afc163 b9501ef414 Merge pull request #40 from react-component/fix-controlled-slider
Fix controlled slider
2015-11-18 14:20:27 +08:00
Benjy Cui a329b00347 chore: update examples 2015-11-17 16:42:19 +08:00
Benjy Cui ccf3803d20 chore: remove initMochaPhantomJS 2015-11-17 11:41:55 +08:00
Benjy Cui b585f2fa1a bump 2.4.0 2015-11-17 10:35:02 +08:00
Benjy Cui 6f0e618a30 fix: Slider should be a controlled component 2015-11-17 10:31:10 +08:00
Benjy Cui ae4be7392d feat: unify onBeforeChange onChange onAfterChange 2015-11-17 09:54:09 +08:00
Benjy Cui d5891c79df chore: update examples 2015-11-17 09:48:35 +08:00
simaQ 87442eac35 Merge pull request #38 from react-component/chore
Chore
2015-11-16 18:24:47 +08:00
Benjy Cui 5e559a1555 chore: update test case 2015-11-16 16:35:17 +08:00
Benjy Cui 315849a0f3 refactor: rename variables 2015-11-16 16:33:45 +08:00
Benjy Cui 5f91e9a648 chore: update examples 2015-11-16 16:33:45 +08:00
yiminghe 610531be12 2.3.2 2015-11-16 14:05:16 +08:00
17 changed files with 390 additions and 354 deletions
+11 -23
View File
@@ -88,11 +88,17 @@ ReactDOM.render(<Rcslider />, container);
<td>100</td>
<td>The maximum value of the slider</td>
</tr>
<tr>
<td>marks</td>
<td>object {number: string}</td>
<td>{}</td>
<td>Mark on the slider. The key determines the position, and the value determines what will show.</td>
</tr>
<tr>
<td>step</td>
<td>number</td>
<td>number or `null`</td>
<td>1</td>
<td>Value to be added or subtracted on each step the slider makes. Must be greater than zero. max - min should be evenly divisible by the step value.</td>
<td>Value to be added or subtracted on each step the slider makes. Must be greater than zero. max - min should be evenly divisible by the step value. When `marks` is not an empty object, `step` can be set to `null`, to make marks as steps.</td>
</tr>
<tr>
<td>range</td>
@@ -112,35 +118,17 @@ ReactDOM.render(<Rcslider />, container);
<td></td>
<td>Set current positions of handles. If range is `false`, the type of `defaultValue` should be `number`. Otherwise, `[number, number]`</td>
</tr>
<tr>
<td>marks</td>
<td>array</td>
<td>[]</td>
<td>Mark every step for the slider, it will ignore the `step` parameter if it has been defined. Does not work with `range`</td>
</tr>
<tr>
<td>included</td>
<td>boolean</td>
<td>true</td>
<td>If the value is `true`, it means a continuous value interval, otherwise, it is a independent value.</td>
</tr>
<tr>
<td>defaultIndex</td>
<td>number</td>
<td>0</td>
<td>For step or marks slider, determines the initial position of the handle. Does not work with `range`</td>
</tr>
<tr>
<td>index</td>
<td>number</td>
<td></td>
<td>For step or marks slider, determines current position of the handle. Does not work with `range`</td>
</tr>
<tr>
<td>disabled</td>
<td>boolean</td>
<td>false</td>
<td>If true the handles can't be moved.</td>
<td>If `true`, handles can't be moved.</td>
</tr>
<tr>
<td>tipTransitionName</td>
@@ -150,7 +138,7 @@ ReactDOM.render(<Rcslider />, container);
</tr>
<tr>
<td>tipFormatter</td>
<td>func</td>
<td>function</td>
<td></td>
<td>Format the value of the tooltip if it shows.</td>
</tr>
@@ -158,7 +146,7 @@ ReactDOM.render(<Rcslider />, container);
<td>dots</td>
<td>bool</td>
<td>false</td>
<td>For linear slider, when the `step` value is greater than 1, you can set the `dots` to `true` if you want to render the slider bar with dots.</td>
<td>When the `step` value is greater than 1, you can set the `dots` to `true` if you want to render the slider with dots.</td>
</tr>
</tbody>
</table>
+48 -49
View File
@@ -131,55 +131,6 @@
cursor: not-allowed!important;
}
}
// slider tooltip style
&-tooltip {
position: absolute;
left: -9999px;
top: -9999px;
z-index: 4;
visibility: visible;
.borderBox();
&-hidden {
display: none;
}
&-placement-top {
padding: @tooltip-arrow-width 0 @tooltip-distance 0;
}
&-inner {
padding: 6px 2px;
min-width: 24px;
height: 24px;
font-size: 12px;
line-height: 1;
color: @tooltip-color;
text-align: center;
text-decoration: none;
background-color: @tooltip-bg;
border-radius: @border-radius-base;
box-shadow: 0 0 4px #d9d9d9;
}
&-arrow {
position: absolute;
width: 0;
height: 0;
border-color: transparent;
border-style: solid;
}
&-placement-top &-arrow {
bottom: @tooltip-distance - @tooltip-arrow-width;
left: 50%;
margin-left: -@tooltip-arrow-width;
border-width: @tooltip-arrow-width @tooltip-arrow-width 0;
border-top-color: @tooltip-arrow-color;
}
}
}
.motion-common() {
@@ -241,3 +192,51 @@
transform: scale(0, 0);
}
}
.rc-tooltip {
position: absolute;
left: -9999px;
top: -9999px;
z-index: 4;
visibility: visible;
.borderBox();
&-hidden {
display: none;
}
&-placement-top {
padding: @tooltip-arrow-width 0 @tooltip-distance 0;
}
&-inner {
padding: 6px 2px;
min-width: 24px;
height: 24px;
font-size: 12px;
line-height: 1;
color: @tooltip-color;
text-align: center;
text-decoration: none;
background-color: @tooltip-bg;
border-radius: @border-radius-base;
box-shadow: 0 0 4px #d9d9d9;
}
&-arrow {
position: absolute;
width: 0;
height: 0;
border-color: transparent;
border-style: solid;
}
&-placement-top &-arrow {
bottom: @tooltip-distance - @tooltip-arrow-width;
left: 50%;
margin-left: -@tooltip-arrow-width;
border-width: @tooltip-arrow-width @tooltip-arrow-width 0;
border-top-color: @tooltip-arrow-color;
}
}
+1
View File
@@ -0,0 +1 @@
placeholder
+51
View File
@@ -0,0 +1,51 @@
'use strict';
require('rc-slider/assets/index.less');
var React = require('react');
var ReactDOM = require('react-dom');
var Slider = require('rc-slider');
var style = {width: 400, margin: 50};
var marks = {
'-10': '-10°C',
0: '0°C',
26: '26°C',
37: '37°C',
100: '100°C'
};
var log = function(value) {
console.log(value);
};
ReactDOM.render(
<div>
<div style={style}>
<p>Slider with marks, `step=null`</p>
<Slider min={-10} marks={marks} step={null} onChange={log} defaultValue={20} />
</div>
<div style={style}>
<p>Slider with marks and steps</p>
<Slider min={-10} marks={marks} step={10} onChange={log} defaultValue={20} />
</div>
<div style={style}>
<p>Slider with marks, `included=false`</p>
<Slider min={-10} marks={marks} included={false} defaultValue={20} />
</div>
<div style={style}>
<p>Slider with marks and steps, `included=false`</p>
<Slider min={-10} marks={marks} step={10} included={false} defaultValue={20} />
</div>
<div style={style}>
<p>Range with marks</p>
<Slider min={-10} range marks={marks} onChange={log} defaultValue={[20, 40]} />
</div>
<div style={style}>
<p>Range with marks and steps</p>
<Slider min={-10} range marks={marks} step={10} onChange={log} defaultValue={[20, 40]} />
</div>
</div>
, document.getElementById('__react-content'));
+1
View File
@@ -0,0 +1 @@
placeholder
+54
View File
@@ -0,0 +1,54 @@
'use strict';
require('rc-slider/assets/index.less');
var React = require('react');
var ReactDOM = require('react-dom');
var Slider = require('rc-slider');
var style = {width: 400, margin: 50};
var log = function(value) {
console.log(value);
};
var CustomizedRange = React.createClass({
getInitialState: function() {
return {
value: [20, 40]
}
},
onChange: function(value) {
log(value);
this.setState({
value: value
});
},
render: function() {
return <Slider range value={this.state.value} onChange={this.onChange} />;
}
});
ReactDOM.render(
<div>
<div style={style}>
<p>Basic Range</p>
<Slider range defaultValue={[0, 20]} onChange={log} />
</div>
<div style={style}>
<p>Basic Range`step=20` </p>
<Slider range step={20} defaultValue={[20, 40]} onBeforeChange={log} />
</div>
<div style={style}>
<p>Basic Range`step=20, dots` </p>
<Slider range dots step={20} defaultValue={[20, 40]} onAfterChange={log} />
</div>
<div style={style}>
<p>Controlled Range</p>
<Slider range value={[20, 40]} />
</div>
<div style={style}>
<p>Customized Range</p>
<CustomizedRange />
</div>
</div>
, document.getElementById('__react-content'));
-58
View File
@@ -1,58 +0,0 @@
'use strict';
require('rc-slider/assets/index.less');
var React = require('react');
var ReactDOM = require('react-dom');
var Slider = require('rc-slider');
var style = {width:400,margin:50};
var log = console.log.bind(console);
var marks = ["状态1","状态2","状态3","状态4"];
function percentFormatter(v) {
return v + " %";
}
ReactDOM.render(
<div>
<div style={style}>
<p>基础滑块</p>
<Slider onChange={log} tipTransitionName='rc-slider-tooltip-zoom-down' />
</div>
<div style={style}>
<p>基础滑块step=20</p>
<Slider step={20} />
</div>
<div style={style}>
<p>基础滑块step=20 带圆点</p>
<Slider dots step={20} />
</div>
<div style={style}>
<p>双滑块</p>
<Slider range min={10} max={90} defaultValue={[0, 30]} onChange={log} />
</div>
<div style={style}>
<p>双滑块step=20 </p>
<Slider range dots step={20} defaultValue={[0, 30]} onAfterChange={log} included={false} />
</div>
<div style={style}>
<p>分段式滑块包含关系</p>
<Slider marks={marks} defaultIndex={1} />
</div>
<div style={style}>
<p>分段式滑块 ()</p>
<Slider range marks={marks} onChange={log} defaultIndex={[1,2]} />
</div>
<div style={style}>
<p>分段式滑块并列关系</p>
<Slider marks={marks} included={false} defaultIndex={1} />
</div>
<div style={style}>
<p>基础滑块</p>
<Slider onChange={log} tipFormatter={percentFormatter} tipTransitionName='rc-slider-tooltip-zoom-down' />
</div>
</div>
, document.getElementById('__react-content'));
+63
View File
@@ -0,0 +1,63 @@
'use strict';
require('rc-slider/assets/index.less');
var React = require('react');
var ReactDOM = require('react-dom');
var Slider = require('rc-slider');
var style = {width: 400, margin: 50};
var log = function(value) {
console.log(value);
};
function percentFormatter(v) {
return v + ' %';
}
var CustomizedSlider = React.createClass({
getInitialState: function() {
return {
value: 50
}
},
onChange: function(value) {
log(value);
this.setState({
value: value
});
},
render: function() {
return <Slider value={this.state.value} onChange={this.onChange} />;
}
});
ReactDOM.render(
<div>
<div style={style}>
<p>Basic Slider</p>
<Slider tipTransitionName='rc-slider-tooltip-zoom-down' onChange={log} />
</div>
<div style={style}>
<p>Basic Slider`step=20`</p>
<Slider step={20} defaultValue={50} onBeforeChange={log} />
</div>
<div style={style}>
<p>Basic Slider`step=20, dots`</p>
<Slider dots step={20} defaultValue={100} onAfterChange={log} />
</div>
<div style={style}>
<p>Basic Slider with `tipFormatter`</p>
<Slider tipFormatter={percentFormatter} tipTransitionName='rc-slider-tooltip-zoom-down' onChange={log} />
</div>
<div style={style}>
<p>Controlled Slider</p>
<Slider value={50} />
</div>
<div style={style}>
<p>Customized Slider</p>
<CustomizedSlider />
</div>
</div>
, document.getElementById('__react-content'));
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "rc-slider",
"version": "2.3.1",
"version": "3.1.2",
"description": "slider ui component for react",
"keywords": [
"react",
+33
View File
@@ -0,0 +1,33 @@
import React from 'react';
import { classSet } from 'rc-util';
function calcPoints(marks, dots, step, min, max) {
const points = Object.keys(marks).map(parseFloat);
if (dots) {
for (let i = min; i <= max; i = i + step) {
points.push(i);
}
}
return points;
}
const Dots = ({prefixCls, marks, dots, step, included, lowerBound, upperBound, max, min}) => {
const range = max - min;
const elements = calcPoints(marks, dots, step, min, max).map((point) => {
const offset = (point - min) / range * 100 + '%';
const style = { left: offset };
const isActived = (!included && point === upperBound) ||
(included && point <= upperBound && point >= lowerBound);
const pointClassName = classSet({
[prefixCls + '-dot']: true,
[prefixCls + '-dot-active']: isActived,
});
return <span className={pointClassName} style={style} key={point} />;
});
return <div className={prefixCls + '-step'}>{elements}</div>;
};
export default Dots;
+1 -1
View File
@@ -39,7 +39,7 @@ export default class Handle extends React.Component {
const isTooltipVisible = dragging || this.state.isTooltipVisible;
return (<Tooltip
prefixCls={className.replace('handle', 'tooltip')}
prefixCls={className.replace('slider-handle', 'tooltip')}
placement="top"
visible={isTooltipVisible}
overlay={<span>{tipFormatter ? tipFormatter(value) : value}</span>}
+16 -21
View File
@@ -1,35 +1,30 @@
import React from 'react';
import rcUtil from 'rc-util';
const Marks = ({className, marks, index, included}) => {
const marksLen = marks.length;
const unit = 100 / (marksLen - 1);
const markWidth = unit / 2 + '%';
const Marks = ({className, marks, included, upperBound, lowerBound, max, min}) => {
const marksKeys = Object.keys(marks);
const marksCount = marksKeys.length;
const unit = 100 / (marksCount - 1);
const markWidth = unit * 0.9;
const elements = [];
for (let i = 0; i < marksLen; i++) {
const isActived = (included && i <= index) || (!included && i === index);
const range = max - min;
const elements = marksKeys.map(parseFloat).map((point) => {
const isActived = (!included && point === upperBound) ||
(included && point <= upperBound && point >= lowerBound);
const markClassName = rcUtil.classSet({
[className + '-text']: true,
[className + '-text-active']: isActived,
});
const style = { width: markWidth };
const offset = unit * i;
if (i === marksLen - 1) {
style.right = -unit / 4 + '%';
} else {
style.left = (i > 0 ? offset - unit / 4 : -unit / 4) + '%';
}
const style = { width: markWidth + '%' };
style.left = (point - min) / range * 100 - markWidth / 2 + '%';
elements.push(<span className={markClassName} style={style} key={i}>
{marks[i]}
</span>);
}
return (<span className={markClassName} style={style} key={point}>
{marks[point]}
</span>);
});
return (<div className={className}>
{elements}
</div>);
return <div className={className}>{elements}</div>;
};
export default Marks;
+103 -167
View File
@@ -2,7 +2,7 @@ import React from 'react';
import {Dom as DomUtils, classSet} from 'rc-util';
import Track from './Track';
import Handle from './Handle';
import Steps from './Steps';
import Dots from './Dots';
import Marks from './Marks';
function noop() {
@@ -25,40 +25,30 @@ function pauseEvent(e) {
e.preventDefault();
}
// This is an utility method, tries to get property, then defaultPropery with
// special check using 'in', because propery can be '0'
function propOrDefault(props, name, fallback) {
const defaultName = 'default' + name.charAt(0).toUpperCase() + name.substring(1);
const defaultValue = (defaultName in props ? props[defaultName] : fallback);
return (name in props ? props[name] : defaultValue);
}
class Slider extends React.Component {
constructor(props) {
super(props);
const {range, min, max} = props;
const initialValue = range ? [min, min] : min;
const defaultValue = ('defaultValue' in props ? props.defaultValue : initialValue);
const value = ('value' in props ? props.value : defaultValue);
let upperBound;
let lowerBound;
const initialValue = props.range ? [0, 0] : 0;
if (props.marks.length > 0) {
const index = propOrDefault(props, 'index', initialValue);
({lowerBound, upperBound} = this.getBoundsFromIndex(index, props));
if (props.range) {
lowerBound = this.trimAlignValue(value[0]);
upperBound = this.trimAlignValue(value[1]);
} else {
const value = propOrDefault(props, 'value', initialValue);
if (props.range) {
lowerBound = this.trimAlignValue(value[0]);
upperBound = this.trimAlignValue(value[1]);
} else {
upperBound = this.trimAlignValue(value);
}
upperBound = this.trimAlignValue(value);
}
let recent;
if (props.range && upperBound === lowerBound) {
if (lowerBound === props.max) {
if (lowerBound === max) {
recent = 'lowerBound';
}
if (upperBound === props.min) {
if (upperBound === min) {
recent = 'upperBound';
}
} else {
@@ -70,7 +60,7 @@ class Slider extends React.Component {
recent: recent,
upperBound: upperBound,
// If Slider is not range, set `lowerBound` equal to `min`.
lowerBound: (lowerBound || props.min),
lowerBound: (lowerBound || min),
};
}
@@ -87,12 +77,26 @@ class Slider extends React.Component {
this.setState({
upperBound: nextProps.value,
});
} else if ('index' in nextProps) {
const index = ('index' in nextProps ? nextProps.index : nextProps.defaultIndex);
this.setState(this.getBoundsFromIndex(index, nextProps));
}
}
onChange(handle, value) {
const props = this.props;
const isNotControlled = !('value' in props);
if (isNotControlled) {
this.setState({[handle]: value});
}
const state = this.state;
const data = {
upperBound: state.upperBound,
lowerBound: state.lowerBound,
};
data[handle] = value;
const changedValue = props.range ? [data.lowerBound, data.upperBound] : data.upperBound;
props.onChange(changedValue);
}
onMouseMove(e) {
const position = getMousePosition(e);
this.onMove(e, position);
@@ -120,11 +124,7 @@ class Slider extends React.Component {
const oldValue = state[state.handle];
if (value === oldValue) return;
if (!('value' in props) && !('index' in props)) {
this.setState({[state.handle]: value}, () => {
this.triggerEvents('onChange', this.getValue());
});
}
this.onChange(state.handle, value);
}
onTouchStart(e) {
@@ -136,7 +136,7 @@ class Slider extends React.Component {
pauseEvent(e);
}
onSliderMouseDown(e) {
onMouseDown(e) {
const position = getMousePosition(e);
this.onStart(position);
this.addDocumentEvents('mouse');
@@ -144,7 +144,8 @@ class Slider extends React.Component {
}
onStart(position) {
this.triggerEvents('onBeforeChange');
const props = this.props;
props.onBeforeChange(this.getValue());
const value = this.calcValueByPos(position);
this.startValue = value;
@@ -173,10 +174,12 @@ class Slider extends React.Component {
this.setState({
handle: valueNeedChanging,
recent: valueNeedChanging,
[valueNeedChanging]: value,
}, () => {
this.triggerEvents('onChange', this.getValue());
});
const oldValue = state[valueNeedChanging];
if (value === oldValue) return;
this.onChange(valueNeedChanging, value);
}
getValue() {
@@ -184,28 +187,6 @@ class Slider extends React.Component {
return this.props.range ? [lowerBound, upperBound] : upperBound;
}
getIndex(value) {
const {marks, min, max, step} = this.props;
if (marks.length === 0) {
return Math.floor((value - min) / step);
}
const unit = ((max - min) / (marks.length - 1)).toFixed(5);
return Math.round(value / unit);
}
getBoundsFromIndex(value, props) {
if (props.range) {
return {
lowerBound: this.calcValueFromIndex(value[0], props),
upperBound: this.calcValueFromIndex(value[1], props),
};
}
return {
upperBound: this.calcValueFromIndex(value, props),
};
}
getSliderLength() {
const slider = this.refs.slider;
if (!slider) {
@@ -222,11 +203,20 @@ class Slider extends React.Component {
return rect.left;
}
getPrecision() {
const props = this.props;
const stepString = props.step.toString();
let precision = 0;
if (stepString.indexOf('.') >= 0) {
precision = stepString.length - stepString.indexOf('.') - 1;
}
return precision;
}
trimAlignValue(v) {
const state = this.state || {};
const props = this.props;
const {marks, min, max} = props;
const step = marks.length > 0 ? (max - min) / (marks.length - 1) : props.step;
const {handle, lowerBound, upperBound} = state;
const {marks, step, min, max} = this.props;
let val = v;
if (val <= min) {
@@ -235,21 +225,23 @@ class Slider extends React.Component {
if (val >= max) {
val = max;
}
if (state.handle === 'upperBound' && val <= state.lowerBound) {
val = state.lowerBound;
if (handle === 'upperBound' && val <= lowerBound) {
val = lowerBound;
}
if (state.handle === 'lowerBound' && val >= state.upperBound) {
val = state.upperBound;
if (handle === 'lowerBound' && val >= upperBound) {
val = upperBound;
}
const valModStep = (val - min) % step;
let alignValue = val - valModStep;
if (Math.abs(valModStep) * 2 >= step) {
alignValue += (valModStep > 0) ? step : (-step);
const points = Object.keys(marks).map(parseFloat);
if (step !== null) {
const closestStep = Math.round(val / step) * step;
points.push(closestStep);
}
return parseFloat(alignValue.toFixed(5));
const diffs = points.map((point) => Math.abs(val - point));
const closestPoint = points[diffs.indexOf(Math.min.apply(Math, diffs))];
return step !== null ? parseFloat(closestPoint.toFixed(this.getPrecision())) : closestPoint;
}
calcOffset(value) {
@@ -270,36 +262,6 @@ class Slider extends React.Component {
return nextValue;
}
calcValueFromIndex(index, props) {
const marksLen = props.marks.length;
if (marksLen > 0) {
const value = ((props.max - props.min) / (marksLen - 1)) * (index);
// `'1' / 1 => 1`, to make sure that the returned value is a `Number`.
return value.toFixed(5) / 1;
}
return ('value' in props ? props.value : props.defaultValue);
}
triggerEvents(event, v) {
const props = this.props;
const hasMarks = (props.marks.length > 0);
if (props[event]) {
let data;
if (hasMarks) {
if (props.range) {
data = v.map(bound => this.getIndex(bound));
} else {
data = this.getIndex(v);
}
} else if (v === undefined) {
data = this.state.value;
} else {
data = v;
}
props[event](data);
}
}
addDocumentEvents(type) {
if (type === 'touch') {
// just work for chrome iOS Safari and Android Browser
@@ -311,7 +273,7 @@ class Slider extends React.Component {
}
}
removeEventons(type) {
removeEvents(type) {
if (type === 'touch') {
this.onTouchMoveListener.remove();
this.onTouchUpListener.remove();
@@ -322,71 +284,53 @@ class Slider extends React.Component {
}
end(type) {
this.removeEventons(type);
this.triggerEvents('onAfterChange', this.getValue());
this.removeEvents(type);
this.props.onAfterChange(this.getValue());
this.setState({handle: null});
}
render() {
const {handle, upperBound, lowerBound} = this.state;
const props = this.props;
const {className, prefixCls, disabled, included, isIncluded, dots, range} = props;
const {marks, step, max, min, tipTransitionName, tipFormatter, children} = props;
const marksLen = marks.length;
const {className, prefixCls, disabled, dots, included, range, step,
marks, max, min, tipTransitionName, tipFormatter, children} = this.props;
const upperOffset = this.calcOffset(upperBound);
const lowerOffset = this.calcOffset(lowerBound);
const handleClassName = prefixCls + '-handle';
const isNoTip = (step === null) && !tipFormatter;
const upper = (<Handle className={handleClassName}
noTip={isNoTip} tipTransitionName={tipTransitionName} tipFormatter={tipFormatter}
offset={upperOffset} value={upperBound} dragging={handle === 'upperBound'} />);
let lower = null;
if (range) {
lower = (<Handle className={handleClassName}
noTip={isNoTip} tipTransitionName={tipTransitionName} tipFormatter={tipFormatter}
offset={lowerOffset} value={lowerBound} dragging={handle === 'lowerBound'} />);
}
const sliderClassName = classSet({
[prefixCls]: true,
[prefixCls + '-disabled']: disabled,
[className]: !!className,
});
const upperOffset = this.calcOffset(upperBound);
const lowerOffset = this.calcOffset(lowerBound);
let track = null;
if ((included && isIncluded) || range) {
const trackClassName = prefixCls + '-track';
track = <Track className={trackClassName} offset={lowerOffset} length={upperOffset - lowerOffset}/>;
}
const handleClassName = prefixCls + '-handle';
const isNoTip = (marksLen > 0) && !tipFormatter;
const upper = (<Handle className={handleClassName} tipTransitionName={tipTransitionName} noTip={isNoTip} tipFormatter={tipFormatter}
offset={upperOffset} value={upperBound} dragging={handle === 'upperBound'} />);
let lower = null;
if (range) {
lower = (<Handle className={handleClassName} tipTransitionName={tipTransitionName} noTip={isNoTip} tipFormatter={tipFormatter}
offset={lowerOffset} value={lowerBound} dragging={handle === 'lowerBound'} />);
}
const upperIndex = this.getIndex(upperBound);
let steps = null;
if (marksLen > 0 || (step > 1 && dots)) {
const stepsClassName = prefixCls + '-step';
const stepNum = marksLen > 0 ? marksLen : Math.floor((max - min) / step) + 1;
steps = (<Steps className={stepsClassName} stepNum={stepNum}
lowerIndex={this.getIndex(lowerBound)} upperIndex={upperIndex}
included={(included && isIncluded) || range}/>);
}
let mark = null;
if (marksLen > 0) {
const markClassName = prefixCls + '-mark';
mark = (<Marks className={markClassName} marks={marks}
index={upperIndex} included={(included && isIncluded)}/>);
}
const isIncluded = included || range;
return (
<div ref="slider" className={sliderClassName}
onTouchStart={disabled ? noop : this.onTouchStart.bind(this)}
onMouseDown={disabled ? noop : this.onSliderMouseDown.bind(this)}>
{track}
onMouseDown={disabled ? noop : this.onMouseDown.bind(this)}>
{upper}
{lower}
{steps}
{mark}
<Track className={prefixCls + '-track'} included={isIncluded}
offset={lowerOffset} length={upperOffset - lowerOffset}/>
<Dots prefixCls={prefixCls} marks={marks} dots={dots} step={step}
included={isIncluded} lowerBound={lowerBound}
upperBound={upperBound} max={max} min={min} />
<Marks className={prefixCls + '-mark'} marks={marks}
included={isIncluded} lowerBound={lowerBound}
upperBound={upperBound} max={max} min={min} />
{children}
</div>
);
@@ -401,20 +345,11 @@ Slider.propTypes = {
React.PropTypes.number,
React.PropTypes.arrayOf(React.PropTypes.number),
]),
defaultIndex: React.PropTypes.oneOfType([
React.PropTypes.number,
React.PropTypes.arrayOf(React.PropTypes.number),
]),
value: React.PropTypes.oneOfType([
React.PropTypes.number,
React.PropTypes.arrayOf(React.PropTypes.number),
]),
index: React.PropTypes.oneOfType([
React.PropTypes.number,
React.PropTypes.arrayOf(React.PropTypes.number),
]),
marks: React.PropTypes.array,
isIncluded: React.PropTypes.bool, // @Deprecated
marks: React.PropTypes.object,
included: React.PropTypes.bool,
className: React.PropTypes.string,
prefixCls: React.PropTypes.string,
@@ -430,17 +365,18 @@ Slider.propTypes = {
};
Slider.defaultProps = {
prefixCls: 'rc-slider',
className: '',
tipTransitionName: '',
min: 0,
max: 100,
step: 1,
defaultIndex: 0,
marks: [],
isIncluded: true, // @Deprecated
marks: {},
onBeforeChange: noop,
onChange: noop,
onAfterChange: noop,
included: true,
className: '',
prefixCls: 'rc-slider',
disabled: false,
tipTransitionName: '',
dots: false,
range: false,
};
-28
View File
@@ -1,28 +0,0 @@
import React from 'react';
import rcUtil from 'rc-util';
const Steps = ({className, stepNum, included, lowerIndex, upperIndex}) => {
const dotClassName = className.replace('step', 'dot');
const unit = 100 / (stepNum - 1);
const elements = [];
for (let i = 0; i < stepNum; i++) {
const offset = unit * i + '%';
const style = { left: offset };
const isActived = (included && i <= upperIndex && i >= lowerIndex ) ||
(!included && i === upperIndex);
const stepClassName = rcUtil.classSet({
[dotClassName]: true,
[dotClassName + '-active']: isActived,
});
elements.push(<span className={stepClassName} style={style} key={i} />);
}
return (<div className={className}>
{elements}
</div>);
};
export default Steps;
+2 -1
View File
@@ -1,9 +1,10 @@
import React from 'react';
const Track = ({className, offset, length}) => {
const Track = ({className, included, offset, length}) => {
const style = {
left: offset + '%',
width: length + '%',
visibility: included ? 'visible' : 'hidden',
};
return <div className={className} style={style} />;
};
+5 -5
View File
@@ -14,7 +14,7 @@ describe('rc-slider', function () {
document.body.appendChild(div);
afterEach(function () {
React.unmountComponentAtNode(div);
ReactDOM.unmountComponentAtNode(div);
});
it('should render a simple slider with value correctly!', function () {
@@ -52,17 +52,17 @@ describe('rc-slider', function () {
it('should render a slider with marks correctly!', function () {
var slider = ReactDOM.render(
<Slider marks={["一","二","三","四","五"]} defaultIndex={3} />,
<Slider marks={{0: "一", 20: "二", 40: "三", 60: "四", 100: "五"}} defaultValue={40} />,
div
);
var node = $(div);
expect(node.find('.rc-slider').length).to.be(1);
expect(node.find('.rc-slider-handle').length).to.be(1);
expect(node.find('.rc-slider-track').length).to.be(1);
expect(node.find('.rc-slider-dot').length).to.be(slider.props.marks.length);
expect(node.find('.rc-slider-dot').length).to.be(Object.keys(slider.props.marks).length);
expect(node.find('.rc-slider-mark').length).to.be(1);
expect(node.find('.rc-slider-mark-text').length).to.be(slider.props.marks.length);
expect(slider.getIndex(slider.state.upperBound)).to.be(3);
expect(node.find('.rc-slider-mark-text').length).to.be(Object.keys(slider.props.marks).length);
expect(slider.state.upperBound).to.be(40);
});
// it('should mouseDown works!', function (done) {