Skip to content

Datepicker maintain focus prop #2391

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

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
14 changes: 13 additions & 1 deletion components/component-docs.json
Original file line number Diff line number Diff line change
Expand Up @@ -5890,12 +5890,24 @@
"returns": null
},
{
"name": "openDialog",
"name": "openDialogFromIcon",
"docblock": null,
"modifiers": [],
"params": [],
"returns": null
},
{
"name": "openDialog",
"docblock": null,
"modifiers": [],
"params": [
{
"name": "isRequestFromIcon",
"type": null
}
],
"returns": null
},
{
"name": "parseDate",
"docblock": null,
Expand Down
25 changes: 21 additions & 4 deletions components/date-picker/date-picker.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,7 @@ class Datepicker extends React.Component {

this.state = {
isOpen: false,
isOpenFromIcon: false,
value: props.value,
formattedValue: initDate || '',
inputValue: initDate || '',
Expand Down Expand Up @@ -309,6 +310,7 @@ class Datepicker extends React.Component {
this.props.assistiveTextPreviousMonth || assistiveText.previousMonth // eslint-disable-line react/prop-types
}
assistiveTextYear={assistiveText.year}
canFocusCalendar={this.state.isOpenFromIcon}
id={this.getId()}
isIsoWeekday={this.props.isIsoWeekday}
monthLabels={
Expand Down Expand Up @@ -401,7 +403,9 @@ class Datepicker extends React.Component {
aria-expanded={this.getIsOpen()}
category="utility"
name="event"
onClick={this.openDialog}
onClick={() => {
this.openDialogFromIcon();
}}
type="button"
/>
),
Expand Down Expand Up @@ -524,6 +528,11 @@ class Datepicker extends React.Component {
this.setState({ isOpen: true });
}

if (event.keyCode === KEYS.ESCAPE || event.keyCode === KEYS.ENTER) {
EventUtil.trapEvent(event);
this.setState({ isOpen: false });
}

// Please remove `onKeyDown` on the next breaking change.
/* eslint-disable react/prop-types */
if (this.props.onKeyDown) {
Expand All @@ -537,7 +546,7 @@ class Datepicker extends React.Component {
this.props.onOpen(event, { portal });
}

if (this.selectedDateCell) {
if (this.selectedDateCell && this.state.isOpenFromIcon) {
this.selectedDateCell.focus();
}
};
Expand All @@ -548,15 +557,23 @@ class Datepicker extends React.Component {
}

if (this.getIsOpen()) {
this.setState({ isOpen: false });
this.setState({ isOpen: false, isOpenFromIcon: false });

if (this.inputRef) {
this.inputRef.focus();
}
}
};

openDialog = () => {
openDialogFromIcon = () => {
this.setState({ isOpenFromIcon: true });
this.openDialog(true);
};

openDialog = (isRequestFromIcon = false) => {
if (!isRequestFromIcon) {
this.setState({ isOpenFromIcon: false });
}
if (this.props.onRequestOpen) {
this.props.onRequestOpen();
} else {
Expand Down
13 changes: 5 additions & 8 deletions components/date-picker/private/calendar-wrapper.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ class DatepickerCalendarWrapper extends React.Component {
* One letter abbreviations of the days of the week, starting on Sunday.
*/,
abbreviatedWeekDayLabels: PropTypes.array.isRequired,
/**
* Whether or not the `CalendarWrapper` can steal focus from the main `Input`
*/
canFocusCalendar: PropTypes.bool.isRequired,
/**
* CSS classes to be added to tag with `slds-datepicker`.
*/
Expand Down Expand Up @@ -108,26 +112,21 @@ class DatepickerCalendarWrapper extends React.Component {

state = {
initialDateForCalendarRender: this.props.selectedDate,
isCalendarFocused: true,
};

handleCalendarBlur = (event, { direction }) => {
if (direction === 'next' && this.previousMonthRef) {
this.setState({ isCalendarFocused: false });
if (this.props.onCalendarFocus) {
this.props.onCalendarFocus(event, {
direction,
isCalendarFocused: false,
ref: this.previousMonthRef,
});
}
this.previousMonthRef.focus();
} else if (direction === 'previous' && this.todayRef) {
this.setState({ isCalendarFocused: false });
if (this.props.onCalendarFocus) {
this.props.onCalendarFocus(event, {
direction,
isCalendarFocused: false,
ref: this.todayRef,
});
}
Expand All @@ -138,7 +137,6 @@ class DatepickerCalendarWrapper extends React.Component {
handleFirstFocusableNodeKeyDown = (event) => {
if (event.shiftKey && event.keyCode === KEYS.TAB) {
EventUtil.trapEvent(event);
this.setState({ isCalendarFocused: true });
}
};

Expand All @@ -159,7 +157,6 @@ class DatepickerCalendarWrapper extends React.Component {
handleLastFocusableNodeKeyDown = (event) => {
if (!event.shiftKey && event.keyCode === KEYS.TAB) {
EventUtil.trapEvent(event);
this.setState({ isCalendarFocused: true });
}
};

Expand All @@ -171,7 +168,7 @@ class DatepickerCalendarWrapper extends React.Component {

handleRequestFocusDate = (event, data) => {
// will be called three times, due to re-render
if (data.ref && this.state.isCalendarFocused) {
if (data.ref && this.props.canFocusCalendar) {
data.ref.focus();
}

Expand Down