Slider滑动输入条

    当用户需要在数值区间/自定义区间内进行选择时,可为连续或离散值。

    基本

    基本滑动条。当 为 true 时,渲染为双滑块。当 disabledtrue 时,滑块处于不可用状态。

    Slider 滑动输入条 - 图2

    滑块左右可以设置图标来表达业务含义。

    1. import { Slider, Icon } from 'antd';
    2. class IconSlider extends React.Component {
    3. state = {
    4. value: 0,
    5. };
    6. handleChange = value => {
    7. this.setState({ value });
    8. };
    9. render() {
    10. const { max, min } = this.props;
    11. const { value } = this.state;
    12. const mid = ((max - min) / 2).toFixed(5);
    13. const preColor = value >= mid ? '' : 'rgba(0, 0, 0, .45)';
    14. const nextColor = value >= mid ? 'rgba(0, 0, 0, .45)' : '';
    15. return (
    16. <div className="icon-wrapper">
    17. <Icon style={{ color: preColor }} type="frown-o" />
    18. <Slider {...this.props} onChange={this.handleChange} value={value} />
    19. <Icon style={{ color: nextColor }} type="smile-o" />
    20. </div>
    21. );
    22. }
    23. }
    24. ReactDOM.render(<IconSlider min={0} max={20} />, mountNode);
    1. .icon-wrapper {
    2. position: relative;
    3. padding: 0px 30px;
    4. }
    5. .icon-wrapper .anticon {
    6. position: absolute;
    7. top: -2px;
    8. width: 16px;
    9. height: 16px;
    10. line-height: 1;
    11. font-size: 16px;
    12. color: rgba(0, 0, 0, 0.25);
    13. }
    14. .icon-wrapper .anticon:first-child {
    15. left: 0;
    16. }
    17. .icon-wrapper .anticon:last-child {
    18. right: 0;
    19. }

    事件

    当 Slider 的值发生改变时,会触发 onChange 事件,并把改变后的值作为参数传入。在 onmouseup 时,会触发 onAfterChange 事件,并把当前值作为参数传入。

    垂直方向的 Slider。

    1. import { Slider } from 'antd';
    2. const style = {
    3. float: 'left',
    4. height: 300,
    5. marginLeft: 70,
    6. };
    7. const marks = {
    8. 0: '0°C',
    9. 26: '26°C',
    10. 37: '37°C',
    11. 100: {
    12. style: {
    13. color: '#f50',
    14. label: <strong>100°C</strong>,
    15. },
    16. };
    17. ReactDOM.render(
    18. <div style={{ height: 300 }}>
    19. <div style={style}>
    20. <Slider vertical defaultValue={30} />
    21. <div style={style}>
    22. <Slider vertical range step={10} defaultValue={[20, 50]} />
    23. </div>
    24. <div style={style}>
    25. <Slider vertical range marks={marks} defaultValue={[26, 37]} />
    26. </div>
    27. </div>,
    28. mountNode,
    29. );

    Slider 滑动输入条 - 图5

    带输入框的滑块

    1. import { Slider, InputNumber, Row, Col } from 'antd';
    2. class IntegerStep extends React.Component {
    3. state = {
    4. inputValue: 1,
    5. };
    6. onChange = value => {
    7. this.setState({
    8. inputValue: value,
    9. });
    10. };
    11. render() {
    12. const { inputValue } = this.state;
    13. return (
    14. <Row>
    15. <Col span={12}>
    16. <Slider
    17. min={1}
    18. max={20}
    19. onChange={this.onChange}
    20. value={typeof inputValue === 'number' ? inputValue : 0}
    21. />
    22. </Col>
    23. <Col span={4}>
    24. <InputNumber
    25. min={1}
    26. max={20}
    27. style={{ marginLeft: 16 }}
    28. value={inputValue}
    29. onChange={this.onChange}
    30. />
    31. </Col>
    32. </Row>
    33. );
    34. }
    35. }
    36. class DecimalStep extends React.Component {
    37. state = {
    38. inputValue: 0,
    39. };
    40. onChange = value => {
    41. if (Number.isNaN(value)) {
    42. return;
    43. }
    44. this.setState({
    45. inputValue: value,
    46. });
    47. render() {
    48. const { inputValue } = this.state;
    49. return (
    50. <Row>
    51. <Col span={12}>
    52. <Slider
    53. min={0}
    54. max={1}
    55. onChange={this.onChange}
    56. value={typeof inputValue === 'number' ? inputValue : 0}
    57. step={0.01}
    58. />
    59. </Col>
    60. <Col span={4}>
    61. <InputNumber
    62. min={0}
    63. max={1}
    64. style={{ marginLeft: 16 }}
    65. value={inputValue}
    66. onChange={this.onChange}
    67. />
    68. </Col>
    69. </Row>
    70. );
    71. }
    72. }
    73. ReactDOM.render(
    74. <div>
    75. <IntegerStep />
    76. <DecimalStep />
    77. </div>,
    78. mountNode,
    79. );

    使用 tipFormatter 可以格式化 Tooltip 的内容,设置 tipFormatter={null},则隐藏 Tooltip

    Slider 滑动输入条 - 图7

    带标签的滑块

    使用 marks 属性标注分段式滑块,使用 value / defaultValue 指定滑块位置。当 included=false 时,表明不同标记间为并列关系。当 step=null 时,Slider 的可选值仅有 marks 标出来的部分。

    1. import { Slider } from 'antd';
    2. const marks = {
    3. 0: '0°C',
    4. 26: '26°C',
    5. 37: '37°C',
    6. 100: {
    7. style: {
    8. color: '#f50',
    9. },
    10. label: <strong>100°C</strong>,
    11. },
    12. };
    13. ReactDOM.render(
    14. <div>
    15. <h4>included=true</h4>
    16. <Slider marks={marks} defaultValue={37} />
    17. <Slider range marks={marks} defaultValue={[26, 37]} />
    18. <h4>included=false</h4>
    19. <Slider marks={marks} included={false} defaultValue={37} />
    20. <h4>marks & step</h4>
    21. <Slider marks={marks} step={10} defaultValue={37} />
    22. <h4>step=null</h4>
    23. <Slider marks={marks} step={null} defaultValue={37} />
    24. </div>,
    25. mountNode,
    26. );

    tooltipVisibletrue 时,将始终显示 ToolTip;反之则始终不显示,即使在拖动、移入时也是如此。

    1. import { Slider } from 'antd';
    名称描述
    blur()移除焦点
    focus()获取焦点