Search code examples
reactjsdatepickercalendar

Add calendar icon in React Date-Picker


What should I do in React to create something like this: Input Field with clickable calendar icon which pops up a calendar.


Solution

  • You can do it like this:

    JSX

    import { forwardRef, useState } from "react";
    import DatePicker from "react-datepicker";
    import "react-datepicker/dist/react-datepicker.css";
    import "./styles.css";
    
    export default function App() {
      const [startDate, setStartDate] = useState();
      const ExampleCustomInput = forwardRef(({ value, onClick, onChange }, ref) => (
        <input
          value={value}
          className="example-custom-input"
          onClick={onClick}
          onChange={onChange}
          ref={ref}
        ></input>
      ));
      return (
        <DatePicker
          selected={startDate}
          onChange={(date) => setStartDate(date)}
          customInput={<ExampleCustomInput />}
          dayClassName={() => "example-datepicker-day-class"}
          popperClassName="example-datepicker-class"
          todayButton="TODAY"
        />
      );
    }
    
    

    CSS

    .example-custom-input {
      background: url("/calendar.svg") no-repeat right;
      background-size: 20px;
      width: 240px;
      height: 24px;
    }
    
    .example-custom-input:focus-visible {
      border-bottom-color: #b51148;
      outline: none;
    }
    
    .react-datepicker__day--selected,
    .react-datepicker__day--in-selecting-range,
    .react-datepicker__day--in-range,
    .react-datepicker__month-text--selected,
    .react-datepicker__month-text--in-selecting-range,
    .react-datepicker__month-text--in-range,
    .react-datepicker__quarter-text--selected,
    .react-datepicker__quarter-text--in-selecting-range,
    .react-datepicker__quarter-text--in-range,
    .react-datepicker__year-text--selected,
    .react-datepicker__year-text--in-selecting-range,
    .react-datepicker__year-text--in-range,
    .react-datepicker__day--keyboard-selected,
    .react-datepicker__month-text--keyboard-selected,
    .react-datepicker__quarter-text--keyboard-selected,
    .react-datepicker__year-text--keyboard-selected {
      background-color: #b51148;
      border-radius: 50%;
      box-shadow: inset 0 0 0 2px white;
      border: 1px solid #b51148;
      margin: 0.066rem;
    }
    
    .react-datepicker__day--selected:hover,
    .react-datepicker__day--in-selecting-range:hover,
    .react-datepicker__day--in-range:hover,
    .react-datepicker__month-text--selected:hover,
    .react-datepicker__month-text--in-selecting-range:hover,
    .react-datepicker__month-text--in-range:hover,
    .react-datepicker__quarter-text--selected:hover,
    .react-datepicker__quarter-text--in-selecting-range:hover,
    .react-datepicker__quarter-text--in-range:hover,
    .react-datepicker__year-text--selected:hover,
    .react-datepicker__year-text--in-selecting-range:hover,
    .react-datepicker__year-text--in-range:hover,
    .react-datepicker__day--keyboard-selected:hover,
    .react-datepicker__month-text--keyboard-selected:hover,
    .react-datepicker__quarter-text--keyboard-selected:hover,
    .react-datepicker__year-text--keyboard-selected:hover,
    .react-datepicker__day--keyboard-selected:hover,
    .react-datepicker__month-text--keyboard-selected:hover,
    .react-datepicker__quarter-text--keyboard-selected:hover,
    .react-datepicker__year-text--keyboard-selected:hover {
      background-color: #b51148;
      border-radius: 50%;
    }
    
    .react-datepicker__day:hover,
    .react-datepicker__month-text:hover,
    .react-datepicker__quarter-text:hover,
    .react-datepicker__year-text:hover {
      border-radius: 50%;
    }
    
    .example-datepicker-class .react-datepicker__today-button {
      color: #b51148;
      background-color: white;
      border-top: 0px;
      text-align: right;
      margin-right: 20px;
    }
    

    You can take a look at this stackblitz for a live working example.