Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 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
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,15 @@ export enum Weekday {
Saturday
}

export interface CalendarMonthInlineProps {
/** Component wrapping the calendar month when used inline. Recommended to be 'article'. */
component?: keyof JSX.IntrinsicElements;
/** Title of the calendar rendered above the inline calendar month. Recommended to be a 'title' component. */
title?: React.ReactNode;
/** Id of the accessible label of the calendar month. Recommended to map to the title. */
ariaLabelledby?: string;
}

/** Additional properties that extend from and can be passed to the main component. These
* properties allow customizing the calendar formatting and aria-labels.
*/
Expand Down Expand Up @@ -49,6 +58,8 @@ export interface CalendarFormat {
weekStart?: 0 | 1 | 2 | 3 | 4 | 5 | 6 | Weekday;
/** Accessible label for the year input. */
yearInputAriaLabel?: string;
/** Props used to ensure accessibility when displaying the calendar month inline. */
inlineProps?: CalendarMonthInlineProps;
}

export interface CalendarProps extends CalendarFormat, Omit<React.HTMLProps<HTMLDivElement>, 'onChange'> {
Expand Down Expand Up @@ -133,6 +144,7 @@ export const CalendarMonth = ({
yearInputAriaLabel = 'Select year',
cellAriaLabel,
isDateFocused = false,
inlineProps,
...props
}: CalendarProps) => {
const longMonths = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11].map(monthNum => new Date(1990, monthNum)).map(monthFormat);
Expand Down Expand Up @@ -232,7 +244,8 @@ export const CalendarMonth = ({
const isHoveredDateValid = isValidated(hoveredDate);
const monthFormatted = monthFormat(focusedDate);
const yearFormatted = yearFormat(focusedDate);
return (

const calendarToRender = (
<div className={css(styles.calendarMonth, className)} {...props}>
<div className={styles.calendarMonthHeader}>
<div className={css(styles.calendarMonthHeaderNavControl, styles.modifiers.prevMonth)}>
Expand Down Expand Up @@ -386,4 +399,16 @@ export const CalendarMonth = ({
</table>
</div>
);

if (inlineProps !== undefined) {
const Component = (inlineProps.component ? inlineProps.component : 'article') as any;
return (
<Component {...(inlineProps.ariaLabelledby && { 'aria-labelledby': inlineProps.ariaLabelledby })}>
{inlineProps.title}
{calendarToRender}
</Component>
);
}
return calendarToRender;
};
CalendarMonth.displayName = 'CalendarMonth';
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,11 @@ test('Next year dates have correct year in aria label', () => {
const nextYearDate = screen.getByRole('button', { name: '1 January 2025' });
expect(nextYearDate).toBeVisible();
});

test('InlineProps render correct wrapper component and attributes', () => {
const { asFragment } = render(
<CalendarMonth inlineProps={{wrapperComponent: 'article', title: <div id="hi">Title</div>, ariaLabelledby: "hi"}} />
);

expect(asFragment()).toMatchSnapshot();
});
Loading