mirror of
https://github.com/wassname/slider.git
synced 2026-09-11 12:42:49 +08:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
610531be12 | ||
|
|
a06b43dcd7 | ||
|
|
3e11978bfc | ||
|
|
48dab67950 |
+9
-2
@@ -9,6 +9,9 @@ var style = {width:400,margin:50};
|
|||||||
|
|
||||||
var log = console.log.bind(console);
|
var log = console.log.bind(console);
|
||||||
|
|
||||||
|
|
||||||
|
var marks = ["状态1","状态2","状态3","状态4"];
|
||||||
|
|
||||||
function percentFormatter(v) {
|
function percentFormatter(v) {
|
||||||
return v + " %";
|
return v + " %";
|
||||||
}
|
}
|
||||||
@@ -37,11 +40,15 @@ ReactDOM.render(
|
|||||||
</div>
|
</div>
|
||||||
<div style={style}>
|
<div style={style}>
|
||||||
<p>分段式滑块(包含关系)</p>
|
<p>分段式滑块(包含关系)</p>
|
||||||
<Slider marks={["状态1","状态2","状态3","状态4"]} defaultIndex={1} />
|
<Slider marks={marks} defaultIndex={1} />
|
||||||
|
</div>
|
||||||
|
<div style={style}>
|
||||||
|
<p>分段式滑块 (双)</p>
|
||||||
|
<Slider range marks={marks} onChange={log} defaultIndex={[1,2]} />
|
||||||
</div>
|
</div>
|
||||||
<div style={style}>
|
<div style={style}>
|
||||||
<p>分段式滑块(并列关系)</p>
|
<p>分段式滑块(并列关系)</p>
|
||||||
<Slider marks={["状态1","状态2","状态3","状态4"]} included={false} defaultIndex={1} />
|
<Slider marks={marks} included={false} defaultIndex={1} />
|
||||||
</div>
|
</div>
|
||||||
<div style={style}>
|
<div style={style}>
|
||||||
<p>基础滑块</p>
|
<p>基础滑块</p>
|
||||||
|
|||||||
+1
-1
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "rc-slider",
|
"name": "rc-slider",
|
||||||
"version": "2.2.1",
|
"version": "2.3.2",
|
||||||
"description": "slider ui component for react",
|
"description": "slider ui component for react",
|
||||||
"keywords": [
|
"keywords": [
|
||||||
"react",
|
"react",
|
||||||
|
|||||||
+43
-15
@@ -25,25 +25,33 @@ function pauseEvent(e) {
|
|||||||
e.preventDefault();
|
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 {
|
class Slider extends React.Component {
|
||||||
constructor(props) {
|
constructor(props) {
|
||||||
super(props);
|
super(props);
|
||||||
|
|
||||||
let upperBound;
|
let upperBound;
|
||||||
let lowerBound;
|
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));
|
||||||
|
} else {
|
||||||
|
const value = propOrDefault(props, 'value', initialValue);
|
||||||
if (props.range) {
|
if (props.range) {
|
||||||
const value = (props.value || props.defaultValue || [0, 0]);
|
lowerBound = this.trimAlignValue(value[0]);
|
||||||
upperBound = this.trimAlignValue(value[1]);
|
upperBound = this.trimAlignValue(value[1]);
|
||||||
lowerBound = this.trimAlignValue(value[0]);
|
|
||||||
} else if (props.marks.length > 0) {
|
|
||||||
upperBound = this.calcValueFromProps(props);
|
|
||||||
} else {
|
} else {
|
||||||
// Note: Maybe `value` is `0`.
|
|
||||||
// So, check the existence of `value` with `in`.
|
|
||||||
const defaultValue = ('defaultValue' in props ? props.defaultValue : 0);
|
|
||||||
const value = ('value' in props ? props.value : defaultValue);
|
|
||||||
upperBound = this.trimAlignValue(value);
|
upperBound = this.trimAlignValue(value);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
let recent;
|
let recent;
|
||||||
if (props.range && upperBound === lowerBound) {
|
if (props.range && upperBound === lowerBound) {
|
||||||
@@ -80,9 +88,8 @@ class Slider extends React.Component {
|
|||||||
upperBound: nextProps.value,
|
upperBound: nextProps.value,
|
||||||
});
|
});
|
||||||
} else if ('index' in nextProps) {
|
} else if ('index' in nextProps) {
|
||||||
this.setState({
|
const index = ('index' in nextProps ? nextProps.index : nextProps.defaultIndex);
|
||||||
upperBound: this.calcValueFromProps(nextProps),
|
this.setState(this.getBoundsFromIndex(index, nextProps));
|
||||||
});
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -187,6 +194,18 @@ class Slider extends React.Component {
|
|||||||
return Math.round(value / unit);
|
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() {
|
getSliderLength() {
|
||||||
const slider = this.refs.slider;
|
const slider = this.refs.slider;
|
||||||
if (!slider) {
|
if (!slider) {
|
||||||
@@ -251,10 +270,9 @@ class Slider extends React.Component {
|
|||||||
return nextValue;
|
return nextValue;
|
||||||
}
|
}
|
||||||
|
|
||||||
calcValueFromProps(props) {
|
calcValueFromIndex(index, props) {
|
||||||
const marksLen = props.marks.length;
|
const marksLen = props.marks.length;
|
||||||
if (marksLen > 0) {
|
if (marksLen > 0) {
|
||||||
const index = ('index' in props ? props.index : props.defaultIndex);
|
|
||||||
const value = ((props.max - props.min) / (marksLen - 1)) * (index);
|
const value = ((props.max - props.min) / (marksLen - 1)) * (index);
|
||||||
// `'1' / 1 => 1`, to make sure that the returned value is a `Number`.
|
// `'1' / 1 => 1`, to make sure that the returned value is a `Number`.
|
||||||
return value.toFixed(5) / 1;
|
return value.toFixed(5) / 1;
|
||||||
@@ -268,7 +286,11 @@ class Slider extends React.Component {
|
|||||||
if (props[event]) {
|
if (props[event]) {
|
||||||
let data;
|
let data;
|
||||||
if (hasMarks) {
|
if (hasMarks) {
|
||||||
|
if (props.range) {
|
||||||
|
data = v.map(bound => this.getIndex(bound));
|
||||||
|
} else {
|
||||||
data = this.getIndex(v);
|
data = this.getIndex(v);
|
||||||
|
}
|
||||||
} else if (v === undefined) {
|
} else if (v === undefined) {
|
||||||
data = this.state.value;
|
data = this.state.value;
|
||||||
} else {
|
} else {
|
||||||
@@ -379,12 +401,18 @@ Slider.propTypes = {
|
|||||||
React.PropTypes.number,
|
React.PropTypes.number,
|
||||||
React.PropTypes.arrayOf(React.PropTypes.number),
|
React.PropTypes.arrayOf(React.PropTypes.number),
|
||||||
]),
|
]),
|
||||||
defaultIndex: React.PropTypes.number,
|
defaultIndex: React.PropTypes.oneOfType([
|
||||||
|
React.PropTypes.number,
|
||||||
|
React.PropTypes.arrayOf(React.PropTypes.number),
|
||||||
|
]),
|
||||||
value: React.PropTypes.oneOfType([
|
value: React.PropTypes.oneOfType([
|
||||||
React.PropTypes.number,
|
React.PropTypes.number,
|
||||||
React.PropTypes.arrayOf(React.PropTypes.number),
|
React.PropTypes.arrayOf(React.PropTypes.number),
|
||||||
]),
|
]),
|
||||||
index: React.PropTypes.number,
|
index: React.PropTypes.oneOfType([
|
||||||
|
React.PropTypes.number,
|
||||||
|
React.PropTypes.arrayOf(React.PropTypes.number),
|
||||||
|
]),
|
||||||
marks: React.PropTypes.array,
|
marks: React.PropTypes.array,
|
||||||
isIncluded: React.PropTypes.bool, // @Deprecated
|
isIncluded: React.PropTypes.bool, // @Deprecated
|
||||||
included: React.PropTypes.bool,
|
included: React.PropTypes.bool,
|
||||||
|
|||||||
Reference in New Issue
Block a user