hypeserver / react-date-range

A React component for choosing dates and date ranges.
MIT License
2.57k stars 658 forks source link

How can we not have initial selection in DateRange? #330

Open rajubushanam opened 4 years ago

rajubushanam commented 4 years ago

I am trying to use a DateRange component and I would like to have my initial Calendar empty with nothing selected. Once I start clicking on the calendar then I expect the onChange event to be triggered so we can select the ranges. Looks like if I pass dateRange: {startDate: null, endDate: null} it comes up with no selection, but if I click on the calendar, onChange event is not being called. Am I doing something wrong here? Any help would be appreciated. Thanks.

onurkerimov commented 4 years ago

I think in your case, you should also set a key named 'key' to your selection:

import { useState } from 'react';

const [state, setState] = useState([
  {
    startDate: null,
    endDate: null,
    key: 'selectionName'
  }
]);

<DateRangePicker
  onChange={item => setState([item.selectionName])}
  showSelectionPreview={true}
  moveRangeOnFirstSelection={false}
  ranges={state}
  direction="horizontal"
/>;
tristramjones commented 4 years ago

I'm curious about this as well. I only have 1 range and am setting the initial state of startDate and endDate to null while setting a key. I would expect the calendar to initially render with no selection. The initial render shows the current month's dates highlighted as if selected even though startDate and endDate are set to null.

https://codesandbox.io/s/react-date-range-demo-s1tik

gaieges commented 3 years ago

I've got the same problem, seems as though there's no way to have an empty selection. You either have:

an empty array in the initial state: throws an exception:

Cannot read property 'color' of undefined
DateRange.eval [as updatePreview]
https://4y5g3.csb.app/node_modules/react-date-range/dist/components/DateRange/index.js:182:43
onPreviewChange
https://4y5g3.csb.app/node_modules/react-date-range/dist/components/DateRange/index.js:209:18

the null suggestion above which results in the entire month being selected:

Screen Shot 2020-11-03 at 8 52 32 PM
Veda-cmd commented 3 years ago

I found a solution. Do not just pass null. Pass it as new Date(null). startDate: new Date(null), endDate: new Date(null).

image

gaieges commented 3 years ago

Very cool, but defaults to Jan 1 1970.

I ended up hacking around this by using:

            startDate: new Date(),
            endDate: addDays(new Date(), 1),
evorgevol commented 3 years ago

I too am looking to not have an initial value for the DateRange picker. Can we re-open this issue as it's currently not resolved?

Wgil commented 3 years ago

I'm also facing this issue.

jasonharrison commented 3 years ago

There is currently no way to not have an initial value for the DateRange picker. This issue should not have been closed.

icelic commented 3 years ago

I found a workaround. If you have multiple ranges in your initial state object just leave out the range which you do not want to define in the calendar.

Instead of:

const [ranges, setRanges] = useState({
    selection: {
      startDate: new Date(),
      endDate: subDays(new Date(), 7),
      key: 'selection',
    },
    compare: {
      startDate: new Date(),
      endDate: subDays(new Date(), 7),
      key: 'compare',
    },
  });

Leave out compare:

const [ranges, setRanges] = useState({
    selection: {
      startDate: new Date(),
      endDate: subDays(new Date(), 7),
      key: 'selection',
    },
  });

And then in the onChange handler add the missing range in the state object and it should appear on the calendar.

Note: I am using custom date display inputs instead of default ones, so I am not sure if this will work but maybe the idea will help.

gaieges commented 3 years ago

ranges expects an array, are you using that ranges var directly in the DatePicker?

I played around with the codesandbox in the comment above and couldn't get it to work that way. Curious if you would have any luck with that approach there @icelic ?

icelic commented 3 years ago

I had multiple ranges and I wanted the second one not to be selected by default, but I am not sure if that can be applied to this case/sandobx.

Victor-Varghese commented 3 years ago

any new updates? i'm also facing same issue @onurkerimov

how to show picker without a range selected? set start/end as null, its selecting all days

fmarcoccia commented 3 years ago

Any update for this issue?

ahmedrafayat commented 3 years ago

This shouldn't require "hacking" for a solution. Should be fixed asap

aaron5670 commented 3 years ago

Any update for this issue?

davisraimon commented 3 years ago

Hey guys...Try this

startDate : null
endDate : new Date("")
key : 'selection'
fmarcoccia commented 3 years ago

Hey guys...Try this

startDate : null
endDate : new Date("")
key : 'selection'

It works, but this is a workaround, maybe the date range should have an empty state

davisraimon commented 3 years ago

#

Hey guys...Try this

startDate : null
endDate : new Date("")
key : 'selection'

It works, but this is a workaround, maybe the date range should have an empty state

Yes ofc....it should support that.

maks-plotnikoff commented 3 years ago

why is it closed? The problem is still not resolved

bstrihova commented 3 years ago

hi, are there any plans to solve this anytime soon?

strangeweb3guy commented 3 years ago

It should be the default non-selected state. Any updates?

russellr922 commented 3 years ago

Same problem here

hugo-licon commented 2 years ago

I have the same issue

hugo-licon commented 2 years ago

I found a "fix" for this, but I updated the source code to accomplish this and I don't know if it can affect the functionality of other components: chrome-capture

I just set the startDate and the endDate to null and to avoid errors with that, on the DayCell component I added this condition:

const inRanges = ranges.reduce((result, range) => { 
  let startDate = range.startDate;
  let endDate = range.endDate;
+ if (!startDate || !endDate) return result;
  if (startDate && endDate && isBefore(endDate, startDate)) {
      [startDate, endDate] = [endDate, startDate];
  }  
  startDate = startDate ? endOfDay(startDate) : null;
  endDate = endDate ? startOfDay(endDate) : null;
  const isInRange =
        (!startDate || isAfter(day, startDate)) && (!endDate || isBefore(day, endDate));
  const isStartEdge = !isInRange && isSameDay(day, startDate);
  const isEndEdge = !isInRange && isSameDay(day, endDate);
  if (isInRange || isStartEdge || isEndEdge) {
    return [
      ...result,
      {
            isStartEdge,
            isEndEdge: isEndEdge,
            isInRange,
            ...range,
          },
      ];
   }
  return result;
}, []);
atefBB commented 2 years ago

@hugo-licon I do what you've mentioned but without success ! Any ideas to solve this? Thx !

ihtisham1211 commented 2 years ago

Any fixes for this ?

lConstantine commented 2 years ago

the problem is still live and kicking

kkallasm commented 2 years ago

Any updates?

osvaldo-dias-kununu commented 2 years ago

Any updates about that?

povilasbaranovas commented 2 years ago

+1 on this one. Currently had to resort to setting the endDate to be invalid as @davisraimon has suggested, but i really do not like this workaround. :/

artursOs commented 2 years ago

+1 here

kdoda commented 2 years ago

Any update on this issue?

atefBB commented 1 year ago

Any update on this issue?

Hey @kdoda! I made my own fork to fix this issue see the link it may help you. I have a PR opened about this https://github.com/hypeserver/react-date-range/pull/530.

subashrimal01 commented 1 year ago

+1 here

yurdigrfnn commented 1 year ago

+1 here

marta-256 commented 11 months ago

+1 here

avijit-devdynamics commented 11 months ago

Can anyone solve this issue. same problem.

R4hman commented 5 months ago

+1

iddk0321 commented 5 months ago

I initially set up the calendar with the following initial values and left it blank.

startDate: null
endDate: new Date("")
key: 'selection'

After selecting a range and then resetting it to the initial values to clear the selected range, I found that the month displayed on the calendar behaves strangely. If you are experiencing the same issue, I recommend changing the endDate to new Date(1).

startDate: null
endDate: new Date(1)
key: 'selection'
hagealex commented 1 month ago

+1 here