-
Notifications
You must be signed in to change notification settings - Fork 2.2k
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: Screen reader does not announce selected date, current month #3817
Merged
martijnrusschen
merged 3 commits into
Hacker0x01:master
from
SpaNb4:fix/selected-date-announce
Jan 30, 2023
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -376,6 +376,7 @@ export default class DatePicker extends React.Component { | |
// used to focus day in inline version after month has changed, but not on | ||
// initial render | ||
shouldFocusDayInline: false, | ||
isRenderAriaLiveMessage: false, | ||
}; | ||
}; | ||
|
||
|
@@ -527,6 +528,7 @@ export default class DatePicker extends React.Component { | |
this.props.onChangeRaw(event); | ||
} | ||
this.setSelected(date, event, false, monthSelectedIn); | ||
this.setState({ isRenderAriaLiveMessage: true }); | ||
if (!this.props.shouldCloseOnSelect || this.props.showTimeSelect) { | ||
this.setPreSelection(date); | ||
} else if (!this.props.inline) { | ||
|
@@ -670,6 +672,9 @@ export default class DatePicker extends React.Component { | |
if (this.props.showTimeInput) { | ||
this.setOpen(true); | ||
} | ||
if (this.props.showTimeSelectOnly || this.props.showTimeSelect) { | ||
this.setState({ isRenderAriaLiveMessage: true }); | ||
} | ||
this.setState({ inputValue: null }); | ||
}; | ||
|
||
|
@@ -1015,6 +1020,75 @@ export default class DatePicker extends React.Component { | |
); | ||
}; | ||
|
||
renderAriaLiveRegion = () => { | ||
const { dateFormat, locale } = this.props; | ||
const isContainsTime = | ||
this.props.showTimeInput || this.props.showTimeSelect; | ||
const longDateFormat = isContainsTime ? "PPPPp" : "PPPP"; | ||
let ariaLiveMessage; | ||
|
||
if (this.props.selectsRange) { | ||
ariaLiveMessage = `Selected start date: ${safeDateFormat( | ||
this.props.startDate, | ||
{ | ||
dateFormat: longDateFormat, | ||
locale, | ||
} | ||
)}. ${ | ||
this.props.endDate | ||
? "End date: " + | ||
safeDateFormat(this.props.endDate, { | ||
dateFormat: longDateFormat, | ||
locale, | ||
}) | ||
: "" | ||
}`; | ||
} else { | ||
if (this.props.showTimeSelectOnly) { | ||
ariaLiveMessage = `Selected time: ${safeDateFormat( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hardcoded english texts will ruin accessibility for all non-english users. This prefix should be diffferent prop. |
||
this.props.selected, | ||
{ dateFormat, locale } | ||
)}`; | ||
} else if (this.props.showYearPicker) { | ||
ariaLiveMessage = `Selected year: ${safeDateFormat( | ||
this.props.selected, | ||
{ dateFormat: "yyyy", locale } | ||
)}`; | ||
} else if (this.props.showMonthYearPicker) { | ||
ariaLiveMessage = `Selected month: ${safeDateFormat( | ||
this.props.selected, | ||
{ dateFormat: "MMMM yyyy", locale } | ||
)}`; | ||
} else if (this.props.showQuarterYearPicker) { | ||
ariaLiveMessage = `Selected quarter: ${safeDateFormat( | ||
this.props.selected, | ||
{ | ||
dateFormat: "yyyy, QQQ", | ||
locale, | ||
} | ||
)}`; | ||
} else { | ||
ariaLiveMessage = `Selected date: ${safeDateFormat( | ||
this.props.selected, | ||
{ | ||
dateFormat: longDateFormat, | ||
locale, | ||
} | ||
)}`; | ||
} | ||
} | ||
|
||
return ( | ||
<span | ||
role="alert" | ||
aria-live="polite" | ||
className="react-datepicker__aria-live" | ||
> | ||
{this.state.isRenderAriaLiveMessage && ariaLiveMessage} | ||
</span> | ||
); | ||
}; | ||
|
||
renderDateInput = () => { | ||
const className = classnames(this.props.className, { | ||
[outsideClickIgnoreClass]: this.state.open, | ||
|
@@ -1096,6 +1170,7 @@ export default class DatePicker extends React.Component { | |
renderInputContainer() { | ||
return ( | ||
<div className="react-datepicker__input-container"> | ||
{this.renderAriaLiveRegion()} | ||
{this.renderDateInput()} | ||
{this.renderClearButton()} | ||
</div> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't this span be optional?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed, this ariaLiveRegion should be optional
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is this the double Month Year we see now?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah noo, this is already covered here :( #3924
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree, this should be optional. This is showing up in my application and I cannot opt out of showing the "Selected date:" text.