@gravity-ui/date-components 2.3.1 → 2.5.0
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/dist/cjs/components/Calendar/Calendar.js +1 -0
- package/dist/cjs/components/CalendarView/CalendarView.js +1 -0
- package/dist/cjs/components/DateField/DateField.js +5 -3
- package/dist/cjs/components/DateField/hooks/useBaseDateFieldState.d.ts +2 -0
- package/dist/cjs/components/DateField/hooks/useBaseDateFieldState.js +1 -0
- package/dist/cjs/components/DateField/hooks/useDateFieldState.js +1 -1
- package/dist/cjs/components/DateField/utils.d.ts +1 -2
- package/dist/cjs/components/DateField/utils.js +6 -17
- package/dist/cjs/components/DatePicker/DatePicker.js +5 -3
- package/dist/cjs/components/DatePicker/MobileCalendar.js +1 -0
- package/dist/cjs/components/HiddenInput/HiddenInput.d.ts +14 -0
- package/dist/cjs/components/HiddenInput/HiddenInput.js +37 -0
- package/dist/cjs/components/RangeCalendar/RangeCalendar.js +1 -0
- package/dist/cjs/components/RangeDateField/RangeDateField.js +6 -1
- package/dist/cjs/components/RangeDatePicker/RangeDatePicker.js +5 -1
- package/dist/cjs/components/RelativeDateField/RelativeDateField.js +5 -4
- package/dist/cjs/components/RelativeDateField/hooks/useRelativeDateFieldState.js +2 -8
- package/dist/cjs/components/RelativeDatePicker/RelativeDatePicker.js +6 -3
- package/dist/cjs/components/RelativeRangeDatePicker/RelativeRangeDatePicker.d.ts +2 -2
- package/dist/cjs/components/RelativeRangeDatePicker/RelativeRangeDatePicker.js +40 -33
- package/dist/cjs/components/RelativeRangeDatePicker/components/PickerDialog/PickerDialog.js +1 -0
- package/dist/cjs/components/RelativeRangeDatePicker/components/Presets/Presets.js +1 -0
- package/dist/cjs/components/RelativeRangeDatePicker/components/Presets/PresetsDoc.js +1 -0
- package/dist/cjs/components/RelativeRangeDatePicker/components/Zones/Zones.js +1 -0
- package/dist/cjs/components/types/inputs.d.ts +5 -0
- package/dist/cjs/components/utils/mergeProps.js +3 -3
- package/dist/cjs/components/utils/validation/datePicker.js +2 -2
- package/dist/esm/components/Calendar/Calendar.js +1 -0
- package/dist/esm/components/CalendarView/CalendarView.js +1 -0
- package/dist/esm/components/DateField/DateField.js +5 -3
- package/dist/esm/components/DateField/hooks/useBaseDateFieldState.d.ts +2 -0
- package/dist/esm/components/DateField/hooks/useBaseDateFieldState.js +1 -0
- package/dist/esm/components/DateField/hooks/useDateFieldState.js +1 -1
- package/dist/esm/components/DateField/utils.d.ts +1 -2
- package/dist/esm/components/DateField/utils.js +5 -15
- package/dist/esm/components/DatePicker/DatePicker.js +5 -3
- package/dist/esm/components/DatePicker/MobileCalendar.js +1 -0
- package/dist/esm/components/HiddenInput/HiddenInput.d.ts +14 -0
- package/dist/esm/components/HiddenInput/HiddenInput.js +31 -0
- package/dist/esm/components/RangeCalendar/RangeCalendar.js +1 -0
- package/dist/esm/components/RangeDateField/RangeDateField.js +7 -2
- package/dist/esm/components/RangeDatePicker/RangeDatePicker.js +5 -1
- package/dist/esm/components/RelativeDateField/RelativeDateField.js +5 -4
- package/dist/esm/components/RelativeDateField/hooks/useRelativeDateFieldState.js +3 -11
- package/dist/esm/components/RelativeDatePicker/RelativeDatePicker.js +6 -3
- package/dist/esm/components/RelativeRangeDatePicker/RelativeRangeDatePicker.d.ts +2 -2
- package/dist/esm/components/RelativeRangeDatePicker/RelativeRangeDatePicker.js +41 -34
- package/dist/esm/components/RelativeRangeDatePicker/components/PickerDialog/PickerDialog.js +1 -0
- package/dist/esm/components/RelativeRangeDatePicker/components/Presets/Presets.js +1 -0
- package/dist/esm/components/RelativeRangeDatePicker/components/Presets/PresetsDoc.js +1 -0
- package/dist/esm/components/RelativeRangeDatePicker/components/Zones/Zones.js +1 -0
- package/dist/esm/components/types/inputs.d.ts +5 -0
- package/dist/esm/components/utils/mergeProps.js +3 -3
- package/dist/esm/components/utils/validation/datePicker.js +2 -2
- package/package.json +25 -19
|
@@ -1,8 +1,10 @@
|
|
|
1
|
+
'use client';
|
|
1
2
|
import { __rest } from "tslib";
|
|
2
3
|
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
3
4
|
import React from 'react';
|
|
4
5
|
import { TextInput, useFocusWithin } from '@gravity-ui/uikit';
|
|
5
6
|
import { block } from '../../utils/cn';
|
|
7
|
+
import { HiddenInput } from '../HiddenInput/HiddenInput';
|
|
6
8
|
import { useDateFieldProps } from './hooks/useDateFieldProps';
|
|
7
9
|
import { useDateFieldState } from './hooks/useDateFieldState';
|
|
8
10
|
import './DateField.css';
|
|
@@ -17,7 +19,7 @@ export function DateField(_a) {
|
|
|
17
19
|
setActive(isFocusWithin);
|
|
18
20
|
},
|
|
19
21
|
});
|
|
20
|
-
return (_jsxs("div", Object.assign({ className: b(null, className), style: props.style }, focusWithinProps, { children: [_jsx(TextInput, Object.assign({}, inputProps, { value: state.isEmpty && !isActive && props.placeholder ? '' : inputProps.value })), _jsx(
|
|
21
|
-
|
|
22
|
-
|
|
22
|
+
return (_jsxs("div", Object.assign({ className: b(null, className), style: props.style }, focusWithinProps, { children: [_jsx(TextInput, Object.assign({}, inputProps, { value: state.isEmpty && !isActive && props.placeholder ? '' : inputProps.value })), _jsx(HiddenInput, { name: props.name, value: state.value, toStringValue: (value) => { var _a; return (_a = value === null || value === void 0 ? void 0 : value.toISOString()) !== null && _a !== void 0 ? _a : ''; }, onReset: (value) => {
|
|
23
|
+
state.setDate(value);
|
|
24
|
+
}, disabled: state.disabled, form: props.form })] })));
|
|
23
25
|
}
|
|
@@ -37,6 +37,8 @@ export type DateFieldState<T = DateTime> = {
|
|
|
37
37
|
displayValue: T;
|
|
38
38
|
/** Sets the field's value. */
|
|
39
39
|
setValue: (value: T) => void;
|
|
40
|
+
/** Sets the date value. */
|
|
41
|
+
setDate: (value: T | null) => void;
|
|
40
42
|
/** Formatted value */
|
|
41
43
|
text: string;
|
|
42
44
|
/** Whether the field is read only. */
|
|
@@ -135,7 +135,7 @@ export function useDateFieldState(props) {
|
|
|
135
135
|
}).timeZone(timeZone);
|
|
136
136
|
}
|
|
137
137
|
function setValueFromString(str) {
|
|
138
|
-
const date = parseDateFromString(str,
|
|
138
|
+
const date = parseDateFromString(str, format, timeZone);
|
|
139
139
|
if (isValid(date)) {
|
|
140
140
|
handleUpdateDate(date);
|
|
141
141
|
return true;
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import type { DateTime } from '@gravity-ui/date-utils';
|
|
2
2
|
import type { DateFieldSection, DateFieldSectionType, DateFieldSectionWithoutPosition } from './types';
|
|
3
3
|
export declare const EDITABLE_SEGMENTS: Partial<Record<DateFieldSectionType, boolean>>;
|
|
4
|
-
export declare function expandFormat(format: string): string;
|
|
5
4
|
export declare function getSectionLimits(section: DateFieldSectionWithoutPosition, date: DateTime): {
|
|
6
5
|
minValue: number;
|
|
7
6
|
maxValue: number;
|
|
@@ -9,7 +8,7 @@ export declare function getSectionLimits(section: DateFieldSectionWithoutPositio
|
|
|
9
8
|
minValue?: undefined;
|
|
10
9
|
maxValue?: undefined;
|
|
11
10
|
};
|
|
12
|
-
export declare function getSectionValue(
|
|
11
|
+
export declare function getSectionValue(section: DateFieldSectionWithoutPosition, date: DateTime): number | undefined;
|
|
13
12
|
export declare function getDurationUnitFromSectionType(type: DateFieldSectionType): "year" | "month" | "day" | "hour" | "minute" | "second" | "date";
|
|
14
13
|
export declare function addSegment(section: DateFieldSection, date: DateTime, amount: number): DateTime;
|
|
15
14
|
export declare function setSegment(section: DateFieldSection, date: DateTime, amount: number): DateTime;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import React from 'react';
|
|
2
|
-
import { dateTime,
|
|
2
|
+
import { dateTime, expandFormat, isValid } from '@gravity-ui/date-utils';
|
|
3
3
|
import { mergeDateTime } from '../utils/dates';
|
|
4
4
|
import { i18n } from './i18n';
|
|
5
5
|
export const EDITABLE_SEGMENTS = {
|
|
@@ -12,16 +12,6 @@ export const EDITABLE_SEGMENTS = {
|
|
|
12
12
|
dayPeriod: true,
|
|
13
13
|
weekday: true,
|
|
14
14
|
};
|
|
15
|
-
export function expandFormat(format) {
|
|
16
|
-
const localeFormats = settings.getLocaleData().formats;
|
|
17
|
-
const getShortLocalizedFormatFromLongLocalizedFormat = (formatBis) => formatBis.replace(/(\[[^\]]+])|(MMMM|MM|DD|dddd)/g, (_, escapeSequence, localizedFormat) => escapeSequence || localizedFormat.slice(1));
|
|
18
|
-
return format.replace(/(\[[^\]]+])|(LTS?|l{1,4}|L{1,4})/g, (_, escapeSequence, localizedFormat) => {
|
|
19
|
-
const LongLocalizedFormat = localizedFormat && localizedFormat.toUpperCase();
|
|
20
|
-
return (escapeSequence ||
|
|
21
|
-
localeFormats[localizedFormat] ||
|
|
22
|
-
getShortLocalizedFormatFromLongLocalizedFormat(localeFormats[LongLocalizedFormat]));
|
|
23
|
-
});
|
|
24
|
-
}
|
|
25
15
|
const escapedCharacters = { start: '[', end: ']' };
|
|
26
16
|
const formatTokenMap = {
|
|
27
17
|
// Year
|
|
@@ -142,13 +132,13 @@ export function getSectionLimits(section, date) {
|
|
|
142
132
|
}
|
|
143
133
|
return {};
|
|
144
134
|
}
|
|
145
|
-
export function getSectionValue(
|
|
146
|
-
const type =
|
|
135
|
+
export function getSectionValue(section, date) {
|
|
136
|
+
const type = section.type;
|
|
147
137
|
switch (type) {
|
|
148
138
|
case 'year': {
|
|
149
|
-
return isFourDigitYearFormat(
|
|
139
|
+
return isFourDigitYearFormat(section.format)
|
|
150
140
|
? date.year()
|
|
151
|
-
: Number(date.format(
|
|
141
|
+
: Number(date.format(section.format));
|
|
152
142
|
}
|
|
153
143
|
case 'month':
|
|
154
144
|
case 'hour':
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
'use client';
|
|
1
2
|
import { __rest } from "tslib";
|
|
2
3
|
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
3
4
|
import React from 'react';
|
|
@@ -5,6 +6,7 @@ import { Calendar as CalendarIcon, Clock as ClockIcon } from '@gravity-ui/icons'
|
|
|
5
6
|
import { Button, Icon, Popup, TextInput, useMobile } from '@gravity-ui/uikit';
|
|
6
7
|
import { Calendar } from '../Calendar';
|
|
7
8
|
import { DateField } from '../DateField';
|
|
9
|
+
import { HiddenInput } from '../HiddenInput/HiddenInput';
|
|
8
10
|
import { MobileCalendar } from './MobileCalendar';
|
|
9
11
|
import { StubButton } from './StubButton';
|
|
10
12
|
import { useDatePickerProps } from './hooks/useDatePickerProps';
|
|
@@ -18,7 +20,7 @@ export function DatePicker(_a) {
|
|
|
18
20
|
const { groupProps, fieldProps, calendarButtonProps, popupProps, calendarProps, timeInputProps } = useDatePickerProps(state, props);
|
|
19
21
|
const isMobile = useMobile();
|
|
20
22
|
const isOnlyTime = state.hasTime && !state.hasDate;
|
|
21
|
-
return (_jsxs("div", Object.assign({ className: b(null, className) }, groupProps, { children: [isMobile ? (_jsx(MobileCalendar, { props: props, state: state })) : (!isOnlyTime && (_jsx("div", { ref: anchorRef, className: b('popup-anchor'), children: _jsx(Popup, Object.assign({ anchorRef: anchorRef }, popupProps, { children: _jsxs("div", { className: b('popup-content'), children: [typeof props.children === 'function' ? (props.children(calendarProps)) : (_jsx(Calendar, Object.assign({}, calendarProps))), state.hasTime && (_jsx("div", { className: b('time-field-wrapper'), children: _jsx(DateField, Object.assign({}, timeInputProps)) }))] }) })) }))), _jsx(TextInput, Object.assign({}, fieldProps, { className: b('field', { mobile: isMobile }), hasClear: !isMobile && fieldProps.hasClear, endContent: _jsxs(React.Fragment, { children: [!isMobile && !isOnlyTime && (_jsx(Button, Object.assign({}, calendarButtonProps, { children: _jsx(Icon, { data: CalendarIcon }) }))), !isMobile && isOnlyTime && (_jsx(StubButton, { size: calendarButtonProps.size, icon: ClockIcon })), isMobile && (_jsx(StubButton, { size: calendarButtonProps.size, icon: isOnlyTime ? ClockIcon : CalendarIcon }))] }) })), _jsx(
|
|
22
|
-
|
|
23
|
-
|
|
23
|
+
return (_jsxs("div", Object.assign({ className: b(null, className) }, groupProps, { children: [isMobile ? (_jsx(MobileCalendar, { props: props, state: state })) : (!isOnlyTime && (_jsx("div", { ref: anchorRef, className: b('popup-anchor'), children: _jsx(Popup, Object.assign({ anchorRef: anchorRef }, popupProps, { children: _jsxs("div", { className: b('popup-content'), children: [typeof props.children === 'function' ? (props.children(calendarProps)) : (_jsx(Calendar, Object.assign({}, calendarProps))), state.hasTime && (_jsx("div", { className: b('time-field-wrapper'), children: _jsx(DateField, Object.assign({}, timeInputProps)) }))] }) })) }))), _jsx(TextInput, Object.assign({}, fieldProps, { className: b('field', { mobile: isMobile }), hasClear: !isMobile && fieldProps.hasClear, endContent: _jsxs(React.Fragment, { children: [!isMobile && !isOnlyTime && (_jsx(Button, Object.assign({}, calendarButtonProps, { children: _jsx(Icon, { data: CalendarIcon }) }))), !isMobile && isOnlyTime && (_jsx(StubButton, { size: calendarButtonProps.size, icon: ClockIcon })), isMobile && (_jsx(StubButton, { size: calendarButtonProps.size, icon: isOnlyTime ? ClockIcon : CalendarIcon }))] }) })), _jsx(HiddenInput, { name: props.name, value: state.value, toStringValue: (value) => { var _a; return (_a = value === null || value === void 0 ? void 0 : value.toISOString()) !== null && _a !== void 0 ? _a : ''; }, onReset: (value) => {
|
|
24
|
+
state.setValue(value);
|
|
25
|
+
}, disabled: state.disabled, form: props.form })] })));
|
|
24
26
|
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
interface HiddenInputProps<T> {
|
|
2
|
+
value: T;
|
|
3
|
+
onReset?: (value: T) => void;
|
|
4
|
+
toStringValue?: (value: T) => string;
|
|
5
|
+
name?: string;
|
|
6
|
+
disabled?: boolean;
|
|
7
|
+
form?: string;
|
|
8
|
+
}
|
|
9
|
+
export declare function HiddenInput<T>({ name, value, onReset, form, disabled, toStringValue, }: HiddenInputProps<T>): import("react/jsx-runtime").JSX.Element | null;
|
|
10
|
+
export declare function useFormResetHandler<T>({ initialValue, onReset, }: {
|
|
11
|
+
initialValue: T;
|
|
12
|
+
onReset?: (value: T) => void;
|
|
13
|
+
}): (node: HTMLInputElement | HTMLTextAreaElement | HTMLSelectElement | null) => void;
|
|
14
|
+
export {};
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
+
import React from 'react';
|
|
3
|
+
export function HiddenInput({ name, value, onReset, form, disabled, toStringValue, }) {
|
|
4
|
+
const ref = useFormResetHandler({ initialValue: value, onReset });
|
|
5
|
+
if (!name) {
|
|
6
|
+
return null;
|
|
7
|
+
}
|
|
8
|
+
const v = toStringValue ? toStringValue(value) : `${value !== null && value !== void 0 ? value : ''}`;
|
|
9
|
+
return _jsx("input", { ref: ref, type: "hidden", name: name, value: v, disabled: disabled, form: form });
|
|
10
|
+
}
|
|
11
|
+
export function useFormResetHandler({ initialValue, onReset, }) {
|
|
12
|
+
const [formElement, setFormElement] = React.useState(null);
|
|
13
|
+
const resetValue = React.useRef(initialValue);
|
|
14
|
+
React.useEffect(() => {
|
|
15
|
+
if (!formElement || !onReset) {
|
|
16
|
+
return undefined;
|
|
17
|
+
}
|
|
18
|
+
const handleReset = () => {
|
|
19
|
+
onReset(resetValue.current);
|
|
20
|
+
};
|
|
21
|
+
formElement.addEventListener('reset', handleReset);
|
|
22
|
+
return () => {
|
|
23
|
+
formElement.removeEventListener('reset', handleReset);
|
|
24
|
+
};
|
|
25
|
+
}, [formElement, onReset]);
|
|
26
|
+
const ref = React.useCallback((node) => {
|
|
27
|
+
var _a;
|
|
28
|
+
setFormElement((_a = node === null || node === void 0 ? void 0 : node.form) !== null && _a !== void 0 ? _a : null);
|
|
29
|
+
}, []);
|
|
30
|
+
return ref;
|
|
31
|
+
}
|
|
@@ -1,13 +1,16 @@
|
|
|
1
|
+
'use client';
|
|
1
2
|
import { __rest } from "tslib";
|
|
2
|
-
import { jsx as _jsx } from "react/jsx-runtime";
|
|
3
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
3
4
|
import React from 'react';
|
|
4
5
|
import { TextInput, useFocusWithin } from '@gravity-ui/uikit';
|
|
5
6
|
import { block } from '../../utils/cn';
|
|
6
7
|
import { useDateFieldProps } from '../DateField/hooks/useDateFieldProps';
|
|
8
|
+
import { HiddenInput } from '../HiddenInput/HiddenInput';
|
|
7
9
|
import { useRangeDateFieldState } from './hooks/useRangeDateFieldState';
|
|
8
10
|
import './RangeDateField.css';
|
|
9
11
|
const b = block('range-date-field');
|
|
10
12
|
export function RangeDateField(_a) {
|
|
13
|
+
var _b, _c;
|
|
11
14
|
var { className } = _a, props = __rest(_a, ["className"]);
|
|
12
15
|
const state = useRangeDateFieldState(props);
|
|
13
16
|
const { inputProps } = useDateFieldProps(state, props);
|
|
@@ -17,5 +20,7 @@ export function RangeDateField(_a) {
|
|
|
17
20
|
setActive(isFocusWithin);
|
|
18
21
|
},
|
|
19
22
|
});
|
|
20
|
-
return (
|
|
23
|
+
return (_jsxs("div", Object.assign({ className: b(null, className), style: props.style }, focusWithinProps, { children: [_jsx(TextInput, Object.assign({}, inputProps, { value: state.isEmpty && !isActive && props.placeholder ? '' : inputProps.value })), _jsx(HiddenInput, { name: props.name, form: props.form, onReset: (v) => {
|
|
24
|
+
state.setDate(v);
|
|
25
|
+
}, value: (_b = state.value) !== null && _b !== void 0 ? _b : null, toStringValue: (v) => (v ? v.start.toISOString() : ''), disabled: state.disabled }), _jsx(HiddenInput, { name: props.name, form: props.form, value: (_c = state.value) !== null && _c !== void 0 ? _c : null, toStringValue: (v) => (v ? v.end.toISOString() : ''), disabled: state.disabled })] })));
|
|
21
26
|
}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
'use client';
|
|
1
2
|
import { __rest } from "tslib";
|
|
2
3
|
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
3
4
|
import React from 'react';
|
|
@@ -7,6 +8,7 @@ import { block } from '../../utils/cn';
|
|
|
7
8
|
import { RangeCalendar } from '../Calendar';
|
|
8
9
|
import { useDatePickerProps } from '../DatePicker';
|
|
9
10
|
import { StubButton } from '../DatePicker/StubButton';
|
|
11
|
+
import { HiddenInput } from '../HiddenInput/HiddenInput';
|
|
10
12
|
import { RangeDateField } from '../RangeDateField';
|
|
11
13
|
import { useRangeDatePickerState } from './hooks/useRangeDatePickerState';
|
|
12
14
|
import './RangeDatePicker.css';
|
|
@@ -17,5 +19,7 @@ export function RangeDatePicker(_a) {
|
|
|
17
19
|
const state = useRangeDatePickerState(props);
|
|
18
20
|
const { groupProps, fieldProps, calendarButtonProps, popupProps, calendarProps, timeInputProps } = useDatePickerProps(state, props);
|
|
19
21
|
const isOnlyTime = state.hasTime && !state.hasDate;
|
|
20
|
-
return (_jsxs("div", Object.assign({ className: b(null, className) }, groupProps, { children: [!isOnlyTime && (_jsx("div", { ref: anchorRef, className: b('popup-anchor'), children: _jsx(Popup, Object.assign({ anchorRef: anchorRef }, popupProps, { children: _jsxs("div", { className: b('popup-content'), children: [typeof props.children === 'function' ? (props.children(Object.assign(Object.assign({}, calendarProps), { value: state.value }))) : (_jsx(RangeCalendar, Object.assign({}, calendarProps, { value: state.value }))), state.hasTime && (_jsx("div", { className: b('time-field-wrapper'), children: _jsx(RangeDateField, Object.assign({}, timeInputProps)) }))] }) })) })), _jsx(TextInput, Object.assign({}, fieldProps, { className: b('field'), hasClear: fieldProps.hasClear, endContent: _jsxs(React.Fragment, { children: [!isOnlyTime && (_jsx(Button, Object.assign({}, calendarButtonProps, { children: _jsx(Icon, { data: CalendarIcon }) }))), isOnlyTime && (_jsx(StubButton, { size: calendarButtonProps.size, icon: ClockIcon }))] }) }))
|
|
22
|
+
return (_jsxs("div", Object.assign({ className: b(null, className) }, groupProps, { children: [!isOnlyTime && (_jsx("div", { ref: anchorRef, className: b('popup-anchor'), children: _jsx(Popup, Object.assign({ anchorRef: anchorRef }, popupProps, { children: _jsxs("div", { className: b('popup-content'), children: [typeof props.children === 'function' ? (props.children(Object.assign(Object.assign({}, calendarProps), { value: state.value }))) : (_jsx(RangeCalendar, Object.assign({}, calendarProps, { value: state.value }))), state.hasTime && (_jsx("div", { className: b('time-field-wrapper'), children: _jsx(RangeDateField, Object.assign({}, timeInputProps)) }))] }) })) })), _jsx(TextInput, Object.assign({}, fieldProps, { className: b('field'), hasClear: fieldProps.hasClear, endContent: _jsxs(React.Fragment, { children: [!isOnlyTime && (_jsx(Button, Object.assign({}, calendarButtonProps, { children: _jsx(Icon, { data: CalendarIcon }) }))), isOnlyTime && (_jsx(StubButton, { size: calendarButtonProps.size, icon: ClockIcon }))] }) })), _jsx(HiddenInput, { name: props.name, form: props.form, onReset: (v) => {
|
|
23
|
+
state.dateFieldState.setDate(v);
|
|
24
|
+
}, value: state.value, toStringValue: (v) => (v ? v.start.toISOString() : ''), disabled: state.disabled }), _jsx(HiddenInput, { name: props.name, form: props.form, value: state.value, toStringValue: (v) => (v ? v.end.toISOString() : ''), disabled: state.disabled })] })));
|
|
21
25
|
}
|
|
@@ -1,15 +1,16 @@
|
|
|
1
|
+
'use client';
|
|
1
2
|
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
3
|
import React from 'react';
|
|
3
4
|
import { Popup, TextInput, useFocusWithin, useMobile } from '@gravity-ui/uikit';
|
|
4
5
|
import { block } from '../../utils/cn';
|
|
5
6
|
import { Calendar } from '../Calendar';
|
|
6
7
|
import { DateField } from '../DateField';
|
|
8
|
+
import { HiddenInput } from '../HiddenInput/HiddenInput';
|
|
7
9
|
import { useRelativeDateFieldProps } from './hooks/useRelativeDateFieldProps';
|
|
8
10
|
import { useRelativeDateFieldState } from './hooks/useRelativeDateFieldState';
|
|
9
11
|
import './RelativeDateField.css';
|
|
10
12
|
const b = block('relative-date-field');
|
|
11
13
|
export function RelativeDateField(props) {
|
|
12
|
-
var _a;
|
|
13
14
|
const state = useRelativeDateFieldState(props);
|
|
14
15
|
const { inputProps, calendarProps, timeInputProps } = useRelativeDateFieldProps(state, props);
|
|
15
16
|
const isMobile = useMobile();
|
|
@@ -27,7 +28,7 @@ export function RelativeDateField(props) {
|
|
|
27
28
|
setOpen(true);
|
|
28
29
|
}
|
|
29
30
|
(_a = props.onFocus) === null || _a === void 0 ? void 0 : _a.call(props, e);
|
|
30
|
-
}, onBlur: props.onBlur })), _jsx(
|
|
31
|
-
|
|
32
|
-
|
|
31
|
+
}, onBlur: props.onBlur })), _jsx(HiddenInput, { name: props.name, value: state.value, onReset: (value) => {
|
|
32
|
+
state.setValue(value);
|
|
33
|
+
}, disabled: state.disabled, form: props.form }), !isMobile && (_jsx(Popup, { anchorRef: anchorRef, open: isOpen, children: _jsxs("div", { className: b('popup-content'), children: [_jsx(Calendar, Object.assign({}, calendarProps)), props.hasTime ? (_jsx("div", { className: b('time-field-wrapper'), children: _jsx(DateField, Object.assign({ className: b('time-field') }, timeInputProps)) })) : null] }) }))] })));
|
|
33
34
|
}
|
|
@@ -1,7 +1,5 @@
|
|
|
1
1
|
import React from 'react';
|
|
2
|
-
import { dateTimeParse,
|
|
3
|
-
// @ts-expect-error added in new version of date-utils
|
|
4
|
-
isLikeRelative, isValid, } from '@gravity-ui/date-utils';
|
|
2
|
+
import { dateTimeParse, isLikeRelative, isValid } from '@gravity-ui/date-utils';
|
|
5
3
|
import { useControlledState } from '@gravity-ui/uikit';
|
|
6
4
|
export function useRelativeDateFieldState(props) {
|
|
7
5
|
var _a;
|
|
@@ -15,7 +13,7 @@ export function useRelativeDateFieldState(props) {
|
|
|
15
13
|
return;
|
|
16
14
|
}
|
|
17
15
|
setText(t);
|
|
18
|
-
if (
|
|
16
|
+
if (isLikeRelative(t)) {
|
|
19
17
|
const date = dateTimeParse(t);
|
|
20
18
|
if (date && isValid(date)) {
|
|
21
19
|
setValue(t);
|
|
@@ -34,7 +32,7 @@ export function useRelativeDateFieldState(props) {
|
|
|
34
32
|
return null;
|
|
35
33
|
}
|
|
36
34
|
return (_a = dateTimeParse(value, { timeZone: props.timeZone, roundUp: props.roundUp })) !== null && _a !== void 0 ? _a : null;
|
|
37
|
-
}, [value, props.timeZone]);
|
|
35
|
+
}, [value, props.timeZone, props.roundUp]);
|
|
38
36
|
const [lastCorrectDate, setLastCorrectDate] = React.useState(parsedDate);
|
|
39
37
|
if (parsedDate && parsedDate !== lastCorrectDate) {
|
|
40
38
|
setLastCorrectDate(parsedDate);
|
|
@@ -57,9 +55,3 @@ export function useRelativeDateFieldState(props) {
|
|
|
57
55
|
readOnly: props.readOnly,
|
|
58
56
|
};
|
|
59
57
|
}
|
|
60
|
-
function isLikeRelativeDate(text) {
|
|
61
|
-
if (typeof isLikeRelative === 'function') {
|
|
62
|
-
return isLikeRelative(text);
|
|
63
|
-
}
|
|
64
|
-
return /^now/i.test(text);
|
|
65
|
-
}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
'use client';
|
|
1
2
|
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
3
|
import React from 'react';
|
|
3
4
|
import { Calendar as CalendarIcon, Clock as ClockIcon, Function as FunctionIcon, } from '@gravity-ui/icons';
|
|
@@ -7,11 +8,13 @@ import { Calendar } from '../Calendar';
|
|
|
7
8
|
import { DateField } from '../DateField';
|
|
8
9
|
import { MobileCalendar } from '../DatePicker/MobileCalendar';
|
|
9
10
|
import { StubButton } from '../DatePicker/StubButton';
|
|
11
|
+
import { HiddenInput } from '../HiddenInput/HiddenInput';
|
|
10
12
|
import { useRelativeDatePickerProps } from './hooks/useRelativeDatePickerProps';
|
|
11
13
|
import { useRelativeDatePickerState } from './hooks/useRelativeDatePickerState';
|
|
12
14
|
import './RelativeDatePicker.css';
|
|
13
15
|
const b = block('relative-date-picker');
|
|
14
16
|
export function RelativeDatePicker(props) {
|
|
17
|
+
var _a;
|
|
15
18
|
const state = useRelativeDatePickerState(props);
|
|
16
19
|
const { groupProps, fieldProps, modeSwitcherProps, calendarButtonProps, popupProps, calendarProps, timeInputProps, } = useRelativeDatePickerProps(state, props);
|
|
17
20
|
const anchorRef = React.useRef(null);
|
|
@@ -23,9 +26,9 @@ export function RelativeDatePicker(props) {
|
|
|
23
26
|
readOnly: props.readOnly,
|
|
24
27
|
placeholderValue: props.placeholderValue,
|
|
25
28
|
timeZone: props.timeZone,
|
|
26
|
-
} })), _jsx(TextInput, Object.assign({}, fieldProps, { controlProps: Object.assign(Object.assign({}, fieldProps.controlProps), { disabled: isMobile && state.mode === 'absolute', className: b('input', { mobile: isMobile && state.mode === 'absolute' }) }), hasClear: props.hasClear && !(isMobile && state.mode === 'absolute'), startContent: _jsx(Button, Object.assign({}, modeSwitcherProps, { children: _jsx(Icon, { data: FunctionIcon }) })), endContent: _jsxs(React.Fragment, { children: [!isMobile && !isOnlyTime && (_jsx(Button, Object.assign({}, calendarButtonProps, { children: _jsx(Icon, { data: CalendarIcon }) }))), !isMobile && isOnlyTime && (_jsx(StubButton, { size: calendarButtonProps.size, icon: ClockIcon })), isMobile && state.mode === 'absolute' && (_jsx(StubButton, { size: calendarButtonProps.size, icon: isOnlyTime ? ClockIcon : CalendarIcon }))] }) })), _jsx(
|
|
27
|
-
|
|
28
|
-
|
|
29
|
+
} })), _jsx(TextInput, Object.assign({}, fieldProps, { controlProps: Object.assign(Object.assign({}, fieldProps.controlProps), { disabled: isMobile && state.mode === 'absolute', className: b('input', { mobile: isMobile && state.mode === 'absolute' }) }), hasClear: props.hasClear && !(isMobile && state.mode === 'absolute'), startContent: _jsx(Button, Object.assign({}, modeSwitcherProps, { children: _jsx(Icon, { data: FunctionIcon }) })), endContent: _jsxs(React.Fragment, { children: [!isMobile && !isOnlyTime && (_jsx(Button, Object.assign({}, calendarButtonProps, { children: _jsx(Icon, { data: CalendarIcon }) }))), !isMobile && isOnlyTime && (_jsx(StubButton, { size: calendarButtonProps.size, icon: ClockIcon })), isMobile && state.mode === 'absolute' && (_jsx(StubButton, { size: calendarButtonProps.size, icon: isOnlyTime ? ClockIcon : CalendarIcon }))] }) })), _jsx(HiddenInput, { name: props.name, value: (_a = state.value) === null || _a === void 0 ? void 0 : _a.type, disabled: state.disabled, form: props.form }), _jsx(HiddenInput, { name: props.name, value: state.value, toStringValue: (value) => getNativeValue(value), onReset: (value) => {
|
|
30
|
+
state.setValue(value);
|
|
31
|
+
}, disabled: state.disabled, form: props.form }), !isMobile && !isOnlyTime && (_jsx(Popup, Object.assign({}, popupProps, { anchorRef: anchorRef, children: _jsxs("div", { className: b('popup-content'), children: [typeof props.children === 'function' ? (props.children(calendarProps)) : (_jsx(Calendar, Object.assign({}, calendarProps))), state.datePickerState.hasTime && (_jsx("div", { className: b('time-field-wrapper'), children: _jsx(DateField, Object.assign({}, timeInputProps)) }))] }) })))] })));
|
|
29
32
|
}
|
|
30
33
|
function getNativeValue(value) {
|
|
31
34
|
if (!value) {
|
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
import type { DateTime } from '@gravity-ui/date-utils';
|
|
2
2
|
import type { Value } from '../RelativeDatePicker';
|
|
3
|
-
import type { DomProps, FocusableProps, InputBase, RangeValue, StyleProps, TextInputProps, Validation } from '../types';
|
|
3
|
+
import type { DomProps, FocusableProps, InputBase, InputDOMProps, RangeValue, StyleProps, TextInputProps, Validation } from '../types';
|
|
4
4
|
import type { Preset } from './components/Presets/defaultPresets';
|
|
5
5
|
import type { PresetTab } from './components/Presets/utils';
|
|
6
6
|
import type { RelativeRangeDatePickerStateOptions } from './hooks/useRelativeRangeDatePickerState';
|
|
7
7
|
import './RelativeRangeDatePicker.css';
|
|
8
|
-
export interface RelativeRangeDatePickerProps extends RelativeRangeDatePickerStateOptions, DomProps, InputBase, TextInputProps, Validation, FocusableProps, StyleProps {
|
|
8
|
+
export interface RelativeRangeDatePickerProps extends RelativeRangeDatePickerStateOptions, DomProps, InputBase, InputDOMProps, TextInputProps, Validation, FocusableProps, StyleProps {
|
|
9
9
|
/** Format of the date when rendered in the input. [Available formats](https://day.js.org/docs/en/display/format) */
|
|
10
10
|
format?: string;
|
|
11
11
|
/** A placeholder date that controls the default values of each segment when the user first interacts with them. Defaults to today's date at midnight. */
|
|
@@ -1,13 +1,13 @@
|
|
|
1
|
+
'use client';
|
|
1
2
|
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
3
|
import React from 'react';
|
|
3
|
-
import { dateTimeParse } from '@gravity-ui/date-utils';
|
|
4
4
|
import { Calendar as CalendarIcon } from '@gravity-ui/icons';
|
|
5
|
-
import { Button, Icon,
|
|
5
|
+
import { Button, Icon, TextInput, useFocusWithin, useMobile } from '@gravity-ui/uikit';
|
|
6
6
|
import { block } from '../../utils/cn';
|
|
7
|
+
import { HiddenInput } from '../HiddenInput/HiddenInput';
|
|
7
8
|
import { getButtonSizeForInput } from '../utils/getButtonSizeForInput';
|
|
8
9
|
import { PickerDialog } from './components/PickerDialog/PickerDialog';
|
|
9
10
|
import { useRelativeRangeDatePickerState } from './hooks/useRelativeRangeDatePickerState';
|
|
10
|
-
import { i18n } from './i18n';
|
|
11
11
|
import { getDefaultTitle } from './utils';
|
|
12
12
|
import './RelativeRangeDatePicker.css';
|
|
13
13
|
const b = block('relative-range-date-picker');
|
|
@@ -51,46 +51,53 @@ export function RelativeRangeDatePicker(props) {
|
|
|
51
51
|
}), [alwaysShowAsAbsolute, format, getRangeTitle, presetTabs, state.timeZone, state.value]);
|
|
52
52
|
const validationState = props.validationState || (state.isInvalid ? 'invalid' : undefined);
|
|
53
53
|
const errorMessage = (_a = props.errorMessage) !== null && _a !== void 0 ? _a : state.errors.join('\n');
|
|
54
|
-
return (_jsxs("div", Object.assign({ ref: anchorRef }, focusWithinProps, { className: b(null, props.className), style: props.style, children: [_jsx(
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
54
|
+
return (_jsxs("div", Object.assign({ ref: anchorRef }, focusWithinProps, { className: b(null, props.className), style: props.style, children: [_jsx(TextInput, { autoFocus: props.autoFocus, controlRef: inputRef, value: text, placeholder: props.placeholder, onUpdate: (v) => {
|
|
55
|
+
if (!props.readOnly && !v) {
|
|
56
|
+
state.setValue(null, 'default');
|
|
57
|
+
}
|
|
58
|
+
}, controlProps: {
|
|
59
|
+
'aria-haspopup': 'dialog',
|
|
60
|
+
'aria-expanded': open,
|
|
61
|
+
disabled: isMobile,
|
|
62
|
+
className: b('input', { mobile: isMobile }),
|
|
63
|
+
}, onKeyDown: (e) => {
|
|
64
|
+
if (props.disabled) {
|
|
65
|
+
return;
|
|
66
|
+
}
|
|
67
|
+
if (e.altKey && (e.key === 'ArrowDown' || e.key === 'ArrowUp')) {
|
|
68
|
+
e.preventDefault();
|
|
69
|
+
setOpen(true);
|
|
70
|
+
}
|
|
71
|
+
}, onFocus: () => {
|
|
72
|
+
if (!isActive) {
|
|
73
|
+
setIsActive(true);
|
|
74
|
+
setOpen(true);
|
|
75
|
+
}
|
|
76
|
+
}, validationState: validationState, errorMessage: errorMessage, errorPlacement: props.errorPlacement, pin: props.pin, size: props.size, label: props.label, hasClear: props.hasClear, disabled: props.disabled, endContent: _jsx(Button, { view: "flat-secondary", size: getButtonSizeForInput(props.size), disabled: props.disabled, extraProps: {
|
|
59
77
|
'aria-haspopup': 'dialog',
|
|
60
78
|
'aria-expanded': open,
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
return;
|
|
66
|
-
}
|
|
67
|
-
if (e.altKey && (e.key === 'ArrowDown' || e.key === 'ArrowUp')) {
|
|
68
|
-
e.preventDefault();
|
|
69
|
-
setOpen(true);
|
|
70
|
-
}
|
|
71
|
-
}, onFocus: () => {
|
|
72
|
-
if (!isActive) {
|
|
73
|
-
setIsActive(true);
|
|
74
|
-
setOpen(true);
|
|
75
|
-
}
|
|
76
|
-
}, validationState: validationState, errorMessage: errorMessage, errorPlacement: props.errorPlacement, pin: props.pin, size: props.size, label: props.label, hasClear: props.hasClear, disabled: props.disabled, endContent: _jsx(Button, { view: "flat-secondary", size: getButtonSizeForInput(props.size), disabled: props.disabled, extraProps: {
|
|
77
|
-
'aria-haspopup': 'dialog',
|
|
78
|
-
'aria-expanded': open,
|
|
79
|
-
}, onClick: () => {
|
|
80
|
-
setIsActive(true);
|
|
81
|
-
setOpen(!open);
|
|
82
|
-
}, children: _jsx(Icon, { data: CalendarIcon }) }) }) }), isMobile ? (_jsx("button", { className: b('mobile-trigger', {
|
|
79
|
+
}, onClick: () => {
|
|
80
|
+
setIsActive(true);
|
|
81
|
+
setOpen(!open);
|
|
82
|
+
}, children: _jsx(Icon, { data: CalendarIcon }) }) }), isMobile ? (_jsx("button", { className: b('mobile-trigger', {
|
|
83
83
|
'has-clear': Boolean(props.hasClear && state.value),
|
|
84
84
|
'has-errors': state.isInvalid && props.errorPlacement === 'inside',
|
|
85
85
|
size: props.size,
|
|
86
86
|
}), onClick: () => {
|
|
87
87
|
setIsActive(true);
|
|
88
88
|
setOpen(true);
|
|
89
|
-
} })) : null, _jsx(
|
|
89
|
+
} })) : null, _jsx(HiddenInput, { name: props.name, form: props.form, value: state.value, toStringValue: (v) => { var _a, _b; return (_b = (_a = v === null || v === void 0 ? void 0 : v.start) === null || _a === void 0 ? void 0 : _a.type) !== null && _b !== void 0 ? _b : ''; }, disabled: props.disabled }), _jsx(HiddenInput, { name: props.name, form: props.form, value: state.value, toStringValue: (v) => { var _a; return getNativeValue((_a = v === null || v === void 0 ? void 0 : v.start) !== null && _a !== void 0 ? _a : null); }, disabled: props.disabled }), _jsx(HiddenInput, { name: props.name, form: props.form, value: state.value, toStringValue: (v) => { var _a, _b; return (_b = (_a = v === null || v === void 0 ? void 0 : v.end) === null || _a === void 0 ? void 0 : _a.type) !== null && _b !== void 0 ? _b : ''; }, disabled: props.disabled }), _jsx(HiddenInput, { name: props.name, form: props.form, value: state.value, toStringValue: (v) => { var _a; return getNativeValue((_a = v === null || v === void 0 ? void 0 : v.end) !== null && _a !== void 0 ? _a : null); }, disabled: props.disabled }), _jsx(HiddenInput, { name: props.name, form: props.form, onReset: (v) => {
|
|
90
|
+
state.setValue(v.value, v.timeZone);
|
|
91
|
+
}, value: { value: state.value, timeZone: state.timeZone }, toStringValue: (v) => v.timeZone, disabled: props.disabled }), _jsx(PickerDialog, { state: state, props: props, open: open, onClose: () => {
|
|
90
92
|
setOpen(false);
|
|
91
93
|
}, anchorRef: anchorRef, isMobile: isMobile, className: props.className })] })));
|
|
92
94
|
}
|
|
93
|
-
function
|
|
94
|
-
|
|
95
|
-
|
|
95
|
+
function getNativeValue(value) {
|
|
96
|
+
if (!value) {
|
|
97
|
+
return '';
|
|
98
|
+
}
|
|
99
|
+
if (value.type === 'relative') {
|
|
100
|
+
return value.value;
|
|
101
|
+
}
|
|
102
|
+
return value.value.toISOString();
|
|
96
103
|
}
|
|
@@ -29,6 +29,11 @@ export interface InputDOMProps {
|
|
|
29
29
|
* The name of the input element, used when submitting an HTML form.
|
|
30
30
|
*/
|
|
31
31
|
name?: string;
|
|
32
|
+
/**
|
|
33
|
+
* The id of a <form> in the same document. If this attribute is not set,
|
|
34
|
+
* the <input> is associated with its ancestor <form> element, if any.
|
|
35
|
+
*/
|
|
36
|
+
form?: string;
|
|
32
37
|
}
|
|
33
38
|
export interface InputBase {
|
|
34
39
|
/**
|
|
@@ -34,9 +34,9 @@ export function mergeProps(...args) {
|
|
|
34
34
|
}
|
|
35
35
|
function chain(...fns) {
|
|
36
36
|
return (...args) => {
|
|
37
|
-
for (const
|
|
38
|
-
if (typeof
|
|
39
|
-
|
|
37
|
+
for (const fn of fns) {
|
|
38
|
+
if (typeof fn === 'function') {
|
|
39
|
+
fn(...args);
|
|
40
40
|
}
|
|
41
41
|
}
|
|
42
42
|
};
|
|
@@ -6,13 +6,13 @@ export function getValidationResult(value, minValue, maxValue, isDateUnavailable
|
|
|
6
6
|
const isInvalid = rangeOverflow || rangeUnderflow || isUnavailable;
|
|
7
7
|
const errors = [];
|
|
8
8
|
if (isInvalid) {
|
|
9
|
-
if (rangeUnderflow) {
|
|
9
|
+
if (rangeUnderflow && minValue) {
|
|
10
10
|
errors.push(i18n('Value must be {minValue} or later.', {
|
|
11
11
|
minValue: minValue.timeZone(timeZone).format(),
|
|
12
12
|
value: valueTitle,
|
|
13
13
|
}));
|
|
14
14
|
}
|
|
15
|
-
if (rangeOverflow) {
|
|
15
|
+
if (rangeOverflow && maxValue) {
|
|
16
16
|
errors.push(i18n('Value must be {maxValue} or earlier.', {
|
|
17
17
|
maxValue: maxValue.timeZone(timeZone).format(),
|
|
18
18
|
value: valueTitle,
|