@alfalab/core-components-calendar-range 4.2.4 → 5.1.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/CHANGELOG.md +92 -0
- package/dist/Component.d.ts +25 -3
- package/dist/Component.js +19 -185
- package/dist/cssm/Component.d.ts +25 -3
- package/dist/cssm/Component.js +20 -184
- package/dist/cssm/hooks.d.ts +27 -0
- package/dist/cssm/hooks.js +149 -0
- package/dist/cssm/index.js +15 -6
- package/dist/cssm/tslib.es6-ce870b46.d.ts +32 -0
- package/dist/cssm/tslib.es6-ce870b46.js +42 -0
- package/dist/cssm/utils.d.ts +1 -10
- package/dist/cssm/utils.js +0 -15
- package/dist/cssm/{index.module.css → views/index.module.css} +30 -0
- package/dist/cssm/views/popover.d.ts +5 -0
- package/dist/cssm/views/popover.js +118 -0
- package/dist/cssm/views/static.d.ts +5 -0
- package/dist/cssm/views/static.js +170 -0
- package/dist/esm/Component.d.ts +25 -3
- package/dist/esm/Component.js +20 -185
- package/dist/esm/hooks.d.ts +27 -0
- package/dist/esm/hooks.js +134 -0
- package/dist/esm/index.js +14 -5
- package/dist/esm/index.module-bbf7d46d.d.ts +32 -0
- package/dist/esm/index.module-bbf7d46d.js +43 -0
- package/dist/esm/utils.d.ts +1 -10
- package/dist/esm/utils.js +1 -14
- package/dist/esm/views/index.css +66 -0
- package/dist/esm/views/popover.d.ts +5 -0
- package/dist/esm/views/popover.js +106 -0
- package/dist/esm/views/static.d.ts +5 -0
- package/dist/esm/views/static.js +154 -0
- package/dist/hooks.d.ts +27 -0
- package/dist/hooks.js +149 -0
- package/dist/index.js +14 -5
- package/dist/index.module-a9bc9731.d.ts +32 -0
- package/dist/index.module-a9bc9731.js +46 -0
- package/dist/modern/Component.d.ts +25 -3
- package/dist/modern/Component.js +20 -168
- package/dist/modern/hooks.d.ts +27 -0
- package/dist/modern/hooks.js +130 -0
- package/dist/modern/index.js +13 -4
- package/dist/modern/index.module-4c141936.js +4 -0
- package/dist/modern/utils.d.ts +1 -10
- package/dist/modern/utils.js +1 -14
- package/dist/modern/views/index.css +66 -0
- package/dist/modern/views/popover.d.ts +5 -0
- package/dist/modern/views/popover.js +115 -0
- package/dist/modern/views/static.d.ts +5 -0
- package/dist/modern/views/static.js +154 -0
- package/dist/utils.d.ts +1 -10
- package/dist/utils.js +0 -15
- package/dist/views/index.css +66 -0
- package/dist/views/popover.d.ts +5 -0
- package/dist/views/popover.js +116 -0
- package/dist/views/static.d.ts +5 -0
- package/dist/views/static.js +168 -0
- package/package.json +6 -6
- package/dist/cssm/useCalendarMaxMinDates.d.ts +0 -16
- package/dist/cssm/useCalendarMaxMinDates.js +0 -22
- package/dist/cssm/useCalendarMonthes.d.ts +0 -14
- package/dist/cssm/useCalendarMonthes.js +0 -54
- package/dist/esm/index.css +0 -36
- package/dist/esm/useCalendarMaxMinDates.d.ts +0 -16
- package/dist/esm/useCalendarMaxMinDates.js +0 -18
- package/dist/esm/useCalendarMonthes.d.ts +0 -14
- package/dist/esm/useCalendarMonthes.js +0 -50
- package/dist/index.css +0 -36
- package/dist/modern/index.css +0 -36
- package/dist/modern/useCalendarMaxMinDates.d.ts +0 -16
- package/dist/modern/useCalendarMaxMinDates.js +0 -17
- package/dist/modern/useCalendarMonthes.d.ts +0 -14
- package/dist/modern/useCalendarMonthes.js +0 -49
- package/dist/useCalendarMaxMinDates.d.ts +0 -16
- package/dist/useCalendarMaxMinDates.js +0 -22
- package/dist/useCalendarMonthes.d.ts +0 -14
- package/dist/useCalendarMonthes.js +0 -54
package/dist/modern/Component.js
CHANGED
|
@@ -1,172 +1,24 @@
|
|
|
1
|
-
import React
|
|
2
|
-
import
|
|
3
|
-
import
|
|
4
|
-
import
|
|
5
|
-
import
|
|
6
|
-
import
|
|
7
|
-
import
|
|
8
|
-
import
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import 'classnames';
|
|
3
|
+
import 'date-fns/startOfMonth';
|
|
4
|
+
import '@alfalab/core-components-calendar-input/dist/modern';
|
|
5
|
+
import '@alfalab/core-components-date-input/dist/modern';
|
|
6
|
+
import 'date-fns/addMonths';
|
|
7
|
+
import 'date-fns/subMonths';
|
|
8
|
+
import 'date-fns/max';
|
|
9
|
+
import 'date-fns/min';
|
|
10
|
+
import 'date-fns/isEqual';
|
|
11
|
+
import './hooks.js';
|
|
12
|
+
import './index.module-4c141936.js';
|
|
13
|
+
import { CalendarRangePopover } from './views/popover.js';
|
|
14
|
+
import 'date-fns/endOfMonth';
|
|
15
|
+
import '@alfalab/core-components-calendar/dist/modern';
|
|
16
|
+
import './utils.js';
|
|
17
|
+
import { CalendarRangeStatic } from './views/static.js';
|
|
9
18
|
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
/* eslint-disable multiline-comment-style */
|
|
14
|
-
const CalendarRange = ({ className, defaultMonth = startOfMonth(new Date()).getTime(), minDate, maxDate, valueFrom, valueTo, onDateFromChange, onDateToChange, inputFromProps = {}, inputToProps = {}, calendarPosition = 'static', dataTestId, }) => {
|
|
15
|
-
const uncontrolled = valueFrom === undefined && valueTo === undefined;
|
|
16
|
-
const isPopover = calendarPosition === 'popover';
|
|
17
|
-
const period = usePeriod({
|
|
18
|
-
initialSelectedFrom: valueFrom ? parseDateString(valueFrom).getTime() : undefined,
|
|
19
|
-
initialSelectedTo: valueTo ? parseDateString(valueTo).getTime() : undefined,
|
|
20
|
-
});
|
|
21
|
-
const { setStart, setEnd, resetPeriod } = period;
|
|
22
|
-
let { selectedFrom, selectedTo } = period;
|
|
23
|
-
if (!dateInLimits(selectedFrom, minDate, maxDate))
|
|
24
|
-
selectedFrom = undefined;
|
|
25
|
-
if (!dateInLimits(selectedTo, minDate, maxDate))
|
|
26
|
-
selectedTo = undefined;
|
|
27
|
-
const [stateFrom, setStateFrom] = useState(initialValueState);
|
|
28
|
-
const [stateTo, setStateTo] = useState(initialValueState);
|
|
29
|
-
const inputValueFrom = getCorrectValueState(stateFrom, valueFrom, minDate, maxDate);
|
|
30
|
-
const inputValueTo = getCorrectValueState(stateTo, valueTo, minDate, maxDate);
|
|
31
|
-
const handleStateFromChange = useCallback((newFromState) => {
|
|
32
|
-
if (uncontrolled)
|
|
33
|
-
setStateFrom(newFromState);
|
|
34
|
-
if (onDateFromChange)
|
|
35
|
-
onDateFromChange(newFromState);
|
|
36
|
-
}, [onDateFromChange, uncontrolled]);
|
|
37
|
-
const handleStateToChange = useCallback((newToState) => {
|
|
38
|
-
if (uncontrolled)
|
|
39
|
-
setStateTo(newToState);
|
|
40
|
-
if (onDateToChange)
|
|
41
|
-
onDateToChange(newToState);
|
|
42
|
-
}, [onDateToChange, uncontrolled]);
|
|
43
|
-
const { monthFrom, monthTo, handleMonthFromChange, handleMonthToChange } = useCalendarMonthes({
|
|
44
|
-
inputValueFrom,
|
|
45
|
-
inputValueTo,
|
|
46
|
-
defaultMonth,
|
|
47
|
-
isPopover,
|
|
48
|
-
});
|
|
49
|
-
const handleInputFromChange = useCallback((_, { value, date }) => {
|
|
50
|
-
if (value === '') {
|
|
51
|
-
setStart(undefined);
|
|
52
|
-
handleStateFromChange(initialValueState);
|
|
53
|
-
}
|
|
54
|
-
if (!isCompleteDateInput(value)) {
|
|
55
|
-
return;
|
|
56
|
-
}
|
|
57
|
-
if (dateInLimits(date, minDate, maxDate)) {
|
|
58
|
-
setStart(date.getTime());
|
|
59
|
-
handleMonthFromChange(startOfMonth(date).getTime());
|
|
60
|
-
handleStateFromChange({ date: date.getTime(), value });
|
|
61
|
-
}
|
|
62
|
-
else {
|
|
63
|
-
setStart(undefined);
|
|
64
|
-
handleStateFromChange({ date: null, value });
|
|
65
|
-
}
|
|
66
|
-
}, [handleMonthFromChange, handleStateFromChange, maxDate, minDate, setStart]);
|
|
67
|
-
const handleInputToChange = useCallback((_, { value, date }) => {
|
|
68
|
-
if (value === '') {
|
|
69
|
-
setEnd(undefined);
|
|
70
|
-
handleStateToChange(initialValueState);
|
|
71
|
-
}
|
|
72
|
-
if (!isCompleteDateInput(value)) {
|
|
73
|
-
return;
|
|
74
|
-
}
|
|
75
|
-
if (dateInLimits(date, minDate, maxDate)) {
|
|
76
|
-
setEnd(date.getTime());
|
|
77
|
-
handleMonthToChange(subMonths(startOfMonth(date), 1).getTime());
|
|
78
|
-
handleStateToChange({ date: date.getTime(), value });
|
|
79
|
-
}
|
|
80
|
-
else {
|
|
81
|
-
setEnd(undefined);
|
|
82
|
-
handleStateToChange({ date: null, value });
|
|
83
|
-
}
|
|
84
|
-
}, [handleMonthToChange, handleStateToChange, maxDate, minDate, setEnd]);
|
|
85
|
-
const handleCalendarChange = useCallback((date) => {
|
|
86
|
-
if (!inputValueFrom.date) {
|
|
87
|
-
setStart(date);
|
|
88
|
-
handleStateFromChange({ date, value: formatDate(date) });
|
|
89
|
-
return;
|
|
90
|
-
}
|
|
91
|
-
if (date < inputValueFrom.date) {
|
|
92
|
-
resetPeriod();
|
|
93
|
-
setStart(date);
|
|
94
|
-
handleStateToChange(initialValueState);
|
|
95
|
-
handleStateFromChange({ date, value: formatDate(date) });
|
|
96
|
-
return;
|
|
97
|
-
}
|
|
98
|
-
if (date === inputValueFrom.date && date === inputValueTo.date) {
|
|
99
|
-
resetPeriod();
|
|
100
|
-
handleStateFromChange(initialValueState);
|
|
101
|
-
handleStateToChange(initialValueState);
|
|
102
|
-
return;
|
|
103
|
-
}
|
|
104
|
-
handleStateToChange({ date, value: formatDate(date) });
|
|
105
|
-
setEnd(date);
|
|
106
|
-
}, [
|
|
107
|
-
inputValueFrom.date,
|
|
108
|
-
inputValueTo.date,
|
|
109
|
-
handleStateToChange,
|
|
110
|
-
setEnd,
|
|
111
|
-
setStart,
|
|
112
|
-
handleStateFromChange,
|
|
113
|
-
resetPeriod,
|
|
114
|
-
]);
|
|
115
|
-
const handleFromCalendarChange = useCallback((date) => {
|
|
116
|
-
if (!isPopover) {
|
|
117
|
-
handleCalendarChange(date);
|
|
118
|
-
return;
|
|
119
|
-
}
|
|
120
|
-
setStart(date);
|
|
121
|
-
handleStateFromChange({ date, value: formatDate(date) });
|
|
122
|
-
}, [handleCalendarChange, handleStateFromChange, isPopover, setStart]);
|
|
123
|
-
const handleToCalendarChange = useCallback((date) => {
|
|
124
|
-
if (!isPopover) {
|
|
125
|
-
handleCalendarChange(date);
|
|
126
|
-
return;
|
|
127
|
-
}
|
|
128
|
-
handleStateToChange({ date, value: formatDate(date) });
|
|
129
|
-
setEnd(date);
|
|
130
|
-
}, [handleCalendarChange, handleStateToChange, isPopover, setEnd]);
|
|
131
|
-
const [nextMonthHighlighted, setNextMonthHighlighted] = useState(false);
|
|
132
|
-
const handleCalendarToMouseOver = useCallback((event) => {
|
|
133
|
-
const target = event.target;
|
|
134
|
-
const dayHighlighted = isDayButton(target) || isDayButton(target.parentElement);
|
|
135
|
-
if (nextMonthHighlighted && !dayHighlighted)
|
|
136
|
-
setNextMonthHighlighted(false);
|
|
137
|
-
if (!nextMonthHighlighted && dayHighlighted)
|
|
138
|
-
setNextMonthHighlighted(true);
|
|
139
|
-
}, [nextMonthHighlighted]);
|
|
140
|
-
const selectorView = isPopover ? 'full' : 'month-only';
|
|
141
|
-
const calendarSelectedTo = selectedTo || (nextMonthHighlighted ? monthTo : undefined);
|
|
142
|
-
const maxMinDates = useCalendarMaxMinDates({
|
|
143
|
-
isPopover,
|
|
144
|
-
monthTo,
|
|
145
|
-
monthFrom,
|
|
146
|
-
selectedFrom,
|
|
147
|
-
selectedTo: calendarSelectedTo,
|
|
148
|
-
maxDate,
|
|
149
|
-
minDate,
|
|
150
|
-
});
|
|
151
|
-
return (React.createElement("div", { className: cn(styles.component, className), "data-test-id": dataTestId },
|
|
152
|
-
React.createElement(CalendarInput, Object.assign({}, inputFromProps, { useAnchorWidth: false, calendarPosition: calendarPosition, onInputChange: handleInputFromChange, onCalendarChange: handleFromCalendarChange, value: inputValueFrom.value, minDate: maxMinDates.fromMinDate, maxDate: maxMinDates.fromMaxDate, calendarProps: {
|
|
153
|
-
...inputFromProps.calendarProps,
|
|
154
|
-
month: monthFrom,
|
|
155
|
-
onMonthChange: handleMonthFromChange,
|
|
156
|
-
selectorView,
|
|
157
|
-
selectedFrom,
|
|
158
|
-
selectedTo: calendarSelectedTo,
|
|
159
|
-
} })),
|
|
160
|
-
React.createElement("span", { className: styles.divider }),
|
|
161
|
-
React.createElement("div", { onMouseOver: handleCalendarToMouseOver },
|
|
162
|
-
React.createElement(CalendarInput, Object.assign({}, inputToProps, { useAnchorWidth: false, calendarPosition: calendarPosition, popoverPosition: 'bottom-end', onInputChange: handleInputToChange, onCalendarChange: handleToCalendarChange, value: inputValueTo.value, minDate: maxMinDates.toMinDate, maxDate: maxMinDates.toMaxDate, calendarProps: {
|
|
163
|
-
...inputToProps.calendarProps,
|
|
164
|
-
month: monthTo,
|
|
165
|
-
onMonthChange: handleMonthToChange,
|
|
166
|
-
selectorView,
|
|
167
|
-
selectedFrom,
|
|
168
|
-
selectedTo,
|
|
169
|
-
} })))));
|
|
19
|
+
const CalendarRange = ({ calendarPosition = 'static', ...restProps }) => {
|
|
20
|
+
const View = calendarPosition === 'popover' ? CalendarRangePopover : CalendarRangeStatic;
|
|
21
|
+
return React.createElement(View, Object.assign({}, restProps));
|
|
170
22
|
};
|
|
171
23
|
|
|
172
24
|
export { CalendarRange };
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
declare function usePopoverViewMonthes({ dateFrom, dateTo, defaultMonth, resetKey, }: {
|
|
2
|
+
defaultMonth: number;
|
|
3
|
+
dateFrom: number | null;
|
|
4
|
+
dateTo: number | null;
|
|
5
|
+
resetKey?: number;
|
|
6
|
+
}): {
|
|
7
|
+
monthFrom: number | undefined;
|
|
8
|
+
monthTo: number | undefined;
|
|
9
|
+
handleMonthFromChange: (newMonthFrom: number) => void;
|
|
10
|
+
handleMonthToChange: (newMonthTo: number) => void;
|
|
11
|
+
};
|
|
12
|
+
declare function useStaticViewMonthes({ selectedFrom, selectedTo, defaultMonth, }: {
|
|
13
|
+
selectedFrom?: number;
|
|
14
|
+
selectedTo?: number;
|
|
15
|
+
defaultMonth: number;
|
|
16
|
+
}): {
|
|
17
|
+
monthFrom: number;
|
|
18
|
+
monthTo: number;
|
|
19
|
+
handleMonthFromChange: (newMonthFrom: number) => void;
|
|
20
|
+
handleMonthToChange: (newMonthTo: number) => void;
|
|
21
|
+
};
|
|
22
|
+
declare function useSelectionProps(from?: number, to?: number, highlighted?: number): {
|
|
23
|
+
rangeComplete: boolean;
|
|
24
|
+
selectedFrom: number | undefined;
|
|
25
|
+
selectedTo: number | undefined;
|
|
26
|
+
};
|
|
27
|
+
export { usePopoverViewMonthes, useStaticViewMonthes, useSelectionProps };
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
import { useState, useCallback, useEffect, useMemo } from 'react';
|
|
2
|
+
import startOfMonth from 'date-fns/startOfMonth';
|
|
3
|
+
import addMonths from 'date-fns/addMonths';
|
|
4
|
+
import subMonths from 'date-fns/subMonths';
|
|
5
|
+
import max from 'date-fns/max';
|
|
6
|
+
import min from 'date-fns/min';
|
|
7
|
+
import isEqual from 'date-fns/isEqual';
|
|
8
|
+
|
|
9
|
+
function usePopoverViewMonthes({ dateFrom, dateTo, defaultMonth, resetKey, }) {
|
|
10
|
+
const [monthFrom, setMonthFrom] = useState();
|
|
11
|
+
const [monthTo, setMonthTo] = useState();
|
|
12
|
+
const handleMonthFromChange = useCallback((newMonthFrom) => {
|
|
13
|
+
setMonthFrom(newMonthFrom);
|
|
14
|
+
if (!dateTo) {
|
|
15
|
+
setMonthTo(newMonthFrom);
|
|
16
|
+
}
|
|
17
|
+
}, [dateTo]);
|
|
18
|
+
const handleMonthToChange = useCallback((newMonthTo) => {
|
|
19
|
+
setMonthTo(newMonthTo);
|
|
20
|
+
if (!dateFrom) {
|
|
21
|
+
setMonthFrom(newMonthTo);
|
|
22
|
+
}
|
|
23
|
+
}, [dateFrom]);
|
|
24
|
+
useEffect(() => {
|
|
25
|
+
setMonthFrom(dateFrom ? startOfMonth(dateFrom).getTime() : defaultMonth);
|
|
26
|
+
}, [defaultMonth, dateFrom, resetKey]);
|
|
27
|
+
useEffect(() => {
|
|
28
|
+
setMonthTo(dateTo ? startOfMonth(dateTo).getTime() : monthFrom);
|
|
29
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
30
|
+
}, [dateTo, resetKey]);
|
|
31
|
+
return {
|
|
32
|
+
monthFrom,
|
|
33
|
+
monthTo,
|
|
34
|
+
handleMonthFromChange,
|
|
35
|
+
handleMonthToChange,
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
function useStaticViewMonthes({ selectedFrom, selectedTo, defaultMonth, }) {
|
|
39
|
+
/**
|
|
40
|
+
* Если указана начальная дата — левый месяц равен ей, иначе используется дата конца.
|
|
41
|
+
* Если обе даты не указаны, то используется дефолтный месяц
|
|
42
|
+
*/
|
|
43
|
+
const initialMonthFrom = useMemo(() => startOfMonth(selectedFrom || selectedTo || defaultMonth).getTime(),
|
|
44
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
45
|
+
[]);
|
|
46
|
+
/**
|
|
47
|
+
* Правый месяц должен быть как минимум на 1 месяц больше левого
|
|
48
|
+
*/
|
|
49
|
+
const initialMonthTo = useMemo(() => max([
|
|
50
|
+
selectedTo ? startOfMonth(selectedTo) : 0,
|
|
51
|
+
addMonths(initialMonthFrom, 1),
|
|
52
|
+
]).getTime(),
|
|
53
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
54
|
+
[]);
|
|
55
|
+
const [monthFrom, setMonthFrom] = useState(initialMonthFrom);
|
|
56
|
+
const [monthTo, setMonthTo] = useState(initialMonthTo);
|
|
57
|
+
const handleMonthFromChange = useCallback((newMonthFrom) => {
|
|
58
|
+
setMonthFrom(newMonthFrom);
|
|
59
|
+
if (monthTo && isEqual(newMonthFrom, monthTo)) {
|
|
60
|
+
const nextMonth = addMonths(newMonthFrom, 1).getTime();
|
|
61
|
+
setMonthTo(nextMonth);
|
|
62
|
+
}
|
|
63
|
+
}, [monthTo]);
|
|
64
|
+
const handleMonthToChange = useCallback((newMonthTo) => {
|
|
65
|
+
setMonthTo(newMonthTo);
|
|
66
|
+
if (monthFrom && isEqual(newMonthTo, monthFrom)) {
|
|
67
|
+
const prevMonth = subMonths(newMonthTo, 1).getTime();
|
|
68
|
+
setMonthFrom(prevMonth);
|
|
69
|
+
}
|
|
70
|
+
}, [monthFrom]);
|
|
71
|
+
// eslint-disable-next-line complexity
|
|
72
|
+
useEffect(() => {
|
|
73
|
+
const selectedFromMonth = selectedFrom ? startOfMonth(selectedFrom).getTime() : undefined;
|
|
74
|
+
const selectedToMonth = selectedTo ? startOfMonth(selectedTo).getTime() : undefined;
|
|
75
|
+
// Проверяем, показываются ли выбранные месяцы в левой или правой части компонента
|
|
76
|
+
const fromMonthOnLeft = selectedFromMonth && selectedFromMonth === monthFrom;
|
|
77
|
+
const fromMonthOnRight = selectedFromMonth && selectedFromMonth === monthTo;
|
|
78
|
+
const toMonthOnRight = selectedToMonth && selectedToMonth === monthTo;
|
|
79
|
+
const toMonthOnLeft = selectedToMonth && selectedToMonth === monthFrom;
|
|
80
|
+
const fromMonthOnScreen = fromMonthOnLeft || fromMonthOnRight;
|
|
81
|
+
const toMonthOnScreen = toMonthOnLeft || toMonthOnRight;
|
|
82
|
+
if (fromMonthOnLeft && toMonthOnLeft) {
|
|
83
|
+
setMonthTo(max([addMonths(selectedFromMonth, 1), monthTo]).getTime());
|
|
84
|
+
return;
|
|
85
|
+
}
|
|
86
|
+
if (fromMonthOnRight && toMonthOnRight) {
|
|
87
|
+
setMonthFrom(min([subMonths(selectedToMonth, 1), monthFrom]).getTime());
|
|
88
|
+
return;
|
|
89
|
+
}
|
|
90
|
+
if (selectedFromMonth && selectedToMonth) {
|
|
91
|
+
setMonthFrom(selectedFromMonth);
|
|
92
|
+
setMonthTo(max([addMonths(selectedFromMonth, 1), selectedToMonth]).getTime());
|
|
93
|
+
return;
|
|
94
|
+
}
|
|
95
|
+
if (selectedFromMonth && !selectedToMonth && !fromMonthOnScreen) {
|
|
96
|
+
setMonthFrom(selectedFromMonth);
|
|
97
|
+
setMonthTo(max([addMonths(selectedFromMonth, 1), monthTo]).getTime());
|
|
98
|
+
}
|
|
99
|
+
if (selectedToMonth && !selectedFromMonth && !toMonthOnScreen) {
|
|
100
|
+
setMonthTo(selectedToMonth);
|
|
101
|
+
setMonthFrom(min([subMonths(selectedToMonth, 1), monthFrom]).getTime());
|
|
102
|
+
}
|
|
103
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
104
|
+
}, [selectedFrom, selectedTo]);
|
|
105
|
+
return {
|
|
106
|
+
monthFrom,
|
|
107
|
+
monthTo,
|
|
108
|
+
handleMonthFromChange,
|
|
109
|
+
handleMonthToChange,
|
|
110
|
+
};
|
|
111
|
+
}
|
|
112
|
+
function useSelectionProps(from, to, highlighted) {
|
|
113
|
+
return useMemo(() => {
|
|
114
|
+
if (from && to) {
|
|
115
|
+
return {
|
|
116
|
+
rangeComplete: true,
|
|
117
|
+
selectedFrom: min([from, to]).getTime(),
|
|
118
|
+
selectedTo: max([from, to]).getTime(),
|
|
119
|
+
};
|
|
120
|
+
}
|
|
121
|
+
const dates = [from, to, highlighted].filter(date => date !== undefined);
|
|
122
|
+
return {
|
|
123
|
+
rangeComplete: false,
|
|
124
|
+
selectedFrom: from || dates.length === 2 ? min(dates).getTime() : undefined,
|
|
125
|
+
selectedTo: to || dates.length === 2 ? max(dates).getTime() : undefined,
|
|
126
|
+
};
|
|
127
|
+
}, [from, highlighted, to]);
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
export { usePopoverViewMonthes, useSelectionProps, useStaticViewMonthes };
|
package/dist/modern/index.js
CHANGED
|
@@ -1,9 +1,18 @@
|
|
|
1
1
|
import 'react';
|
|
2
2
|
import 'classnames';
|
|
3
|
-
import 'date-fns';
|
|
4
|
-
import '@alfalab/core-components-calendar/dist/modern';
|
|
3
|
+
import 'date-fns/startOfMonth';
|
|
5
4
|
import '@alfalab/core-components-calendar-input/dist/modern';
|
|
5
|
+
import '@alfalab/core-components-date-input/dist/modern';
|
|
6
|
+
import 'date-fns/addMonths';
|
|
7
|
+
import 'date-fns/subMonths';
|
|
8
|
+
import 'date-fns/max';
|
|
9
|
+
import 'date-fns/min';
|
|
10
|
+
import 'date-fns/isEqual';
|
|
11
|
+
import './hooks.js';
|
|
12
|
+
import './index.module-4c141936.js';
|
|
13
|
+
import './views/popover.js';
|
|
14
|
+
import 'date-fns/endOfMonth';
|
|
15
|
+
import '@alfalab/core-components-calendar/dist/modern';
|
|
6
16
|
import './utils.js';
|
|
7
|
-
import './
|
|
8
|
-
import './useCalendarMaxMinDates.js';
|
|
17
|
+
import './views/static.js';
|
|
9
18
|
export { CalendarRange } from './Component.js';
|
package/dist/modern/utils.d.ts
CHANGED
|
@@ -1,11 +1,2 @@
|
|
|
1
|
-
type ValueState = {
|
|
2
|
-
date: number | null;
|
|
3
|
-
value: string;
|
|
4
|
-
};
|
|
5
|
-
declare const initialValueState: {
|
|
6
|
-
date: null;
|
|
7
|
-
value: string;
|
|
8
|
-
};
|
|
9
|
-
declare const getCorrectValueState: (stateValue: ValueState, propValue?: string | undefined, minDate?: number | undefined, maxDate?: number | undefined) => ValueState;
|
|
10
1
|
declare const isDayButton: (node: HTMLElement | null) => string | false | null | undefined;
|
|
11
|
-
export {
|
|
2
|
+
export { isDayButton };
|
package/dist/modern/utils.js
CHANGED
|
@@ -1,16 +1,3 @@
|
|
|
1
|
-
import { dateInLimits } from '@alfalab/core-components-calendar/dist/modern';
|
|
2
|
-
import { parseDateString } from '@alfalab/core-components-calendar-input/dist/modern';
|
|
3
|
-
|
|
4
|
-
const initialValueState = { date: null, value: '' };
|
|
5
|
-
const getCorrectValueState = (stateValue, propValue, minDate, maxDate) => {
|
|
6
|
-
const state = propValue === undefined
|
|
7
|
-
? stateValue
|
|
8
|
-
: { value: propValue, date: parseDateString(propValue).getTime() };
|
|
9
|
-
if (!dateInLimits(state.date, minDate, maxDate)) {
|
|
10
|
-
state.date = null;
|
|
11
|
-
}
|
|
12
|
-
return state;
|
|
13
|
-
};
|
|
14
1
|
const isDayButton = (node) => node && node.tagName === 'BUTTON' && node.dataset.date;
|
|
15
2
|
|
|
16
|
-
export {
|
|
3
|
+
export { isDayButton };
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
/* hash: 13dnh */
|
|
2
|
+
:root {
|
|
3
|
+
--color-light-graphic-primary: #0b1f35;
|
|
4
|
+
}
|
|
5
|
+
:root {
|
|
6
|
+
|
|
7
|
+
/* Hard */
|
|
8
|
+
|
|
9
|
+
/* Up */
|
|
10
|
+
|
|
11
|
+
/* Hard up */
|
|
12
|
+
}
|
|
13
|
+
:root {
|
|
14
|
+
--gap-xs: 8px;
|
|
15
|
+
}
|
|
16
|
+
:root {
|
|
17
|
+
--calendar-inner-width: 280px;
|
|
18
|
+
|
|
19
|
+
/* Кнопки выбора месяцев и годов */
|
|
20
|
+
|
|
21
|
+
/* Шапка */
|
|
22
|
+
|
|
23
|
+
/* День */
|
|
24
|
+
|
|
25
|
+
/* today */
|
|
26
|
+
|
|
27
|
+
/* highlighted */
|
|
28
|
+
|
|
29
|
+
/* range */
|
|
30
|
+
|
|
31
|
+
/* selected */
|
|
32
|
+
|
|
33
|
+
/* disabled */
|
|
34
|
+
|
|
35
|
+
/* marker */
|
|
36
|
+
}
|
|
37
|
+
.calendar-range__component_eksd6 {
|
|
38
|
+
display: flex
|
|
39
|
+
}
|
|
40
|
+
.calendar-range__component_eksd6 button[aria-selected='true'] {
|
|
41
|
+
cursor: pointer;
|
|
42
|
+
}
|
|
43
|
+
.calendar-range__component_eksd6 *[class*='errorIcon_'] {
|
|
44
|
+
display: none;
|
|
45
|
+
}
|
|
46
|
+
.calendar-range__component_eksd6 *[class*='calendarIcon_'] {
|
|
47
|
+
margin-right: 0;
|
|
48
|
+
}
|
|
49
|
+
.calendar-range__divider_eksd6 {
|
|
50
|
+
height: 48px;
|
|
51
|
+
display: flex;
|
|
52
|
+
align-items: center;
|
|
53
|
+
justify-content: center;
|
|
54
|
+
width: 16px;
|
|
55
|
+
margin: 0 var(--gap-xs)
|
|
56
|
+
}
|
|
57
|
+
.calendar-range__divider_eksd6:after {
|
|
58
|
+
content: '';
|
|
59
|
+
display: block;
|
|
60
|
+
width: 100%;
|
|
61
|
+
height: 1px;
|
|
62
|
+
background-color: var(--color-light-graphic-primary);
|
|
63
|
+
}
|
|
64
|
+
.calendar-range__static_eksd6 .calendar-range__calendar_eksd6 {
|
|
65
|
+
width: var(--calendar-inner-width);
|
|
66
|
+
}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { FC } from 'react';
|
|
2
|
+
import { CalendarRangeProps } from "../Component";
|
|
3
|
+
type CalendarRangePopoverProps = Omit<CalendarRangeProps, 'calendarPosition'>;
|
|
4
|
+
declare const CalendarRangePopover: FC<CalendarRangePopoverProps>;
|
|
5
|
+
export { CalendarRangePopoverProps, CalendarRangePopover };
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
import React, { useState, useCallback, useEffect } from 'react';
|
|
2
|
+
import cn from 'classnames';
|
|
3
|
+
import startOfMonth from 'date-fns/startOfMonth';
|
|
4
|
+
import { isValidInputValue, parseDateString, CalendarInput } from '@alfalab/core-components-calendar-input/dist/modern';
|
|
5
|
+
import { isCompleteDateInput } from '@alfalab/core-components-date-input/dist/modern';
|
|
6
|
+
import 'date-fns/addMonths';
|
|
7
|
+
import 'date-fns/subMonths';
|
|
8
|
+
import 'date-fns/max';
|
|
9
|
+
import 'date-fns/min';
|
|
10
|
+
import 'date-fns/isEqual';
|
|
11
|
+
import { usePopoverViewMonthes } from '../hooks.js';
|
|
12
|
+
import { s as styles } from '../index.module-4c141936.js';
|
|
13
|
+
|
|
14
|
+
const CalendarRangePopover = ({ className, defaultMonth = startOfMonth(new Date()).getTime(), minDate, maxDate, valueFrom = '', valueTo = '', onDateFromChange = () => null, onDateToChange = () => null, onChange = () => null, inputFromProps = {}, inputToProps = {}, offDays, events, dataTestId, }) => {
|
|
15
|
+
const [inputFromValue, setInputFromValue] = useState(valueFrom);
|
|
16
|
+
const [inputToValue, setInputToValue] = useState(valueTo);
|
|
17
|
+
/**
|
|
18
|
+
* Ключ для сброса календарей
|
|
19
|
+
* Пользователь открыл календарь, изменил месяц, но ничего не выбрал
|
|
20
|
+
* — при следующем открытии в календаре будет установлен начальный месяц
|
|
21
|
+
*/
|
|
22
|
+
const [resetKey, setResetKey] = useState(0);
|
|
23
|
+
const dateFrom = isValidInputValue(inputFromValue, minDate, maxDate, offDays)
|
|
24
|
+
? parseDateString(inputFromValue).getTime()
|
|
25
|
+
: null;
|
|
26
|
+
const dateTo = isValidInputValue(inputToValue, dateFrom || minDate, maxDate, offDays)
|
|
27
|
+
? parseDateString(inputToValue).getTime()
|
|
28
|
+
: null;
|
|
29
|
+
const [inputFromInvalid, setInputFromInvalid] = useState(isCompleteDateInput(inputFromValue) && dateFrom === null);
|
|
30
|
+
const [inputToInvalid, setInputToInvalid] = useState(isCompleteDateInput(inputToValue) && dateTo === null);
|
|
31
|
+
const bothInvalid = isCompleteDateInput(inputFromValue) &&
|
|
32
|
+
isCompleteDateInput(inputToValue) &&
|
|
33
|
+
parseDateString(inputFromValue).getTime() > parseDateString(inputToValue).getTime();
|
|
34
|
+
const { monthFrom, monthTo, handleMonthFromChange, handleMonthToChange, } = usePopoverViewMonthes({
|
|
35
|
+
dateFrom,
|
|
36
|
+
dateTo,
|
|
37
|
+
defaultMonth,
|
|
38
|
+
resetKey,
|
|
39
|
+
});
|
|
40
|
+
const handleValidInputFrom = useCallback(() => {
|
|
41
|
+
setInputFromInvalid(inputFromValue !== '' && !isValidInputValue(inputFromValue, minDate, maxDate, offDays));
|
|
42
|
+
}, [inputFromValue, maxDate, minDate, offDays]);
|
|
43
|
+
const handleValidInputTo = useCallback(() => {
|
|
44
|
+
setInputToInvalid(inputToValue !== '' &&
|
|
45
|
+
!isValidInputValue(inputToValue, dateFrom || minDate, maxDate, offDays));
|
|
46
|
+
}, [dateFrom, inputToValue, maxDate, minDate, offDays]);
|
|
47
|
+
const handleInputFromChange = useCallback((_, payload) => {
|
|
48
|
+
setInputFromValue(payload.value);
|
|
49
|
+
}, []);
|
|
50
|
+
const handleInputToChange = useCallback((_, payload) => {
|
|
51
|
+
setInputToValue(payload.value);
|
|
52
|
+
}, []);
|
|
53
|
+
const handleInputFromBlur = useCallback(() => {
|
|
54
|
+
handleValidInputFrom();
|
|
55
|
+
setResetKey(+new Date());
|
|
56
|
+
}, [handleValidInputFrom]);
|
|
57
|
+
const handleInputToBlur = useCallback(() => {
|
|
58
|
+
handleValidInputTo();
|
|
59
|
+
setResetKey(+new Date());
|
|
60
|
+
}, [handleValidInputTo]);
|
|
61
|
+
const handleFromChange = useCallback((_, payload) => {
|
|
62
|
+
setInputFromValue(payload.value);
|
|
63
|
+
}, []);
|
|
64
|
+
const handleToChange = useCallback((_, payload) => {
|
|
65
|
+
setInputToValue(payload.value);
|
|
66
|
+
}, []);
|
|
67
|
+
useEffect(() => {
|
|
68
|
+
setInputFromValue(valueFrom);
|
|
69
|
+
}, [valueFrom]);
|
|
70
|
+
useEffect(() => {
|
|
71
|
+
setInputToValue(valueTo);
|
|
72
|
+
}, [valueTo]);
|
|
73
|
+
useEffect(() => {
|
|
74
|
+
onDateFromChange({ value: inputFromValue, date: dateFrom });
|
|
75
|
+
onChange({
|
|
76
|
+
valueFrom: inputFromValue,
|
|
77
|
+
valueTo: inputToValue,
|
|
78
|
+
dateFrom,
|
|
79
|
+
dateTo,
|
|
80
|
+
});
|
|
81
|
+
if (!inputFromValue || isCompleteDateInput(inputFromValue)) {
|
|
82
|
+
handleValidInputFrom();
|
|
83
|
+
}
|
|
84
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
85
|
+
}, [inputFromValue]);
|
|
86
|
+
useEffect(() => {
|
|
87
|
+
onDateToChange({ value: inputToValue, date: dateTo });
|
|
88
|
+
onChange({
|
|
89
|
+
valueFrom: inputFromValue,
|
|
90
|
+
valueTo: inputToValue,
|
|
91
|
+
dateFrom,
|
|
92
|
+
dateTo,
|
|
93
|
+
});
|
|
94
|
+
if (!inputToValue || isCompleteDateInput(inputToValue)) {
|
|
95
|
+
handleValidInputTo();
|
|
96
|
+
}
|
|
97
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
98
|
+
}, [inputToValue]);
|
|
99
|
+
return (React.createElement("div", { className: cn(styles.component, className), "data-test-id": dataTestId },
|
|
100
|
+
React.createElement(CalendarInput, Object.assign({}, inputFromProps, { useAnchorWidth: false, calendarPosition: 'popover', popoverPosition: 'bottom-start', error: inputFromInvalid || bothInvalid || inputFromProps.error, onChange: handleFromChange, onInputChange: handleInputFromChange, onBlur: handleInputFromBlur, value: inputFromValue, minDate: minDate, maxDate: maxDate, offDays: offDays, events: events, calendarProps: {
|
|
101
|
+
...inputFromProps.calendarProps,
|
|
102
|
+
month: monthFrom,
|
|
103
|
+
onMonthChange: handleMonthFromChange,
|
|
104
|
+
selectorView: 'full',
|
|
105
|
+
} })),
|
|
106
|
+
React.createElement("span", { className: styles.divider }),
|
|
107
|
+
React.createElement(CalendarInput, Object.assign({}, inputToProps, { useAnchorWidth: false, calendarPosition: 'popover', popoverPosition: 'bottom-end', error: inputToInvalid || bothInvalid || inputToProps.error, onChange: handleToChange, onInputChange: handleInputToChange, onBlur: handleInputToBlur, value: inputToValue, minDate: dateFrom || minDate, maxDate: maxDate, offDays: offDays, events: events, calendarProps: {
|
|
108
|
+
...inputToProps.calendarProps,
|
|
109
|
+
month: monthTo,
|
|
110
|
+
onMonthChange: handleMonthToChange,
|
|
111
|
+
selectorView: 'full',
|
|
112
|
+
} }))));
|
|
113
|
+
};
|
|
114
|
+
|
|
115
|
+
export { CalendarRangePopover };
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { FC } from 'react';
|
|
2
|
+
import { CalendarRangeProps } from "../Component";
|
|
3
|
+
type CalendarRangeStaticProps = Omit<CalendarRangeProps, 'calendarPosition'>;
|
|
4
|
+
declare const CalendarRangeStatic: FC<CalendarRangeStaticProps>;
|
|
5
|
+
export { CalendarRangeStaticProps, CalendarRangeStatic };
|