Skip to content

Commit

Permalink
fix(DatePicker): fix invalid current calendar value
Browse files Browse the repository at this point in the history
  • Loading branch information
ZxBing0066 committed Jun 4, 2021
1 parent 1e5fd14 commit 18511c1
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/components/DatePicker/usePicker.tsx
Expand Up @@ -61,6 +61,9 @@ interface DisplayToFormatAndTimeMode<D> {
(display?: D): [string[]] | [string[], string[]];
}

const getValidCurrentDate = (value: TDate | null | undefined, d: Date) =>
value != null && moment(+value).isValid() ? value : d;

const usePicker = <D,>(
props: TProps<D>,
displayToFormatAndTimeMode: DisplayToFormatAndTimeMode<D>,
Expand Down Expand Up @@ -98,7 +101,7 @@ const usePicker = <D,>(
const d = useMemo(() => new Date(), []);

let [value, onChange] = useUncontrolled<TDate | null | undefined, Moment | null>(_value, defaultValue, _onChange);
const [calCurrentValue, setCalCurrentValue] = useState(value);
const [calCurrentValue, setCalCurrentValue] = useState(() => getValidCurrentDate(value, d));
if (!nullable && value == null) value = d;
const [inputValue, setInputValue] = useState(() => formatDate(value, format));
const [calValue, setCalValue] = useState(value);
Expand Down Expand Up @@ -177,7 +180,7 @@ const usePicker = <D,>(

const handleInputFocus = useCallback(() => {
setCalValue(value == null ? null : value);
setCalCurrentValue(value == null ? d : value);
setCalCurrentValue(getValidCurrentDate(value, d));
setUseInputValue(true);
setActive(true);
}, [d, value]);
Expand Down

0 comments on commit 18511c1

Please sign in to comment.