Skip to content

add: readonly attributes for CalendarCell #8072

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
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: 5 additions & 1 deletion packages/@react-aria/calendar/src/useCalendarCell.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ export interface CalendarCellAria {
isPressed: boolean,
/** Whether the cell is selected. */
isSelected: boolean,
/** Whether the cell is read only. */
isReadOnly: boolean,
/** Whether the cell is focused. */
isFocused: boolean,
/**
Expand Down Expand Up @@ -305,7 +307,8 @@ export function useCalendarCell(props: AriaCalendarCellProps, state: CalendarSta
role: 'gridcell',
'aria-disabled': !isSelectable || undefined,
'aria-selected': isSelected || undefined,
'aria-invalid': isInvalid || undefined
'aria-invalid': isInvalid || undefined,
'aria-readonly': state.isReadOnly || undefined
},
buttonProps: mergeProps(pressProps, {
onFocus() {
Expand Down Expand Up @@ -343,6 +346,7 @@ export function useCalendarCell(props: AriaCalendarCellProps, state: CalendarSta
}),
isPressed,
isFocused,
isReadOnly: state.isReadOnly,
isSelected,
isDisabled,
isUnavailable,
Expand Down
8 changes: 7 additions & 1 deletion packages/react-aria-components/src/Calendar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,11 @@ export interface CalendarCellRenderProps {
* @selector [data-selected]
*/
isSelected: boolean,
/**
* Whether the cell is read only.
* @selector [data-readonly]
*/
isReadOnly: boolean,
/**
* Whether the cell is the first date in a range selection.
* @selector [data-selection-start]
Expand Down Expand Up @@ -541,7 +546,8 @@ export const CalendarCell = /*#__PURE__*/ (forwardRef as forwardRefType)(functio
'data-selected': states.isSelected || undefined,
'data-selection-start': isSelectionStart || undefined,
'data-selection-end': isSelectionEnd || undefined,
'data-invalid': states.isInvalid || undefined
'data-invalid': states.isInvalid || undefined,
'data-readonly': states.isReadOnly || undefined
};

return (
Expand Down
12 changes: 12 additions & 0 deletions packages/react-aria-components/test/Calendar.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,18 @@ describe('Calendar', () => {
expect(cell).toHaveClass('disabled');
});

it('should support read only state', () => {
let {getByRole} = renderCalendar({isReadOnly: true}, {}, {className: ({isReadOnly}) => isReadOnly ? 'readonly' : ''});
let grid = getByRole('grid');
let cell = within(grid).getAllByRole('gridcell')[7];
let button = within(cell).getByRole('button');


expect(cell).toHaveAttribute('aria-readonly', 'true');
expect(button).toHaveAttribute('data-readonly');
expect(button).toHaveClass('readonly');
});

it('should support invalid state', () => {
let {getByRole} = renderCalendar({isInvalid: true, value: startOfWeek(startOfMonth(today(getLocalTimeZone())), 'en-US').add({days: 7})}, {}, {className: ({isInvalid}) => isInvalid ? 'invalid' : ''});
let grid = getByRole('grid');
Expand Down