dash-core-components 4.0.0-rc2 → 4.0.0-rc3
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/dash_core_components/async-datepicker.js +1 -1
- package/dash_core_components/async-datepicker.js.map +1 -1
- package/dash_core_components/async-dropdown.js +1 -2
- package/dash_core_components/async-dropdown.js.map +1 -1
- package/dash_core_components/async-graph.js +1 -1
- package/dash_core_components/async-graph.js.map +1 -1
- package/dash_core_components/async-highlight.js +1 -1
- package/dash_core_components/async-highlight.js.map +1 -1
- package/dash_core_components/async-markdown.js +1 -1
- package/dash_core_components/async-markdown.js.map +1 -1
- package/dash_core_components/async-mathjax.js +1 -1
- package/dash_core_components/async-slider.js +1 -1
- package/dash_core_components/async-slider.js.map +1 -1
- package/dash_core_components/async-upload.js +1 -1
- package/dash_core_components/async-upload.js.map +1 -1
- package/dash_core_components/dash_core_components-shared.js +1 -1
- package/dash_core_components/dash_core_components-shared.js.map +1 -1
- package/dash_core_components/dash_core_components.js +1 -1
- package/dash_core_components/dash_core_components.js.map +1 -1
- package/dash_core_components/proptypes.js +116 -12
- package/lib/components/Clipboard.react.js +1 -1
- package/lib/components/DatePickerRange.tsx +70 -0
- package/lib/components/DatePickerSingle.tsx +72 -0
- package/lib/components/Input.tsx +1 -1
- package/lib/components/Tab.tsx +19 -0
- package/lib/components/Tabs.tsx +311 -0
- package/lib/components/css/calendar.css +66 -0
- package/lib/components/css/datepickers.css +233 -0
- package/lib/components/css/dcc.css +1 -0
- package/lib/components/css/dropdown.css +6 -3
- package/lib/components/css/input.css +8 -2
- package/lib/components/css/sliders.css +22 -0
- package/lib/components/css/tabs.css +113 -0
- package/lib/fragments/DatePickerRange.tsx +444 -0
- package/lib/fragments/DatePickerSingle.tsx +249 -0
- package/lib/fragments/Dropdown.tsx +8 -4
- package/lib/fragments/Graph.react.js +18 -45
- package/lib/fragments/RangeSlider.tsx +2 -0
- package/lib/index.ts +4 -6
- package/lib/types.ts +353 -27
- package/lib/utils/DatePickerPersistence.ts +14 -0
- package/lib/utils/LazyLoader/datePickerRange.ts +4 -0
- package/lib/utils/LazyLoader/datePickerSingle.ts +4 -0
- package/lib/utils/calendar/Calendar.tsx +382 -0
- package/lib/utils/calendar/CalendarDay.tsx +75 -0
- package/lib/utils/calendar/CalendarDayPadding.tsx +15 -0
- package/lib/utils/calendar/CalendarMonth.tsx +247 -0
- package/lib/utils/calendar/CalendarMonthHeader.tsx +23 -0
- package/lib/utils/calendar/createMonthGrid.ts +28 -0
- package/lib/utils/calendar/helpers.ts +272 -0
- package/lib/utils/computeSliderMarkers.ts +71 -116
- package/lib/utils/optionRendering.tsx +22 -15
- package/lib/utils/sliderRendering.tsx +40 -2
- package/package.json +16 -13
- package/lib/components/DatePickerRange.react.js +0 -244
- package/lib/components/DatePickerSingle.react.js +0 -209
- package/lib/components/Dropdown.react.js.old +0 -228
- package/lib/components/Tab.react.js +0 -80
- package/lib/components/Tab.react.js.old +0 -79
- package/lib/components/Tabs.react.js +0 -295
- package/lib/components/Textarea.react.js.old +0 -287
- package/lib/fragments/DatePickerRange.react.js +0 -188
- package/lib/fragments/DatePickerSingle.react.js +0 -132
- package/lib/utils/DatePickerPersistence.js +0 -18
- package/lib/utils/LazyLoader/datePickerRange.js +0 -8
- package/lib/utils/LazyLoader/datePickerSingle.js +0 -8
- package/lib/utils/convertToMoment.js +0 -30
- package/lib/utils/optionTypes.js +0 -39
|
@@ -74,6 +74,10 @@
|
|
|
74
74
|
box-shadow: 0 4px 6px -1px var(--Dash-Shading-Weak);
|
|
75
75
|
}
|
|
76
76
|
|
|
77
|
+
.dash-slider-thumb:focus {
|
|
78
|
+
outline: 1px solid var(--Dash-Fill-Interactive-Strong);
|
|
79
|
+
}
|
|
80
|
+
|
|
77
81
|
.dash-slider-thumb:focus .dash-slider-tooltip,
|
|
78
82
|
.dash-slider-thumb:hover .dash-slider-tooltip {
|
|
79
83
|
display: block;
|
|
@@ -93,6 +97,10 @@
|
|
|
93
97
|
pointer-events: none;
|
|
94
98
|
}
|
|
95
99
|
|
|
100
|
+
.dash-slider-mark-outside-selection {
|
|
101
|
+
color: var(--Dash-Text-Disabled);
|
|
102
|
+
}
|
|
103
|
+
|
|
96
104
|
.dash-slider-mark:before {
|
|
97
105
|
content: '';
|
|
98
106
|
position: absolute;
|
|
@@ -119,6 +127,10 @@
|
|
|
119
127
|
width: 8px;
|
|
120
128
|
height: 8px;
|
|
121
129
|
border-radius: 50%;
|
|
130
|
+
background-color: var(--Dash-Fill-Primary-Active);
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
.dash-slider-dot-outside-selection {
|
|
122
134
|
background-color: var(--Dash-Fill-Disabled);
|
|
123
135
|
}
|
|
124
136
|
|
|
@@ -203,6 +215,16 @@
|
|
|
203
215
|
appearance: textfield;
|
|
204
216
|
}
|
|
205
217
|
|
|
218
|
+
.dash-range-slider-input::selection,
|
|
219
|
+
.dash-range-slider-input::-webkit-selection {
|
|
220
|
+
color: var(--Dash-Fill-Inverse-Strong);
|
|
221
|
+
background: var(--Dash-Fill-Interactive-Strong);
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
.dash-range-slider-input:focus {
|
|
225
|
+
outline: none;
|
|
226
|
+
}
|
|
227
|
+
|
|
206
228
|
/* Hide the number input spinners */
|
|
207
229
|
.dash-range-slider-input::-webkit-inner-spin-button,
|
|
208
230
|
.dash-range-slider-input::-webkit-outer-spin-button {
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
/* Tab parent container */
|
|
2
|
+
.tab-parent {
|
|
3
|
+
display: flex;
|
|
4
|
+
flex-direction: column;
|
|
5
|
+
overflow: hidden;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
/* Tab container (holds all tabs) */
|
|
9
|
+
.tab-container {
|
|
10
|
+
display: flex;
|
|
11
|
+
flex-direction: column;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
.tab-container--vert {
|
|
15
|
+
display: inline-flex;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/* Individual tab */
|
|
19
|
+
.tab {
|
|
20
|
+
flex: 1 1 0;
|
|
21
|
+
min-width: 0;
|
|
22
|
+
background-color: var(--tabs-background);
|
|
23
|
+
border: 1px solid var(--tabs-border);
|
|
24
|
+
border-bottom: none;
|
|
25
|
+
padding: 20px 25px;
|
|
26
|
+
transition: background-color, color 200ms;
|
|
27
|
+
text-align: center;
|
|
28
|
+
box-sizing: border-box;
|
|
29
|
+
cursor: pointer;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/* Tab selected state */
|
|
33
|
+
.tab--selected {
|
|
34
|
+
border-top: 2px solid var(--tabs-primary);
|
|
35
|
+
color: black;
|
|
36
|
+
background-color: white;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
.tab--selected:hover {
|
|
40
|
+
background-color: white;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/* Tab disabled state */
|
|
44
|
+
.tab--disabled {
|
|
45
|
+
opacity: 0.6;
|
|
46
|
+
cursor: not-allowed;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/* Tab content area */
|
|
50
|
+
.tab-content--vert {
|
|
51
|
+
display: inline-flex;
|
|
52
|
+
flex-direction: column;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/* Desktop/tablet styles (when above breakpoint) */
|
|
56
|
+
.tab-parent--above-breakpoint .tab {
|
|
57
|
+
border: 1px solid var(--tabs-border);
|
|
58
|
+
border-right: none;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
.tab-parent--above-breakpoint .tab:last-child {
|
|
62
|
+
border-right: 1px solid var(--tabs-border);
|
|
63
|
+
border-bottom: 1px solid var(--tabs-border);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
.tab-parent--above-breakpoint .tab--selected,
|
|
67
|
+
.tab-parent--above-breakpoint .tab:last-child.tab--selected {
|
|
68
|
+
border-bottom: none;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/* Vertical tabs: left border for selected */
|
|
72
|
+
.tab-parent--above-breakpoint .tab-container--vert .tab--selected {
|
|
73
|
+
border-left: 2px solid var(--tabs-primary);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/* Horizontal tabs: top border for selected */
|
|
77
|
+
.tab-parent--above-breakpoint
|
|
78
|
+
.tab-container:not(.tab-container--vert)
|
|
79
|
+
.tab--selected,
|
|
80
|
+
.tab-parent--above-breakpoint
|
|
81
|
+
.tab-container:not(.tab-container--vert)
|
|
82
|
+
.tab:last-child.tab--selected {
|
|
83
|
+
border-top: 2px solid var(--tabs-primary);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
.tab-parent--above-breakpoint .tab-container--vert .tab {
|
|
87
|
+
width: auto;
|
|
88
|
+
border-right: none !important;
|
|
89
|
+
border-bottom: none !important;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
.tab-parent--above-breakpoint .tab-container--vert .tab:last-child {
|
|
93
|
+
border-bottom: 1px solid var(--tabs-border) !important;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
.tab-parent--above-breakpoint .tab-container--vert .tab--selected {
|
|
97
|
+
border-top: 1px solid var(--tabs-border);
|
|
98
|
+
border-left: 2px solid var(--tabs-primary);
|
|
99
|
+
border-right: none;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
.tab-parent--above-breakpoint .tab-container {
|
|
103
|
+
flex-direction: row;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
.tab-parent--above-breakpoint .tab-container--vert {
|
|
107
|
+
flex-direction: column;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
.tab-parent--above-breakpoint.tab-parent--vert {
|
|
111
|
+
display: inline-flex;
|
|
112
|
+
flex-direction: row;
|
|
113
|
+
}
|
|
@@ -0,0 +1,444 @@
|
|
|
1
|
+
import React, {useCallback, useEffect, useMemo, useRef, useState} from 'react';
|
|
2
|
+
import * as Popover from '@radix-ui/react-popover';
|
|
3
|
+
import {
|
|
4
|
+
ArrowLeftIcon,
|
|
5
|
+
ArrowRightIcon,
|
|
6
|
+
CalendarIcon,
|
|
7
|
+
CaretDownIcon,
|
|
8
|
+
Cross1Icon,
|
|
9
|
+
} from '@radix-ui/react-icons';
|
|
10
|
+
import {addDays, subDays} from 'date-fns';
|
|
11
|
+
import AutosizeInput from 'react-input-autosize';
|
|
12
|
+
import uuid from 'uniqid';
|
|
13
|
+
|
|
14
|
+
import {CalendarDirection, DatePickerRangeProps} from '../types';
|
|
15
|
+
import Calendar, {CalendarHandle} from '../utils/calendar/Calendar';
|
|
16
|
+
import {
|
|
17
|
+
dateAsStr,
|
|
18
|
+
formatDate,
|
|
19
|
+
isDateDisabled,
|
|
20
|
+
isSameDay,
|
|
21
|
+
strAsDate,
|
|
22
|
+
} from '../utils/calendar/helpers';
|
|
23
|
+
import '../components/css/datepickers.css';
|
|
24
|
+
|
|
25
|
+
const DatePickerRange = ({
|
|
26
|
+
id,
|
|
27
|
+
className,
|
|
28
|
+
start_date,
|
|
29
|
+
end_date,
|
|
30
|
+
min_date_allowed,
|
|
31
|
+
max_date_allowed,
|
|
32
|
+
initial_visible_month = start_date ?? min_date_allowed ?? max_date_allowed,
|
|
33
|
+
disabled_days,
|
|
34
|
+
minimum_nights,
|
|
35
|
+
first_day_of_week,
|
|
36
|
+
show_outside_days,
|
|
37
|
+
clearable,
|
|
38
|
+
reopen_calendar_on_clear,
|
|
39
|
+
disabled,
|
|
40
|
+
display_format,
|
|
41
|
+
month_format = 'MMMM YYYY',
|
|
42
|
+
stay_open_on_select,
|
|
43
|
+
is_RTL = false,
|
|
44
|
+
setProps,
|
|
45
|
+
style,
|
|
46
|
+
// eslint-disable-next-line no-magic-numbers
|
|
47
|
+
day_size = 34,
|
|
48
|
+
number_of_months_shown = 1,
|
|
49
|
+
calendar_orientation,
|
|
50
|
+
updatemode,
|
|
51
|
+
start_date_id,
|
|
52
|
+
end_date_id,
|
|
53
|
+
start_date_placeholder_text = 'Start Date',
|
|
54
|
+
end_date_placeholder_text = 'End Date',
|
|
55
|
+
}: DatePickerRangeProps) => {
|
|
56
|
+
const [internalStartDate, setInternalStartDate] = useState(
|
|
57
|
+
strAsDate(start_date)
|
|
58
|
+
);
|
|
59
|
+
const [internalEndDate, setInternalEndDate] = useState(strAsDate(end_date));
|
|
60
|
+
const direction = is_RTL
|
|
61
|
+
? CalendarDirection.RightToLeft
|
|
62
|
+
: CalendarDirection.LeftToRight;
|
|
63
|
+
const initialCalendarDate =
|
|
64
|
+
strAsDate(initial_visible_month) ||
|
|
65
|
+
internalStartDate ||
|
|
66
|
+
internalEndDate;
|
|
67
|
+
|
|
68
|
+
const minDate = strAsDate(min_date_allowed);
|
|
69
|
+
const maxDate = strAsDate(max_date_allowed);
|
|
70
|
+
const disabledDates = useMemo(() => {
|
|
71
|
+
const baseDates =
|
|
72
|
+
disabled_days
|
|
73
|
+
?.map(d => strAsDate(d))
|
|
74
|
+
.filter((d): d is Date => d !== undefined) || [];
|
|
75
|
+
|
|
76
|
+
// Add minimum_nights constraint: disable dates within the minimum nights range
|
|
77
|
+
if (
|
|
78
|
+
internalStartDate &&
|
|
79
|
+
minimum_nights &&
|
|
80
|
+
minimum_nights > 0 &&
|
|
81
|
+
!internalEndDate
|
|
82
|
+
) {
|
|
83
|
+
const minimumNightsDates: Date[] = [];
|
|
84
|
+
for (let i = 1; i < minimum_nights; i++) {
|
|
85
|
+
minimumNightsDates.push(addDays(internalStartDate, i));
|
|
86
|
+
minimumNightsDates.push(subDays(internalStartDate, i));
|
|
87
|
+
}
|
|
88
|
+
return [...baseDates, ...minimumNightsDates];
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
return baseDates;
|
|
92
|
+
}, [disabled_days, internalStartDate, internalEndDate, minimum_nights]);
|
|
93
|
+
|
|
94
|
+
const [isCalendarOpen, setIsCalendarOpen] = useState(false);
|
|
95
|
+
const [startInputValue, setStartInputValue] = useState(
|
|
96
|
+
formatDate(internalStartDate, display_format)
|
|
97
|
+
);
|
|
98
|
+
const [endInputValue, setEndInputValue] = useState(
|
|
99
|
+
formatDate(internalEndDate, display_format)
|
|
100
|
+
);
|
|
101
|
+
|
|
102
|
+
const containerRef = useRef<HTMLDivElement>(null);
|
|
103
|
+
const startInputRef = useRef<HTMLInputElement | null>(null);
|
|
104
|
+
const endInputRef = useRef<HTMLInputElement | null>(null);
|
|
105
|
+
const calendarRef = useRef<CalendarHandle>(null);
|
|
106
|
+
|
|
107
|
+
useEffect(() => {
|
|
108
|
+
setInternalStartDate(strAsDate(start_date));
|
|
109
|
+
}, [start_date]);
|
|
110
|
+
|
|
111
|
+
useEffect(() => {
|
|
112
|
+
setInternalEndDate(strAsDate(end_date));
|
|
113
|
+
}, [end_date]);
|
|
114
|
+
|
|
115
|
+
useEffect(() => {
|
|
116
|
+
setStartInputValue(formatDate(internalStartDate, display_format));
|
|
117
|
+
}, [internalStartDate, display_format]);
|
|
118
|
+
|
|
119
|
+
useEffect(() => {
|
|
120
|
+
setEndInputValue(formatDate(internalEndDate, display_format));
|
|
121
|
+
}, [internalEndDate, display_format]);
|
|
122
|
+
|
|
123
|
+
useEffect(() => {
|
|
124
|
+
// Controls when setProps is called. Basically, whenever internal state
|
|
125
|
+
// diverges from props (i.e., user interaction)
|
|
126
|
+
const startChanged = !isSameDay(start_date, internalStartDate);
|
|
127
|
+
const endChanged = !isSameDay(end_date, internalEndDate);
|
|
128
|
+
|
|
129
|
+
if (!startChanged && !endChanged) {
|
|
130
|
+
return;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
if (internalStartDate && internalEndDate) {
|
|
134
|
+
// Both dates are set - send both
|
|
135
|
+
setProps({
|
|
136
|
+
start_date: dateAsStr(internalStartDate),
|
|
137
|
+
end_date: dateAsStr(internalEndDate),
|
|
138
|
+
});
|
|
139
|
+
} else if (updatemode === 'singledate' && internalStartDate) {
|
|
140
|
+
// Only start changed - send just that one
|
|
141
|
+
setProps({start_date: dateAsStr(internalStartDate)});
|
|
142
|
+
} else if (updatemode === 'singledate' && internalEndDate) {
|
|
143
|
+
// Only end changed - send just that one
|
|
144
|
+
setProps({end_date: dateAsStr(internalEndDate)});
|
|
145
|
+
}
|
|
146
|
+
}, [internalStartDate, internalEndDate, updatemode]);
|
|
147
|
+
|
|
148
|
+
const sendStartInputAsDate = useCallback(
|
|
149
|
+
(focusCalendar = false) => {
|
|
150
|
+
if (startInputValue) {
|
|
151
|
+
setInternalStartDate(undefined);
|
|
152
|
+
}
|
|
153
|
+
const parsed = strAsDate(startInputValue, display_format);
|
|
154
|
+
const isValid =
|
|
155
|
+
parsed &&
|
|
156
|
+
!isDateDisabled(parsed, minDate, maxDate, disabledDates);
|
|
157
|
+
|
|
158
|
+
if (isValid) {
|
|
159
|
+
setInternalStartDate(parsed);
|
|
160
|
+
if (focusCalendar) {
|
|
161
|
+
calendarRef.current?.focusDate(parsed);
|
|
162
|
+
} else {
|
|
163
|
+
calendarRef.current?.setVisibleDate(parsed);
|
|
164
|
+
}
|
|
165
|
+
} else {
|
|
166
|
+
// Invalid or disabled input: revert to previous valid date with proper formatting
|
|
167
|
+
const previousDate = strAsDate(start_date);
|
|
168
|
+
setStartInputValue(
|
|
169
|
+
previousDate ? formatDate(previousDate, display_format) : ''
|
|
170
|
+
);
|
|
171
|
+
if (focusCalendar) {
|
|
172
|
+
calendarRef.current?.focusDate(previousDate);
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
},
|
|
176
|
+
[
|
|
177
|
+
startInputValue,
|
|
178
|
+
display_format,
|
|
179
|
+
start_date,
|
|
180
|
+
minDate,
|
|
181
|
+
maxDate,
|
|
182
|
+
disabledDates,
|
|
183
|
+
]
|
|
184
|
+
);
|
|
185
|
+
|
|
186
|
+
const sendEndInputAsDate = useCallback(
|
|
187
|
+
(focusCalendar = false) => {
|
|
188
|
+
if (endInputValue === '') {
|
|
189
|
+
setInternalEndDate(undefined);
|
|
190
|
+
}
|
|
191
|
+
const parsed = strAsDate(endInputValue, display_format);
|
|
192
|
+
const isValid =
|
|
193
|
+
parsed &&
|
|
194
|
+
!isDateDisabled(parsed, minDate, maxDate, disabledDates);
|
|
195
|
+
|
|
196
|
+
if (isValid) {
|
|
197
|
+
setInternalEndDate(parsed);
|
|
198
|
+
if (focusCalendar) {
|
|
199
|
+
calendarRef.current?.focusDate(parsed);
|
|
200
|
+
} else {
|
|
201
|
+
calendarRef.current?.setVisibleDate(parsed);
|
|
202
|
+
}
|
|
203
|
+
} else {
|
|
204
|
+
// Invalid or disabled input: revert to previous valid date with proper formatting
|
|
205
|
+
const previousDate = strAsDate(end_date);
|
|
206
|
+
setEndInputValue(
|
|
207
|
+
previousDate ? formatDate(previousDate, display_format) : ''
|
|
208
|
+
);
|
|
209
|
+
if (focusCalendar) {
|
|
210
|
+
calendarRef.current?.focusDate(previousDate);
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
},
|
|
214
|
+
[
|
|
215
|
+
endInputValue,
|
|
216
|
+
display_format,
|
|
217
|
+
end_date,
|
|
218
|
+
minDate,
|
|
219
|
+
maxDate,
|
|
220
|
+
disabledDates,
|
|
221
|
+
]
|
|
222
|
+
);
|
|
223
|
+
|
|
224
|
+
const clearSelection = useCallback(
|
|
225
|
+
e => {
|
|
226
|
+
setInternalStartDate(undefined);
|
|
227
|
+
setInternalEndDate(undefined);
|
|
228
|
+
startInputRef.current?.focus();
|
|
229
|
+
e.preventDefault();
|
|
230
|
+
e.stopPropagation();
|
|
231
|
+
if (reopen_calendar_on_clear) {
|
|
232
|
+
setIsCalendarOpen(true);
|
|
233
|
+
}
|
|
234
|
+
},
|
|
235
|
+
[reopen_calendar_on_clear]
|
|
236
|
+
);
|
|
237
|
+
|
|
238
|
+
const handleStartInputKeyDown = useCallback(
|
|
239
|
+
(e: React.KeyboardEvent<HTMLInputElement>) => {
|
|
240
|
+
if (['ArrowUp', 'ArrowDown'].includes(e.key)) {
|
|
241
|
+
e.preventDefault();
|
|
242
|
+
sendStartInputAsDate(true);
|
|
243
|
+
if (!isCalendarOpen) {
|
|
244
|
+
// open the calendar after resolving prop changes, so that
|
|
245
|
+
// it opens with the correct date showing
|
|
246
|
+
setTimeout(() => setIsCalendarOpen(true), 0);
|
|
247
|
+
}
|
|
248
|
+
} else if (['Enter', 'Tab'].includes(e.key)) {
|
|
249
|
+
sendStartInputAsDate();
|
|
250
|
+
}
|
|
251
|
+
},
|
|
252
|
+
[isCalendarOpen, sendStartInputAsDate]
|
|
253
|
+
);
|
|
254
|
+
|
|
255
|
+
const handleEndInputKeyDown = useCallback(
|
|
256
|
+
(e: React.KeyboardEvent<HTMLInputElement>) => {
|
|
257
|
+
if (['ArrowUp', 'ArrowDown'].includes(e.key)) {
|
|
258
|
+
e.preventDefault();
|
|
259
|
+
sendEndInputAsDate(true);
|
|
260
|
+
if (!isCalendarOpen) {
|
|
261
|
+
// open the calendar after resolving prop changes, so that
|
|
262
|
+
// it opens with the correct date showing
|
|
263
|
+
setTimeout(() => setIsCalendarOpen(true), 0);
|
|
264
|
+
}
|
|
265
|
+
} else if (['Enter', 'Tab'].includes(e.key)) {
|
|
266
|
+
sendEndInputAsDate();
|
|
267
|
+
}
|
|
268
|
+
},
|
|
269
|
+
[isCalendarOpen, sendEndInputAsDate]
|
|
270
|
+
);
|
|
271
|
+
|
|
272
|
+
const accessibleId = id ?? uuid();
|
|
273
|
+
let classNames = 'dash-datepicker-input-wrapper';
|
|
274
|
+
if (disabled) {
|
|
275
|
+
classNames += ' dash-datepicker-input-wrapper-disabled';
|
|
276
|
+
}
|
|
277
|
+
if (className) {
|
|
278
|
+
classNames += ' ' + className;
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
const ArrowIcon =
|
|
282
|
+
direction === CalendarDirection.LeftToRight
|
|
283
|
+
? ArrowRightIcon
|
|
284
|
+
: ArrowLeftIcon;
|
|
285
|
+
|
|
286
|
+
const handleSelectionChange = useCallback(
|
|
287
|
+
(start?: Date, end?: Date) => {
|
|
288
|
+
const isNewSelection =
|
|
289
|
+
isSameDay(start, end) &&
|
|
290
|
+
((!internalStartDate && !internalEndDate) ||
|
|
291
|
+
(internalStartDate && internalEndDate));
|
|
292
|
+
|
|
293
|
+
if (isNewSelection) {
|
|
294
|
+
setInternalStartDate(start);
|
|
295
|
+
setInternalEndDate(undefined);
|
|
296
|
+
} else {
|
|
297
|
+
// Normalize dates: ensure start <= end
|
|
298
|
+
if (start && end && start > end) {
|
|
299
|
+
setInternalStartDate(end);
|
|
300
|
+
setInternalEndDate(start);
|
|
301
|
+
} else {
|
|
302
|
+
setInternalStartDate(start);
|
|
303
|
+
setInternalEndDate(end);
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
if (end && !stay_open_on_select) {
|
|
307
|
+
setIsCalendarOpen(false);
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
},
|
|
311
|
+
[internalStartDate, internalEndDate, stay_open_on_select]
|
|
312
|
+
);
|
|
313
|
+
|
|
314
|
+
return (
|
|
315
|
+
<div className="dash-datepicker" ref={containerRef}>
|
|
316
|
+
<Popover.Root
|
|
317
|
+
open={!disabled && isCalendarOpen}
|
|
318
|
+
onOpenChange={disabled ? undefined : setIsCalendarOpen}
|
|
319
|
+
>
|
|
320
|
+
<Popover.Trigger asChild disabled={disabled}>
|
|
321
|
+
<div
|
|
322
|
+
id={accessibleId + '-wrapper'}
|
|
323
|
+
className={classNames}
|
|
324
|
+
style={style}
|
|
325
|
+
aria-labelledby={`${accessibleId} ${accessibleId}-end-date ${start_date_id} ${end_date_id}`}
|
|
326
|
+
aria-haspopup="dialog"
|
|
327
|
+
aria-expanded={isCalendarOpen}
|
|
328
|
+
aria-disabled={disabled}
|
|
329
|
+
onClick={e => {
|
|
330
|
+
e.preventDefault();
|
|
331
|
+
if (!isCalendarOpen && !disabled) {
|
|
332
|
+
setIsCalendarOpen(true);
|
|
333
|
+
}
|
|
334
|
+
}}
|
|
335
|
+
>
|
|
336
|
+
<CalendarIcon className="dash-datepicker-trigger-icon" />
|
|
337
|
+
<AutosizeInput
|
|
338
|
+
inputRef={node => {
|
|
339
|
+
startInputRef.current = node;
|
|
340
|
+
}}
|
|
341
|
+
type="text"
|
|
342
|
+
id={start_date_id || accessibleId}
|
|
343
|
+
inputClassName="dash-datepicker-input dash-datepicker-start-date"
|
|
344
|
+
value={startInputValue}
|
|
345
|
+
onChange={e => setStartInputValue(e.target.value)}
|
|
346
|
+
onKeyDown={handleStartInputKeyDown}
|
|
347
|
+
onFocus={() => {
|
|
348
|
+
if (internalStartDate) {
|
|
349
|
+
calendarRef.current?.setVisibleDate(
|
|
350
|
+
internalStartDate
|
|
351
|
+
);
|
|
352
|
+
}
|
|
353
|
+
}}
|
|
354
|
+
placeholder={start_date_placeholder_text}
|
|
355
|
+
disabled={disabled}
|
|
356
|
+
dir={direction}
|
|
357
|
+
aria-label={start_date_placeholder_text}
|
|
358
|
+
/>
|
|
359
|
+
<ArrowIcon />
|
|
360
|
+
<AutosizeInput
|
|
361
|
+
inputRef={node => {
|
|
362
|
+
endInputRef.current = node;
|
|
363
|
+
}}
|
|
364
|
+
type="text"
|
|
365
|
+
id={end_date_id || accessibleId + '-end-date'}
|
|
366
|
+
inputClassName="dash-datepicker-input dash-datepicker-end-date"
|
|
367
|
+
value={endInputValue}
|
|
368
|
+
onChange={e => setEndInputValue(e.target.value)}
|
|
369
|
+
onKeyDown={handleEndInputKeyDown}
|
|
370
|
+
onFocus={() => {
|
|
371
|
+
if (internalEndDate) {
|
|
372
|
+
calendarRef.current?.setVisibleDate(
|
|
373
|
+
internalEndDate
|
|
374
|
+
);
|
|
375
|
+
}
|
|
376
|
+
}}
|
|
377
|
+
placeholder={end_date_placeholder_text}
|
|
378
|
+
disabled={disabled}
|
|
379
|
+
dir={direction}
|
|
380
|
+
aria-label={end_date_placeholder_text}
|
|
381
|
+
/>
|
|
382
|
+
{clearable && !disabled && (
|
|
383
|
+
<a
|
|
384
|
+
className="dash-datepicker-clear"
|
|
385
|
+
onClick={clearSelection}
|
|
386
|
+
aria-label="Clear Dates"
|
|
387
|
+
>
|
|
388
|
+
<Cross1Icon />
|
|
389
|
+
</a>
|
|
390
|
+
)}
|
|
391
|
+
<CaretDownIcon className="dash-datepicker-caret-icon" />
|
|
392
|
+
</div>
|
|
393
|
+
</Popover.Trigger>
|
|
394
|
+
|
|
395
|
+
<Popover.Portal container={containerRef.current}>
|
|
396
|
+
<Popover.Content
|
|
397
|
+
className="dash-datepicker-content"
|
|
398
|
+
align="start"
|
|
399
|
+
sideOffset={5}
|
|
400
|
+
onOpenAutoFocus={e => e.preventDefault()}
|
|
401
|
+
onCloseAutoFocus={e => {
|
|
402
|
+
e.preventDefault();
|
|
403
|
+
// Only focus if focus is not already on one of the inputs
|
|
404
|
+
const inputs: (Element | null)[] = [
|
|
405
|
+
startInputRef.current,
|
|
406
|
+
endInputRef.current,
|
|
407
|
+
];
|
|
408
|
+
if (inputs.includes(document.activeElement)) {
|
|
409
|
+
return;
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
// Keeps focus on the component when the calendar closes
|
|
413
|
+
if (!startInputValue) {
|
|
414
|
+
startInputRef.current?.focus();
|
|
415
|
+
} else {
|
|
416
|
+
endInputRef.current?.focus();
|
|
417
|
+
}
|
|
418
|
+
}}
|
|
419
|
+
>
|
|
420
|
+
<Calendar
|
|
421
|
+
ref={calendarRef}
|
|
422
|
+
initialVisibleDate={initialCalendarDate}
|
|
423
|
+
selectionStart={internalStartDate}
|
|
424
|
+
selectionEnd={internalEndDate}
|
|
425
|
+
minDateAllowed={minDate}
|
|
426
|
+
maxDateAllowed={maxDate}
|
|
427
|
+
disabledDates={disabledDates}
|
|
428
|
+
firstDayOfWeek={first_day_of_week}
|
|
429
|
+
showOutsideDays={show_outside_days}
|
|
430
|
+
monthFormat={month_format}
|
|
431
|
+
numberOfMonthsShown={number_of_months_shown}
|
|
432
|
+
calendarOrientation={calendar_orientation}
|
|
433
|
+
daySize={day_size}
|
|
434
|
+
direction={direction}
|
|
435
|
+
onSelectionChange={handleSelectionChange}
|
|
436
|
+
/>
|
|
437
|
+
</Popover.Content>
|
|
438
|
+
</Popover.Portal>
|
|
439
|
+
</Popover.Root>
|
|
440
|
+
</div>
|
|
441
|
+
);
|
|
442
|
+
};
|
|
443
|
+
|
|
444
|
+
export default DatePickerRange;
|