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: improve screen reader instructions for DatePicker #1166

Merged
merged 3 commits into from
Aug 13, 2020
Merged
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
54 changes: 43 additions & 11 deletions src/Calendar/Calendar.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class Calendar extends Component {
refocusGrid: props.focusOnInit,
currentDateDisplayed: currentDateDisplayed,
arrSelectedDates: props.enableRangeSelection ? selectedDateOrDates : [],
screenReaderText: '',
selectedDate: !props.enableRangeSelection ? selectedDateOrDates : null,
showMonths: false,
showYears: false,
Expand Down Expand Up @@ -166,6 +167,7 @@ class Calendar extends Component {

this.setState({
currentDateDisplayed: newDate,
screenReaderText: this.props.localizedText.dayInstructions,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be monthInstructions and the one below be yearInstructions?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is upon selection, so this is when it switches back to the day view.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah ok I see it now.

showMonths: false
});
}
Expand All @@ -175,6 +177,7 @@ class Calendar extends Component {

this.setState({
currentDateDisplayed: newDate,
screenReaderText: this.props.localizedText.dayInstructions,
showYears: false
});
}
Expand Down Expand Up @@ -353,23 +356,27 @@ class Calendar extends Component {

handleNext = () => {
const { currentDateDisplayed } = this.state;
const months = moment.localeData(this.props.locale).months();

if (this.state.showYears) {
const newDate = moment(currentDateDisplayed).add(12, 'year');
this.setState({ currentDateDisplayed: newDate });
this.setState({ currentDateDisplayed: newDate, screenReaderText: newDate.year() });
} else {
const newDate = moment(currentDateDisplayed).add(1, 'month');
this.setState({ currentDateDisplayed: newDate });
this.setState({ currentDateDisplayed: newDate, screenReaderText: `${months[newDate.month()]} ${newDate.year()}` });
}
}

handlePrevious = () => {
const { currentDateDisplayed } = this.state;
const months = moment.localeData(this.props.locale).months();

if (this.state.showYears) {
const newDate = moment(currentDateDisplayed).subtract(12, 'year');
this.setState({ currentDateDisplayed: newDate });
this.setState({ currentDateDisplayed: newDate, screenReaderText: newDate.year() });
} else {
const newDate = moment(currentDateDisplayed).subtract(1, 'month');
this.setState({ currentDateDisplayed: newDate });
this.setState({ currentDateDisplayed: newDate, screenReaderText: `${months[newDate.month()]} ${newDate.year()}` });
}
}

Expand Down Expand Up @@ -404,6 +411,7 @@ class Calendar extends Component {

this.setState({
currentDateDisplayed: day,
screenReaderText: isRangeEnabled ? this.props.localizedText.rangeInstructions : '',
selectedDate: day,
arrSelectedDates: selectedDates
}, function() {
Expand Down Expand Up @@ -457,7 +465,7 @@ class Calendar extends Component {

return (
<div className='fd-calendar__header'>
<div aria-live='assertive' className='fd-calendar__navigation'>
<div className='fd-calendar__navigation'>
<div className='fd-calendar__action'>
<Button
aria-label={previousButtonLabel}
Expand Down Expand Up @@ -693,12 +701,27 @@ class Calendar extends Component {
className={calendarClasses}
onKeyDown={(e) => this.onKeyDownCalendar(e)}>
{this.generateNavigation()}
<div className='fd-calendar__content'>
<div className='fd-calendar__content'
onBlur={(e) => {
if (!e.currentTarget.contains(e.relatedTarget)) {
this.setState({ screenReaderText: '' });
}
}}
onFocus={() => {
let instructions = localizedText.dayInstructions;
if (this.state.showYears) {
instructions = localizedText.yearInstructions;
} else if (this.state.showMonths) {
instructions = localizedText.monthInstructions;
}
this.setState({ screenReaderText: instructions });
}}>
{this._renderContent(monthListProps, yearListProps, tableProps, tableHeaderProps, tableBodyProps)}
</div>
</div>
<div aria-live='polite' className='fd-calendar__content fd-calendar__content--screen-reader-only'>
{localizedText.calendarInstructions}
<div aria-live='polite'
className='fd-calendar__content fd-calendar__content--screen-reader-only'>
{this.state.screenReaderText}
</div>
</>
);
Expand Down Expand Up @@ -741,8 +764,14 @@ Calendar.propTypes = {
locale: PropTypes.string,
/** Localized text to be updated based on location/language */
localizedText: CustomPropTypes.i18n({
/** Localized string informing screen reader users the calendar can be navigated by arrow keys */
calendarInstructions: PropTypes.string,
/** Localized string informing screen reader users the calendar can be navigated by arrow keys while in day view */
dayInstructions: PropTypes.string,
/** Localized string informing screen reader users the calendar can be navigated by arrow keys while in month view */
monthInstructions: PropTypes.string,
/** Localized string informing screen reader users the calendar can be navigated by arrow keys while in year view */
yearInstructions: PropTypes.string,
/** Localized string informing screen reader users to select a second date when in range selection */
rangeInstructions: PropTypes.string,
/** aria-label for next button */
nextMonth: PropTypes.string,
/** aria-label for previous button */
Expand Down Expand Up @@ -780,7 +809,10 @@ Calendar.defaultProps = {
compact: false,
locale: 'en',
localizedText: {
calendarInstructions: 'Use arrow keys to move between dates.',
dayInstructions: 'Use arrow keys to move between days.',
monthInstructions: 'Use arrow keys to move between months.',
yearInstructions: 'Use arrow keys to move between years.',
rangeInstructions: 'First date selected. Please select a second date.',
nextMonth: 'Next month',
previousMonth: 'Previous month',
show12NextYears: 'Show 12 next years',
Expand Down