React-datepicker: Close calendar from customInput

Created on 7 Oct 2016  路  6Comments  路  Source: Hacker0x01/react-datepicker

I'm using custom input to enable form submit on enter, but input calendar stays open.
Is there a way to close it?

wontfix

Most helpful comment

I use the following solution:

closeDatePicker() {
  const {datepicker} = this.refs;

  datepicker.setOpen(false);
}

render() {
   return (
      <div>
        <DateInput
          ref="datepicker"
          {...options}
        />
      </div>
    );
}

All 6 comments

+1
This is another reason why we need isOpen property

More discussion about an isOpen prop on #283 and #531

I use the following solution:

closeDatePicker() {
  const {datepicker} = this.refs;

  datepicker.setOpen(false);
}

render() {
   return (
      <div>
        <DateInput
          ref="datepicker"
          {...options}
        />
      </div>
    );
}

I'm use a footer button to close my calendar, like this:

const [myRef, setMyRef] = useState(false)

const closeCalendar = () => {
  myRef.setOpen(false)
}

return (
  <DatePicker
      ref={(r) => {
        setMyRef(r)
      }} 
     {...props}
  >
    <button onClick={closeCalendar}>Close</button>
  </DatePicker>
)

Solution with hooks

let datepickerRef = useRef(null);

  const openDatepicker = () => {
    datepickerRef.setOpen(true);
  };

 <DatePicker
        ref={(r) => datepickerRef = r}
        {...props}
/>

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

jbccollins picture jbccollins  路  3Comments

kkras3 picture kkras3  路  3Comments

sarav1234 picture sarav1234  路  3Comments

tamitutor picture tamitutor  路  3Comments

ali-master picture ali-master  路  3Comments