Skip to content

Scroll resets to first selected element in a multipleDates calendar when state is updated #239

Open
@davidjmstewart

Description

When using multiple dates functionality, the calendar resets to the zoom to focus on the first selected date on a re-render.

I think most users would expect the scroll to remain unchanged. Is there a way to use the InfiniteCalendar API to avoid this?

See this CodeSandbox

ezgif-3-f09e75df0763

Full code

import React, { useState } from "react";
import "./styles.css";
import InfiniteCalendar, {
  Calendar,
  defaultMultipleDateInterpolation,
  withMultipleDates,
} from 'react-infinite-calendar';
import 'react-infinite-calendar/styles.css'; // only needs to be imported once

export default function App() {

  const [selectedCalendarDates, setSelectedCalendarDates] = useState([]);

  const arrayRemove = (arr, value) => {
    return arr.filter(function (ele) {
      return ele != value;
    });
  }

  // Credit: Mayura Wijewickrama https://stackoverflow.com/questions/45504601/react-infinite-calendar-set-the-date
  const updateSelectedCalendarDates = (date) => {
    var converted =  date.toLocaleString('default', { weekday: 'short' }) 
                  + ' ' + date.toLocaleString('default', { month: 'short' }) 
                  + ' ' + date.getDate() + ' ' +date.getFullYear() + ' 00:00:00';

    if (!selectedCalendarDates.includes(converted)) {
        setSelectedCalendarDates([...selectedCalendarDates, converted]);
    } else {
        var result = arrayRemove(selectedCalendarDates, converted);
        setSelectedCalendarDates(result);
    }
  }

    return (
        <InfiniteCalendar
        Component={withMultipleDates(Calendar)}
        selected={selectedCalendarDates}
        width={'100%'}
        height={600}
        interpolateSelection={defaultMultipleDateInterpolation}
        onSelect={updateSelectedCalendarDates}
        minDate={new Date()} // Minimum selectable date is today
        min={new Date()} // Minimum date the calendar can be scrolled to is today
    />
  );
}

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions