diff --git a/src/utils/dates/DateRangeSelector.tsx b/src/utils/dates/DateRangeSelector.tsx index b0a3c104..3e9d10fe 100644 --- a/src/utils/dates/DateRangeSelector.tsx +++ b/src/utils/dates/DateRangeSelector.tsx @@ -1,6 +1,7 @@ import { useState } from 'react'; import { DropdownItem } from 'reactstrap'; import { DropdownBtn } from '../DropdownBtn'; +import { useEffectExceptFirstTime } from '../helpers/hooks'; import { DateInterval, DateRange, @@ -17,10 +18,11 @@ export interface DateRangeSelectorProps { disabled?: boolean; onDatesChange: (dateRange: DateRange) => void; defaultText: string; + updatable?: boolean; } export const DateRangeSelector = ( - { onDatesChange, initialDateRange, defaultText, disabled }: DateRangeSelectorProps, + { onDatesChange, initialDateRange, defaultText, disabled, updatable = false }: DateRangeSelectorProps, ) => { const initialIntervalIsRange = rangeIsInterval(initialDateRange); const [ activeInterval, setActiveInterval ] = useState(initialIntervalIsRange ? initialDateRange : undefined); @@ -37,6 +39,14 @@ export const DateRangeSelector = ( onDatesChange(intervalToDateRange(dateInterval)); }; + updatable && useEffectExceptFirstTime(() => { + if (rangeIsInterval(initialDateRange)) { + updateInterval(initialDateRange); + } else if (initialDateRange) { + updateDateRange(initialDateRange); + } + }, [ initialDateRange ]); + return ( diff --git a/test/utils/dates/DateRangeSelector.test.tsx b/test/utils/dates/DateRangeSelector.test.tsx index ef794918..aabd64fd 100644 --- a/test/utils/dates/DateRangeSelector.test.tsx +++ b/test/utils/dates/DateRangeSelector.test.tsx @@ -44,7 +44,7 @@ describe('', () => { [ 'last7Days' as DateInterval, 1 ], [ 'last30Days' as DateInterval, 1 ], [ 'last90Days' as DateInterval, 1 ], - [ 'last180days' as DateInterval, 1 ], + [ 'last180Days' as DateInterval, 1 ], [ 'last365Days' as DateInterval, 1 ], [{ startDate: new Date() }, 0 ], ])('sets proper element as active based on provided date range', (initialDateRange, expectedActiveIntervalItems) => {