Skip to content

Commit

Permalink
Move focus to datepicker on readOnly input focus for DateRangePicker…
Browse files Browse the repository at this point in the history
…/SingleDatePicker
  • Loading branch information
aporlando authored and majapw committed Apr 12, 2018
1 parent 7218f44 commit 5169d47
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 5 deletions.
6 changes: 5 additions & 1 deletion src/components/DateRangePicker.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -204,14 +204,18 @@ class DateRangePicker extends React.Component {
onDateRangePickerInputFocus(focusedInput) {
const {
onFocusChange,
readOnly,
withPortal,
withFullScreenPortal,
keepFocusOnInput,
} = this.props;

if (focusedInput) {
const withAnyPortal = withPortal || withFullScreenPortal;
const moveFocusToDayPicker = withAnyPortal || (this.isTouchDevice && !keepFocusOnInput);
const moveFocusToDayPicker =
withAnyPortal ||
(readOnly && !keepFocusOnInput) ||
(this.isTouchDevice && !keepFocusOnInput);

if (moveFocusToDayPicker) {
this.onDayPickerFocus();
Expand Down
6 changes: 5 additions & 1 deletion src/components/SingleDatePicker.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -204,13 +204,17 @@ class SingleDatePicker extends React.Component {
const {
disabled,
onFocusChange,
readOnly,
withPortal,
withFullScreenPortal,
keepFocusOnInput,
} = this.props;

const withAnyPortal = withPortal || withFullScreenPortal;
const moveFocusToDayPicker = withAnyPortal || (this.isTouchDevice && !keepFocusOnInput);
const moveFocusToDayPicker =
withAnyPortal ||
(readOnly && !keepFocusOnInput) ||
(this.isTouchDevice && !keepFocusOnInput);

if (moveFocusToDayPicker) {
this.onDayPickerFocus();
Expand Down
17 changes: 15 additions & 2 deletions test/components/DateRangePicker_spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,19 @@ describe('DateRangePicker', () => {
expect(onDayPickerFocusSpy.callCount).to.equal(1);
});

it('calls onDayPickerFocus if focusedInput and readOnly', () => {
const wrapper = shallow((
<DateRangePicker
{...requiredProps}
onDatesChange={sinon.stub()}
onFocusChange={sinon.stub()}
readOnly
/>
)).dive();
wrapper.instance().onDateRangePickerInputFocus(START_DATE);
expect(onDayPickerFocusSpy.callCount).to.equal(1);
});

it('calls onDayPickerFocus if focusedInput and isTouchDevice', () => {
const wrapper = shallow((
<DateRangePicker
Expand All @@ -374,7 +387,7 @@ describe('DateRangePicker', () => {
expect(onDayPickerFocusSpy.callCount).to.equal(1);
});

it('calls onDayPickerBlur if focusedInput and !withPortal/!withFullScreenPortal and keepFocusOnInput', () => {
it('calls onDayPickerBlur if focusedInput and !withPortal/!withFullScreenPortal/!readOnly and keepFocusOnInput', () => {
const wrapper = shallow((
<DateRangePicker
{...requiredProps}
Expand Down Expand Up @@ -402,7 +415,7 @@ describe('DateRangePicker', () => {
expect(onDayPickerFocusSpy.callCount).to.equal(1);
});

it('calls onDayPickerBlur if focusedInput and !withPortal/!withFullScreenPortal', () => {
it('calls onDayPickerBlur if focusedInput and !withPortal/!withFullScreenPortal/!readOnly', () => {
const wrapper = shallow((
<DateRangePicker
{...requiredProps}
Expand Down
41 changes: 40 additions & 1 deletion test/components/SingleDatePicker_spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,18 @@ describe('SingleDatePicker', () => {
expect(onDayPickerFocusSpy.callCount).to.equal(1);
});

it('calls onDayPickerFocus if readOnly', () => {
const wrapper = shallow((
<SingleDatePicker
onDateChange={sinon.stub()}
onFocusChange={sinon.stub()}
readOnly
/>
)).dive();
wrapper.instance().onFocus();
expect(onDayPickerFocusSpy.callCount).to.equal(1);
});

it('calls onDayPickerFocus if isTouchDevice', () => {
const wrapper = shallow((
<SingleDatePicker
Expand All @@ -494,6 +506,19 @@ describe('SingleDatePicker', () => {
expect(onDayPickerFocusSpy.callCount).to.equal(1);
});

it('calls onDayPickerBlur if isTouchDevice and keepFocusOnInput', () => {
const wrapper = shallow((
<SingleDatePicker
onDateChange={sinon.stub()}
onFocusChange={sinon.stub()}
keepFocusOnInput
/>
)).dive();
wrapper.instance().isTouchDevice = true;
wrapper.instance().onFocus();
expect(onDayPickerBlurSpy.callCount).to.equal(1);
});

it('calls onDayPickerBlur if !withPortal/!withFullScreenPortal and keepFocusOnInput', () => {
const wrapper = shallow((
<SingleDatePicker
Expand All @@ -520,7 +545,21 @@ describe('SingleDatePicker', () => {
expect(onDayPickerFocusSpy.callCount).to.equal(1);
});

it('calls onDayPickerBlur if !withPortal/!withFullScreenPortal', () => {

it('calls onDayPickerFocus if readOnly and keepFocusOnInput', () => {
const wrapper = shallow((
<SingleDatePicker
onDateChange={sinon.stub()}
onFocusChange={sinon.stub()}
keepFocusOnInput
withFullScreenPortal
/>
)).dive();
wrapper.instance().onFocus();
expect(onDayPickerFocusSpy.callCount).to.equal(1);
});

it('calls onDayPickerBlur if !withPortal/!withFullScreenPortal/!readOnly', () => {
const wrapper = shallow((
<SingleDatePicker
onDateChange={sinon.stub()}
Expand Down

0 comments on commit 5169d47

Please sign in to comment.