Skip to content

Commit 1a00eaa

Browse files
authored
React Stately docs for DatePicker and Calendar (#2948)
1 parent b778c8d commit 1a00eaa

22 files changed

+452
-69
lines changed

packages/@react-aria/datepicker/docs/useDateField.mdx

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ input via the `aria-describedby` attribute.
7474

7575
Note that most of this anatomy is shared with [useTimeField](useTimeField.html), so you can reuse many components between them if you have both.
7676

77-
State is managed by the <TypeLink links={statelyDocs.links} type={statelyDocs.exports.useDatePickerFieldState} /> hook from `@react-stately/datepicker`.
77+
State is managed by the <TypeLink links={statelyDocs.links} type={statelyDocs.exports.useDateFieldState} /> hook from `@react-stately/datepicker`.
7878
The state object should be passed as an option to `useDateField` and `useDateSegment`.
7979

8080
If the date field does not have a visible label, an `aria-label` or `aria-labelledby` prop must be passed instead to
@@ -86,19 +86,19 @@ Dates and times are represented in many different ways by cultures around the wo
8686

8787
<TypeLink links={docs.links} type={docs.exports.useDateField} /> uses the [@internationalized/date](../internationalized/date/) library to represent dates and times. This package provides a library of objects and functions to perform date and time related manipulation, queries, and conversions that work across locales and calendars. Date and time objects can be converted to and from native JavaScript `Date` objects or [ISO 8601](https://en.wikipedia.org/wiki/ISO_8601) strings. See the [documentation](../internationalized/date/), or the [examples below](#value) for more details.
8888

89-
<TypeLink links={statelyDocs.links} type={statelyDocs.exports.useDatePickerFieldState} /> requires a `createCalendar` function to be provided, which is used to implement date manipulation across multiple calendar systems. The default implementation in `@internationalized/date` includes all supported calendar systems. While this library is quite small (8 kB minified + Brotli), you can reduce its bundle size further by providing your own implementation that includes only your supported calendars. See [below](#reducing-bundle-size) for an example.
89+
<TypeLink links={statelyDocs.links} type={statelyDocs.exports.useDateFieldState} /> requires a `createCalendar` function to be provided, which is used to implement date manipulation across multiple calendar systems. The default implementation in `@internationalized/date` includes all supported calendar systems. While this library is quite small (8 kB minified + Brotli), you can reduce its bundle size further by providing your own implementation that includes only your supported calendars. See [below](#reducing-bundle-size) for an example.
9090

9191
## Example
9292

9393
```tsx example export=true
94-
import {useDatePickerFieldState} from '@react-stately/datepicker';
94+
import {useDateFieldState} from '@react-stately/datepicker';
9595
import {useDateField, useDateSegment} from '@react-aria/datepicker';
9696
import {createCalendar} from '@internationalized/date';
9797
import {useLocale} from '@react-aria/i18n';
9898

9999
export function DateField(props) {
100100
let {locale} = useLocale();
101-
let state = useDatePickerFieldState({
101+
let state = useDateFieldState({
102102
...props,
103103
locale,
104104
createCalendar
@@ -388,14 +388,14 @@ By default, `useDateField` displays times in either 12 or 24 hour hour format de
388388

389389
### Reducing bundle size
390390

391-
In the example above, the <TypeLink links={i18nDocs.links} type={i18nDocs.exports.createCalendar} /> function from the [@internationalized/date](../internationalized/date/) package is passed to the <TypeLink links={statelyDocs.links} type={statelyDocs.exports.useDatePickerFieldState} /> hook. This function receives a [calendar identifier](../internationalized/date/Calendar.html#calendar-identifiers) string, and provides <TypeLink links={i18nDocs.links} type={i18nDocs.exports.Calendar} /> instances to React Stately, which are used to implement date manipulation.
391+
In the example above, the <TypeLink links={i18nDocs.links} type={i18nDocs.exports.createCalendar} /> function from the [@internationalized/date](../internationalized/date/) package is passed to the <TypeLink links={statelyDocs.links} type={statelyDocs.exports.useDateFieldState} /> hook. This function receives a [calendar identifier](../internationalized/date/Calendar.html#calendar-identifiers) string, and provides <TypeLink links={i18nDocs.links} type={i18nDocs.exports.Calendar} /> instances to React Stately, which are used to implement date manipulation.
392392

393393
By default, this includes [all calendar systems](../internationalized/date/Calendar.html#implementations) supported by `@internationalized/date`. However, if your application supports a more limited set of regions, or you know you will only be picking dates in a certain calendar system, you can reduce your bundle size by providing your own implementation of `createCalendar` that includes a subset of these `Calendar` implementations.
394394

395395
For example, if your application only supports Gregorian dates, you could implement a `createCalendar` function like this:
396396

397397
```jsx
398-
import {useDatePickerFieldState} from '@react-stately/datepicker';
398+
import {useDateFieldState} from '@react-stately/datepicker';
399399
import {useLocale} from '@react-aria/i18n';
400400
import {GregorianCalendar} from '@internationalized/date';
401401

@@ -410,7 +410,7 @@ function createCalendar(identifier) {
410410

411411
export function DateField(props) {
412412
let {locale} = useLocale();
413-
let state = useDatePickerFieldState({
413+
let state = useDateFieldState({
414414
...props,
415415
locale,
416416
createCalendar

packages/@react-aria/datepicker/docs/useDatePicker.mdx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -201,12 +201,12 @@ The `DateField` component implements the keyboard editable input used in a `Date
201201

202202
```tsx example export=true render=false
203203
import {useLocale} from '@react-aria/i18n';
204-
import {useDatePickerFieldState} from '@react-stately/datepicker';
204+
import {useDateFieldState} from '@react-stately/datepicker';
205205
import {useDateField, useDateSegment} from '@react-aria/datepicker';
206206

207207
function DateField(props) {
208208
let {locale} = useLocale();
209-
let state = useDatePickerFieldState({
209+
let state = useDateFieldState({
210210
...props,
211211
locale,
212212
createCalendar

packages/@react-aria/datepicker/docs/useDateRangePicker.mdx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -208,12 +208,12 @@ The `DateField` component implements the keyboard editable inputs used in a `Dat
208208

209209
```tsx example export=true render=false
210210
import {useLocale} from '@react-aria/i18n';
211-
import {useDatePickerFieldState} from '@react-stately/datepicker';
211+
import {useDateFieldState} from '@react-stately/datepicker';
212212
import {useDateField, useDateSegment} from '@react-aria/datepicker';
213213

214214
function DateField(props) {
215215
let {locale} = useLocale();
216-
let state = useDatePickerFieldState({
216+
let state = useDateFieldState({
217217
...props,
218218
locale,
219219
createCalendar

packages/@react-aria/datepicker/src/useDateField.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,10 @@
1212

1313
import {AriaDatePickerProps, AriaTimeFieldProps, DateValue, TimeValue} from '@react-types/datepicker';
1414
import {createFocusManager, FocusManager} from '@react-aria/focus';
15-
import {DatePickerFieldState} from '@react-stately/datepicker';
15+
import {DateFieldState} from '@react-stately/datepicker';
1616
import {focusManagerSymbol} from './useDateRangePicker';
1717
import {HTMLAttributes, RefObject, useEffect, useMemo, useRef} from 'react';
1818
import {mergeProps, useDescription} from '@react-aria/utils';
19-
import {useDateFormatter} from '@react-aria/i18n';
2019
import {useDatePickerGroup} from './useDatePickerGroup';
2120
import {useField} from '@react-aria/label';
2221
import {useFocusWithin} from '@react-aria/interactions';
@@ -42,14 +41,14 @@ interface HookData {
4241
focusManager: FocusManager
4342
}
4443

45-
export const hookData = new WeakMap<DatePickerFieldState, HookData>();
44+
export const hookData = new WeakMap<DateFieldState, HookData>();
4645

4746
/**
4847
* Provides the behavior and accessibility implementation for a date field component.
4948
* A date field allows users to enter and edit date and time values using a keyboard.
5049
* Each part of a date value is displayed in an individually editable segment.
5150
*/
52-
export function useDateField<T extends DateValue>(props: DateFieldProps<T>, state: DatePickerFieldState, ref: RefObject<HTMLElement>): DateFieldAria {
51+
export function useDateField<T extends DateValue>(props: DateFieldProps<T>, state: DateFieldState, ref: RefObject<HTMLElement>): DateFieldAria {
5352
let {labelProps, fieldProps, descriptionProps, errorMessageProps} = useField({
5453
...props,
5554
labelElementType: 'span'
@@ -63,8 +62,7 @@ export function useDateField<T extends DateValue>(props: DateFieldProps<T>, stat
6362
}
6463
});
6564

66-
let formatter = useDateFormatter(state.getFormatOptions({month: 'long'}));
67-
let descProps = useDescription(state.value ? formatter.format(state.dateValue) : null);
65+
let descProps = useDescription(state.formatValue({month: 'long'}));
6866

6967
let segmentLabelledBy = fieldProps['aria-labelledby'] || fieldProps.id;
7068
let describedBy = [descProps['aria-describedby'], fieldProps['aria-describedby']].filter(Boolean).join(' ') || undefined;
@@ -108,6 +106,6 @@ export function useDateField<T extends DateValue>(props: DateFieldProps<T>, stat
108106
* A time field allows users to enter and edit time values using a keyboard.
109107
* Each part of a time value is displayed in an individually editable segment.
110108
*/
111-
export function useTimeField<T extends TimeValue>(props: AriaTimeFieldProps<T>, state: DatePickerFieldState, ref: RefObject<HTMLElement>): DateFieldAria {
109+
export function useTimeField<T extends TimeValue>(props: AriaTimeFieldProps<T>, state: DateFieldState, ref: RefObject<HTMLElement>): DateFieldAria {
112110
return useDateField(props, state, ref);
113111
}

packages/@react-aria/datepicker/src/useDatePickerGroup.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
import {DatePickerFieldState, DatePickerState, DateRangePickerState} from '@react-stately/datepicker';
1+
import {DateFieldState, DatePickerState, DateRangePickerState} from '@react-stately/datepicker';
22
import {getFocusableTreeWalker} from '@react-aria/focus';
33
import {KeyboardEvent} from '@react-types/shared';
44
import {mergeProps} from '@react-aria/utils';
55
import {RefObject} from 'react';
66
import {usePress} from '@react-aria/interactions';
77

8-
export function useDatePickerGroup(state: DatePickerState | DateRangePickerState | DatePickerFieldState, ref: RefObject<HTMLElement>) {
8+
export function useDatePickerGroup(state: DatePickerState | DateRangePickerState | DateFieldState, ref: RefObject<HTMLElement>) {
99
// Open the popover on alt + arrow down
1010
let onKeyDown = (e: KeyboardEvent) => {
1111
if (e.altKey && e.key === 'ArrowDown' && 'setOpen' in state) {

packages/@react-aria/datepicker/src/useDateSegment.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
* governing permissions and limitations under the License.
1111
*/
1212

13-
import {DatePickerFieldState, DateSegment} from '@react-stately/datepicker';
13+
import {DateFieldState, DateSegment} from '@react-stately/datepicker';
1414
import {getScrollParent, isIOS, isMac, mergeProps, scrollIntoView, useEvent, useId} from '@react-aria/utils';
1515
import {hookData} from './useDateField';
1616
import {NumberParser} from '@internationalized/number';
@@ -30,7 +30,7 @@ interface DateSegmentAria {
3030
* A date segment displays an individual unit of a date and time, and allows users to edit
3131
* the value by typing or using the arrow keys to increment and decrement.
3232
*/
33-
export function useDateSegment(segment: DateSegment, state: DatePickerFieldState, ref: RefObject<HTMLElement>): DateSegmentAria {
33+
export function useDateSegment(segment: DateSegment, state: DateFieldState, ref: RefObject<HTMLElement>): DateSegmentAria {
3434
let enteredKeys = useRef('');
3535
let {locale, direction} = useLocale();
3636
let displayNames = useDisplayNames();

packages/@react-spectrum/datepicker/src/DateField.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import {Field} from '@react-spectrum/label';
1919
import {Input} from './Input';
2020
import React, {useRef} from 'react';
2121
import {useDateField} from '@react-aria/datepicker';
22-
import {useDatePickerFieldState} from '@react-stately/datepicker';
22+
import {useDateFieldState} from '@react-stately/datepicker';
2323
import {useFormatHelpText} from './utils';
2424
import {useLocale} from '@react-aria/i18n';
2525
import {useProviderProps} from '@react-spectrum/provider';
@@ -40,7 +40,7 @@ export function DateField<T extends DateValue>(props: SpectrumDateFieldProps<T>)
4040

4141
let ref = useRef();
4242
let {locale} = useLocale();
43-
let state = useDatePickerFieldState({
43+
let state = useDateFieldState({
4444
...props,
4545
locale,
4646
createCalendar

packages/@react-spectrum/datepicker/src/DatePickerField.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import datepickerStyles from './index.css';
1717
import {DateValue, SpectrumDatePickerProps} from '@react-types/datepicker';
1818
import React, {useRef} from 'react';
1919
import {useDateField} from '@react-aria/datepicker';
20-
import {useDatePickerFieldState} from '@react-stately/datepicker';
20+
import {useDateFieldState} from '@react-stately/datepicker';
2121
import {useLocale} from '@react-aria/i18n';
2222

2323
interface DatePickerFieldProps<T extends DateValue> extends SpectrumDatePickerProps<T> {
@@ -35,7 +35,7 @@ export function DatePickerField<T extends DateValue>(props: DatePickerFieldProps
3535
} = props;
3636
let ref = useRef();
3737
let {locale} = useLocale();
38-
let state = useDatePickerFieldState({
38+
let state = useDateFieldState({
3939
...props,
4040
locale,
4141
createCalendar

packages/@react-spectrum/datepicker/src/DatePickerSegment.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
*/
1212

1313
import {classNames} from '@react-spectrum/utils';
14+
import {DateFieldState, DateSegment} from '@react-stately/datepicker';
1415
import {DatePickerBase, DateValue} from '@react-types/datepicker';
15-
import {DatePickerFieldState, DateSegment} from '@react-stately/datepicker';
1616
import {NumberParser} from '@internationalized/number';
1717
import React, {useMemo, useRef} from 'react';
1818
import styles from './index.css';
@@ -21,7 +21,7 @@ import {useLocale} from '@react-aria/i18n';
2121

2222
interface DatePickerSegmentProps extends DatePickerBase<DateValue> {
2323
segment: DateSegment,
24-
state: DatePickerFieldState
24+
state: DateFieldState
2525
}
2626

2727
interface LiteralSegmentProps {
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
{/* Copyright 2020 Adobe. All rights reserved.
2+
This file is licensed to you under the Apache License, Version 2.0 (the "License");
3+
you may not use this file except in compliance with the License. You may obtain a copy
4+
of the License at http://www.apache.org/licenses/LICENSE-2.0
5+
Unless required by applicable law or agreed to in writing, software distributed under
6+
the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
7+
OF ANY KIND, either express or implied. See the License for the specific language
8+
governing permissions and limitations under the License. */}
9+
10+
import {Layout} from '@react-spectrum/docs';
11+
export default Layout;
12+
13+
import docs from 'docs:@react-stately/calendar';
14+
import {ClassAPI, HeaderInfo, TypeContext, FunctionAPI, TypeLink, PageDescription} from '@react-spectrum/docs';
15+
import packageData from '@react-stately/calendar/package.json';
16+
17+
---
18+
category: Date and Time
19+
keywords: [date, calendar, state]
20+
---
21+
22+
# useCalendarState
23+
24+
<PageDescription>{docs.exports.useCalendarState.description}</PageDescription>
25+
26+
<HeaderInfo
27+
packageData={packageData}
28+
componentNames={['useCalendarState']} />
29+
30+
## API
31+
32+
<FunctionAPI function={docs.exports.useCalendarState} links={docs.links} />
33+
34+
## Interface
35+
36+
<ClassAPI links={docs.links} class={docs.links[docs.exports.useCalendarState.return.id]} />
37+
38+
## Example
39+
40+
See the docs for [useCalendar](/react-aria/useCalendar.html) in react-aria for an example of `useCalendarState`.

0 commit comments

Comments
 (0)