Skip to content

Commit 2f4808d

Browse files
committed
Refactoring update calendar date
1 parent f82dbad commit 2f4808d

File tree

1 file changed

+16
-4
lines changed

1 file changed

+16
-4
lines changed

src/components/Datepicker.tsx

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -215,15 +215,27 @@ const Datepicker: React.FC<DatepickerType> = ({
215215

216216
useEffect(() => {
217217
if (startFrom && dayjs(startFrom).isValid()) {
218-
if (value?.startDate != null) {
219-
setFirstDate(dayjs(value.startDate));
220-
setSecondDate(nextMonth(dayjs(value.startDate)));
218+
const startDate = value?.startDate;
219+
const endDate = value?.endDate;
220+
if (startDate && dayjs(startDate).isValid()) {
221+
setFirstDate(dayjs(startDate));
222+
if (!asSingle) {
223+
if (
224+
endDate &&
225+
dayjs(endDate).isValid() &&
226+
dayjs(endDate).startOf("month").isAfter(dayjs(startDate))
227+
) {
228+
setSecondDate(dayjs(endDate));
229+
} else {
230+
setSecondDate(nextMonth(dayjs(startDate)));
231+
}
232+
}
221233
} else {
222234
setFirstDate(dayjs(startFrom));
223235
setSecondDate(nextMonth(dayjs(startFrom)));
224236
}
225237
}
226-
}, [startFrom, value]);
238+
}, [asSingle, startFrom, value]);
227239

228240
// Variables
229241
const safePrimaryColor = useMemo(() => {

0 commit comments

Comments
 (0)