Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom definedRanges, multiple range selected #617

Open
justinthomas0789 opened this issue Aug 3, 2023 · 0 comments
Open

Custom definedRanges, multiple range selected #617

justinthomas0789 opened this issue Aug 3, 2023 · 0 comments

Comments

@justinthomas0789
Copy link

Subject of the issue

I have a requirement to configure custom definedRanges, ie: today, last 7 days, current month, current year, and two special ranges
one is "Before date" (selects a range from an initial date(a constant date) to the selected date) and another is "After date"(selects a selected date to today)
I wrote the definedRanges but fell into an issue, which is related to "After date", the condition also satisfies for today and the last 7 days, so in those cases, both today/last 7 days and After date will be highlighted,

[BUG] Bug Reproduce Steps

My definedRange below

const DaterangePicker = props => {
  const initial_ADE_date = new Date('2010-01-01')
  const today = startOfToday() // from date-fns
   const [selectedValue, setSelectedValue] = useState({ startDate: today, endDate: today })
  
  const definedRanges = createStaticRanges([
      {
        label: t('Today'),
        range: () => ({
            startDate: today,
            endDate: today
          })
      },
      {
        label: t('Last 7 days'),
        range: () => ({
            startDate: subDays(today, 6),
            endDate: endOfToday()
          })
      },
  
      {
        label: t('Last month'),
        range: () => ({
            startDate: startOfMonth(today),
            endDate: endOfMonth(today)
          })
      },
      {
        label: t('Current year'),
        range: () => ({
            startDate: startOfYear(today),
            endDate: endOfYear(today)
          })
      },
      {
        label: t('Before day'),
        range: () => ({
            startDate: startOfDay(initial_date),
            endDate: selectedValue.endDate ? endOfDayselectedValue.endDate) : endOfToday()
          })
      },
      {
        label: t('After day'),
        range: () => {
          let startDate = selectedValue.startDate
            ? startOfDay(selectedValue.startDate)
            : today;
          if (isAfter(startDate, today)) {
            startDate = today;
          }
          return {
            startDate,
            endDate: today
          }
        }
      }
    ])
  
  const onDateRangeChange = (ranges, onChange) => {
      if (ranges) {
        const { range1 } = ranges
        const val = {
          startDate: range1.startDate,
          endDate: range1.endDate
        }
        setSelectedValue(val)
      } else {
        setSelectedValue(null)
      }
    }
  return (
    <DateRangePicker
        onChange={selectedRange => onDateRangeChange(selectedRange)}
        showSelectionPreview
        months={1}
        locale={locales[locale]}
        ranges={selectedValue ? [selectedValue] : [{ startDate: new Date(), endDate: new Date() }]}
        staticRanges={definedRanges}
        dateDisplayFormat='dd/MM/yyyy'
        style={{
          color: 'red'
        }}
      />
 )
}

export default DaterangePicker

[BUG] Expected behaviour

By default the selected date will be today, and it will hiighlight the ranges Today and After Date
The expected behaviour is it will only highlight Today

Environment

Package Version: 1.4.0
React version: 18.2.0
Node version: 18.17.0
Browser: Chrome

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant