Skip to content

Commit

Permalink
Fix bug that introduced a race condition in consumers
Browse files Browse the repository at this point in the history
  • Loading branch information
amrocha committed Oct 28, 2019
1 parent 2541f9c commit b3b165b
Showing 1 changed file with 13 additions and 9 deletions.
22 changes: 13 additions & 9 deletions src/components/DatePicker/DatePicker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,18 @@ export function DatePicker({
const i18n = useI18n();
const [hoverDate, setHoverDate] = useState<Date | undefined>(undefined);
const [focusDate, setFocusDate] = useState<Date | undefined>(undefined);
const [newRange, setNewRange] = useState<Range | undefined>(undefined);

useEffect(() => {
setFocusDate(undefined);
}, [selected]);

useEffect(() => {
if (newRange) {
onChange(newRange);
}
}, [newRange, onChange]);

const handleFocus = useCallback((date: Date) => {
setFocusDate(date);
}, []);
Expand All @@ -85,16 +92,13 @@ export function DatePicker({
[onMonthChange],
);

const handleDateSelection = useCallback(
(range: Range) => {
const {end} = range;
const handleDateSelection = useCallback((range: Range) => {
const {end} = range;

setHoverDate(end);
setFocusDate(new Date(end));
onChange(range);
},
[onChange],
);
setHoverDate(end);
setFocusDate(new Date(end));
setNewRange(range);
}, []);

const handleMonthChangeClick = useCallback(
(month: Months, year: Year) => {
Expand Down

0 comments on commit b3b165b

Please sign in to comment.