mirror of
https://github.com/wassname/slider.git
synced 2026-09-10 12:38:09 +08:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
a06b43dcd7 | ||
|
|
3e11978bfc | ||
|
|
48dab67950 | ||
|
|
ab494c9ca8 | ||
|
|
68bb3bcadb | ||
|
|
0f97361257 | ||
|
|
599771dc1a | ||
|
|
a109298acd | ||
|
|
3519ba24c0 |
@@ -148,6 +148,12 @@ ReactDOM.render(<Rcslider />, container);
|
||||
<td>''</td>
|
||||
<td>Set the animation for tooltip if it shows.</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>tipFormatter</td>
|
||||
<td>func</td>
|
||||
<td></td>
|
||||
<td>Format the value of the tooltip if it shows.</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>dots</td>
|
||||
<td>bool</td>
|
||||
|
||||
+2
-2
@@ -151,8 +151,8 @@
|
||||
}
|
||||
|
||||
&-inner {
|
||||
padding: 6px 0;
|
||||
width: 24px;
|
||||
padding: 6px 2px;
|
||||
min-width: 24px;
|
||||
height: 24px;
|
||||
font-size: 12px;
|
||||
line-height: 1;
|
||||
|
||||
+17
-2
@@ -9,6 +9,13 @@ 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}>
|
||||
@@ -33,11 +40,19 @@ ReactDOM.render(
|
||||
</div>
|
||||
<div style={style}>
|
||||
<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 style={style}>
|
||||
<p>分段式滑块(并列关系)</p>
|
||||
<Slider marks={["状态1","状态2","状态3","状态4"]} included={false} defaultIndex={1} />
|
||||
<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'));
|
||||
|
||||
+1
-1
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "rc-slider",
|
||||
"version": "2.2.0",
|
||||
"version": "2.3.1",
|
||||
"description": "slider ui component for react",
|
||||
"keywords": [
|
||||
"react",
|
||||
|
||||
+3
-2
@@ -24,7 +24,7 @@ export default class Handle extends React.Component {
|
||||
|
||||
render() {
|
||||
const props = this.props;
|
||||
const {className, tipTransitionName, offset, value} = props;
|
||||
const {className, tipTransitionName, tipFormatter, offset, value} = props;
|
||||
const {dragging, noTip} = props;
|
||||
|
||||
const style = { left: offset + '%' };
|
||||
@@ -42,7 +42,7 @@ export default class Handle extends React.Component {
|
||||
prefixCls={className.replace('handle', 'tooltip')}
|
||||
placement="top"
|
||||
visible={isTooltipVisible}
|
||||
overlay={<span>{value}</span>}
|
||||
overlay={<span>{tipFormatter ? tipFormatter(value) : value}</span>}
|
||||
delay={0}
|
||||
transitionName={tipTransitionName}>
|
||||
{handle}
|
||||
@@ -54,6 +54,7 @@ Handle.propTypes = {
|
||||
className: React.PropTypes.string,
|
||||
offset: React.PropTypes.number,
|
||||
tipTransitionName: React.PropTypes.string,
|
||||
tipFormatter: React.PropTypes.func,
|
||||
value: React.PropTypes.number,
|
||||
dragging: React.PropTypes.bool,
|
||||
noTip: React.PropTypes.bool,
|
||||
|
||||
+54
-25
@@ -25,24 +25,32 @@ 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);
|
||||
|
||||
let upperBound;
|
||||
let lowerBound;
|
||||
if (props.range) {
|
||||
const value = (props.value || props.defaultValue || [0, 0]);
|
||||
upperBound = this.trimAlignValue(value[1]);
|
||||
lowerBound = this.trimAlignValue(value[0]);
|
||||
} else if (props.marks.length > 0) {
|
||||
upperBound = this.calcValueFromProps(props);
|
||||
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 {
|
||||
// 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);
|
||||
const value = propOrDefault(props, 'value', initialValue);
|
||||
if (props.range) {
|
||||
lowerBound = this.trimAlignValue(value[0]);
|
||||
upperBound = this.trimAlignValue(value[1]);
|
||||
} else {
|
||||
upperBound = this.trimAlignValue(value);
|
||||
}
|
||||
}
|
||||
|
||||
let recent;
|
||||
@@ -80,9 +88,8 @@ class Slider extends React.Component {
|
||||
upperBound: nextProps.value,
|
||||
});
|
||||
} else if ('index' in nextProps) {
|
||||
this.setState({
|
||||
upperBound: this.calcValueFromProps(nextProps),
|
||||
});
|
||||
const index = ('index' in nextProps ? nextProps.index : nextProps.defaultIndex);
|
||||
this.setState(this.getBoundsFromIndex(index, nextProps));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -187,6 +194,18 @@ class Slider extends React.Component {
|
||||
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) {
|
||||
@@ -251,10 +270,9 @@ class Slider extends React.Component {
|
||||
return nextValue;
|
||||
}
|
||||
|
||||
calcValueFromProps(props) {
|
||||
calcValueFromIndex(index, props) {
|
||||
const marksLen = props.marks.length;
|
||||
if (marksLen > 0) {
|
||||
const index = ('index' in props ? props.index : props.defaultIndex);
|
||||
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;
|
||||
@@ -268,7 +286,11 @@ class Slider extends React.Component {
|
||||
if (props[event]) {
|
||||
let data;
|
||||
if (hasMarks) {
|
||||
data = this.getIndex(v);
|
||||
if (props.range) {
|
||||
data = v.map(bound => this.getIndex(bound));
|
||||
} else {
|
||||
data = this.getIndex(v);
|
||||
}
|
||||
} else if (v === undefined) {
|
||||
data = this.state.value;
|
||||
} else {
|
||||
@@ -309,7 +331,7 @@ class Slider extends React.Component {
|
||||
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, children} = props;
|
||||
const {marks, step, max, min, tipTransitionName, tipFormatter, children} = props;
|
||||
const marksLen = marks.length;
|
||||
|
||||
const sliderClassName = classSet({
|
||||
@@ -328,14 +350,14 @@ class Slider extends React.Component {
|
||||
}
|
||||
|
||||
const handleClassName = prefixCls + '-handle';
|
||||
const isNoTip = marksLen > 0;
|
||||
const upper = (<Handle className={handleClassName} tipTransitionName={tipTransitionName} noTip={isNoTip}
|
||||
offset={upperOffset} value={upperBound} dragging={handle === 'upperBound'}/>);
|
||||
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}
|
||||
offset={lowerOffset} value={lowerBound} dragging={handle === 'lowerBound'}/>);
|
||||
lower = (<Handle className={handleClassName} tipTransitionName={tipTransitionName} noTip={isNoTip} tipFormatter={tipFormatter}
|
||||
offset={lowerOffset} value={lowerBound} dragging={handle === 'lowerBound'} />);
|
||||
}
|
||||
|
||||
const upperIndex = this.getIndex(upperBound);
|
||||
@@ -379,12 +401,18 @@ Slider.propTypes = {
|
||||
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([
|
||||
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,
|
||||
isIncluded: React.PropTypes.bool, // @Deprecated
|
||||
included: React.PropTypes.bool,
|
||||
@@ -396,6 +424,7 @@ Slider.propTypes = {
|
||||
onChange: React.PropTypes.func,
|
||||
onAfterChange: React.PropTypes.func,
|
||||
tipTransitionName: React.PropTypes.string,
|
||||
tipFormatter: React.PropTypes.func,
|
||||
dots: React.PropTypes.bool,
|
||||
range: React.PropTypes.bool,
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user