Slider滑动输入条
当用户需要在数值区间/自定义区间内进行选择时,可为连续或离散值。
基本滑动条。当 为 true
时,渲染为双滑块。当 disabled
为 true
时,滑块处于不可用状态。
滑块左右可以设置图标来表达业务含义。
import { Slider, Icon } from 'antd';
class IconSlider extends React.Component {
state = {
value: 0,
};
handleChange = value => {
this.setState({ value });
};
render() {
const { max, min } = this.props;
const { value } = this.state;
const mid = ((max - min) / 2).toFixed(5);
const preColor = value >= mid ? '' : 'rgba(0, 0, 0, .45)';
const nextColor = value >= mid ? 'rgba(0, 0, 0, .45)' : '';
return (
<div className="icon-wrapper">
<Icon style={{ color: preColor }} type="frown-o" />
<Slider {...this.props} onChange={this.handleChange} value={value} />
<Icon style={{ color: nextColor }} type="smile-o" />
</div>
);
}
}
ReactDOM.render(<IconSlider min={0} max={20} />, mountNode);
.icon-wrapper {
position: relative;
padding: 0px 30px;
}
.icon-wrapper .anticon {
position: absolute;
top: -2px;
width: 16px;
height: 16px;
line-height: 1;
font-size: 16px;
color: rgba(0, 0, 0, 0.25);
}
.icon-wrapper .anticon:first-child {
left: 0;
}
.icon-wrapper .anticon:last-child {
right: 0;
}
当 Slider 的值发生改变时,会触发 onChange
事件,并把改变后的值作为参数传入。在 onmouseup
时,会触发 onAfterChange
事件,并把当前值作为参数传入。
垂直方向的 Slider。
import { Slider } from 'antd';
const style = {
float: 'left',
height: 300,
marginLeft: 70,
};
const marks = {
0: '0°C',
26: '26°C',
37: '37°C',
100: {
style: {
color: '#f50',
label: <strong>100°C</strong>,
},
};
ReactDOM.render(
<div style={{ height: 300 }}>
<div style={style}>
<Slider vertical defaultValue={30} />
<div style={style}>
<Slider vertical range step={10} defaultValue={[20, 50]} />
</div>
<div style={style}>
<Slider vertical range marks={marks} defaultValue={[26, 37]} />
</div>
</div>,
mountNode,
);
import { Slider, InputNumber, Row, Col } from 'antd';
class IntegerStep extends React.Component {
state = {
inputValue: 1,
};
onChange = value => {
this.setState({
inputValue: value,
});
};
render() {
const { inputValue } = this.state;
return (
<Row>
<Col span={12}>
<Slider
min={1}
max={20}
onChange={this.onChange}
value={typeof inputValue === 'number' ? inputValue : 0}
/>
</Col>
<Col span={4}>
<InputNumber
min={1}
max={20}
style={{ marginLeft: 16 }}
value={inputValue}
onChange={this.onChange}
/>
</Col>
</Row>
);
}
}
class DecimalStep extends React.Component {
state = {
inputValue: 0,
};
onChange = value => {
if (Number.isNaN(value)) {
return;
}
this.setState({
inputValue: value,
});
render() {
const { inputValue } = this.state;
return (
<Row>
<Col span={12}>
<Slider
min={0}
max={1}
onChange={this.onChange}
value={typeof inputValue === 'number' ? inputValue : 0}
step={0.01}
/>
</Col>
<Col span={4}>
<InputNumber
min={0}
max={1}
style={{ marginLeft: 16 }}
value={inputValue}
onChange={this.onChange}
/>
</Col>
</Row>
);
}
}
ReactDOM.render(
<div>
<IntegerStep />
<DecimalStep />
</div>,
mountNode,
);
使用 tipFormatter
可以格式化 Tooltip
的内容,设置 tipFormatter={null}
,则隐藏 Tooltip
。
使用 marks
属性标注分段式滑块,使用 value
/ defaultValue
指定滑块位置。当 included=false
时,表明不同标记间为并列关系。当 step=null
时,Slider 的可选值仅有 marks
标出来的部分。
import { Slider } from 'antd';
const marks = {
0: '0°C',
26: '26°C',
37: '37°C',
100: {
style: {
color: '#f50',
},
label: <strong>100°C</strong>,
},
};
ReactDOM.render(
<div>
<h4>included=true</h4>
<Slider marks={marks} defaultValue={37} />
<Slider range marks={marks} defaultValue={[26, 37]} />
<h4>included=false</h4>
<Slider marks={marks} included={false} defaultValue={37} />
<h4>marks & step</h4>
<Slider marks={marks} step={10} defaultValue={37} />
<h4>step=null</h4>
<Slider marks={marks} step={null} defaultValue={37} />
</div>,
mountNode,
);
当 tooltipVisible
为 true
时,将始终显示 ToolTip;反之则始终不显示,即使在拖动、移入时也是如此。
import { Slider } from 'antd';
名称 | 描述 |
---|---|
blur() | 移除焦点 |
focus() | 获取焦点 |