From 493636a58141a336e6335ea034e75be49d3bc97a Mon Sep 17 00:00:00 2001 From: Janderson Souza Matias Date: Tue, 23 Jan 2024 14:12:55 -0300 Subject: [PATCH] add clean data button --- src/components/DataRangePicker/index.tsx | 61 +++++++++++++++++------- src/i18n/langs/en.ts | 1 + src/i18n/langs/np.ts | 1 + src/pages/Dashboard/index.tsx | 12 +++-- 4 files changed, 54 insertions(+), 21 deletions(-) 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" >