Skip to content

Commit 384c9cb

Browse files
authored
fix: improve screen reader instructions for DatePicker (#1166)
* fix: improve screen reader instructions * fix: separate instructions for month and year * fix: range instructions
1 parent 0c37279 commit 384c9cb

File tree

1 file changed

+43
-11
lines changed

1 file changed

+43
-11
lines changed

src/Calendar/Calendar.js

Lines changed: 43 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ class Calendar extends Component {
3737
refocusGrid: props.focusOnInit,
3838
currentDateDisplayed: currentDateDisplayed,
3939
arrSelectedDates: props.enableRangeSelection ? selectedDateOrDates : [],
40+
screenReaderText: '',
4041
selectedDate: !props.enableRangeSelection ? selectedDateOrDates : null,
4142
showMonths: false,
4243
showYears: false,
@@ -166,6 +167,7 @@ class Calendar extends Component {
166167

167168
this.setState({
168169
currentDateDisplayed: newDate,
170+
screenReaderText: this.props.localizedText.dayInstructions,
169171
showMonths: false
170172
});
171173
}
@@ -175,6 +177,7 @@ class Calendar extends Component {
175177

176178
this.setState({
177179
currentDateDisplayed: newDate,
180+
screenReaderText: this.props.localizedText.dayInstructions,
178181
showYears: false
179182
});
180183
}
@@ -353,23 +356,27 @@ class Calendar extends Component {
353356

354357
handleNext = () => {
355358
const { currentDateDisplayed } = this.state;
359+
const months = moment.localeData(this.props.locale).months();
360+
356361
if (this.state.showYears) {
357362
const newDate = moment(currentDateDisplayed).add(12, 'year');
358-
this.setState({ currentDateDisplayed: newDate });
363+
this.setState({ currentDateDisplayed: newDate, screenReaderText: newDate.year() });
359364
} else {
360365
const newDate = moment(currentDateDisplayed).add(1, 'month');
361-
this.setState({ currentDateDisplayed: newDate });
366+
this.setState({ currentDateDisplayed: newDate, screenReaderText: `${months[newDate.month()]} ${newDate.year()}` });
362367
}
363368
}
364369

365370
handlePrevious = () => {
366371
const { currentDateDisplayed } = this.state;
372+
const months = moment.localeData(this.props.locale).months();
373+
367374
if (this.state.showYears) {
368375
const newDate = moment(currentDateDisplayed).subtract(12, 'year');
369-
this.setState({ currentDateDisplayed: newDate });
376+
this.setState({ currentDateDisplayed: newDate, screenReaderText: newDate.year() });
370377
} else {
371378
const newDate = moment(currentDateDisplayed).subtract(1, 'month');
372-
this.setState({ currentDateDisplayed: newDate });
379+
this.setState({ currentDateDisplayed: newDate, screenReaderText: `${months[newDate.month()]} ${newDate.year()}` });
373380
}
374381
}
375382

@@ -404,6 +411,7 @@ class Calendar extends Component {
404411

405412
this.setState({
406413
currentDateDisplayed: day,
414+
screenReaderText: isRangeEnabled ? this.props.localizedText.rangeInstructions : '',
407415
selectedDate: day,
408416
arrSelectedDates: selectedDates
409417
}, function() {
@@ -457,7 +465,7 @@ class Calendar extends Component {
457465

458466
return (
459467
<div className='fd-calendar__header'>
460-
<div aria-live='assertive' className='fd-calendar__navigation'>
468+
<div className='fd-calendar__navigation'>
461469
<div className='fd-calendar__action'>
462470
<Button
463471
aria-label={previousButtonLabel}
@@ -693,12 +701,27 @@ class Calendar extends Component {
693701
className={calendarClasses}
694702
onKeyDown={(e) => this.onKeyDownCalendar(e)}>
695703
{this.generateNavigation()}
696-
<div className='fd-calendar__content'>
704+
<div className='fd-calendar__content'
705+
onBlur={(e) => {
706+
if (!e.currentTarget.contains(e.relatedTarget)) {
707+
this.setState({ screenReaderText: '' });
708+
}
709+
}}
710+
onFocus={() => {
711+
let instructions = localizedText.dayInstructions;
712+
if (this.state.showYears) {
713+
instructions = localizedText.yearInstructions;
714+
} else if (this.state.showMonths) {
715+
instructions = localizedText.monthInstructions;
716+
}
717+
this.setState({ screenReaderText: instructions });
718+
}}>
697719
{this._renderContent(monthListProps, yearListProps, tableProps, tableHeaderProps, tableBodyProps)}
698720
</div>
699721
</div>
700-
<div aria-live='polite' className='fd-calendar__content fd-calendar__content--screen-reader-only'>
701-
{localizedText.calendarInstructions}
722+
<div aria-live='polite'
723+
className='fd-calendar__content fd-calendar__content--screen-reader-only'>
724+
{this.state.screenReaderText}
702725
</div>
703726
</>
704727
);
@@ -741,8 +764,14 @@ Calendar.propTypes = {
741764
locale: PropTypes.string,
742765
/** Localized text to be updated based on location/language */
743766
localizedText: CustomPropTypes.i18n({
744-
/** Localized string informing screen reader users the calendar can be navigated by arrow keys */
745-
calendarInstructions: PropTypes.string,
767+
/** Localized string informing screen reader users the calendar can be navigated by arrow keys while in day view */
768+
dayInstructions: PropTypes.string,
769+
/** Localized string informing screen reader users the calendar can be navigated by arrow keys while in month view */
770+
monthInstructions: PropTypes.string,
771+
/** Localized string informing screen reader users the calendar can be navigated by arrow keys while in year view */
772+
yearInstructions: PropTypes.string,
773+
/** Localized string informing screen reader users to select a second date when in range selection */
774+
rangeInstructions: PropTypes.string,
746775
/** aria-label for next button */
747776
nextMonth: PropTypes.string,
748777
/** aria-label for previous button */
@@ -780,7 +809,10 @@ Calendar.defaultProps = {
780809
compact: false,
781810
locale: 'en',
782811
localizedText: {
783-
calendarInstructions: 'Use arrow keys to move between dates.',
812+
dayInstructions: 'Use arrow keys to move between days.',
813+
monthInstructions: 'Use arrow keys to move between months.',
814+
yearInstructions: 'Use arrow keys to move between years.',
815+
rangeInstructions: 'First date selected. Please select a second date.',
784816
nextMonth: 'Next month',
785817
previousMonth: 'Previous month',
786818
show12NextYears: 'Show 12 next years',

0 commit comments

Comments
 (0)