1

How can I place the calendar icon after the react-datepicker? Currently, everytime I'll click the datepicker, it'll go after the line of the date-picker and does not stay beside it.

These are the codes: https://codesandbox.io/s/festive-wescoff-71z8u?file=/src/App.js

<label>
        <DatePicker
          wrapperClassName="datePicker"
          selected={startDate}
          onChange={(date) => setStartDate(date)}
          showTimeSelect
          dateFormat="dd/MM/yyyy hh:mm a"
          timeFormat="hh:mm a"
        />
        <TodayIcon />
      </label>
JS3
  • 1,623
  • 3
  • 23
  • 52
  • 1
    Maybe you can try this: https://react-todo-jp-cksvcu.stackblitz.io. Also, dont forget to accept as helpful answer if it works. – Chandan May 28 '21 at 04:21

2 Answers2

2

You can provide a custom input for your DatePicker like this and it will behave properly

const CustomInput = React.forwardRef((props, ref) => {
  return (
    <div>          // You can style this further
      <label onClick={props.onClick} ref={ref}>         
        {props.value || props.placeholder}
      </label>
      <TodayIcon onClick={props.onClick} />
    </div>
  );
});


 <DatePicker
          wrapperClassName="datePicker"
          selected={startDate}
          onChange={(date) => setStartDate(date)}
          showTimeSelect
          minDate={new Date()}
          minTime={minTime}
          maxTime={new Date(0, 0, 0, 17)} // 5:00pm
          dateFormat="dd/MM/yyyy hh:mm a"
          timeFormat="hh:mm a"
          timeIntervals={30}
          customInput={<CustomInput/>}
    /> 
Vo Quoc Thang
  • 1,338
  • 1
  • 4
  • 5
1

You should keep the <label> position relative and update the following style


.MuiSvgIcon-root {
    fill: currentColor;
    width: 1em;
    height: 1em;
    display: inline-block;
    font-size: 1.5rem;
    transition: fill 200ms cubic-bezier(0.4, 0, 0.2, 1) 0ms;
    flex-shrink: 0;
    user-select: none;
    position: absolute;
    right: 0;
    top: -4px;
}
Momin
  • 3,200
  • 3
  • 30
  • 48