@react-aria/calendar 3.0.0-nightly.3142 → 3.0.0-nightly.3148

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -17,20 +17,45 @@ import {HTMLAttributes, RefObject, useEffect, useMemo, useRef} from 'react';
17
17
  // @ts-ignore
18
18
  import intlMessages from '../intl/*.json';
19
19
  import {mergeProps} from '@react-aria/utils';
20
- import {PressProps, usePress} from '@react-aria/interactions';
21
20
  import {useDateFormatter, useMessageFormatter} from '@react-aria/i18n';
21
+ import {usePress} from '@react-aria/interactions';
22
22
 
23
23
  export interface AriaCalendarCellProps {
24
+ /** The date that this cell represents. */
24
25
  date: CalendarDate,
26
+ /**
27
+ * Whether the cell is disabled. By default, this is determined by the
28
+ * Calendar's `minValue`, `maxValue`, and `isDisabled` props.
29
+ */
25
30
  isDisabled?: boolean
26
31
  }
27
32
 
28
33
  interface CalendarCellAria {
29
- cellProps: PressProps & HTMLAttributes<HTMLElement>,
34
+ /** Props for the grid cell element (e.g. `<td>`). */
35
+ cellProps: HTMLAttributes<HTMLElement>,
36
+ /** Props for the button element within the cell. */
30
37
  buttonProps: HTMLAttributes<HTMLElement>,
31
- isPressed: boolean
38
+ /** Whether the cell is currently being pressed. */
39
+ isPressed: boolean,
40
+ /** Whether the cell is selected. */
41
+ isSelected: boolean,
42
+ /** Whether the cell is focused. */
43
+ isFocused: boolean,
44
+ /** Whether the cell is disabled. */
45
+ isDisabled: boolean,
46
+ /**
47
+ * Whether the cell is outside the visible range of the calendar.
48
+ * For example, dates before the first day of a month in the same week.
49
+ */
50
+ isOutsideVisibleRange: boolean,
51
+ /** The day number formatted according to the current locale. */
52
+ formattedDate: string
32
53
  }
33
54
 
55
+ /**
56
+ * Provides the behavior and accessibility implementation for a calendar cell component.
57
+ * A calendar cell displays a date cell within a calendar grid which can be selected by the user.
58
+ */
34
59
  export function useCalendarCell(props: AriaCalendarCellProps, state: CalendarState | RangeCalendarState, ref: RefObject<HTMLElement>): CalendarCellAria {
35
60
  let {date, isDisabled} = props;
36
61
  let formatMessage = useMessageFormatter(intlMessages);
@@ -204,6 +229,14 @@ export function useCalendarCell(props: AriaCalendarCellProps, state: CalendarSta
204
229
  }
205
230
  }, [isFocused, ref]);
206
231
 
232
+ let cellDateFormatter = useDateFormatter({
233
+ day: 'numeric',
234
+ timeZone: state.timeZone,
235
+ calendar: date.calendar.identifier
236
+ });
237
+
238
+ let formattedDate = useMemo(() => cellDateFormatter.format(nativeDate), [cellDateFormatter, nativeDate]);
239
+
207
240
  return {
208
241
  cellProps: {
209
242
  role: 'gridcell',
@@ -239,6 +272,11 @@ export function useCalendarCell(props: AriaCalendarCellProps, state: CalendarSta
239
272
  e.preventDefault();
240
273
  }
241
274
  }),
242
- isPressed
275
+ isPressed,
276
+ isFocused,
277
+ isSelected,
278
+ isDisabled,
279
+ isOutsideVisibleRange: date.compare(state.visibleRange.start) < 0 || date.compare(state.visibleRange.end) > 0,
280
+ formattedDate
243
281
  };
244
282
  }
@@ -10,24 +10,36 @@
10
10
  * governing permissions and limitations under the License.
11
11
  */
12
12
 
13
- import {CalendarDate} from '@internationalized/date';
13
+ import {CalendarDate, startOfWeek} from '@internationalized/date';
14
14
  import {CalendarGridAria} from './types';
15
15
  import {calendarIds, useSelectedDateDescription, useVisibleRangeDescription} from './utils';
16
- import {CalendarPropsBase} from '@react-types/calendar';
17
16
  import {CalendarState, RangeCalendarState} from '@react-stately/calendar';
18
17
  import {KeyboardEvent} from 'react';
19
18
  import {mergeProps, useDescription, useLabels} from '@react-aria/utils';
20
- import {useLocale} from '@react-aria/i18n';
19
+ import {useDateFormatter, useLocale} from '@react-aria/i18n';
21
20
 
22
- interface CalendarGridProps extends CalendarPropsBase {
21
+ interface CalendarGridProps {
22
+ /**
23
+ * The first date displayed in the calendar grid.
24
+ * Defaults to the first visible date in the calendar.
25
+ * Override this to display multiple date grids in a calendar.
26
+ */
23
27
  startDate?: CalendarDate,
28
+ /**
29
+ * The last date displayed in the calendar grid.
30
+ * Defaults to the last visible date in the calendar.
31
+ * Override this to display multiple date grids in a calendar.
32
+ */
24
33
  endDate?: CalendarDate
25
34
  }
26
35
 
36
+ /**
37
+ * Provides the behavior and accessibility implementation for a calendar grid component.
38
+ * A calendar grid displays a single grid of days within a calendar or range calendar which
39
+ * can be keyboard navigated and selected by the user.
40
+ */
27
41
  export function useCalendarGrid(props: CalendarGridProps, state: CalendarState | RangeCalendarState): CalendarGridAria {
28
42
  let {
29
- isReadOnly = false,
30
- isDisabled = false,
31
43
  startDate = state.visibleRange.start,
32
44
  endDate = state.visibleRange.end
33
45
  } = props;
@@ -108,15 +120,31 @@ export function useCalendarGrid(props: CalendarGridProps, state: CalendarState |
108
120
  'aria-labelledby': calendarIds.get(state)
109
121
  });
110
122
 
123
+ let dayFormatter = useDateFormatter({weekday: 'narrow'});
124
+ let dayFormatterLong = useDateFormatter({weekday: 'long'});
125
+ let {locale} = useLocale();
126
+ let weekStart = startOfWeek(state.visibleRange.start, locale);
127
+ let weekDays = [...new Array(7).keys()].map((index) => {
128
+ let date = weekStart.add({days: index});
129
+ let dateDay = date.toDate(state.timeZone);
130
+ let narrow = dayFormatter.format(dateDay);
131
+ let long = dayFormatterLong.format(dateDay);
132
+ return {
133
+ narrow,
134
+ long
135
+ };
136
+ });
137
+
111
138
  return {
112
139
  gridProps: mergeProps(descriptionProps, labelProps, {
113
140
  role: 'grid',
114
- 'aria-readonly': isReadOnly || null,
115
- 'aria-disabled': isDisabled || null,
141
+ 'aria-readonly': state.isReadOnly || null,
142
+ 'aria-disabled': state.isDisabled || null,
116
143
  'aria-multiselectable': ('highlightedRange' in state) || undefined,
117
144
  onKeyDown,
118
145
  onFocus: () => state.setFocused(true),
119
146
  onBlur: () => state.setFocused(false)
120
- })
147
+ }),
148
+ weekDays
121
149
  };
122
150
  }
@@ -17,6 +17,10 @@ import {RefObject, useRef} from 'react';
17
17
  import {useCalendarBase} from './useCalendarBase';
18
18
  import {useEvent, useId} from '@react-aria/utils';
19
19
 
20
+ /**
21
+ * Provides the behavior and accessibility implementation for a range calendar component.
22
+ * A range calendar displays one or more date grids and allows users to select a contiguous range of dates.
23
+ */
20
24
  export function useRangeCalendar<T extends DateValue>(props: RangeCalendarProps<T>, state: RangeCalendarState, ref: RefObject<HTMLElement>): CalendarAria {
21
25
  let res = useCalendarBase(props, state);
22
26
  res.nextButtonProps.id = useId();
@@ -1,11 +0,0 @@
1
- interface CalendarTableHeaderAria {
2
- columnHeaderProps: {scope?: 'col'}
3
- }
4
-
5
- export function useCalendarTableHeader(): CalendarTableHeaderAria {
6
- return {
7
- columnHeaderProps: {
8
- scope: 'col'
9
- }
10
- };
11
- }