From 352f3abc7bf943028484b6d89354ab5257f6f9ad Mon Sep 17 00:00:00 2001 From: Enochen Date: Fri, 29 Sep 2023 15:55:29 -0400 Subject: [PATCH] refocus calendar btn after close --- frontend/src/components/MiniCal/MiniCal.tsx | 141 ++++++++++---------- 1 file changed, 73 insertions(+), 68 deletions(-) diff --git a/frontend/src/components/MiniCal/MiniCal.tsx b/frontend/src/components/MiniCal/MiniCal.tsx index 19ff68b94..7b4305b2e 100644 --- a/frontend/src/components/MiniCal/MiniCal.tsx +++ b/frontend/src/components/MiniCal/MiniCal.tsx @@ -1,5 +1,5 @@ -import React from 'react'; -import DatePicker from 'react-datepicker'; +import React, { useRef, forwardRef, HTMLProps } from 'react'; +import DatePicker, { ReactDatePickerProps } from 'react-datepicker'; import cn from 'classnames'; import 'react-datepicker/dist/react-datepicker.css'; import './datepicker_override.css'; @@ -33,24 +33,26 @@ const Icon = () => ( const MiniCal = () => { const { curDate, setCurDate } = useDate(); + const inputRef = useRef(null); const updateDate = (d: Date) => { setCurDate(d); }; - class CustomInput extends React.Component { - render() { + + const CustomInput = forwardRef( + ({ onClick, value }: HTMLProps, ref) => { return ( - ); } - } + ); const Indicators = ({ date }: { date: Date }) => ( { window.scroll(x, y); }; + const renderHeader: ReactDatePickerProps['renderCustomHeader'] = ({ + date, + decreaseMonth, + increaseMonth, + prevMonthButtonDisabled, + nextMonthButtonDisabled, + }) => ( +
+
+ + + +
+
+ + + {`${date.toLocaleString('default', { + month: 'long', + })} ${date.getFullYear()}`} + + + +
+
+ ); + return (
} + // Set to arbitrary non empty value to preserve actual CustomInput ref + customInputRef="#" + customInput={} highlightDates={[{ 'custom--today': [new Date()] }]} - renderCustomHeader={({ - date, - changeYear, - changeMonth, - decreaseMonth, - increaseMonth, - prevMonthButtonDisabled, - nextMonthButtonDisabled, - }) => ( -
-
- - - -
-
- - - {`${date.toLocaleString('default', { - month: 'long', - })} ${date.getFullYear()}`} - - - -
-
- )} + onCalendarClose={() => inputRef.current?.focus()} + renderCustomHeader={renderHeader} />
);