Skip to content
Open
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
6 changes: 3 additions & 3 deletions src/date-picker/__tests__/date-picker-calendar.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,9 @@ describe('Date picker calendar', () => {
expect(findCalendarHeaderText(wrapper)).toBe('translated');
// For each calendar we render 6 weeks (42 days) and each requires a label.
// Additionally, we generate short and full labels for weekday names (14 in total),
// and 2 labels for month name.
// 42 + 14 + 2 = 58.
expect(localStringMock).toHaveBeenCalledTimes(58);
// and 2 labels for month name, and 1 label for the selected date button aria-label.
// 42 + 14 + 2 + 1 = 59.
expect(localStringMock).toHaveBeenCalledTimes(59);
expect(localStringMock).toHaveBeenCalledWith(locale, expect.any(Object));
});

Expand Down
34 changes: 34 additions & 0 deletions src/date-picker/__tests__/date-picker.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -422,3 +422,37 @@ describe('generates aria-label for the "Open calendar" button', () => {
expect(button).toHaveAttribute('aria-label', 'Choose Date');
});
});

describe('generates default aria-label for the "Open calendar" button when openCalendarAriaLabel is not provided', () => {
test('Without a selected date', () => {
const { wrapper } = renderDatePicker({
...defaultProps,
openCalendarAriaLabel: undefined,
locale: 'en-US',
});
const button = wrapper.findOpenCalendarButton().getElement();
expect(button).toHaveAttribute('aria-label', 'Choose Date');
});

test('With a selected date', () => {
const { wrapper } = renderDatePicker({
...defaultProps,
openCalendarAriaLabel: undefined,
value: '2003-04-11',
locale: 'en-US',
});
const button = wrapper.findOpenCalendarButton().getElement();
expect(button).toHaveAttribute('aria-label', 'Choose Date, selected date is Friday, April 11, 2003');
});

test('With an incomplete date', () => {
const { wrapper } = renderDatePicker({
...defaultProps,
openCalendarAriaLabel: undefined,
value: '2003-04-0',
locale: 'en-US',
});
const button = wrapper.findOpenCalendarButton().getElement();
expect(button).toHaveAttribute('aria-label', 'Choose Date');
});
});
18 changes: 11 additions & 7 deletions src/date-picker/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -123,13 +123,17 @@ const DatePicker = React.forwardRef(

const hasFullValue = isValidFullDate({ date: value, granularity });

const buttonAriaLabel =
openCalendarAriaLabel &&
openCalendarAriaLabel(
hasFullValue && parsedValue
? getSelectedDateLabel({ date: parsedValue, granularity, locale: normalizedLocale })
: null
);
const selectedDateLabel =
hasFullValue && parsedValue
? getSelectedDateLabel({ date: parsedValue, granularity, locale: normalizedLocale })
: null;

let buttonAriaLabel: string;
if (openCalendarAriaLabel) {
buttonAriaLabel = openCalendarAriaLabel(selectedDateLabel);
} else {
buttonAriaLabel = selectedDateLabel ? `Choose Date, selected date is ${selectedDateLabel}` : 'Choose Date';
}

const trigger = (
<div className={styles['date-picker-trigger']}>
Expand Down
Loading