mirror of
https://github.com/wassname/slider.git
synced 2026-09-11 12:42:49 +08:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
58b18484fe | ||
|
|
3a61ef37c6 | ||
|
|
fc76a93949 | ||
|
|
2cd34c0e40 | ||
|
|
0dc20afce7 | ||
|
|
b69a4af14f | ||
|
|
84e999697f | ||
|
|
8290cb0337 | ||
|
|
be15e1c8db |
@@ -138,7 +138,7 @@ npm start
|
|||||||
|
|
||||||
http://localhost:8000/examples/
|
http://localhost:8000/examples/
|
||||||
|
|
||||||
online example: http://react-component.github.io/slider/build/examples/
|
online example: http://react-component.github.io/slider/examples/
|
||||||
|
|
||||||
## Test Case
|
## Test Case
|
||||||
|
|
||||||
|
|||||||
+1
-1
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "rc-slider",
|
"name": "rc-slider",
|
||||||
"version": "1.2.7",
|
"version": "1.2.9",
|
||||||
"description": "slider ui component for react",
|
"description": "slider ui component for react",
|
||||||
"keywords": [
|
"keywords": [
|
||||||
"react",
|
"react",
|
||||||
|
|||||||
+24
-40
@@ -60,8 +60,6 @@ var Slider = React.createClass({
|
|||||||
}
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
upperBound: 0,
|
|
||||||
sliderLength: 0,
|
|
||||||
value: value,
|
value: value,
|
||||||
active: props.disabled ? '' : ((value > props.min || props.index > 0) ? 'active' : '')
|
active: props.disabled ? '' : ((value > props.min || props.index > 0) ? 'active' : '')
|
||||||
};
|
};
|
||||||
@@ -72,29 +70,20 @@ var Slider = React.createClass({
|
|||||||
this.state.value = this._trimAlignValue(value, newProps);
|
this.state.value = this._trimAlignValue(value, newProps);
|
||||||
},
|
},
|
||||||
|
|
||||||
componentDidMount: function() {
|
|
||||||
this._onHandleResizeListener = DomUtils.addEventListener(window, 'resize', this.handleResize);
|
|
||||||
setTimeout(this.handleResize, 0);
|
|
||||||
},
|
|
||||||
|
|
||||||
componentWillUnmount: function() {
|
|
||||||
if (this._onHandleResizeListener) {
|
|
||||||
this._onHandleResizeListener.remove();
|
|
||||||
}
|
|
||||||
},
|
|
||||||
|
|
||||||
getValue: function() {
|
getValue: function() {
|
||||||
return this.state.value;
|
return this.state.value;
|
||||||
},
|
},
|
||||||
|
|
||||||
getIndex: function() {
|
getIndex: function() {
|
||||||
var props = this.props;
|
var props = this.props;
|
||||||
if (props.marks.length === 0) {
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
var value = this.state.value;
|
var value = this.state.value;
|
||||||
var unit = ((props.max - props.min) / (props.marks.length - 1)).toFixed(5);
|
|
||||||
return Math.floor(value / unit);
|
if (props.marks.length === 0) {
|
||||||
|
return Math.floor((value - props.min) / props.step);
|
||||||
|
} else {
|
||||||
|
var unit = ((props.max - props.min) / (props.marks.length - 1)).toFixed(5);
|
||||||
|
return Math.ceil(value / unit);
|
||||||
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
_trimAlignValue: function(val, props) {
|
_trimAlignValue: function(val, props) {
|
||||||
@@ -121,18 +110,17 @@ var Slider = React.createClass({
|
|||||||
|
|
||||||
_calcOffset: function(value) {
|
_calcOffset: function(value) {
|
||||||
var ratio = (value - this.props.min) / (this.props.max - this.props.min);
|
var ratio = (value - this.props.min) / (this.props.max - this.props.min);
|
||||||
return ratio * this.state.upperBound;
|
return ratio * 100 + '%';
|
||||||
},
|
},
|
||||||
|
|
||||||
_calcValue: function(offset) {
|
_calcValue: function(offset) {
|
||||||
var ratio = offset / this.state.upperBound;
|
var ratio = offset / this.getSliderLength();
|
||||||
return ratio * (this.props.max - this.props.min) + this.props.min;
|
return ratio * (this.props.max - this.props.min) + this.props.min;
|
||||||
},
|
},
|
||||||
|
|
||||||
_calValueByPos: function (position, callback) {
|
_calValueByPos: function (position, callback) {
|
||||||
var pixelOffset = position - this.getSliderStart();
|
var pixelOffset = position - this.getSliderStart();
|
||||||
// pixelOffset -= (this.state.handleSize / 2);
|
// pixelOffset -= (this.state.handleSize / 2);
|
||||||
|
|
||||||
var nextValue = this._trimAlignValue(this._calcValue(pixelOffset));
|
var nextValue = this._trimAlignValue(this._calcValue(pixelOffset));
|
||||||
|
|
||||||
this.setState({value: nextValue, active: 'active'}, callback);
|
this.setState({value: nextValue, active: 'active'}, callback);
|
||||||
@@ -232,7 +220,7 @@ var Slider = React.createClass({
|
|||||||
|
|
||||||
var diffPosition = position - state.startPosition;
|
var diffPosition = position - state.startPosition;
|
||||||
|
|
||||||
var diffValue = diffPosition / (state.sliderLength) * (props.max - props.min);
|
var diffValue = diffPosition / this.getSliderLength() * (props.max - props.min);
|
||||||
var newValue = this._trimAlignValue(state.startValue + diffValue);
|
var newValue = this._trimAlignValue(state.startValue + diffValue);
|
||||||
|
|
||||||
value = newValue;
|
value = newValue;
|
||||||
@@ -242,17 +230,13 @@ var Slider = React.createClass({
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
handleResize: function() {
|
getSliderLength: function() {
|
||||||
var slider = this.refs.slider.getDOMNode();
|
var slider = this.refs.slider;
|
||||||
var rect = slider.getBoundingClientRect();
|
if (!slider) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
var sliderMin = rect.left;
|
return slider.getDOMNode().clientWidth;
|
||||||
var sliderMax = rect.right;
|
|
||||||
|
|
||||||
this.setState({
|
|
||||||
upperBound: slider.clientWidth,
|
|
||||||
sliderLength: Math.abs(sliderMax - sliderMin)
|
|
||||||
});
|
|
||||||
},
|
},
|
||||||
|
|
||||||
getSliderStart: function() {
|
getSliderStart: function() {
|
||||||
@@ -306,20 +290,20 @@ var Slider = React.createClass({
|
|||||||
var props = this.props;
|
var props = this.props;
|
||||||
var marksLen = props.marks.length;
|
var marksLen = props.marks.length;
|
||||||
var stepNum = marksLen > 0 ? marksLen : Math.floor((props.max - props.min) / props.step) + 1;
|
var stepNum = marksLen > 0 ? marksLen : Math.floor((props.max - props.min) / props.step) + 1;
|
||||||
var unit = this.state.sliderLength / (stepNum - 1);
|
var unit = 100 / (stepNum - 1);
|
||||||
|
|
||||||
var prefixCls = props.className;
|
var prefixCls = props.className;
|
||||||
var stepClassName = prefixClsFn(prefixCls, 'step');
|
var stepClassName = prefixClsFn(prefixCls, 'step');
|
||||||
|
|
||||||
var elements = [];
|
var elements = [];
|
||||||
for (var i = 0; i < stepNum; i++) {
|
for (var i = 0; i < stepNum; i++) {
|
||||||
var offset = unit * i;
|
var offset = unit * i + '%';
|
||||||
var style = {
|
var style = {
|
||||||
left: offset.toFixed(5)
|
left: offset
|
||||||
};
|
};
|
||||||
var className = prefixClsFn(prefixCls, 'dot');
|
var className = prefixClsFn(prefixCls, 'dot');
|
||||||
if (props.isIncluded) {
|
if (props.isIncluded) {
|
||||||
if (i <= this.getIndex() || (this._calcValue(offset) <= this.getValue())) {
|
if (i <= this.getIndex()) {
|
||||||
className = prefixClsFn(prefixCls, 'dot', 'dot-active');
|
className = prefixClsFn(prefixCls, 'dot', 'dot-active');
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
@@ -341,17 +325,17 @@ var Slider = React.createClass({
|
|||||||
renderMark: function(i) {
|
renderMark: function(i) {
|
||||||
var marks = this.props.marks;
|
var marks = this.props.marks;
|
||||||
var marksLen = marks.length;
|
var marksLen = marks.length;
|
||||||
var unit = this.state.sliderLength / (marksLen - 1);
|
var unit = 100 / (marksLen - 1);
|
||||||
var offset = unit * i;
|
var offset = unit * i;
|
||||||
|
|
||||||
var style = {
|
var style = {
|
||||||
width: (unit / 2).toFixed(5)
|
width: unit / 2 + '%'
|
||||||
};
|
};
|
||||||
|
|
||||||
if (i === marksLen - 1) {
|
if (i === marksLen - 1) {
|
||||||
style.right = -(style.width / 2).toFixed(5);
|
style.right = -unit / 4 + '%';
|
||||||
} else {
|
} else {
|
||||||
style.left = i > 0 ? (offset - unit / 4).toFixed(5) : -(style.width / 2).toFixed(5);
|
style.left = i > 0 ? offset - unit / 4 + '%' : -unit / 4 + '%';
|
||||||
}
|
}
|
||||||
|
|
||||||
var prefixCls = this.props.className;
|
var prefixCls = this.props.className;
|
||||||
|
|||||||
Reference in New Issue
Block a user