diff --git a/package.json b/package.json index d4c8239..8482037 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "rc-slider", - "version": "3.1.0", + "version": "3.1.1", "description": "slider ui component for react", "keywords": [ "react", diff --git a/src/Dots.jsx b/src/Dots.jsx new file mode 100644 index 0000000..2069295 --- /dev/null +++ b/src/Dots.jsx @@ -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 ; + }); + + return
{elements}
; +}; + +export default Dots; diff --git a/src/Slider.jsx b/src/Slider.jsx index 8923de3..df8b3bb 100644 --- a/src/Slider.jsx +++ b/src/Slider.jsx @@ -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() { @@ -187,17 +187,6 @@ class Slider extends React.Component { return this.props.range ? [lowerBound, upperBound] : upperBound; } - getPoints() { - const {marks, step, min, max} = this.props; - const points = Object.keys(marks); - if (step !== null) { - for (let i = min; i <= max; i = i + step) { - points.push(i); - } - } - return points; - } - getSliderLength() { const slider = this.refs.slider; if (!slider) { @@ -217,7 +206,7 @@ class Slider extends React.Component { trimAlignValue(v) { const state = this.state || {}; const {handle, lowerBound, upperBound} = state; - const {min, max} = this.props; + const {marks, step, min, max} = this.props; let val = v; if (val <= min) { @@ -233,7 +222,12 @@ class Slider extends React.Component { val = upperBound; } - const points = this.getPoints().map(parseFloat); + const points = Object.keys(marks).map(parseFloat); + if (step !== null) { + const closestStep = Math.round(val / step) * step; + points.push(closestStep); + } + const diffs = points.map((point) => Math.abs(val - point)); const closestPoint = points[diffs.indexOf(Math.min.apply(Math, diffs))]; @@ -269,7 +263,7 @@ class Slider extends React.Component { } } - removeEventons(type) { + removeEvents(type) { if (type === 'touch') { this.onTouchMoveListener.remove(); this.onTouchUpListener.remove(); @@ -280,7 +274,7 @@ class Slider extends React.Component { } end(type) { - this.removeEventons(type); + this.removeEvents(type); this.props.onAfterChange(this.getValue()); this.setState({handle: null}); } @@ -290,43 +284,40 @@ class Slider extends React.Component { 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 = (); + + let lower = null; + if (range) { + lower = (); + } + 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 || range) { - const trackClassName = prefixCls + '-track'; - track = ; - } - - const handleClassName = prefixCls + '-handle'; - const isNoTip = (step === null) && !tipFormatter; - const upper = (); - - let lower = null; - if (range) { - lower = (); - } - const isIncluded = included || range; return (
- {track} {upper} {lower} - + + diff --git a/src/Steps.jsx b/src/Steps.jsx deleted file mode 100644 index 38e3394..0000000 --- a/src/Steps.jsx +++ /dev/null @@ -1,24 +0,0 @@ -import React from 'react'; -import { classSet } from 'rc-util'; - -const Steps = ({prefixCls, points, dots, included, lowerBound, upperBound, max, min}) => { - const range = max - min; - const elements = points.filter((point) => typeof point === 'string' || dots).map(parseFloat) - .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 ; - }); - - return
{elements}
; -}; - -export default Steps; diff --git a/src/Track.jsx b/src/Track.jsx index fb1cdf2..70df9ed 100644 --- a/src/Track.jsx +++ b/src/Track.jsx @@ -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
; };