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

Fix: changing month or day in post publish date closes the popover. #17201

Merged
Changes from 1 commit
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
33 changes: 31 additions & 2 deletions packages/components/src/date-time/date.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { DayPickerSingleDateController } from 'react-dates';
/**
* WordPress dependencies
*/
import { Component } from '@wordpress/element';
import { Component, createRef } from '@wordpress/element';

/**
* Module Constants
Expand All @@ -20,6 +20,30 @@ class DatePicker extends Component {
super( ...arguments );

this.onChangeMoment = this.onChangeMoment.bind( this );
this.nodeRef = createRef();
this.keepFocusInside = this.keepFocusInside.bind( this );
}

/*
* Todo: We should remove this function ASAP.
* It is kept because there is focus loose when we click the previous and next month buttons on react dates.
jorgefilipecosta marked this conversation as resolved.
Show resolved Hide resolved
* This focus lose then makes the date picker popover close.
jorgefilipecosta marked this conversation as resolved.
Show resolved Hide resolved
* Ideally we should add an upstream commit on react-dates to fix this issue.
*/
keepFocusInside() {
if ( ! this.nodeRef.current ) {
return;
}
// If focus was lost.
if ( ! document.activeElement || ! this.nodeRef.current.contains( document.activeElement ) ) {
// Retrieve the focus region div.
const focusRegion = this.nodeRef.current.querySelector( '.DayPicker_focusRegion' );
if ( ! focusRegion ) {
return;
}
// Keep the focus on focus region.
focusRegion.focus();
}
}

onChangeMoment( newDate ) {
Expand Down Expand Up @@ -56,7 +80,10 @@ class DatePicker extends Component {
const momentDate = this.getMomentDate( currentDate );

return (
<div className="components-datetime__date">
<div
className="components-datetime__date"
ref={ this.nodeRef }
>
<DayPickerSingleDateController
date={ momentDate }
daySize={ 30 }
Expand All @@ -74,6 +101,8 @@ class DatePicker extends Component {
isOutsideRange={ ( date ) => {
return isInvalidDate && isInvalidDate( date.toDate() );
} }
onPrevMonthClick={ this.keepFocusInside }
onNextMonthClick={ this.keepFocusInside }
/>
</div>
);
Expand Down