This repository was archived by the owner on Jun 25, 2025. It is now read-only.
forked from aboveyunhai/chakra-dayzed-datepicker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchanges.patch
More file actions
195 lines (191 loc) · 6.86 KB
/
Copy pathchanges.patch
File metadata and controls
195 lines (191 loc) · 6.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
diff --git a/src/components/calendarPanel.tsx b/src/components/calendarPanel.tsx
index c360d49..7b287ea 100644
--- a/src/components/calendarPanel.tsx
+++ b/src/components/calendarPanel.tsx
@@ -20,6 +20,8 @@ export interface CalendarPanelProps extends DatepickerProps {
disabledDates?: Set<number>;
onMouseEnterHighlight?: (date: Date) => void;
isInRange?: (date: Date) => boolean | null;
+ isFirstSelectedDate?: (date: Date) => boolean | null;
+ isLastSelectedDate?: (date: Date) => boolean | null;
}
export const CalendarPanel: React.FC<CalendarPanelProps> = ({
@@ -29,6 +31,8 @@ export const CalendarPanel: React.FC<CalendarPanelProps> = ({
disabledDates,
onMouseEnterHighlight,
isInRange,
+ isFirstSelectedDate,
+ isLastSelectedDate
}) => {
const renderProps = useDayzed(dayzedHookProps);
const { calendars, getBackProps, getForwardProps } = renderProps;
@@ -136,6 +140,7 @@ export const CalendarPanel: React.FC<CalendarPanelProps> = ({
return week.map((dateObj, index) => {
const key = `${calendar.month}-${calendar.year}-${weekIdx}-${index}`;
if (!dateObj) return <Box key={key} />;
+
const { date } = dateObj;
return (
<DayOfMonth
@@ -143,6 +148,8 @@ export const CalendarPanel: React.FC<CalendarPanelProps> = ({
dateObj={dateObj}
propsConfigs={propsConfigs}
renderProps={renderProps}
+ isFirstSelectedDate={isFirstSelectedDate && isFirstSelectedDate(date)}
+ isLastSelectedDate={isLastSelectedDate && isLastSelectedDate(date)}
isInRange={isInRange && isInRange(date)}
disabledDates={disabledDates}
onMouseEnter={() => {
diff --git a/src/components/dateNavBtns.tsx b/src/components/dateNavBtns.tsx
index f49be6b..a226a46 100644
--- a/src/components/dateNavBtns.tsx
+++ b/src/components/dateNavBtns.tsx
@@ -18,6 +18,8 @@ export const DatepickerBackBtns: React.FC<DatepickerBackBtnsProps> = (
) => {
const { calendars, getBackProps } = props;
const customBtnProps = props.propsConfigs?.dateNavBtnProps;
+ const customBtnBackwardProps = props.propsConfigs?.dateNavBackwardBtnProps;
+
return (
<Fragment>
<Button
@@ -27,16 +29,12 @@ export const DatepickerBackBtns: React.FC<DatepickerBackBtnsProps> = (
})}
{...DefaultBtnStyle}
{...customBtnProps}
- >
- {'<<'}
- </Button>
+ />
<Button
{...getBackProps({ calendars })}
{...DefaultBtnStyle}
- {...customBtnProps}
- >
- {'<'}
- </Button>
+ {...customBtnBackwardProps}
+ />
</Fragment>
);
};
@@ -51,15 +49,15 @@ export const DatepickerForwardBtns: React.FC<DatepickerForwardBtnsProps> = (
) => {
const { calendars, getForwardProps } = props;
const customBtnProps = props.propsConfigs?.dateNavBtnProps;
+ const customBtnBackwardProps = props.propsConfigs?.dateNavBackwardBtnProps;
+
return (
<Fragment>
<Button
{...getForwardProps({ calendars })}
{...DefaultBtnStyle}
- {...customBtnProps}
- >
- {'>'}
- </Button>
+ {...customBtnBackwardProps}
+ />
<Button
{...getForwardProps({
calendars,
@@ -67,9 +65,7 @@ export const DatepickerForwardBtns: React.FC<DatepickerForwardBtnsProps> = (
})}
{...DefaultBtnStyle}
{...customBtnProps}
- >
- {'>>'}
- </Button>
+ />
</Fragment>
);
};
diff --git a/src/components/dayOfMonth.tsx b/src/components/dayOfMonth.tsx
index a0659ca..48021b0 100644
--- a/src/components/dayOfMonth.tsx
+++ b/src/components/dayOfMonth.tsx
@@ -6,6 +6,8 @@ import { DatepickerProps, DayOfMonthBtnStyleProps } from '../utils/commonTypes';
interface DayOfMonthProps extends DatepickerProps {
renderProps: RenderProps;
isInRange?: boolean | null;
+ isFirstSelectedDate?: boolean | null;
+ isLastSelectedDate?: boolean | null;
disabledDates?: Set<number>;
dateObj: DateObj;
onMouseEnter?: React.MouseEventHandler<HTMLButtonElement> | undefined;
@@ -24,8 +26,14 @@ export const DayOfMonth: React.FC<DayOfMonthProps> = ({
disabledDates,
renderProps,
onMouseEnter,
+ isFirstSelectedDate,
+ isLastSelectedDate
}) => {
const { date, selected, selectable, today } = dateObj;
+ const rangeClassName = isInRange && !(isLastSelectedDate || isFirstSelectedDate) ? 'is-in-range-calendar-button' : '';
+ const lastSelectedClassName= isLastSelectedDate ? 'is-last-selected-calendar-button' : '';
+ const firstSelectedClassName= isFirstSelectedDate ? 'is-first-selected-calendar-button' : '';
+ const todayButtonClassName= today ? 'is-today-calendar-button' : '';
const { getDateProps } = renderProps;
const {
defaultBtnProps,
@@ -81,6 +89,7 @@ export const DayOfMonth: React.FC<DayOfMonthProps> = ({
return (
<Button
+ className={`${rangeClassName} ${todayButtonClassName} ${lastSelectedClassName} ${firstSelectedClassName}`}
{...getDateProps({
dateObj,
disabled: disabled,
diff --git a/src/range.tsx b/src/range.tsx
index af6dd72..f9a0d44 100644
--- a/src/range.tsx
+++ b/src/range.tsx
@@ -71,6 +71,30 @@ export const RangeCalendarPanel: React.FC<RangeCalendarPanelProps> = ({
}
};
+ const isFirstSelectedDate = (date: Date): boolean => {
+ if (!Array.isArray(selected) || selected.length === 0) {
+ return false;
+ }
+
+ if (selected.length === 1) {
+ return hoveredDate ? selected[0].getTime() < hoveredDate.getTime() : false;
+ }
+
+ return selected.length > 0 && selected[0].getTime() === date.getTime();
+ };
+
+const isLastSelectedDate = (date: Date): boolean => {
+ if (!Array.isArray(selected) || selected.length === 0) {
+ return false;
+ }
+
+ if (selected.length === 1) {
+ return hoveredDate ? selected[0].getTime() > hoveredDate.getTime() : false;
+ }
+
+ return selected[selected.length - 1].getTime() === date.getTime();
+};
+
return (
<Flex onMouseLeave={onMouseLeave}>
<CalendarPanel
@@ -79,6 +103,8 @@ export const RangeCalendarPanel: React.FC<RangeCalendarPanelProps> = ({
propsConfigs={propsConfigs}
isInRange={isInRange}
onMouseEnterHighlight={onMouseEnterHighlight}
+ isFirstSelectedDate={isFirstSelectedDate}
+ isLastSelectedDate={isLastSelectedDate}
/>
</Flex>
);
diff --git a/src/utils/commonTypes.ts b/src/utils/commonTypes.ts
index 02acded..2bb7eaf 100644
--- a/src/utils/commonTypes.ts
+++ b/src/utils/commonTypes.ts
@@ -44,6 +44,7 @@ export interface CalendarPanelProps {
export interface PropsConfigs {
dateNavBtnProps?: ButtonProps;
+ dateNavBackwardBtnProps?: ButtonProps;
dayOfMonthBtnProps?: DayOfMonthBtnStyleProps;
inputProps?: InputProps;
triggerBtnProps?: ButtonProps;