Skip to content

Commit

Permalink
Allow tabIndex to be set explicitly on Handle
Browse files Browse the repository at this point in the history
  • Loading branch information
gvincentlh committed Dec 19, 2017
1 parent db32618 commit be3d775
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 2 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,13 +120,15 @@ The following APIs are shared by Slider and Range.
| ------------ | ------- | ------- | ----------- |
| defaultValue | number | `0` | Set initial value of slider. |
| value | number | - | Set current value of slider. |
| tabIndex | number | `0` | Set the tabIndex of the slider handle. |
### Range
| Name | Type | Default | Description |
| ------------ | ------- | ------- | ----------- |
| defaultValue | `number[]` | `[0, 0]` | Set initial positions of handles. |
| value | `number[]` | | Set current positions of handles. |
| tabIndex | number[] | `[0, 0]` | Set the tabIndex of each handle. |
| count | number | `1` | Determine how many ranges to render, and multiple handles will be rendered (number + 1). |
| allowCross | boolean | `true` | `allowCross` could be set as `true` to allow those handles to cross. |
| pushable | boolean or number | `false` | `pushable` could be set as `true` to allow pushing of surrounding handles when moving an handle. When set to a number, the number will be the minimum ensured distance between handles. Example: ![](http://i.giphy.com/l46Cs36c9HrHMExoc.gif) |
Expand Down
5 changes: 3 additions & 2 deletions src/Handle.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export default class Handle extends React.Component {

render() {
const {
className, vertical, offset, style, disabled, min, max, value, ...restProps,
className, vertical, offset, style, disabled, min, max, value, tabIndex, ...restProps,
} = this.props;

const postionStyle = vertical ? { bottom: `${offset}%` } : { left: `${offset}%` };
Expand All @@ -34,7 +34,7 @@ export default class Handle extends React.Component {
<div
ref={node => (this.handle = node)}
role="slider"
tabIndex="0"
tabIndex= {tabIndex || 0}
{...ariaProps}
{...restProps}
className={className}
Expand All @@ -53,4 +53,5 @@ Handle.propTypes = {
min: PropTypes.number,
max: PropTypes.number,
value: PropTypes.number,
tabIndex: PropTypes.number,
};
4 changes: 4 additions & 0 deletions src/Range.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,14 @@ class Range extends React.Component {
]),
allowCross: PropTypes.bool,
disabled: PropTypes.bool,
tabIndex: PropTypes.arrayOf(PropTypes.number),
};

static defaultProps = {
count: 1,
allowCross: true,
pushable: false,
tabIndex: [],
};

constructor(props) {
Expand Down Expand Up @@ -299,6 +301,7 @@ class Range extends React.Component {
handle: handleGenerator,
trackStyle,
handleStyle,
tabIndex,
} = this.props;

const offsets = bounds.map(v => this.calcOffset(v));
Expand All @@ -314,6 +317,7 @@ class Range extends React.Component {
value: v,
dragging: handle === i,
index: i,
tabIndex: tabIndex[i] || 0,
min,
max,
disabled,
Expand Down
3 changes: 3 additions & 0 deletions src/Slider.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class Slider extends React.Component {
value: PropTypes.number,
disabled: PropTypes.bool,
autoFocus: PropTypes.bool,
tabIndex: PropTypes.number,
};

constructor(props) {
Expand Down Expand Up @@ -143,6 +144,7 @@ class Slider extends React.Component {
minimumTrackStyle,
trackStyle,
handleStyle,
tabIndex,
min,
max,
handle: handleGenerator,
Expand All @@ -159,6 +161,7 @@ class Slider extends React.Component {
min,
max,
index: 0,
tabIndex,
style: handleStyle[0] || handleStyle,
ref: h => this.saveHandle(0, h),
});
Expand Down
6 changes: 6 additions & 0 deletions tests/Range.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ describe('Range', () => {
expect(trackStyle.visibility).toMatch('visible');
});

it('should render Range with tabIndex correctly', () => {
const wrapper = mount(<Range tabIndex={[1, 2]} />);
expect(wrapper.find('.rc-slider-handle > .rc-slider-handle').at(0).props().tabIndex).toEqual(1);
expect(wrapper.find('.rc-slider-handle > .rc-slider-handle').at(1).props().tabIndex).toEqual(2);
});

it('should render Multi-Range with value correctly', () => {
const wrapper = mount(<Range count={3} value={[0, 25, 50, 75]} />);
expect(wrapper.state('bounds')[0]).toBe(0);
Expand Down
5 changes: 5 additions & 0 deletions tests/Slider.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ describe('Slider', () => {
expect(trackStyle.visibility).toMatch('visible');
});

it('should allow tabIndex to be set on Handle via Slider', () => {
const wrapper = mount(<Slider tabIndex={1} />);
expect(wrapper.find('.rc-slider-handle').at(1).props().tabIndex).toEqual(1);
});

it('increases the value when key "up" is pressed', () => {
const wrapper = mount(<Slider defaultValue={50} />);
const handler = wrapper.find('.rc-slider-handle').at(1);
Expand Down

0 comments on commit be3d775

Please sign in to comment.