Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implements autoFocus #272

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ your own as long as it conforms to the shape.
})
```

AutoFocus on handle. If multiple handles are used, it focuses on the first one.

```js
autoFocus: PropTypes.bool
```

Custom class name that will be applied to the root of Rheostat.

```js
Expand Down
6 changes: 6 additions & 0 deletions src/Slider.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ const propTypes = forbidExtraProps({
// Automatically adds a top position for large when enabled
autoAdjustVerticalPosition: PropTypes.bool,

// Automatically sets focus on handle
autoFocus: PropTypes.bool,

// the algorithm to use
algorithm: PropTypes.shape({
getValue: PropTypes.func,
Expand Down Expand Up @@ -109,6 +112,7 @@ const propTypes = forbidExtraProps({

const defaultProps = {
autoAdjustVerticalPosition: false,
autoFocus: false,
children: null,
algorithm: LinearScale,
disabled: false,
Expand Down Expand Up @@ -778,6 +782,7 @@ class Rheostat extends React.Component {
const {
css,
autoAdjustVerticalPosition,
autoFocus,
algorithm,
children,
disabled,
Expand Down Expand Up @@ -831,6 +836,7 @@ class Rheostat extends React.Component {
aria-valuemin={this.getMinValue(idx)}
aria-valuenow={values[idx]}
aria-disabled={disabled}
autoFocus={autoFocus && idx === 0}
data-handle-key={idx}
key={idx /* eslint-disable-line react/no-array-index-key */}
orientation={orientation}
Expand Down
2 changes: 2 additions & 0 deletions src/propTypes/HandlePropTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export default {
'aria-valuenow': PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
'aria-disabled': PropTypes.bool,
'data-handle-key': PropTypes.node,
autoFocus: PropTypes.bool,
orientation: PropTypes.string,
disabled: PropTypes.bool,
onClick: PropTypes.func,
Expand All @@ -27,6 +28,7 @@ export const handleDefaultProps = {
'aria-valuemax': undefined,
'aria-valuemin': undefined,
'aria-disabled': undefined,
autoFocus: undefined,
onClick: undefined,
onKeyDown: undefined,
onMouseDown: undefined,
Expand Down
3 changes: 3 additions & 0 deletions stories/ExampleSlider.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ storiesOf('Slider', module)
.add('A Simple Slider', () => (
<LabeledSlider />
))
.add('Handle autoFocus', () => (
<LabeledSlider autoFocus />
))
.add('Custom Handle', () => {
function MyHandle({ style, handleRef, ...passProps }) {
return (
Expand Down
25 changes: 25 additions & 0 deletions test/slider-test.jsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { shallow, mount } from 'enzyme';
import React from 'react';
import PropTypes from 'prop-types';
import createReactClass from 'create-react-class';
import PureRenderMixin from 'react-addons-pure-render-mixin';

Expand Down Expand Up @@ -27,6 +28,16 @@ function testKeys(slider, tests) {
});
}

function CustomHandle({ handleRef, ...passProps }) {
return <button id={Math.random().toString(36).substring(7)} type="button" ref={handleRef} {...passProps} />;
}
CustomHandle.propTypes = {
handleRef: PropTypes.any,
};
CustomHandle.defaultProps = {
handleRef: '',
};

describeWithDOM('<Slider />', () => {
describe('render', () => {
it('should render the slider with one handle by default', () => {
Expand Down Expand Up @@ -93,6 +104,20 @@ describeWithDOM('<Slider />', () => {
assert.isTrue(pitRender.calledOnce, 'one pit was rendered only once');
});

it('should set focus on handle if autoFocus prop is true', () => {
const wrapper = mount(<Slider handle={CustomHandle} autoFocus />);
const handleId = wrapper.find('button').prop('id');
const focusedElementId = document.activeElement.id;
assert.equal(handleId, focusedElementId, 'Slider\'s handle is focused');
});

it('should set focus on first handle when multiple handles are available', () => {
const wrapper = mount(<Slider handle={CustomHandle} values={[0, 50, 100]} autoFocus />);
const handleId = wrapper.find('button').first().prop('id');
const focusedElementId = document.activeElement.id;
assert.equal(handleId, focusedElementId, 'Slider\'s first handle is focused');
});

it('should not throw react errors on disabled', () => {
const slider = mount(<Slider />);
slider.setProps({ disabled: true });
Expand Down