Skip to content

How to clear selection on a date range selection? #2545

Answered by gpbl
jojamu7 asked this question in Support
Discussion options

You must be logged in to vote

@jojamu7 please share some code 🙏

An example with a reset button (=clear selection) is here:

https://github.com/gpbl/react-day-picker/blob/main/examples/Range.tsx

export function Range() {
  const defaultMonth = new Date(2020, 5, 15);

  const defaultSelected: DateRange = {
    from: defaultMonth,
    to: addDays(defaultMonth, 4)
  };
  const [range, setRange] = useState<DateRange | undefined>(defaultSelected);

  return (
    <div>
      <DayPicker
        id="test"
        mode="range"
        defaultMonth={defaultMonth}
        selected={range}
        onSelect={setRange}
      />
      <button type="button" onClick={() => setRange(undefined)}>
        Reset
      </button>
    </div>
  )

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@jojamu7
Comment options

@gpbl
Comment options

Answer selected by gpbl
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants