Skip to content
This repository has been archived by the owner on Jul 12, 2024. It is now read-only.

Commit

Permalink
Fix keyboard navigation of DayPicker elements.
Browse files Browse the repository at this point in the history
Add prop for specifying an element that's allowed to take focus from DateRange.
  • Loading branch information
jeffstieler committed Oct 22, 2019
1 parent e1f0f1a commit d03971b
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
28 changes: 28 additions & 0 deletions packages/components/src/calendar/date-range.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ class DateRange extends Component {
return;
}

const { losesFocusTo } = this.props;

// Blur triggered internal to the DayPicker component.
if (
CONTAINER_DIV === blurSource &&
Expand All @@ -75,6 +77,27 @@ class DateRange extends Component {
)
)
) {
// Allow other DayPicker elements to take focus.
if (
e.relatedTarget &&
(
e.relatedTarget.classList.contains( 'DayPickerNavigation_button' ) ||
e.relatedTarget.classList.contains( 'CalendarDay' )
)
) {
return;
}

// Allow elements inside a specified ref to take focus.
if (
e.relatedTarget &&
losesFocusTo &&
losesFocusTo.contains( e.relatedTarget )
) {
return;
}

console.log( 'looks like a mouseUp() blur', e );
// DayPickerNavigation or CalendarDay mouseUp() is blurring,
// so switch focus to the DayPicker's focus region.
const focusRegion = this.nodeRef.current.querySelector( '.DayPicker_focusRegion' );
Expand Down Expand Up @@ -274,6 +297,11 @@ DateRange.propTypes = {
* The date format in moment.js-style tokens.
*/
shortDateFormat: PropTypes.string.isRequired,
/**
* A ref that the DateRange can lose focus to.
* See: https://github.com/woocommerce/woocommerce-admin/pull/2929.
*/
losesFocusTo: PropTypes.instanceOf( Element ),
};

export default withViewportMatch( {
Expand Down
5 changes: 4 additions & 1 deletion packages/components/src/date-range-filter-picker/content.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* External dependencies
*/
import { __ } from '@wordpress/i18n';
import { Component, Fragment } from '@wordpress/element';
import { Component, createRef, Fragment } from '@wordpress/element';
import { TabPanel, Button } from '@wordpress/components';
import PropTypes from 'prop-types';
import classnames from 'classnames';
Expand All @@ -21,6 +21,7 @@ class DatePickerContent extends Component {
constructor() {
super();
this.onTabSelect = this.onTabSelect.bind( this );
this.controlsRef = createRef();
}
onTabSelect( tab ) {
const { onUpdate, period } = this.props;
Expand Down Expand Up @@ -101,12 +102,14 @@ class DatePickerContent extends Component {
afterError={ afterError }
beforeError={ beforeError }
shortDateFormat={ shortDateFormat }
losesFocusTo={ this.controlsRef.current }
/>
) }
<div
className={ classnames( 'woocommerce-filters-date__content-controls', {
'is-custom': selected.name === 'custom',
} ) }
ref={ this.controlsRef }
>
<H className="woocommerce-filters-date__text">
{ __( 'compare to', 'woocommerce-admin' ) }
Expand Down

0 comments on commit d03971b

Please sign in to comment.