Skip to content

Commit

Permalink
[@mantine/dates] DatePicker: Fix date range being stuck in incorrect …
Browse files Browse the repository at this point in the history
…state when controlled state changes to an empty value (#6092)
  • Loading branch information
rtivital committed Apr 23, 2024
1 parent 16a76e1 commit d372b39
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'dayjs/locale/ru';

import React, { useState } from 'react';
import { Button, Stack } from '@mantine/core';
import { DatesRangeValue } from '../../types';
import { DatePicker } from './DatePicker';

Expand All @@ -22,6 +23,25 @@ export function HideOutsideDates() {
);
}

export function RangeCancelled() {
const [value, setValue] = useState<[Date | null, Date | null]>([null, null]);

const handleChange = (val: [Date | null, Date | null]) => {
setValue(val);
};

const clearRange = () => {
setValue([null, null]);
};

return (
<Stack>
<DatePicker type="range" value={value} onChange={handleChange} allowSingleDateInRange />
<Button onClick={clearRange}>CLEAR</Button>
</Stack>
);
}

export function Unstyled() {
return (
<div style={{ padding: 40 }}>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import dayjs from 'dayjs';
import { useState } from 'react';
import { useEffect, useState } from 'react';
import { DatePickerType, PickerBaseProps } from '../../types';
import { useUncontrolledDates } from '../use-uncontrolled-dates/use-uncontrolled-dates';
import { isInRange } from './is-in-range/is-in-range';
Expand Down Expand Up @@ -161,6 +161,12 @@ export function useDatesState<Type extends DatePickerType = 'default'>({

const onHoveredDateChange = type === 'range' && pickedDate ? setHoveredDate : () => {};

useEffect(() => {
if (type === 'range' && !_value[0] && !_value[1]) {
setPickedDate(null);
}
}, [value]);

return {
onDateChange,
onRootMouseLeave,
Expand Down

0 comments on commit d372b39

Please sign in to comment.