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

Add focus() & blur() #371

Merged
merged 1 commit into from
Dec 1, 2017
Merged
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
9 changes: 9 additions & 0 deletions src/Handle.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@ import React from 'react';
import PropTypes from 'prop-types';

export default class Handle extends React.Component {
focus() {
this.handle.focus();
}

blur() {
this.handle.blur();
}

render() {
const {
className, vertical, offset, style, disabled, min, max, value, ...restProps,
Expand All @@ -24,6 +32,7 @@ export default class Handle extends React.Component {
}
return (
<div
ref={node => (this.handle = node)}
role="slider"
tabIndex="0"
{...ariaProps}
Expand Down
8 changes: 8 additions & 0 deletions src/Slider.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class Slider extends React.Component {
defaultValue: PropTypes.number,
value: PropTypes.number,
disabled: PropTypes.bool,
autoFocus: PropTypes.bool,
};

constructor(props) {
Expand All @@ -37,6 +38,13 @@ class Slider extends React.Component {
}
}

componentDidMount() {
const { autoFocus, disabled } = this.props;
if (autoFocus && !disabled) {
this.focus();
}
}

componentWillReceiveProps(nextProps) {
if (!('value' in nextProps || 'min' in nextProps || 'max' in nextProps)) return;

Expand Down
28 changes: 24 additions & 4 deletions src/common/createSlider.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ export default function createSlider(Component) {
railStyle: PropTypes.object,
dotStyle: PropTypes.object,
activeDotStyle: PropTypes.object,
autoFocus: PropTypes.bool,
onFocus: PropTypes.func,
onBlur: PropTypes.func,
};

static defaultProps = {
Expand Down Expand Up @@ -126,19 +129,24 @@ export default function createSlider(Component) {
}

onFocus = (e) => {
const isVertical = this.props.vertical;

const { onFocus, vertical } = this.props;
if (utils.isEventFromHandle(e, this.handlesRefs)) {
const handlePosition = utils.getHandleCenterPosition(isVertical, e.target);

const handlePosition = utils.getHandleCenterPosition(vertical, e.target);
this.dragOffset = 0;
this.onStart(handlePosition);
utils.pauseEvent(e);
if (onFocus) {
onFocus(e);
}
}
}

onBlur = (e) => {
const { onBlur } = this.props;
this.onEnd(e);
if (onBlur) {
onBlur(e);
}
};

addDocumentTouchEvents() {
Expand Down Expand Up @@ -192,6 +200,18 @@ export default function createSlider(Component) {
}
}

focus() {
if (!this.props.disabled) {
this.handlesRefs[0].focus();
}
}

blur() {
if (!this.props.disabled) {
this.handlesRefs[0].blur();
}
}

getSliderStart() {
const slider = this.sliderRef;
const rect = slider.getBoundingClientRect();
Expand Down
43 changes: 43 additions & 0 deletions tests/Range.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,4 +91,47 @@ describe('Range', () => {
wrapper.find('.rc-slider-handle').at(3).simulate('mouseLeave');
expect(wrapper.state().visibles[1]).toBe(false);
});

describe('focus & blur', () => {
let container;

beforeEach(() => {
container = document.createElement('div');
document.body.appendChild(container);
});

afterEach(() => {
document.body.removeChild(container);
});

const mockRect = (wrapper) => {
wrapper.instance().sliderRef.getBoundingClientRect = () => ({
left: 10,
width: 100,
});
};

it('focus()', () => {
const handleFocus = jest.fn();
const wrapper = mount(
<Range min={0} max={20} onFocus={handleFocus} />,
{ attachTo: container }
);
mockRect(wrapper);
wrapper.instance().focus();
expect(handleFocus).toBeCalled();
});

it('blur', () => {
const handleBlur = jest.fn();
const wrapper = mount(
<Range min={0} max={20} onBlur={handleBlur} />,
{ attachTo: container }
);
mockRect(wrapper);
wrapper.instance().focus();
wrapper.instance().blur();
expect(handleBlur).toBeCalled();
});
});
});
43 changes: 43 additions & 0 deletions tests/Slider.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,4 +101,47 @@ describe('Slider', () => {

expect(wrapper.state('value')).toBe(100);
});

describe('focus & blur', () => {
let container;

beforeEach(() => {
container = document.createElement('div');
document.body.appendChild(container);
});

afterEach(() => {
document.body.removeChild(container);
});

const mockRect = (wrapper) => {
wrapper.instance().sliderRef.getBoundingClientRect = () => ({
left: 10,
width: 100,
});
};

it('focus()', () => {
const handleFocus = jest.fn();
const wrapper = mount(
<Slider min={0} max={10} defaultValue={0} onFocus={handleFocus} />,
{ attachTo: container }
);
mockRect(wrapper);
wrapper.instance().focus();
expect(handleFocus).toBeCalled();
});

it('blur', () => {
const handleBlur = jest.fn();
const wrapper = mount(
<Slider min={0} max={10} defaultValue={0} onBlur={handleBlur} />,
{ attachTo: container }
);
mockRect(wrapper);
wrapper.instance().focus();
wrapper.instance().blur();
expect(handleBlur).toBeCalled();
});
});
});