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

Possible to put a limit to the range selection? #619

Open
eitai opened this issue Aug 7, 2023 · 2 comments
Open

Possible to put a limit to the range selection? #619

eitai opened this issue Aug 7, 2023 · 2 comments

Comments

@eitai
Copy link

eitai commented Aug 7, 2023

Possible to put a limit to the range selection?

@eitai eitai changed the title Possible to put a limit to the range selection Possible to put a limit to the range selection? Aug 7, 2023
@umakantp
Copy link

yes. There is minDate and maxDate prop for it.

@x-Foz3R-x
Copy link

x-Foz3R-x commented Feb 20, 2024

You can achieve it with this workaround

  const [state, setState] = useState({ startDate: new Date(), endDate: new Date(), key: "your-key" });
  const [focusedRange, setFocusedRange] = useState([0, 0] as RangeFocus);
  const [selecting, setSelecting] = useState(false);

  // Selection limited min/max Dates
  const minDateLimit = addDays(state.startDate, -DAYS_LIMIT + 1)
  const maxDateLimit = addDays(state.startDate, DAYS_LIMIT - 1)

  const minDate = selecting ? minDateLimit : addDays(new Date(), -300)
  const maxDate = selecting ? maxDateLimit : addDays(new Date(), 900)

  const handleFocusChange = (newFocusedRange: RangeFocus) => {
    setFocusedRange(newFocusedRange);
    if (newFocusedRange[1] === 1) setSelecting(true);
    else setSelecting(false);
  };

return (
  <DateRange
    ranges={[state]}
    focusedRange={focusedRange}
    onRangeFocusChange={handleFocusChange}
    minDate={minDate}
    maxDate={maxDate}
    // *other props*
  />
);

I hope it helped :))

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

3 participants