9

I'm using react-datepicker but for some reason it is showing the calendar behind a container.

enter image description here

I have tried:

.react-datepicker-popper {
  z-index: 9999 !important;
}

but it doesn't work.

Here is the Date Picker component

<DatePicker
     selected={startDateSingleDay}
     onChange={onChangeDatePickerStartDateSingleDay}
     dateFormat="yyyy-MM-dd"
     className="text-center"
     showMonthDropdown
     showYearDropdown
     dropdownMode="select"
     onChangeRaw={handleDateChangeRaw}
     popperClassName="date-picker-reports"
     placeholderText="Choose a date"
   />

Any suggestions?

Alex Yepes
  • 1,160
  • 1
  • 9
  • 21

6 Answers6

17

This is tested for react-datepicker 4.2.1

Adding a prop portalId should automatically fix the issue.

According to its documentation If the provided portalId cannot be found in the dom, one will be created by default with that id passed in the prop.

<DatePicker
 portalId="root-portal"
 selected={startDateSingleDay}
 onChange={onChangeDatePickerStartDateSingleDay}
 dateFormat="yyyy-MM-dd"
 className="text-center"
 showMonthDropdown
 showYearDropdown
 dropdownMode="select"
 onChangeRaw={handleDateChangeRaw}
 popperClassName="date-picker-reports"
 placeholderText="Choose a date" 
/>
Anil
  • 196
  • 1
  • 3
  • 2
    This fixed it for me. – Nicola Peluchetti Nov 10 '21 at 15:37
  • @Anil, Yes it fixed the hide/show overlaying issue. But it doesn't trigger clicks on the dates. It only closes the popup everytime when anything clicked inside the popup. – S K R Jan 10 '22 at 15:53
  • @SKR Can you provide more information about the issue you are facing ? A codesandbox link to reproduce the issue should be great. – Anil Jan 11 '22 at 16:38
  • I was using OutsideClickHandler plugin which was triggering the click inside the portal as outside click, so, I needed to handle the same. no other issues right now. @Anil – S K R Jan 12 '22 at 13:35
10

I fixed it as react-popper will place the popover in the same constraints as the parent div. For cases where the parent div is somehow constrained -- a scrollable div -- the popper will appear inside the div and will be constrained by it to.

In my case I wanted the popover to be unconstrained it's parent. To fix this, I placed the popover in a container outside of the constrained container.

import { Portal } from "react-overlays";

const CalendarContainer = ({ children }) => {
  const el = document.getElementById("calendar-portal");

  return <Portal container={el}>{children}</Portal>;
};

And added the popperContainer prop to the DatePicker like so:

<DatePicker
    selected={startDate}
    onChange={onChangeDatePickerStartDate}
    dateFormat="yyyy-MM-dd"
    className="text-center date-picker-reports"
    showMonthDropdown
    showYearDropdown
    dropdownMode="select"
    onChangeRaw={handleDateChangeRaw}
    popperPlacement="top-start"
    placeholderText="Choose a start date"
    popperContainer={CalendarContainer}
/>

Final Result:

enter image description here

Alex Yepes
  • 1,160
  • 1
  • 9
  • 21
4

in material table i'm facing same issue.

popperProps={{strategy: 'fixed'}} by using this datepicker property this is how we can use in code

  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - [From Review](/review/late-answers/31477146) – Mahdi Zarei Apr 09 '22 at 09:43
1
<DatePicker minDate={new Date()} 
  selected={new Date()} 
  onChange={this.setDueDate} 
  dateFormat='dd/MM/yyyy' 
  popperPlacement="bottom" 
  popperModifiers={{ flip: { behavior: ["bottom"] }, preventOverflow: { enabled: false }, hide: { enabled: false } }} 
/>
cigien
  • 57,834
  • 11
  • 73
  • 112
Keval
  • 21
  • 2
1

I found a 100% working solution as it is in react-datepicker official documentation in here.

Put these 3 props popperClassName, popperPlacement, popperModifiers as it is in the following and the datepicker will work astonishingly.

<DatePicker
      selected={startDate}
      onChange={(date) => setStartDate(date)}
      popperClassName="some-custom-class"
      popperPlacement="top-end"
      popperModifiers={[
        {
          name: "offset",
          options: {
            offset: [5, 10],
          },
        },
        {
          name: "preventOverflow",
          options: {
            rootBoundary: "viewport",
            tether: false,
            altAxis: true,
          },
        },
      ]}
/>
S K R
  • 552
  • 1
  • 3
  • 16
0

As react-datepicker uses popperjs internally, we can even use popperPrpps prop with strategy: 'fixed' or positionFixed: true for older version of popperjs.

Vrajpal Jhala
  • 231
  • 3
  • 8