ant-design/components/slider/demo/icon-slider.md

97 lines
1.9 KiB
Markdown
Raw Normal View History

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
var Slider = antd.Slider;
var IconSlider = React.createClass({
getInitialState() {
var max = this.props.max;
var min = this.props.min;
var mid = ((max - min) / 2).toFixed(5);
var preIcon, nextIcon;
if (this.props.value >= mid) {
preIcon = this.props.icon[0];
nextIcon = this.props.icon[1] + ' anticon-highlight';
} else {
preIcon = this.props.icon[0] + ' anticon-highlight';
nextIcon = this.props.icon[1];
}
return {
preIcon: preIcon,
nextIcon: nextIcon,
mid: mid,
sliderValue: this.props.value
};
},
handleChange(v) {
var preIcon, nextIcon;
if (v >= this.state.mid) {
preIcon = this.props.icon[0];
nextIcon = this.props.icon[1] + ' anticon-highlight';
} else {
preIcon = this.props.icon[0] + ' anticon-highlight';
nextIcon = this.props.icon[1];
}
2015-07-16 16:48:06 +08:00
this.setState(
2015-07-14 12:06:44 +08:00
{
preIcon: preIcon,
nextIcon: nextIcon,
sliderValue: v
}
);
},
2015-07-16 16:48:06 +08:00
2015-07-14 12:06:44 +08:00
render() {
return (
<div className="iconWrapper">
<i className={this.state.preIcon}></i>
<i className={this.state.nextIcon}></i>
<Slider {...this.props} onChange={this.handleChange} value={this.state.sliderValue} />
</div>
);
}
});
React.render(
<IconSlider min={0} max={20} value={0} icon={['anticon anticon-lock', 'anticon anticon-unlock']} />
, 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;
}
.iconWrapper .anticon-lock {
2015-07-14 12:06:44 +08:00
left: 0;
}
.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>