2016-03-31 09:40:55 +08:00
|
|
|
---
|
|
|
|
order: 2
|
2016-09-22 10:09:22 +08:00
|
|
|
title:
|
2016-07-21 09:51:04 +08:00
|
|
|
zh-CN: 带 icon 的滑块
|
|
|
|
en-US: Slider with icon
|
2016-03-31 09:40:55 +08:00
|
|
|
---
|
2015-07-14 12:06:44 +08:00
|
|
|
|
2016-07-21 09:51:04 +08:00
|
|
|
## zh-CN
|
|
|
|
|
2015-07-16 16:48:06 +08:00
|
|
|
滑块左右可以设置图标来表达业务含义。
|
|
|
|
|
2016-07-21 09:51:04 +08:00
|
|
|
## en-US
|
|
|
|
|
|
|
|
You can add an icon beside the slider to make it meaningful.
|
|
|
|
|
2017-02-13 10:55:53 +08:00
|
|
|
````jsx
|
2015-10-28 20:55:49 +08:00
|
|
|
import { Slider, Icon } from 'antd';
|
2015-07-14 12:06:44 +08:00
|
|
|
|
2017-02-20 22:19:59 +08:00
|
|
|
class IconSlider extends React.Component {
|
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
const { max, min } = props;
|
2015-10-28 20:55:49 +08:00
|
|
|
const mid = ((max - min) / 2).toFixed(5);
|
2017-02-20 22:19:59 +08:00
|
|
|
this.state = {
|
2015-10-02 16:18:43 +08:00
|
|
|
preIconClass: this.props.value >= mid ? '' : 'anticon-highlight',
|
|
|
|
nextIconClass: this.props.value >= mid ? 'anticon-highlight' : '',
|
2016-02-17 15:57:33 +08:00
|
|
|
mid,
|
2016-05-11 09:32:33 +08:00
|
|
|
sliderValue: this.props.value,
|
2015-07-14 12:06:44 +08:00
|
|
|
};
|
2017-02-20 22:19:59 +08:00
|
|
|
}
|
|
|
|
handleChange = (v) => {
|
2015-11-25 17:35:49 +08:00
|
|
|
this.setState({
|
|
|
|
preIconClass: v >= this.state.mid ? '' : 'anticon-highlight',
|
|
|
|
nextIconClass: v >= this.state.mid ? 'anticon-highlight' : '',
|
2016-05-11 09:32:33 +08:00
|
|
|
sliderValue: v,
|
2015-11-25 17:35:49 +08:00
|
|
|
});
|
2017-02-20 22:19:59 +08:00
|
|
|
}
|
2015-07-14 12:06:44 +08:00
|
|
|
render() {
|
|
|
|
return (
|
2016-11-21 16:37:04 +08:00
|
|
|
<div className="icon-wrapper">
|
2015-10-02 16:18:43 +08:00
|
|
|
<Icon className={this.state.preIconClass} type={this.props.icon[0]} />
|
2015-07-14 12:06:44 +08:00
|
|
|
<Slider {...this.props} onChange={this.handleChange} value={this.state.sliderValue} />
|
2016-04-07 12:04:06 +08:00
|
|
|
<Icon className={this.state.nextIconClass} type={this.props.icon[1]} />
|
2015-07-14 12:06:44 +08:00
|
|
|
</div>
|
|
|
|
);
|
2017-02-20 22:19:59 +08:00
|
|
|
}
|
|
|
|
}
|
2015-07-14 12:06:44 +08:00
|
|
|
|
2016-11-21 16:37:04 +08:00
|
|
|
ReactDOM.render(<IconSlider min={0} max={20} value={0} icon={['frown-o', 'smile-o']} />, mountNode);
|
2015-07-14 12:06:44 +08:00
|
|
|
````
|
|
|
|
|
2015-11-14 14:09:08 +08:00
|
|
|
````css
|
2016-11-21 16:37:04 +08:00
|
|
|
.icon-wrapper {
|
2015-07-14 12:06:44 +08:00
|
|
|
position: relative;
|
2015-11-14 14:09:08 +08:00
|
|
|
padding: 0px 30px;
|
2015-07-14 12:06:44 +08:00
|
|
|
}
|
|
|
|
|
2016-11-21 16:37:04 +08:00
|
|
|
.icon-wrapper .anticon {
|
2015-07-14 12:06:44 +08:00
|
|
|
position: absolute;
|
2015-11-14 14:09:08 +08:00
|
|
|
top: -3px;
|
2015-07-14 12:06:44 +08:00
|
|
|
width: 16px;
|
|
|
|
height: 16px;
|
|
|
|
line-height: 1;
|
|
|
|
font-size: 16px;
|
2017-01-01 22:20:06 +08:00
|
|
|
color: @disabled-color;
|
2015-07-14 12:06:44 +08:00
|
|
|
}
|
|
|
|
|
2016-11-21 16:37:04 +08:00
|
|
|
.icon-wrapper .anticon:first-child {
|
2015-07-14 12:06:44 +08:00
|
|
|
left: 0;
|
|
|
|
}
|
2016-04-07 12:04:06 +08:00
|
|
|
|
2016-11-21 16:37:04 +08:00
|
|
|
.icon-wrapper .anticon:last-child {
|
2015-07-14 12:06:44 +08:00
|
|
|
right: 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
.anticon.anticon-highlight {
|
|
|
|
color: #666;
|
|
|
|
}
|
2015-11-14 14:09:08 +08:00
|
|
|
````
|