@react-stately/datepicker 3.0.0-nightly.3175 → 3.0.0-nightly.3180

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.
package/src/index.ts CHANGED
@@ -11,6 +11,6 @@
11
11
  */
12
12
 
13
13
  export * from './useDatePickerState';
14
- export * from './useDatePickerFieldState';
14
+ export * from './useDateFieldState';
15
15
  export * from './useDateRangePickerState';
16
16
  export * from './useTimeFieldState';
@@ -10,42 +10,79 @@
10
10
  * governing permissions and limitations under the License.
11
11
  */
12
12
 
13
- import {Calendar, CalendarDateTime, DateFormatter, getMinimumDayInMonth, getMinimumMonthInYear, GregorianCalendar, toCalendar} from '@internationalized/date';
13
+ import {Calendar, DateFormatter, getMinimumDayInMonth, getMinimumMonthInYear, GregorianCalendar, toCalendar} from '@internationalized/date';
14
14
  import {convertValue, createPlaceholderDate, FieldOptions, getFormatOptions, isInvalid, useDefaultProps} from './utils';
15
15
  import {DatePickerProps, DateValue, Granularity} from '@react-types/datepicker';
16
16
  import {useControlledState} from '@react-stately/utils';
17
17
  import {useEffect, useMemo, useRef, useState} from 'react';
18
18
  import {ValidationState} from '@react-types/shared';
19
19
 
20
+ export type SegmentType = 'era' | 'year' | 'month' | 'day' | 'hour' | 'minute' | 'second' | 'dayPeriod' | 'literal' | 'timeZoneName';
20
21
  export interface DateSegment {
21
- type: Intl.DateTimeFormatPartTypes,
22
+ /** The type of segment. */
23
+ type: SegmentType,
24
+ /** The formatted text for the segment. */
22
25
  text: string,
26
+ /** The numeric value for the segment, if applicable. */
23
27
  value?: number,
28
+ /** The minimum numeric value for the segment, if applicable. */
24
29
  minValue?: number,
30
+ /** The maximum numeric value for the segment, if applicable. */
25
31
  maxValue?: number,
32
+ /** Whether the value is a placeholder. */
26
33
  isPlaceholder: boolean,
34
+ /** Whether the segment is editable. */
27
35
  isEditable: boolean
28
36
  }
29
37
 
30
- export interface DatePickerFieldState {
38
+ export interface DateFieldState {
39
+ /** The current field value. */
31
40
  value: DateValue,
41
+ /** The current value, converted to a native JavaScript `Date` object. */
32
42
  dateValue: Date,
33
- setValue: (value: CalendarDateTime) => void,
43
+ /** Sets the field's value. */
44
+ setValue(value: DateValue): void,
45
+ /** A list of segments for the current value. */
34
46
  segments: DateSegment[],
47
+ /** A date formatter configured for the current locale and format. */
35
48
  dateFormatter: DateFormatter,
49
+ /** The current validation state of the date field, based on the `validationState`, `minValue`, and `maxValue` props. */
36
50
  validationState: ValidationState,
51
+ /** The granularity for the field, based on the `granularity` prop and current value. */
37
52
  granularity: Granularity,
53
+ /** Whether the field is disabled. */
38
54
  isDisabled: boolean,
55
+ /** Whether the field is read only. */
39
56
  isReadOnly: boolean,
57
+ /** Whether the field is required. */
40
58
  isRequired: boolean,
41
- increment: (type: Intl.DateTimeFormatPartTypes) => void,
42
- decrement: (type: Intl.DateTimeFormatPartTypes) => void,
43
- incrementPage: (type: Intl.DateTimeFormatPartTypes) => void,
44
- decrementPage: (type: Intl.DateTimeFormatPartTypes) => void,
45
- setSegment: (type: Intl.DateTimeFormatPartTypes, value: number) => void,
46
- confirmPlaceholder: (type?: Intl.DateTimeFormatPartTypes) => void,
47
- clearSegment: (type?: Intl.DateTimeFormatPartTypes) => void,
48
- getFormatOptions(fieldOptions: FieldOptions): Intl.DateTimeFormatOptions
59
+ /** Increments the given segment. Upon reaching the minimum or maximum value, the value wraps around to the opposite limit. */
60
+ increment(type: SegmentType): void,
61
+ /** Decrements the given segment. Upon reaching the minimum or maximum value, the value wraps around to the opposite limit. */
62
+ decrement(type: SegmentType): void,
63
+ /**
64
+ * Increments the given segment by a larger amount, rounding it to the nearest increment.
65
+ * The amount to increment by depends on the field, for example 15 minutes, 7 days, and 5 years.
66
+ * Upon reaching the minimum or maximum value, the value wraps around to the opposite limit.
67
+ */
68
+ incrementPage(type: SegmentType): void,
69
+ /**
70
+ * Decrements the given segment by a larger amount, rounding it to the nearest increment.
71
+ * The amount to decrement by depends on the field, for example 15 minutes, 7 days, and 5 years.
72
+ * Upon reaching the minimum or maximum value, the value wraps around to the opposite limit.
73
+ */
74
+ decrementPage(type: SegmentType): void,
75
+ /** Sets the value of the given segment. */
76
+ setSegment(type: SegmentType, value: number): void,
77
+ /**
78
+ * Replaces the value of the date field with the placeholder value.
79
+ * If a segment type is provided, only that segment is confirmed. Otherwise, all segments that have not been entered yet are confirmed.
80
+ */
81
+ confirmPlaceholder(type?: SegmentType): void,
82
+ /** Clears the value of the given segment, reverting it to the placeholder. */
83
+ clearSegment(type: SegmentType): void,
84
+ /** Formats the current date value using the given options. */
85
+ formatValue(fieldOptions: FieldOptions): string
49
86
  }
50
87
 
51
88
  const EDITABLE_SEGMENTS = {
@@ -73,13 +110,29 @@ const TYPE_MAPPING = {
73
110
  dayperiod: 'dayPeriod'
74
111
  };
75
112
 
76
- interface DatePickerFieldProps<T extends DateValue> extends DatePickerProps<T> {
77
- maxGranularity?: 'year' | 'month' | DatePickerProps<T>['granularity'],
113
+ interface DatePickerFieldProps extends DatePickerProps<DateValue> {
114
+ /**
115
+ * The maximum unit to display in the date field.
116
+ * @default 'year'
117
+ */
118
+ maxGranularity?: 'year' | 'month' | Granularity,
119
+ /** The locale to display and edit the value according to. */
78
120
  locale: string,
121
+ /**
122
+ * A function that creates a [Calendar](../internationalized/date/Calendar.html)
123
+ * object for a given calendar identifier. Such a function may be imported from the
124
+ * `@internationalized/date` package, or manually implemented to include support for
125
+ * only certain calendars.
126
+ */
79
127
  createCalendar: (name: string) => Calendar
80
128
  }
81
129
 
82
- export function useDatePickerFieldState<T extends DateValue>(props: DatePickerFieldProps<T>): DatePickerFieldState {
130
+ /**
131
+ * Provides state management for a date field component.
132
+ * A date field allows users to enter and edit date and time values using a keyboard.
133
+ * Each part of a date value is displayed in an individually editable segment.
134
+ */
135
+ export function useDateFieldState(props: DatePickerFieldProps): DateFieldState {
83
136
  let {
84
137
  locale,
85
138
  createCalendar,
@@ -287,8 +340,14 @@ export function useDatePickerFieldState<T extends DateValue>(props: DatePickerFi
287
340
  setDate(null);
288
341
  setValue(value);
289
342
  },
290
- getFormatOptions(fieldOptions: FieldOptions) {
291
- return getFormatOptions(fieldOptions, formatOpts);
343
+ formatValue(fieldOptions: FieldOptions) {
344
+ if (!calendarValue) {
345
+ return '';
346
+ }
347
+
348
+ let formatOptions = getFormatOptions(fieldOptions, formatOpts);
349
+ let formatter = new DateFormatter(locale, formatOptions);
350
+ return formatter.format(dateValue);
292
351
  }
293
352
  };
294
353
  }
@@ -27,20 +27,42 @@ export interface DatePickerOptions extends DatePickerProps<DateValue> {
27
27
  }
28
28
 
29
29
  export interface DatePickerState {
30
+ /** The currently selected date. */
30
31
  value: DateValue,
31
- setValue: (value: DateValue) => void,
32
+ /** Sets the selected date. */
33
+ setValue(value: DateValue): void,
34
+ /**
35
+ * The date portion of the value. This may be set prior to `value` if the user has
36
+ * selected a date but has not yet selected a time.
37
+ */
32
38
  dateValue: DateValue,
33
- setDateValue: (value: CalendarDate) => void,
39
+ /** Sets the date portion of the value. */
40
+ setDateValue(value: CalendarDate): void,
41
+ /**
42
+ * The time portion of the value. This may be set prior to `value` if the user has
43
+ * selected a time but has not yet selected a date.
44
+ */
34
45
  timeValue: TimeValue,
35
- setTimeValue: (value: TimeValue) => void,
46
+ /** Sets the time portion of the value. */
47
+ setTimeValue(value: TimeValue): void,
48
+ /** The granularity for the field, based on the `granularity` prop and current value. */
49
+ granularity: Granularity,
50
+ /** Whether the date picker supports selecting a time, according to the `granularity` prop and current value. */
36
51
  hasTime: boolean,
52
+ /** Whether the calendar popover is currently open. */
37
53
  isOpen: boolean,
38
- setOpen: (isOpen: boolean) => void,
54
+ /** Sets whether the calendar popover is open. */
55
+ setOpen(isOpen: boolean): void,
56
+ /** The current validation state of the date picker, based on the `validationState`, `minValue`, and `maxValue` props. */
39
57
  validationState: ValidationState,
40
- formatValue(locale: string, fieldOptions: FieldOptions): string,
41
- granularity: Granularity
58
+ /** Formats the selected value using the given options. */
59
+ formatValue(locale: string, fieldOptions: FieldOptions): string
42
60
  }
43
61
 
62
+ /**
63
+ * Provides state management for a date picker component.
64
+ * A date picker combines a DateField and a Calendar popover to allow users to enter or select a date and time value.
65
+ */
44
66
  export function useDatePickerState(props: DatePickerOptions): DatePickerState {
45
67
  let [isOpen, setOpen] = useState(false);
46
68
  let [value, setValue] = useControlledState<DateValue>(props.value, props.defaultValue || null, props.onChange);
@@ -27,24 +27,51 @@ export interface DateRangePickerOptions extends DateRangePickerProps<DateValue>
27
27
 
28
28
  type TimeRange = RangeValue<TimeValue>;
29
29
  export interface DateRangePickerState {
30
+ /** The currently selected date range. */
30
31
  value: DateRange,
31
- setValue: (value: DateRange) => void,
32
- setDate: (part: keyof DateRange, value: DateValue) => void,
33
- setTime: (part: keyof TimeRange, value: TimeValue) => void,
34
- setDateTime: (part: keyof DateRange, value: DateValue) => void,
32
+ /** Sets the selected date range. */
33
+ setValue(value: DateRange): void,
34
+ /**
35
+ * The date portion of the selected range. This may be set prior to `value` if the user has
36
+ * selected a date range but has not yet selected a time range.
37
+ */
35
38
  dateRange: DateRange,
36
- setDateRange: (value: DateRange) => void,
39
+ /** Sets the date portion of the selected range. */
40
+ setDateRange(value: DateRange): void,
41
+ /**
42
+ * The time portion of the selected range. This may be set prior to `value` if the user has
43
+ * selected a time range but has not yet selected a date range.
44
+ */
37
45
  timeRange: TimeRange,
38
- setTimeRange: (value: TimeRange) => void,
46
+ /** Sets the time portion of the selected range. */
47
+ setTimeRange(value: TimeRange): void,
48
+ /** Sets the date portion of either the start or end of the selected range. */
49
+ setDate(part: 'start' | 'end', value: DateValue): void,
50
+ /** Sets the time portion of either the start or end of the selected range. */
51
+ setTime(part: 'start' | 'end', value: TimeValue): void,
52
+ /** Sets the date and time of either the start or end of the selected range. */
53
+ setDateTime(part: 'start' | 'end', value: DateValue): void,
54
+ /** The granularity for the field, based on the `granularity` prop and current value. */
55
+ granularity: Granularity,
56
+ /** Whether the date range picker supports selecting times, according to the `granularity` prop and current value. */
39
57
  hasTime: boolean,
58
+ /** Whether the calendar popover is currently open. */
40
59
  isOpen: boolean,
41
- setOpen: (isOpen: boolean) => void,
60
+ /** Sets whether the calendar popover is open. */
61
+ setOpen(isOpen: boolean): void,
62
+ /** The current validation state of the date picker, based on the `validationState`, `minValue`, and `maxValue` props. */
42
63
  validationState: ValidationState,
64
+ /** Formats the selected range using the given options. */
43
65
  formatValue(locale: string, fieldOptions: FieldOptions): string,
44
- confirmPlaceholder(): void,
45
- granularity: Granularity
66
+ /** Replaces the start and/or end value of the selected range with the placeholder value if unentered. */
67
+ confirmPlaceholder(): void
46
68
  }
47
69
 
70
+ /**
71
+ * Provides state management for a date range picker component.
72
+ * A date range picker combines two DateFields and a RangeCalendar popover to allow
73
+ * users to enter or select a date and time range.
74
+ */
48
75
  export function useDateRangePickerState(props: DateRangePickerOptions): DateRangePickerState {
49
76
  let [isOpen, setOpen] = useState(false);
50
77
  let [controlledValue, setControlledValue] = useControlledState<DateRange>(props.value, props.defaultValue || null, props.onChange);
@@ -10,17 +10,23 @@
10
10
  * governing permissions and limitations under the License.
11
11
  */
12
12
 
13
+ import {DateFieldState, useDateFieldState} from '.';
13
14
  import {DateValue, TimePickerProps, TimeValue} from '@react-types/datepicker';
14
15
  import {getLocalTimeZone, GregorianCalendar, Time, toCalendarDateTime, today, toTime} from '@internationalized/date';
15
16
  import {useControlledState} from '@react-stately/utils';
16
- import {useDatePickerFieldState} from '.';
17
17
  import {useMemo} from 'react';
18
18
 
19
- interface TimeFieldProps<T extends TimeValue> extends TimePickerProps<T> {
19
+ interface TimeFieldProps extends TimePickerProps<TimeValue> {
20
+ /** The locale to display and edit the value according to. */
20
21
  locale: string
21
22
  }
22
23
 
23
- export function useTimeFieldState<T extends TimeValue>(props: TimeFieldProps<T>) {
24
+ /**
25
+ * Provides state management for a time field component.
26
+ * A time field allows users to enter and edit time values using a keyboard.
27
+ * Each part of a time value is displayed in an individually editable segment.
28
+ */
29
+ export function useTimeFieldState(props: TimeFieldProps): DateFieldState {
24
30
  let {
25
31
  placeholderValue = new Time(),
26
32
  minValue,
@@ -45,7 +51,7 @@ export function useTimeFieldState<T extends TimeValue>(props: TimeFieldProps<T>)
45
51
  setValue(v && 'day' in v ? newValue : newValue && toTime(newValue));
46
52
  };
47
53
 
48
- return useDatePickerFieldState({
54
+ return useDateFieldState({
49
55
  ...props,
50
56
  value: dateTime,
51
57
  defaultValue: undefined,