diff --git a/src/components/DataRangePicker/index.tsx b/src/components/DataRangePicker/index.tsx index c55061b..316ef81 100644 --- a/src/components/DataRangePicker/index.tsx +++ b/src/components/DataRangePicker/index.tsx @@ -1,6 +1,15 @@ import React, { useState } from 'react'; import { DateRangePicker, Range, RangeKeyDict, defaultStaticRanges } from 'react-date-range'; -import { Input, Box, useOutsideClick, FormControl, FormLabel } from '@chakra-ui/react'; +import { + Input, + Box, + useOutsideClick, + FormControl, + FormLabel, + InputGroup, + InputRightElement, + Button, +} from '@chakra-ui/react'; import { useTranslation } from 'react-i18next'; import { format } from 'date-fns'; @@ -61,7 +70,7 @@ const nepaliLocale: Locale = { }; type Props = { - onChange: (range: Range) => void; + onChange: (range?: Range) => void; }; const DataRangePicker: React.FC = ({ onChange }) => { @@ -78,16 +87,27 @@ const DataRangePicker: React.FC = ({ onChange }) => { const [isCalendarOpen, setIsCalendarOpen] = useState(false); const calendarRef = React.useRef(null); - const handleSelect = (ranges: RangeKeyDict) => { - setState({ - selection: { - ...ranges.selection, - }, - }); + const handleSelect = (ranges?: RangeKeyDict) => { + if (!ranges) { + setState({ + selection: { + startDate: undefined, + endDate: undefined, + key: 'selection', + }, + }); + onChange(); + } else { + setState({ + selection: { + ...ranges.selection, + }, + }); - if (ranges.selection.startDate !== ranges.selection.endDate) { - onChange(ranges.selection); - setIsCalendarOpen(false); + if (ranges.selection.startDate !== ranges.selection.endDate) { + onChange(ranges.selection); + setIsCalendarOpen(false); + } } }; @@ -108,12 +128,19 @@ const DataRangePicker: React.FC = ({ onChange }) => { return ( {t('dashboard.filters.data-range')} - setIsCalendarOpen(true)} - placeholder={t('dashboard.filters.data-range-placeholder') || ''} - /> + + setIsCalendarOpen(true)} + placeholder={t('dashboard.filters.data-range-placeholder') || ''} + /> + + + + {isCalendarOpen && ( { ); }; - const handleChangeDateRange = (range: Range) => { - if (dateRange?.startDate !== range.startDate || dateRange?.endDate !== range.endDate) { - setDateRange(range); + const handleChangeDateRange = (range?: Range) => { + if (range) { + if (dateRange?.startDate !== range.startDate || dateRange?.endDate !== range.endDate) { + setDateRange(range); + } + } else { + setDateRange(undefined); } }; @@ -71,7 +75,7 @@ const DashboardPage: React.FC = () => { flex={1} ml={isMobile ? 0 : '12px'} mt={isMobile ? '12px' : 0} - maxW={isMobile ? undefined : '220px'} + maxW={isMobile ? undefined : '270px'} w="full" >