2015-07-14 12:06:44 +08:00
|
|
|
# 带 icon 的滑块
|
|
|
|
|
|
|
|
- order: 2
|
|
|
|
|
2015-07-16 16:48:06 +08:00
|
|
|
滑块左右可以设置图标来表达业务含义。
|
|
|
|
|
2015-07-14 12:06:44 +08:00
|
|
|
---
|
|
|
|
|
|
|
|
````jsx
|
2015-10-28 20:55:49 +08:00
|
|
|
import { Slider, Icon } from 'antd';
|
2015-07-14 12:06:44 +08:00
|
|
|
|
2015-10-28 20:55:49 +08:00
|
|
|
const IconSlider = React.createClass({
|
2015-07-14 12:06:44 +08:00
|
|
|
getInitialState() {
|
2015-10-28 20:55:49 +08:00
|
|
|
const max = this.props.max;
|
|
|
|
const min = this.props.min;
|
|
|
|
const mid = ((max - min) / 2).toFixed(5);
|
2015-07-14 12:06:44 +08:00
|
|
|
|
|
|
|
return {
|
2015-10-02 16:18:43 +08:00
|
|
|
preIconClass: this.props.value >= mid ? '' : 'anticon-highlight',
|
|
|
|
nextIconClass: this.props.value >= mid ? 'anticon-highlight' : '',
|
2015-07-14 12:06:44 +08:00
|
|
|
mid: mid,
|
|
|
|
sliderValue: this.props.value
|
|
|
|
};
|
|
|
|
},
|
|
|
|
|
|
|
|
handleChange(v) {
|
2015-07-16 16:48:06 +08:00
|
|
|
this.setState(
|
2015-07-14 12:06:44 +08:00
|
|
|
{
|
2015-10-02 16:18:43 +08:00
|
|
|
preIconClass: v >= this.state.mid ? '' : 'anticon-highlight',
|
|
|
|
nextIconClass: v >= this.state.mid ? 'anticon-highlight': '',
|
2015-07-14 12:06:44 +08:00
|
|
|
sliderValue: v
|
|
|
|
}
|
|
|
|
);
|
|
|
|
},
|
2015-07-16 16:48:06 +08:00
|
|
|
|
2015-07-14 12:06:44 +08:00
|
|
|
render() {
|
|
|
|
return (
|
|
|
|
<div className="iconWrapper">
|
2015-10-02 16:18:43 +08:00
|
|
|
<Icon className={this.state.preIconClass} type={this.props.icon[0]} />
|
|
|
|
<Icon className={this.state.nextIconClass} type={this.props.icon[1]} />
|
2015-07-14 12:06:44 +08:00
|
|
|
<Slider {...this.props} onChange={this.handleChange} value={this.state.sliderValue} />
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2015-10-20 16:47:55 +08:00
|
|
|
ReactDOM.render(
|
2015-10-02 16:18:43 +08:00
|
|
|
<IconSlider min={0} max={20} value={0} icon={['lock', 'unlock']} />
|
2015-07-14 12:06:44 +08:00
|
|
|
, document.getElementById('components-slider-demo-icon-slider'));
|
|
|
|
````
|
|
|
|
|
|
|
|
<style>
|
|
|
|
.iconWrapper {
|
|
|
|
position: relative;
|
2015-07-17 15:13:20 +08:00
|
|
|
padding: 0 24px 0 9px;
|
2015-07-14 12:06:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
.iconWrapper .anticon {
|
|
|
|
position: absolute;
|
|
|
|
top: -6px;
|
|
|
|
width: 16px;
|
|
|
|
height: 16px;
|
|
|
|
line-height: 1;
|
|
|
|
font-size: 16px;
|
|
|
|
color: #ccc;
|
|
|
|
}
|
|
|
|
|
2015-08-18 17:34:27 +08:00
|
|
|
.iconWrapper .anticon-lock {
|
2015-07-14 12:06:44 +08:00
|
|
|
left: 0;
|
|
|
|
}
|
2015-08-18 17:34:27 +08:00
|
|
|
.iconWrapper .anticon-unlock{
|
2015-07-14 12:06:44 +08:00
|
|
|
right: 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
.anticon.anticon-highlight {
|
|
|
|
color: #666;
|
|
|
|
}
|
2015-07-16 16:48:06 +08:00
|
|
|
</style>
|