@gravity-ui/date-components 2.1.0 → 2.2.1

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.
Files changed (58) hide show
  1. package/dist/cjs/components/DateField/hooks/useBaseDateFieldState.d.ts +2 -2
  2. package/dist/cjs/components/DateField/hooks/useDateFieldProps.d.ts +2 -2
  3. package/dist/cjs/components/DateField/hooks/useDateFieldState.d.ts +1 -2
  4. package/dist/cjs/components/DateField/utils.js +2 -1
  5. package/dist/cjs/components/DatePicker/DatePicker.d.ts +3 -2
  6. package/dist/cjs/components/DatePicker/hooks/datePickerStateFactory.d.ts +49 -0
  7. package/dist/cjs/components/DatePicker/hooks/datePickerStateFactory.js +121 -0
  8. package/dist/cjs/components/DatePicker/hooks/useDatePickerProps.d.ts +9 -5
  9. package/dist/cjs/components/DatePicker/hooks/useDatePickerProps.js +2 -1
  10. package/dist/cjs/components/DatePicker/hooks/useDatePickerState.d.ts +2 -41
  11. package/dist/cjs/components/DatePicker/hooks/useDatePickerState.js +11 -131
  12. package/dist/cjs/components/DatePicker/{utils.js → utils/cn.js} +1 -1
  13. package/dist/cjs/components/DatePicker/utils/getDateTimeValue.d.ts +3 -0
  14. package/dist/cjs/components/DatePicker/utils/getDateTimeValue.js +10 -0
  15. package/dist/cjs/components/DatePicker/utils/index.d.ts +2 -0
  16. package/dist/cjs/components/DatePicker/utils/index.js +5 -0
  17. package/dist/cjs/components/RangeCalendar/RangeCalendar.d.ts +5 -10
  18. package/dist/cjs/components/RangeDateField/hooks/useRangeDateFieldState.d.ts +2 -2
  19. package/dist/cjs/components/RangeDatePicker/RangeDatePicker.css +22 -0
  20. package/dist/cjs/components/RangeDatePicker/RangeDatePicker.d.ts +6 -0
  21. package/dist/cjs/components/RangeDatePicker/RangeDatePicker.js +25 -0
  22. package/dist/cjs/components/RangeDatePicker/hooks/useRangeDatePickerState.d.ts +7 -0
  23. package/dist/cjs/components/RangeDatePicker/hooks/useRangeDatePickerState.js +28 -0
  24. package/dist/cjs/components/RangeDatePicker/index.d.ts +2 -0
  25. package/dist/cjs/components/RangeDatePicker/index.js +5 -0
  26. package/dist/cjs/components/index.d.ts +1 -0
  27. package/dist/cjs/components/index.js +1 -0
  28. package/dist/esm/components/DateField/hooks/useBaseDateFieldState.d.ts +2 -2
  29. package/dist/esm/components/DateField/hooks/useDateFieldProps.d.ts +2 -2
  30. package/dist/esm/components/DateField/hooks/useDateFieldState.d.ts +1 -2
  31. package/dist/esm/components/DateField/utils.js +2 -1
  32. package/dist/esm/components/DatePicker/DatePicker.d.ts +3 -2
  33. package/dist/esm/components/DatePicker/hooks/datePickerStateFactory.d.ts +49 -0
  34. package/dist/esm/components/DatePicker/hooks/datePickerStateFactory.js +116 -0
  35. package/dist/esm/components/DatePicker/hooks/useDatePickerProps.d.ts +9 -5
  36. package/dist/esm/components/DatePicker/hooks/useDatePickerProps.js +2 -1
  37. package/dist/esm/components/DatePicker/hooks/useDatePickerState.d.ts +2 -41
  38. package/dist/esm/components/DatePicker/hooks/useDatePickerState.js +11 -129
  39. package/dist/esm/components/DatePicker/utils/cn.js +2 -0
  40. package/dist/esm/components/DatePicker/utils/getDateTimeValue.d.ts +3 -0
  41. package/dist/esm/components/DatePicker/utils/getDateTimeValue.js +6 -0
  42. package/dist/esm/components/DatePicker/utils/index.d.ts +2 -0
  43. package/dist/esm/components/DatePicker/utils/index.js +2 -0
  44. package/dist/esm/components/RangeCalendar/RangeCalendar.d.ts +5 -10
  45. package/dist/esm/components/RangeDateField/hooks/useRangeDateFieldState.d.ts +2 -2
  46. package/dist/esm/components/RangeDatePicker/RangeDatePicker.css +22 -0
  47. package/dist/esm/components/RangeDatePicker/RangeDatePicker.d.ts +6 -0
  48. package/dist/esm/components/RangeDatePicker/RangeDatePicker.js +21 -0
  49. package/dist/esm/components/RangeDatePicker/hooks/useRangeDatePickerState.d.ts +7 -0
  50. package/dist/esm/components/RangeDatePicker/hooks/useRangeDatePickerState.js +25 -0
  51. package/dist/esm/components/RangeDatePicker/index.d.ts +2 -0
  52. package/dist/esm/components/RangeDatePicker/index.js +2 -0
  53. package/dist/esm/components/index.d.ts +1 -0
  54. package/dist/esm/components/index.js +1 -0
  55. package/package.json +1 -1
  56. package/dist/esm/components/DatePicker/utils.js +0 -2
  57. /package/dist/cjs/components/DatePicker/{utils.d.ts → utils/cn.d.ts} +0 -0
  58. /package/dist/esm/components/DatePicker/{utils.d.ts → utils/cn.d.ts} +0 -0
@@ -28,7 +28,7 @@ export type BaseDateFieldStateOptions<T = DateTime> = {
28
28
  createPlaceholder: () => T;
29
29
  setValueFromString: (str: string) => boolean;
30
30
  };
31
- export type BaseDateFieldState<T = DateTime> = {
31
+ export type DateFieldState<T = DateTime> = {
32
32
  /** The current field value. */
33
33
  value: T | null;
34
34
  /** Is no part of value is filled. */
@@ -94,4 +94,4 @@ export type BaseDateFieldState<T = DateTime> = {
94
94
  onInput: (key: string) => void;
95
95
  setValueFromString: (str: string) => boolean;
96
96
  };
97
- export declare function useBaseDateFieldState<T = DateTime>(props: BaseDateFieldStateOptions<T>): BaseDateFieldState<T>;
97
+ export declare function useBaseDateFieldState<T = DateTime>(props: BaseDateFieldStateOptions<T>): DateFieldState<T>;
@@ -1,9 +1,9 @@
1
1
  import type { DateTime } from '@gravity-ui/date-utils';
2
2
  import type { TextInputProps } from '@gravity-ui/uikit';
3
3
  import type { AccessibilityProps, DateFieldBase, TextInputProps as DateFieldTextInputProps, DomProps, FocusableProps, InputDOMProps, KeyboardEvents, StyleProps, TextInputExtendProps } from '../../types';
4
- import type { BaseDateFieldState } from './useBaseDateFieldState';
4
+ import type { DateFieldState } from './useBaseDateFieldState';
5
5
  export interface DateFieldProps<T = DateTime> extends DateFieldBase<T>, DateFieldTextInputProps, TextInputExtendProps, DomProps, InputDOMProps, FocusableProps, KeyboardEvents, StyleProps, AccessibilityProps {
6
6
  }
7
- export declare function useDateFieldProps<T = DateTime>(state: BaseDateFieldState<T>, props: DateFieldProps<T>): {
7
+ export declare function useDateFieldProps<T = DateTime>(state: DateFieldState<T>, props: DateFieldProps<T>): {
8
8
  inputProps: TextInputProps;
9
9
  };
@@ -1,6 +1,5 @@
1
1
  import type { DateFieldBase } from '../../types/datePicker';
2
- import type { BaseDateFieldState } from './useBaseDateFieldState';
2
+ import type { DateFieldState } from './useBaseDateFieldState';
3
3
  export interface DateFieldStateOptions extends DateFieldBase {
4
4
  }
5
- export type DateFieldState = BaseDateFieldState;
6
5
  export declare function useDateFieldState(props: DateFieldStateOptions): DateFieldState;
@@ -69,6 +69,7 @@ const formatTokenMap = {
69
69
  function getDateSectionConfigFromFormatToken(formatToken) {
70
70
  const config = formatTokenMap[formatToken];
71
71
  if (!config) {
72
+ // eslint-disable-next-line no-console
72
73
  console.error([
73
74
  `The token "${formatToken}" is not supported by the Date field.`,
74
75
  'Please try using another token.',
@@ -193,7 +194,7 @@ function addSegment(section, date, amount) {
193
194
  var _a;
194
195
  let val = (_a = section.value) !== null && _a !== void 0 ? _a : 0;
195
196
  if (section.type === 'dayPeriod') {
196
- val = date.hour() + (date.hour() > 12 ? -12 : 12);
197
+ val = date.hour() + (date.hour() >= 12 ? -12 : 12);
197
198
  }
198
199
  else {
199
200
  val = val + amount;
@@ -1,8 +1,9 @@
1
1
  import React from 'react';
2
+ import type { DateTime } from '@gravity-ui/date-utils';
2
3
  import type { CalendarProps } from '../Calendar';
3
4
  import type { AccessibilityProps, DateFieldBase, DomProps, FocusableProps, InputDOMProps, KeyboardEvents, StyleProps, TextInputProps } from '../types';
4
5
  import './DatePicker.css';
5
- export interface DatePickerProps extends DateFieldBase, TextInputProps, FocusableProps, KeyboardEvents, DomProps, InputDOMProps, StyleProps, AccessibilityProps {
6
- children?: (props: CalendarProps) => React.ReactNode;
6
+ export interface DatePickerProps<T = DateTime> extends DateFieldBase<T>, TextInputProps, FocusableProps, KeyboardEvents, DomProps, InputDOMProps, StyleProps, AccessibilityProps {
7
+ children?: (props: CalendarProps<T>) => React.ReactNode;
7
8
  }
8
9
  export declare function DatePicker({ className, ...props }: DatePickerProps): import("react/jsx-runtime").JSX.Element;
@@ -0,0 +1,49 @@
1
+ import type { DateTime } from '@gravity-ui/date-utils';
2
+ import type { DateFieldState } from '../../DateField';
3
+ import type { DateFieldBase } from '../../types';
4
+ export interface DatePickerState<T = DateTime> {
5
+ /** The currently selected date. */
6
+ value: T | null;
7
+ /** Sets the selected date. */
8
+ setValue: (value: T | null) => void;
9
+ /**
10
+ * The date portion of the value. This may be set prior to `value` if the user has
11
+ * selected a date but has not yet selected a time.
12
+ */
13
+ dateValue: T | null;
14
+ /** Sets the date portion of the value. */
15
+ setDateValue: (value: T) => void;
16
+ /**
17
+ * The time portion of the value. This may be set prior to `value` if the user has
18
+ * selected a time but has not yet selected a date.
19
+ */
20
+ timeValue: T | null;
21
+ /** Sets the time portion of the value. */
22
+ setTimeValue: (value: T | null) => void;
23
+ /** Whether the field is read only. */
24
+ readOnly?: boolean;
25
+ /** Whether the field is disabled. */
26
+ disabled?: boolean;
27
+ /** Format of the date when rendered in the input. */
28
+ format: string;
29
+ /** Whether the date picker supports selecting a date. */
30
+ hasDate: boolean;
31
+ /** Whether the date picker supports selecting a time. */
32
+ hasTime: boolean;
33
+ /** Format of the time when rendered in the input. */
34
+ timeFormat?: string;
35
+ timeZone: string;
36
+ /** Whether the calendar popover is currently open. */
37
+ isOpen: boolean;
38
+ /** Sets whether the calendar popover is open. */
39
+ setOpen: (isOpen: boolean) => void;
40
+ dateFieldState: DateFieldState<T>;
41
+ }
42
+ export interface DatePickerStateFactoryOptions<T, O extends DateFieldBase<T>> {
43
+ getPlaceholderTime: (placeholderValue: DateTime | undefined, timeZone?: string) => T;
44
+ mergeDateTime: (date: T, time: T) => T;
45
+ setTimezone: (date: T, timeZone: string) => T;
46
+ getDateTime: (date: T | null | undefined) => DateTime | undefined;
47
+ useDateFieldState: (props: O) => DateFieldState<T>;
48
+ }
49
+ export declare function datePickerStateFactory<T, O extends DateFieldBase<T>>({ getPlaceholderTime, mergeDateTime, setTimezone, getDateTime, useDateFieldState, }: DatePickerStateFactoryOptions<T, O>): (props: O) => DatePickerState<T>;
@@ -0,0 +1,121 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.datePickerStateFactory = void 0;
4
+ const tslib_1 = require("tslib");
5
+ const react_1 = tslib_1.__importDefault(require("react"));
6
+ const uikit_1 = require("@gravity-ui/uikit");
7
+ const useDefaultTimeZone_1 = require("../../utils/useDefaultTimeZone");
8
+ function datePickerStateFactory({ getPlaceholderTime, mergeDateTime, setTimezone, getDateTime, useDateFieldState, }) {
9
+ return function useDatePickerState(props) {
10
+ var _a;
11
+ const { disabled, readOnly } = props;
12
+ const [isOpen, setOpen] = react_1.default.useState(false);
13
+ const [value, setValue] = (0, uikit_1.useControlledState)(props.value, (_a = props.defaultValue) !== null && _a !== void 0 ? _a : null, props.onUpdate);
14
+ const [selectedDateInner, setSelectedDate] = react_1.default.useState(null);
15
+ const [selectedTimeInner, setSelectedTime] = react_1.default.useState(null);
16
+ const inputTimeZone = (0, useDefaultTimeZone_1.useDefaultTimeZone)(getDateTime(props.value) || getDateTime(props.defaultValue) || props.placeholderValue);
17
+ const timeZone = props.timeZone || inputTimeZone;
18
+ let selectedDate = selectedDateInner;
19
+ let selectedTime = selectedTimeInner;
20
+ const format = props.format || 'L';
21
+ const commitValue = (date, time) => {
22
+ if (disabled || readOnly) {
23
+ return;
24
+ }
25
+ setValue(setTimezone(mergeDateTime(date, time), inputTimeZone));
26
+ setSelectedDate(null);
27
+ setSelectedTime(null);
28
+ };
29
+ const dateFieldState = useDateFieldState(Object.assign(Object.assign({}, props), { value,
30
+ onUpdate(date) {
31
+ if (date) {
32
+ commitValue(date, date);
33
+ }
34
+ else {
35
+ setValue(null);
36
+ }
37
+ },
38
+ disabled,
39
+ readOnly, validationState: props.validationState, minValue: props.minValue, maxValue: props.maxValue, isDateUnavailable: props.isDateUnavailable, format, placeholderValue: props.placeholderValue, timeZone }));
40
+ const timeFormat = react_1.default.useMemo(() => {
41
+ const hasSeconds = dateFieldState.sections.some((s) => s.type === 'second');
42
+ return hasSeconds ? 'LTS' : 'LT';
43
+ }, [dateFieldState.sections]);
44
+ if (value) {
45
+ selectedDate = setTimezone(value, timeZone);
46
+ if (dateFieldState.hasTime) {
47
+ selectedTime = setTimezone(value, timeZone);
48
+ }
49
+ }
50
+ // Intercept setValue to make sure the Time section is not changed by date selection in Calendar
51
+ const selectDate = (newValue) => {
52
+ if (disabled || readOnly) {
53
+ return;
54
+ }
55
+ const shouldClose = !dateFieldState.hasTime;
56
+ if (dateFieldState.hasTime) {
57
+ if (selectedTime || shouldClose) {
58
+ commitValue(newValue, selectedTime || newValue);
59
+ }
60
+ else {
61
+ setSelectedDate(newValue);
62
+ }
63
+ }
64
+ else {
65
+ commitValue(newValue, newValue);
66
+ }
67
+ if (shouldClose) {
68
+ setOpen(false);
69
+ }
70
+ };
71
+ const selectTime = (newValue) => {
72
+ if (disabled || readOnly) {
73
+ return;
74
+ }
75
+ const newTime = newValue !== null && newValue !== void 0 ? newValue : getPlaceholderTime(props.placeholderValue, timeZone);
76
+ if (selectedDate) {
77
+ commitValue(selectedDate, newTime);
78
+ }
79
+ else {
80
+ setSelectedTime(newTime);
81
+ }
82
+ };
83
+ if (dateFieldState.hasTime && !selectedTime) {
84
+ selectedTime = dateFieldState.displayValue;
85
+ }
86
+ return {
87
+ value,
88
+ setValue(newDate) {
89
+ if (props.readOnly || props.disabled) {
90
+ return;
91
+ }
92
+ if (newDate) {
93
+ setValue(setTimezone(newDate, inputTimeZone));
94
+ }
95
+ else {
96
+ setValue(null);
97
+ }
98
+ },
99
+ dateValue: selectedDate,
100
+ timeValue: selectedTime,
101
+ setDateValue: selectDate,
102
+ setTimeValue: selectTime,
103
+ disabled,
104
+ readOnly,
105
+ format,
106
+ hasDate: dateFieldState.hasDate,
107
+ hasTime: dateFieldState.hasTime,
108
+ timeFormat,
109
+ timeZone,
110
+ isOpen,
111
+ setOpen(newIsOpen) {
112
+ if (!newIsOpen && !value && selectedDate && dateFieldState.hasTime) {
113
+ commitValue(selectedDate, selectedTime || getPlaceholderTime(props.placeholderValue, props.timeZone));
114
+ }
115
+ setOpen(newIsOpen);
116
+ },
117
+ dateFieldState,
118
+ };
119
+ };
120
+ }
121
+ exports.datePickerStateFactory = datePickerStateFactory;
@@ -1,10 +1,12 @@
1
1
  import React from 'react';
2
+ import type { DateTime } from '@gravity-ui/date-utils';
2
3
  import type { ButtonProps, PopupProps, TextInputProps } from '@gravity-ui/uikit';
3
- import type { Calendar } from '../../Calendar';
4
+ import type { CalendarInstance, CalendarProps } from '../../Calendar';
4
5
  import type { DateFieldProps } from '../../DateField';
6
+ import type { RangeValue } from '../../types';
5
7
  import type { DatePickerProps } from '../DatePicker';
6
8
  import type { DatePickerState } from './useDatePickerState';
7
- interface InnerRelativeDatePickerProps {
9
+ interface InnerDatePickerProps<T = DateTime> {
8
10
  groupProps: React.HTMLAttributes<unknown> & {
9
11
  ref: React.Ref<any>;
10
12
  };
@@ -13,8 +15,10 @@ interface InnerRelativeDatePickerProps {
13
15
  ref: React.Ref<HTMLButtonElement>;
14
16
  };
15
17
  popupProps: PopupProps;
16
- calendarProps: React.ComponentProps<typeof Calendar>;
17
- timeInputProps: DateFieldProps;
18
+ calendarProps: CalendarProps<T> & {
19
+ ref: React.Ref<CalendarInstance>;
20
+ };
21
+ timeInputProps: DateFieldProps<T>;
18
22
  }
19
- export declare function useDatePickerProps(state: DatePickerState, { onFocus, onBlur, ...props }: DatePickerProps): InnerRelativeDatePickerProps;
23
+ export declare function useDatePickerProps<T extends DateTime | RangeValue<DateTime>>(state: DatePickerState<T>, { onFocus, onBlur, ...props }: DatePickerProps<T>): InnerDatePickerProps<T>;
20
24
  export {};
@@ -8,6 +8,7 @@ const DateField_1 = require("../../DateField");
8
8
  const getButtonSizeForInput_1 = require("../../utils/getButtonSizeForInput");
9
9
  const mergeProps_1 = require("../../utils/mergeProps");
10
10
  const i18n_1 = require("../i18n");
11
+ const utils_1 = require("../utils");
11
12
  function useDatePickerProps(state, _a) {
12
13
  var { onFocus, onBlur } = _a, props = tslib_1.__rest(_a, ["onFocus", "onBlur"]);
13
14
  const [isActive, setActive] = react_1.default.useState(false);
@@ -97,7 +98,7 @@ function useDatePickerProps(state, _a) {
97
98
  },
98
99
  timeInputProps: {
99
100
  value: state.timeValue,
100
- placeholderValue: state.dateFieldState.displayValue,
101
+ placeholderValue: (0, utils_1.getDateTimeValue)(state.dateFieldState.displayValue),
101
102
  onUpdate: state.setTimeValue,
102
103
  format: state.timeFormat,
103
104
  readOnly: state.readOnly,
@@ -1,45 +1,6 @@
1
1
  import type { DateTime } from '@gravity-ui/date-utils';
2
- import type { DateFieldState } from '../../DateField';
3
2
  import type { DateFieldBase } from '../../types';
4
- export type Granularity = 'day' | 'hour' | 'minute' | 'second';
5
- export interface DatePickerState {
6
- /** The currently selected date. */
7
- value: DateTime | null;
8
- /** Sets the selected date. */
9
- setValue: (value: DateTime | null) => void;
10
- /**
11
- * The date portion of the value. This may be set prior to `value` if the user has
12
- * selected a date but has not yet selected a time.
13
- */
14
- dateValue: DateTime | null;
15
- /** Sets the date portion of the value. */
16
- setDateValue: (value: DateTime) => void;
17
- /**
18
- * The time portion of the value. This may be set prior to `value` if the user has
19
- * selected a time but has not yet selected a date.
20
- */
21
- timeValue: DateTime | null;
22
- /** Sets the time portion of the value. */
23
- setTimeValue: (value: DateTime | null) => void;
24
- /** Whether the field is read only. */
25
- readOnly?: boolean;
26
- /** Whether the field is disabled. */
27
- disabled?: boolean;
28
- /** Format of the date when rendered in the input. */
29
- format: string;
30
- /** Whether the date picker supports selecting a date. */
31
- hasDate: boolean;
32
- /** Whether the date picker supports selecting a time. */
33
- hasTime: boolean;
34
- /** Format of the time when rendered in the input. */
35
- timeFormat?: string;
36
- timeZone: string;
37
- /** Whether the calendar popover is currently open. */
38
- isOpen: boolean;
39
- /** Sets whether the calendar popover is open. */
40
- setOpen: (isOpen: boolean) => void;
41
- dateFieldState: DateFieldState;
42
- }
3
+ export type { DatePickerState } from './datePickerStateFactory';
43
4
  export interface DatePickerStateOptions extends DateFieldBase {
44
5
  }
45
- export declare function useDatePickerState(props: DatePickerStateOptions): DatePickerState;
6
+ export declare const useDatePickerState: (props: import("../../DateField").DateFieldStateOptions) => import("./datePickerStateFactory").DatePickerState<DateTime>;
@@ -1,137 +1,17 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.useDatePickerState = void 0;
4
- const tslib_1 = require("tslib");
5
- const react_1 = tslib_1.__importDefault(require("react"));
6
- const uikit_1 = require("@gravity-ui/uikit");
7
4
  const DateField_1 = require("../../DateField");
8
5
  const dates_1 = require("../../utils/dates");
9
- const useDefaultTimeZone_1 = require("../../utils/useDefaultTimeZone");
10
- function useDatePickerState(props) {
11
- var _a;
12
- const { disabled, readOnly } = props;
13
- const [isOpen, setOpen] = react_1.default.useState(false);
14
- const [value, setValue] = (0, uikit_1.useControlledState)(props.value, (_a = props.defaultValue) !== null && _a !== void 0 ? _a : null, props.onUpdate);
15
- const [selectedDateInner, setSelectedDate] = react_1.default.useState(null);
16
- const [selectedTimeInner, setSelectedTime] = react_1.default.useState(null);
17
- const inputTimeZone = (0, useDefaultTimeZone_1.useDefaultTimeZone)(props.value || props.defaultValue || props.placeholderValue);
18
- const timeZone = props.timeZone || inputTimeZone;
19
- let selectedDate = selectedDateInner;
20
- let selectedTime = selectedTimeInner;
21
- const format = props.format || 'L';
22
- const commitValue = (date, time) => {
23
- if (disabled || readOnly) {
24
- return;
25
- }
26
- setValue((0, dates_1.mergeDateTime)(date, time).timeZone(inputTimeZone));
27
- setSelectedDate(null);
28
- setSelectedTime(null);
29
- };
30
- const dateFieldState = (0, DateField_1.useDateFieldState)({
31
- value,
32
- onUpdate(date) {
33
- if (date) {
34
- commitValue(date, date);
35
- }
36
- else {
37
- setValue(null);
38
- }
39
- },
40
- disabled,
41
- readOnly,
42
- validationState: props.validationState,
43
- minValue: props.minValue,
44
- maxValue: props.maxValue,
45
- isDateUnavailable: props.isDateUnavailable,
46
- format,
47
- placeholderValue: props.placeholderValue,
48
- timeZone,
49
- });
50
- const timeFormat = react_1.default.useMemo(() => {
51
- const hasSeconds = dateFieldState.sections.some((s) => s.type === 'second');
52
- return hasSeconds ? 'LTS' : 'LT';
53
- }, [dateFieldState.sections]);
54
- if (value) {
55
- selectedDate = value.timeZone(timeZone);
56
- if (dateFieldState.hasTime) {
57
- selectedTime = value.timeZone(timeZone);
58
- }
59
- }
60
- // Intercept setValue to make sure the Time section is not changed by date selection in Calendar
61
- const selectDate = (newValue) => {
62
- if (disabled || readOnly) {
63
- return;
64
- }
65
- const shouldClose = !dateFieldState.hasTime;
66
- if (dateFieldState.hasTime) {
67
- if (selectedTime || shouldClose) {
68
- commitValue(newValue, selectedTime || newValue);
69
- }
70
- else {
71
- setSelectedDate(newValue);
72
- }
73
- }
74
- else {
75
- commitValue(newValue, newValue);
76
- }
77
- if (shouldClose) {
78
- setOpen(false);
79
- }
80
- };
81
- const selectTime = (newValue) => {
82
- if (disabled || readOnly) {
83
- return;
84
- }
85
- const newTime = newValue !== null && newValue !== void 0 ? newValue : (0, dates_1.createPlaceholderValue)({
86
- placeholderValue: props.placeholderValue,
87
- timeZone,
88
- });
89
- if (selectedDate) {
90
- commitValue(selectedDate, newTime);
91
- }
92
- else {
93
- setSelectedTime(newTime);
94
- }
95
- };
96
- if (dateFieldState.hasTime && !selectedTime) {
97
- selectedTime = dateFieldState.displayValue;
98
- }
99
- return {
100
- value,
101
- setValue(newDate) {
102
- if (props.readOnly || props.disabled) {
103
- return;
104
- }
105
- if (newDate) {
106
- setValue(newDate.timeZone(inputTimeZone));
107
- }
108
- else {
109
- setValue(null);
110
- }
111
- },
112
- dateValue: selectedDate,
113
- timeValue: selectedTime,
114
- setDateValue: selectDate,
115
- setTimeValue: selectTime,
116
- disabled,
117
- readOnly,
118
- format,
119
- hasDate: dateFieldState.hasDate,
120
- hasTime: dateFieldState.hasTime,
121
- timeFormat,
122
- timeZone,
123
- isOpen,
124
- setOpen(newIsOpen) {
125
- if (!newIsOpen && !value && selectedDate && dateFieldState.hasTime) {
126
- commitValue(selectedDate, selectedTime ||
127
- (0, dates_1.createPlaceholderValue)({
128
- placeholderValue: props.placeholderValue,
129
- timeZone: inputTimeZone,
130
- }));
131
- }
132
- setOpen(newIsOpen);
133
- },
134
- dateFieldState,
135
- };
6
+ const utils_1 = require("../utils");
7
+ const datePickerStateFactory_1 = require("./datePickerStateFactory");
8
+ exports.useDatePickerState = (0, datePickerStateFactory_1.datePickerStateFactory)({
9
+ getPlaceholderTime,
10
+ mergeDateTime: dates_1.mergeDateTime,
11
+ setTimezone: (date, timeZone) => date.timeZone(timeZone),
12
+ getDateTime: utils_1.getDateTimeValue,
13
+ useDateFieldState: DateField_1.useDateFieldState,
14
+ });
15
+ function getPlaceholderTime(placeholderValue, timeZone) {
16
+ return (0, dates_1.createPlaceholderValue)({ placeholderValue, timeZone });
136
17
  }
137
- exports.useDatePickerState = useDatePickerState;
@@ -1,5 +1,5 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.b = void 0;
4
- const cn_1 = require("../../utils/cn");
4
+ const cn_1 = require("../../../utils/cn");
5
5
  exports.b = (0, cn_1.block)('date-picker');
@@ -0,0 +1,3 @@
1
+ import type { DateTime } from '@gravity-ui/date-utils';
2
+ import type { RangeValue } from '../../types';
3
+ export declare function getDateTimeValue(value: DateTime | RangeValue<DateTime> | null | undefined): DateTime | undefined;
@@ -0,0 +1,10 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getDateTimeValue = void 0;
4
+ function getDateTimeValue(value) {
5
+ if (!value) {
6
+ return undefined;
7
+ }
8
+ return 'start' in value && 'end' in value ? value.start : value;
9
+ }
10
+ exports.getDateTimeValue = getDateTimeValue;
@@ -0,0 +1,2 @@
1
+ export * from './cn';
2
+ export * from './getDateTimeValue';
@@ -0,0 +1,5 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const tslib_1 = require("tslib");
4
+ tslib_1.__exportStar(require("./cn"), exports);
5
+ tslib_1.__exportStar(require("./getDateTimeValue"), exports);
@@ -1,13 +1,8 @@
1
1
  import React from 'react';
2
- import type { CalendarInstance, CalendarSize } from '../CalendarView/CalendarView';
3
- import type { RangeCalendarStateOptions } from '../CalendarView/hooks/useRangeCalendarState';
4
- import type { AccessibilityProps, DomProps, FocusEvents, StyleProps } from '../types';
2
+ import type { DateTime } from '@gravity-ui/date-utils';
3
+ import type { CalendarProps } from '../Calendar/Calendar';
4
+ import type { CalendarInstance } from '../CalendarView/CalendarView';
5
+ import type { RangeValue } from '../types';
5
6
  import '../CalendarView/Calendar.css';
6
- export interface RangeCalendarProps extends RangeCalendarStateOptions, DomProps, StyleProps, FocusEvents, AccessibilityProps {
7
- /**
8
- * The size of the element.
9
- * @default m
10
- */
11
- size?: CalendarSize;
12
- }
7
+ export type RangeCalendarProps = CalendarProps<RangeValue<DateTime>>;
13
8
  export declare const RangeCalendar: React.ForwardRefExoticComponent<RangeCalendarProps & React.RefAttributes<CalendarInstance>>;
@@ -1,9 +1,9 @@
1
1
  import type { DateTime } from '@gravity-ui/date-utils';
2
- import type { BaseDateFieldState } from '../../DateField';
2
+ import type { DateFieldState } from '../../DateField';
3
3
  import type { DateFieldBase } from '../../types/datePicker';
4
4
  import type { RangeValue } from '../../types/inputs';
5
5
  export interface RangeDateFieldStateOptions extends DateFieldBase<RangeValue<DateTime>> {
6
6
  delimiter?: string;
7
7
  }
8
- export type RangeDateFieldState = BaseDateFieldState<RangeValue<DateTime>>;
8
+ export type RangeDateFieldState = DateFieldState<RangeValue<DateTime>>;
9
9
  export declare function useRangeDateFieldState(props: RangeDateFieldStateOptions): RangeDateFieldState;
@@ -0,0 +1,22 @@
1
+ .g-date-range-date-picker {
2
+ position: relative;
3
+ display: inline-block;
4
+ outline: none;
5
+ }
6
+ .g-date-range-date-picker__field {
7
+ width: 100%;
8
+ }
9
+ .g-date-range-date-picker__popup-anchor {
10
+ position: absolute;
11
+ z-index: -1;
12
+ inset: 0;
13
+ }
14
+ .g-date-range-date-picker__popup-content {
15
+ outline: none;
16
+ }
17
+ .g-date-range-date-picker__time-field {
18
+ width: 100%;
19
+ }
20
+ .g-date-range-date-picker__time-field-wrapper {
21
+ padding: 10px;
22
+ }
@@ -0,0 +1,6 @@
1
+ import type { DateTime } from '@gravity-ui/date-utils';
2
+ import type { DatePickerProps } from '../DatePicker';
3
+ import type { RangeValue } from '../types';
4
+ import './RangeDatePicker.css';
5
+ export type RangeDatePickerProps = DatePickerProps<RangeValue<DateTime>>;
6
+ export declare function RangeDatePicker({ className, ...props }: RangeDatePickerProps): import("react/jsx-runtime").JSX.Element;
@@ -0,0 +1,25 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.RangeDatePicker = void 0;
4
+ const tslib_1 = require("tslib");
5
+ const jsx_runtime_1 = require("react/jsx-runtime");
6
+ const react_1 = tslib_1.__importDefault(require("react"));
7
+ const icons_1 = require("@gravity-ui/icons");
8
+ const uikit_1 = require("@gravity-ui/uikit");
9
+ const cn_1 = require("../../utils/cn");
10
+ const Calendar_1 = require("../Calendar");
11
+ const DatePicker_1 = require("../DatePicker");
12
+ const StubButton_1 = require("../DatePicker/StubButton");
13
+ const RangeDateField_1 = require("../RangeDateField");
14
+ const useRangeDatePickerState_1 = require("./hooks/useRangeDatePickerState");
15
+ require("./RangeDatePicker.css");
16
+ const b = (0, cn_1.block)('range-date-picker');
17
+ function RangeDatePicker(_a) {
18
+ var { className } = _a, props = tslib_1.__rest(_a, ["className"]);
19
+ const anchorRef = react_1.default.useRef(null);
20
+ const state = (0, useRangeDatePickerState_1.useRangeDatePickerState)(props);
21
+ const { groupProps, fieldProps, calendarButtonProps, popupProps, calendarProps, timeInputProps } = (0, DatePicker_1.useDatePickerProps)(state, props);
22
+ const isOnlyTime = state.hasTime && !state.hasDate;
23
+ return ((0, jsx_runtime_1.jsxs)("div", Object.assign({ className: b(null, className) }, groupProps, { children: [!isOnlyTime && ((0, jsx_runtime_1.jsx)("div", { ref: anchorRef, className: b('popup-anchor'), children: (0, jsx_runtime_1.jsx)(uikit_1.Popup, Object.assign({ anchorRef: anchorRef }, popupProps, { children: (0, jsx_runtime_1.jsxs)("div", { className: b('popup-content'), children: [typeof props.children === 'function' ? (props.children(Object.assign(Object.assign({}, calendarProps), { value: state.value }))) : ((0, jsx_runtime_1.jsx)(Calendar_1.RangeCalendar, Object.assign({}, calendarProps, { value: state.value }))), state.hasTime && ((0, jsx_runtime_1.jsx)("div", { className: b('time-field-wrapper'), children: (0, jsx_runtime_1.jsx)(RangeDateField_1.RangeDateField, Object.assign({}, timeInputProps)) }))] }) })) })), (0, jsx_runtime_1.jsx)(uikit_1.TextInput, Object.assign({}, fieldProps, { className: b('field'), hasClear: fieldProps.hasClear, endContent: (0, jsx_runtime_1.jsxs)(react_1.default.Fragment, { children: [!isOnlyTime && ((0, jsx_runtime_1.jsx)(uikit_1.Button, Object.assign({}, calendarButtonProps, { children: (0, jsx_runtime_1.jsx)(uikit_1.Icon, { data: icons_1.Calendar }) }))), isOnlyTime && ((0, jsx_runtime_1.jsx)(StubButton_1.StubButton, { size: calendarButtonProps.size, icon: icons_1.Clock }))] }) }))] })));
24
+ }
25
+ exports.RangeDatePicker = RangeDatePicker;
@@ -0,0 +1,7 @@
1
+ import type { DateTime } from '@gravity-ui/date-utils';
2
+ import type { DatePickerState } from '../../DatePicker';
3
+ import type { RangeDateFieldStateOptions } from '../../RangeDateField';
4
+ import type { RangeValue } from '../../types';
5
+ export type RangeDatePickerState = DatePickerState<RangeValue<DateTime>>;
6
+ export type RangeDatePickerStateOptions = RangeDateFieldStateOptions;
7
+ export declare const useRangeDatePickerState: (props: RangeDateFieldStateOptions) => DatePickerState<RangeValue<DateTime>>;
@@ -0,0 +1,28 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.useRangeDatePickerState = void 0;
4
+ const datePickerStateFactory_1 = require("../../DatePicker/hooks/datePickerStateFactory");
5
+ const utils_1 = require("../../DatePicker/utils");
6
+ const RangeDateField_1 = require("../../RangeDateField");
7
+ const dates_1 = require("../../utils/dates");
8
+ exports.useRangeDatePickerState = (0, datePickerStateFactory_1.datePickerStateFactory)({
9
+ getPlaceholderTime,
10
+ mergeDateTime: mergeRangeDateTime,
11
+ setTimezone,
12
+ getDateTime: utils_1.getDateTimeValue,
13
+ useDateFieldState: RangeDateField_1.useRangeDateFieldState,
14
+ });
15
+ function getPlaceholderTime(placeholderValue, timeZone) {
16
+ const date = (0, dates_1.createPlaceholderValue)({ placeholderValue, timeZone });
17
+ return { start: date, end: date };
18
+ }
19
+ function mergeRangeDateTime(date, time) {
20
+ const start = (0, dates_1.mergeDateTime)(date.start, time.start);
21
+ const end = (0, dates_1.mergeDateTime)(date.end, time.end);
22
+ return { start, end };
23
+ }
24
+ function setTimezone(date, timeZone) {
25
+ const start = date.start.timeZone(timeZone);
26
+ const end = date.end.timeZone(timeZone);
27
+ return { start, end };
28
+ }