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.
|
|
|
|
|
2019-05-07 14:57:32 +08:00
|
|
|
```jsx
|
2019-08-13 14:07:17 +08:00
|
|
|
import { Slider } from 'antd';
|
2019-11-28 12:34:33 +08:00
|
|
|
import { FrownOutlined, SmileOutlined } from '@ant-design/icons';
|
2015-07-14 12:06:44 +08:00
|
|
|
|
2017-02-20 22:19:59 +08:00
|
|
|
class IconSlider extends React.Component {
|
2017-11-06 21:28:51 +08:00
|
|
|
state = {
|
|
|
|
value: 0,
|
2019-05-07 14:57:32 +08:00
|
|
|
};
|
2018-06-27 15:55:04 +08:00
|
|
|
|
2019-05-07 14:57:32 +08:00
|
|
|
handleChange = value => {
|
2017-11-06 21:28:51 +08:00
|
|
|
this.setState({ value });
|
2019-05-07 14:57:32 +08:00
|
|
|
};
|
2018-06-27 15:55:04 +08:00
|
|
|
|
2015-07-14 12:06:44 +08:00
|
|
|
render() {
|
2017-11-06 21:28:51 +08:00
|
|
|
const { max, min } = this.props;
|
|
|
|
const { value } = this.state;
|
|
|
|
const mid = ((max - min) / 2).toFixed(5);
|
2020-02-06 14:43:16 +08:00
|
|
|
const preColorCls = value >= mid ? '' : 'icon-wrapper-active';
|
|
|
|
const nextColorCls = value >= mid ? 'icon-wrapper-active' : '';
|
2015-07-14 12:06:44 +08:00
|
|
|
return (
|
2016-11-21 16:37:04 +08:00
|
|
|
<div className="icon-wrapper">
|
2020-02-06 14:43:16 +08:00
|
|
|
<FrownOutlined className={preColorCls} />
|
2017-11-06 21:28:51 +08:00
|
|
|
<Slider {...this.props} onChange={this.handleChange} value={value} />
|
2020-02-06 14:43:16 +08:00
|
|
|
<SmileOutlined className={nextColorCls} />
|
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
|
|
|
|
2017-11-06 21:28:51 +08:00
|
|
|
ReactDOM.render(<IconSlider min={0} max={20} />, mountNode);
|
2019-05-07 14:57:32 +08:00
|
|
|
```
|
2015-07-14 12:06:44 +08:00
|
|
|
|
2019-05-07 14:57:32 +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;
|
2017-11-06 21:28:51 +08:00
|
|
|
top: -2px;
|
2015-07-14 12:06:44 +08:00
|
|
|
width: 16px;
|
|
|
|
height: 16px;
|
|
|
|
line-height: 1;
|
|
|
|
font-size: 16px;
|
2019-05-07 14:57:32 +08:00
|
|
|
color: rgba(0, 0, 0, 0.25);
|
2015-07-14 12:06:44 +08:00
|
|
|
}
|
|
|
|
|
2020-02-06 14:43:16 +08:00
|
|
|
.icon-wrapper .icon-wrapper-active {
|
2020-02-19 13:20:43 +08:00
|
|
|
color: rgba(0, 0, 0, 0.45);
|
2020-02-06 14:43:16 +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;
|
|
|
|
}
|
2019-05-07 14:57:32 +08:00
|
|
|
```
|
2020-02-06 14:43:16 +08:00
|
|
|
|
|
|
|
<style>
|
|
|
|
[data-theme="dark"] .icon-wrapper .anticon {
|
|
|
|
color: rgba(255, 255, 255, 0.25);
|
|
|
|
}
|
|
|
|
[data-theme="dark"] .icon-wrapper .icon-wrapper-active {
|
|
|
|
color: rgba(255, 255, 255, .45);
|
|
|
|
}
|
|
|
|
</style>
|