You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: packages/@react-aria/calendar/docs/useCalendar.mdx
+40-3Lines changed: 40 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -46,7 +46,7 @@ after_version: 3.0.0
46
46
47
47
There is no standalone calendar element in HTML. `<input type="date">` is close, but this is very limited in functionality, lacking in internationalization capabilities, inconsistent between browsers, and difficult to style. `useCalendar` helps achieve accessible and international calendar components that can be styled as needed.
48
48
49
-
***Flexible** – Display one or more months at once, or a custom time range for use cases like a week view.
49
+
***Flexible** – Display one or more months at once, or a custom time range for use cases like a week view. Minimum and maximum values, unavailable dates, and non-contiguous selections are supported as well.
50
50
***International** – Support for 13 calendar systems used around the world, including Gregorian, Buddhist, Islamic, Persian, and more. Locale-specific formatting, number systems, and right-to-left support are available as well.
51
51
***Accessible** – Calendar cells can be navigated and selected using the keyboard, and localized screen reader messages are included to announce when the selection and visible date range change.
52
52
***Customizable** – As with all of React Aria, the DOM structure and styling of all elements can be fully customized.
@@ -57,18 +57,24 @@ There is no standalone calendar element in HTML. `<input type="date">` is close,
57
57
58
58
A calendar consists of a grouping element containing one or more date grids (e.g. months), and a previous and next button for navigating between date ranges. Each calendar grid consists of cells containing button elements that can be pressed and navigated to using the arrow keys to select a date.
59
59
60
+
### useCalendar
61
+
60
62
`useCalendar` returns props that you should spread onto the appropriate elements:
`useCalendarGrid` returns props for an individual grid of dates, such as one month, along with a list of formatted weekday names in the current locale for use during rendering:
`useCalendarCell` returns props for an individual cell, along with states and information useful during rendering:
73
79
74
80
<TypeContext.Providervalue={docs.links}>
@@ -184,7 +190,7 @@ function CalendarGrid({state, ...props}) {
184
190
185
191
### CalendarCell
186
192
187
-
Finally, the `CalendarCell` component renders an individual cell in a calendar. It consists of two elements: a `<td>` to represent the grid cell, and a `<div>` to represent a button that can be clicked to select the date. The `useCalendarCell` hook also returns some information about the cell's state that can be useful for styling, as well as the formatted date string in the current locale.
193
+
Finally, the `CalendarCell` component renders an individual cell in a calendar. It consists of two elements: a `<td>` to represent the grid cell, and a `<div>` to represent a button that can be clicked to select the date. The `useCalendarCell` hook also returns the formatted date string in the current locale, as well as some information about the cell's state that can be useful for styling. See [above](#usecalendarcell) for details.
188
194
189
195
**Note**: this component is the same as the `CalendarCell` component shown in the [useRangeCalendar](useRangeCalendar.html) docs, and you can reuse it between both `Calendar` and `RangeCalendar`.
190
196
@@ -199,6 +205,7 @@ function CalendarCell({state, date}) {
199
205
isSelected,
200
206
isOutsideVisibleRange,
201
207
isDisabled,
208
+
isUnavailable,
202
209
formattedDate
203
210
} =useCalendarCell({date}, state, ref);
204
211
@@ -208,7 +215,7 @@ function CalendarCell({state, date}) {
`useCalendar` supports marking certain dates as _unavailable_. These dates remain focusable with the keyboard so that navigation is consistent, but cannot be selected by the user. In this example, they are displayed in red. The `isDateUnavailable` prop accepts a callback that is called to evaluate whether each visible date is unavailable.
399
+
400
+
This example includes multiple unavailable date ranges, e.g. dates when no appointments are available. In addition, all weekends are unavailable. The `minValue` prop is also used to prevent selecting dates before today.
By default, the selected date is focused when a `Calendar` first mounts. If no `value` or `defaultValue` prop is provided, then the current date is focused. However, `useCalendar` supports controlling which date is focused using the `focusedValue` and `onFocusChange` props. This also determines which month is visible. The `defaultFocusedValue` prop allows setting the initial focused date when the `Calendar` first mounts, without controlling it.
Copy file name to clipboardExpand all lines: packages/@react-aria/calendar/docs/useRangeCalendar.mdx
+57-3Lines changed: 57 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -45,7 +45,7 @@ after_version: 3.0.0
45
45
46
46
There is no standalone range calendar element in HTML. Two separate `<input type="date">` elements could be used, but this is very limited in functionality, lacking in internationalization capabilities, inconsistent between browsers, and difficult to style. `useRangeCalendar` helps achieve accessible and international range calendar components that can be styled as needed.
47
47
48
-
***Flexible** – Display one or more months at once, or a custom time range for use cases like a week view.
48
+
***Flexible** – Display one or more months at once, or a custom time range for use cases like a week view. Minimum and maximum values, unavailable dates, and non-contiguous selections are supported as well.
49
49
***International** – Support for 13 calendar systems used around the world, including Gregorian, Buddhist, Islamic, Persian, and more. Locale-specific formatting, number systems, and right-to-left support are available as well.
50
50
***Accessible** – Calendar cells can be navigated and selected using the keyboard, and localized screen reader messages are included to announce when the selection and visible date range change.
51
51
***Touch friendly** – Date ranges can be selected by dragging over dates in the calendar using a touch screen, and all interactions are accessible using touch-based screen readers.
@@ -57,18 +57,24 @@ There is no standalone range calendar element in HTML. Two separate `<input type
57
57
58
58
A range calendar consists of a grouping element containing one or more date grids (e.g. months), and a previous and next button for navigating through time. Each calendar grid consists of cells containing button elements that can be pressed and navigated to using the arrow keys to select a date range. Once a start date is selected, the user can navigate to another date using the keyboard or by hovering over it, and clicking it or pressing the <Keyboard>Enter</Keyboard> key commits the selected date range.
59
59
60
+
### useRangeCalendar
61
+
60
62
`useRangeCalendar` returns props that you should spread onto the appropriate elements:
`useCalendarGrid` returns props for an individual grid of dates, such as one month, along with a list of formatted weekday names in the current locale for use during rendering:
`useCalendarCell` returns props for an individual cell, along with states and information useful during rendering:
73
79
74
80
<TypeContext.Providervalue={docs.links}>
@@ -184,7 +190,7 @@ function CalendarGrid({state, ...props}) {
184
190
185
191
### CalendarCell
186
192
187
-
Finally, the `CalendarCell` component renders an individual cell in a calendar. It consists of two elements: a `<td>` to represent the grid cell, and a `<div>` to represent a button that can be clicked to select the date. The `useCalendarCell` hook also returns some information about the cell's state that can be useful for styling, as well as the formatted date string in the current locale.
193
+
Finally, the `CalendarCell` component renders an individual cell in a calendar. It consists of two elements: a `<td>` to represent the grid cell, and a `<div>` to represent a button that can be clicked to select the date. The `useCalendarCell` hook also returns the formatted date string in the current locale, as well as some information about the cell's state that can be useful for styling. See [above](#usecalendarcell) for details.
188
194
189
195
**Note**: this component is the same as the `CalendarCell` component shown in the [useCalendar](useCalendar.html) docs, and you can reuse it between both `Calendar` and `RangeCalendar`.
190
196
@@ -199,6 +205,7 @@ function CalendarCell({state, date}) {
199
205
isSelected,
200
206
isOutsideVisibleRange,
201
207
isDisabled,
208
+
isUnavailable,
202
209
formattedDate
203
210
} =useCalendarCell({date}, state, ref);
204
211
@@ -208,7 +215,7 @@ function CalendarCell({state, date}) {
`useRangeCalendar` supports marking certain dates as _unavailable_. These dates remain focusable with the keyboard so that navigation is consistent, but cannot be selected by the user. In this example, they are displayed in red. The `isDateUnavailable` prop accepts a callback that is called to evaluate whether each visible date is unavailable.
409
+
410
+
Note that by default, users may not select non-contiguous ranges, i.e. ranges that contain unavailable dates within them. Once a start date is selected, enabled dates will be restricted to subsequent dates until an unavailable date is hit. See [below](#non-contiguous-ranges) for an example of how to allow non-contiguous ranges.
411
+
412
+
This example includes multiple unavailable date ranges, e.g. dates when a rental house is not available. The `minValue` prop is also used to prevent selecting dates before today.
413
+
414
+
```tsx example
415
+
import {today} from'@internationalized/date';
416
+
import {useLocale} from'@react-aria/i18n';
417
+
418
+
function Example() {
419
+
let now =today(getLocalTimeZone());
420
+
let disabledRanges = [
421
+
[now, now.add({days: 5})],
422
+
[now.add({days: 14}), now.add({days: 16})],
423
+
[now.add({days: 23}), now.add({days: 24})],
424
+
];
425
+
426
+
let {locale} =useLocale();
427
+
let isDateUnavailable = (date) =>disabledRanges.some((interval) =>date.compare(interval[0]) >=0&&date.compare(interval[1]) <=0);
The `allowsNonContiguousRanges` prop enables a range to be selected even if there are unavailable dates in the middle. The value emitted in the `onChange` event will still be a single range with a `start` and `end` property, but unavailable dates will not be displayed as selected. It is up to applications to split the full selected range into multiple as needed for business logic.
436
+
437
+
This example prevents selecting weekends, but allows selecting ranges that span multiple weeks.
438
+
439
+
```tsx example
440
+
import {isWeekend} from'@internationalized/date';
441
+
442
+
function Example() {
443
+
let {locale} =useLocale();
444
+
445
+
return <RangeCalendararia-label="Time off request"isDateUnavailable={date=>isWeekend(date, locale)}allowsNonContiguousRanges />
446
+
}
447
+
```
448
+
395
449
### Controlling the focused date
396
450
397
451
By default, the first selected date is focused when a `RangeCalendar` first mounts. If no `value` or `defaultValue` prop is provided, then the current date is focused. However, `useRangeCalendar` supports controlling which date is focused using the `focusedValue` and `onFocusChange` props. This also determines which month is visible. The `defaultFocusedValue` prop allows setting the initial focused date when the `Calendar` first mounts, without controlling it.
0 commit comments