UNPKG

9.69 kBMarkdownView Raw
1# rc-slider
2
3Slider UI component for React
4
5[![NPM version][npm-image]][npm-url] [![dumi](https://img.shields.io/badge/docs%20by-dumi-blue?style=flat-square)](https://github.com/umijs/dumi) [![build status][github-actions-image]][github-actions-url] [![Test coverage][coveralls-image]][coveralls-url] [![Dependencies][david-image]][david-url] [![DevDependencies][david-dev-image]][david-dev-url] [![npm download][download-image]][download-url] [![bundle size][bundlephobia-image]][bundlephobia-url]
6
7[npm-image]: http://img.shields.io/npm/v/rc-slider.svg?style=flat-square
8[npm-url]: http://npmjs.org/package/rc-slider
9[github-actions-image]: https://github.com/react-component/slider/workflows/CI/badge.svg
10[github-actions-url]: https://github.com/react-component/slider/actions
11[circleci-image]: https://img.shields.io/circleci/react-component/slider/master?style=flat-square
12[circleci-url]: https://circleci.com/gh/react-component/slider
13[coveralls-image]: https://img.shields.io/coveralls/react-component/slider.svg?style=flat-square
14[coveralls-url]: https://coveralls.io/r/react-component/slider?branch=master
15[david-url]: https://david-dm.org/react-component/slider
16[david-image]: https://david-dm.org/react-component/slider/status.svg?style=flat-square
17[david-dev-url]: https://david-dm.org/react-component/slider?type=dev
18[david-dev-image]: https://david-dm.org/react-component/slider/dev-status.svg?style=flat-square
19[download-image]: https://img.shields.io/npm/dm/rc-slider.svg?style=flat-square
20[download-url]: https://npmjs.org/package/rc-slider
21[bundlephobia-url]: https://bundlephobia.com/result?p=rc-slider
22[bundlephobia-image]: https://badgen.net/bundlephobia/minzip/rc-slider
23
24## Install
25
26[![rc-slider](https://nodei.co/npm/rc-slider.png)](https://npmjs.org/package/rc-slider)
27
28## Example
29
30`npm start` and then go to http://localhost:8000
31
32Online examples: https://slider.react-component.now.sh/
33
34## Usage
35
36```js
37import Slider, { Range } from 'rc-slider';
38import 'rc-slider/assets/index.css';
39
40export default () => (
41 <>
42 <Slider />
43 <Range />
44 </>
45);
46```
47
48## Compatibility
49
50| [<img src="https://raw.githubusercontent.com/alrra/browser-logos/master/src/edge/edge_48x48.png" alt="IE / Edge" width="24px" height="24px" />](http://godban.github.io/browsers-support-badges/)<br>IE / Edge | [<img src="https://raw.githubusercontent.com/alrra/browser-logos/master/src/firefox/firefox_48x48.png" alt="Firefox" width="24px" height="24px" />](http://godban.github.io/browsers-support-badges/)<br>Firefox | [<img src="https://raw.githubusercontent.com/alrra/browser-logos/master/src/chrome/chrome_48x48.png" alt="Chrome" width="24px" height="24px" />](http://godban.github.io/browsers-support-badges/)<br>Chrome | [<img src="https://raw.githubusercontent.com/alrra/browser-logos/master/src/safari/safari_48x48.png" alt="Safari" width="24px" height="24px" />](http://godban.github.io/browsers-support-badges/)<br>Safari | [<img src="https://raw.githubusercontent.com/alrra/browser-logos/master/src/electron/electron_48x48.png" alt="Electron" width="24px" height="24px" />](http://godban.github.io/browsers-support-badges/)<br>Electron |
51| --- | --- | --- | --- | --- |
52| IE11, Edge | last 2 versions | last 2 versions | last 2 versions | last 2 versions |
53
54## API
55
56### createSliderWithTooltip(Slider | Range) => React.Component
57
58An extension to make Slider or Range support Tooltip on handle.
59
60```js
61const Slider = require('rc-slider');
62const createSliderWithTooltip = Slider.createSliderWithTooltip;
63const Range = createSliderWithTooltip(Slider.Range);
64```
65
66[Online demo](http://react-component.github.io/slider/?path=/story/rc-slider--handle)
67
68After Range or Slider was wrapped by createSliderWithTooltip, it will have the following props:
69
70| Name | Type | Default | Description |
71| ------------ | ------- | ------- | ----------- |
72| tipFormatter | (value: number): React.ReactNode | `value => value` | A function to format tooltip's overlay |
73| tipProps | Object | `{` <br>`placement: 'top',` <br> ` prefixCls: 'rc-slider-tooltip',` <br> `overlay: tipFormatter(value)` <br> `}` | A function to format tooltip's overlay |
74
75### Common API
76
77The following APIs are shared by Slider and Range.
78
79| Name | Type | Default | Description |
80| ------------ | ------- | ------- | ----------- |
81| className | string | `''` | Additional CSS class for the root DOM node |
82| min | number | `0` | The minimum value of the slider |
83| max | number | `100` | The maximum value of the slider |
84| marks | `{number: ReactNode}` or`{number: { style, label }}` | `{}` | Marks on the slider. The key determines the position, and the value determines what will show. If you want to set the style of a specific mark point, the value should be an object which contains `style` and `label` properties. |
85| step | number or `null` | `1` | Value to be added or subtracted on each step the slider makes. Must be greater than zero, and `max` - `min` should be evenly divisible by the step value. <br /> When `marks` is not an empty object, `step` can be set to `null`, to make `marks` as steps. |
86| vertical | boolean | `false` | If vertical is `true`, the slider will be vertical. |
87| handle | (props) => React.ReactNode | | A handle generator which could be used to customized handle. |
88| included | boolean | `true` | If the value is `true`, it means a continuous value interval, otherwise, it is a independent value. |
89| reverse | boolean | `false` | If the value is `true`, it means the component is rendered reverse. |
90| disabled | boolean | `false` | If `true`, handles can't be moved. |
91| dots | boolean | `false` | When the `step` value is greater than 1, you can set the `dots` to `true` if you want to render the slider with dots. |
92| onBeforeChange | Function | NOOP | `onBeforeChange` will be triggered when `ontouchstart` or `onmousedown` is triggered. |
93| onChange | Function | NOOP | `onChange` will be triggered while the value of Slider changing. |
94| onAfterChange | Function | NOOP | `onAfterChange` will be triggered when `ontouchend` or `onmouseup` is triggered. |
95| minimumTrackStyle | Object | | please use `trackStyle` instead. (`only used for slider, just for compatibility , will be deprecate at rc-slider@9.x `) |
96| maximumTrackStyle | Object | | please use `railStyle` instead (`only used for slider, just for compatibility , will be deprecate at rc-slider@9.x`) |
97| handleStyle | Array[Object] \| Object | `[{}]` | The style used for handle. (`both for slider(`Object`) and range(`Array of Object`), the array will be used for multi handle following element order`) |
98| trackStyle | Array[Object] \| Object | `[{}]` | The style used for track. (`both for slider(`Object`) and range(`Array of Object`), the array will be used for multi track following element order`)|
99| railStyle | Object | `{}` | The style used for the track base color. |
100| dotStyle | Object | `{}` | The style used for the dots. |
101| activeDotStyle | Object | `{}` | The style used for the active dots. |
102
103### Slider
104
105| Name | Type | Default | Description |
106| ------------ | ------- | ------- | ----------- |
107| defaultValue | number | `0` | Set initial value of slider. |
108| value | number | - | Set current value of slider. |
109| startPoint | number | `undefined` | Track starts from this value. If `undefined`, `min` is used. |
110| tabIndex | number | `0` | Set the tabIndex of the slider handle. |
111| ariaLabelForHandle | string | - | Set the `aria-label` attribute on the slider handle. |
112| ariaLabelledByForHandle | string | - | Set the `aria-labelledby` attribute on the slider handle. |
113| ariaValueTextFormatterForHandle | (value) => string | - | A function to set the `aria-valuetext` attribute on the slider handle. It receives the current value of the slider and returns a formatted string describing the value. See [WAI-ARIA Authoring Practices 1.1](https://www.w3.org/TR/wai-aria-practices-1.1/#slider) for more information. |
114
115### Range
116
117| Name | Type | Default | Description |
118| ------------ | ------- | ------- | ----------- |
119| defaultValue | `number[]` | `[0, 0]` | Set initial positions of handles. |
120| value | `number[]` | | Set current positions of handles. |
121| tabIndex | number[] | `[0, 0]` | Set the tabIndex of each handle. |
122| ariaLabelGroupForHandles | Array[string] | - | Set the `aria-label` attribute on each handle. |
123| ariaLabelledByGroupForHandles | Array[string] | - | Set the `aria-labelledby` attribute on each handle. |
124| ariaValueTextFormatterGroupForHandles | Array[(value) => string] | - | A function to set the `aria-valuetext` attribute on each handle. It receives the current value of the slider and returns a formatted string describing the value. See [WAI-ARIA Authoring Practices 1.1](https://www.w3.org/TR/wai-aria-practices-1.1/#slider) for more information. |
125| count | number | `1` | Determine how many ranges to render, and multiple handles will be rendered (number + 1). |
126| allowCross | boolean | `true` | `allowCross` could be set as `true` to allow those handles to cross. |
127| pushable | boolean or number | `false` | `pushable` could be set as `true` to allow pushing of surrounding handles when moving a handle. When set to a number, the number will be the minimum ensured distance between handles. Example: ![](http://i.giphy.com/l46Cs36c9HrHMExoc.gif) |
128| draggableTrack | boolean | `false` | Open the track drag. open after click on the track will be invalid. |
129
130### SliderTooltip
131
132The Tooltip Component that keep following with content.
133
134## Development
135
136```
137npm install
138npm start
139```
140
141## Test Case
142
143`npm run test`
144
145## Coverage
146
147`npm run coverage`
148## License
149
150`rc-slider` is released under the MIT license.