Skip to content

Commit

Permalink
Merge pull request #3055 from ingef/feature/datepicker-autofocus-fix
Browse files Browse the repository at this point in the history
fixed datepicker autofocus issue
  • Loading branch information
awildturtok authored May 8, 2023
2 parents de6c37e + 8d25f73 commit db30fa4
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
3 changes: 3 additions & 0 deletions frontend/src/js/ui-components/BaseInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ export interface Props {
currencyConfig?: CurrencyConfigT;
onFocus?: (e: FocusEvent<HTMLInputElement>) => void;
onBlur?: (e: FocusEvent<HTMLInputElement>) => void;
onClick?: (e: React.MouseEvent<HTMLInputElement>) => void;
onChange: (val: string | number | null) => void;
}

Expand Down Expand Up @@ -123,6 +124,7 @@ const BaseInput = forwardRef<HTMLInputElement, Props>(
onChange,
onFocus,
onBlur,
onClick,
placeholder,
large,
inputType,
Expand Down Expand Up @@ -179,6 +181,7 @@ const BaseInput = forwardRef<HTMLInputElement, Props>(
large={large}
onFocus={onFocus}
onBlur={onBlur}
onClick={onClick}
onWheel={
(e) =>
(e.target as any).blur() /* to disable scrolling for number */
Expand Down
18 changes: 16 additions & 2 deletions frontend/src/js/ui-components/InputDate.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import styled from "@emotion/styled";
import { createElement, createRef, forwardRef, useState } from "react";
import { createElement, forwardRef, useRef, useState } from "react";
import ReactDatePicker from "react-datepicker";
import "react-datepicker/dist/react-datepicker.css";

Expand Down Expand Up @@ -53,11 +53,14 @@ const InputDate = forwardRef<HTMLInputElement, Props>(
onChange,
onCalendarSelect,
onFocus,
onBlur,
onClick,
...props
},
ref,
) => {
const datePickerRef = createRef<ReactDatePicker>();
const datePickerRef = useRef<ReactDatePicker>(null);
const [hasFocus, setHasFocus] = useState(false);
const [focusBlocked, setFocusBlocked] = useState(false);

return (
Expand All @@ -81,6 +84,17 @@ const InputDate = forwardRef<HTMLInputElement, Props>(
setFocusBlocked(false);
} else {
onFocus?.(e);
setHasFocus(true);
datePickerRef.current?.setOpen(true);
}
}}
onBlur={(e) => {
onBlur?.(e);
setHasFocus(false);
}}
onClick={(e) => {
onClick?.(e);
if (hasFocus) {
datePickerRef.current?.setOpen(true);
}
}}
Expand Down

0 comments on commit db30fa4

Please sign in to comment.