React-datepicker: Open + Close programmatically

Created on 15 Feb 2020  路  3Comments  路  Source: Hacker0x01/react-datepicker

For my use case, this seems perfect, but i can't seem to find a way to open and close the calendar programmatically.

I'm using range for a pick-up and drop-off scenario with time. onSelect (whether it's date or time), i'd like it to close the pick-up calendar, and open the drop-off calendar.

Is this possible?

Most helpful comment

I actually figured it out. I'll post a quick overview of what i've used in case someone needs this in the future.

  const startRef = useRef<DatePicker>();
  const endRef = useRef<DatePicker>();

  const onStartChange = () => {
    // used the following line as i'm using time as well, but it's not obvious that 
    // you need to click it to close the pickup calendar
    startRef.current.setOpen(false); 
    endRef.current.setOpen(true);    
  }

  return (
    <>
      <DatePicker ref={startRef} onChange={onStartChange} ... />
      <DatePicker ref={endRef} ... />
    </>
  );

All 3 comments

this might be a hack but it's working for me. I added a ref to the component and when I need to open/close the popper, I call this.refs.datePicker.state.open = false;

I've used customInput prop with ref, worked like a charm

I actually figured it out. I'll post a quick overview of what i've used in case someone needs this in the future.

  const startRef = useRef<DatePicker>();
  const endRef = useRef<DatePicker>();

  const onStartChange = () => {
    // used the following line as i'm using time as well, but it's not obvious that 
    // you need to click it to close the pickup calendar
    startRef.current.setOpen(false); 
    endRef.current.setOpen(true);    
  }

  return (
    <>
      <DatePicker ref={startRef} onChange={onStartChange} ... />
      <DatePicker ref={endRef} ... />
    </>
  );
Was this page helpful?
0 / 5 - 0 ratings

Related issues

ali-master picture ali-master  路  3Comments

evolve2k picture evolve2k  路  3Comments

jcabrerazuniga picture jcabrerazuniga  路  3Comments

sarav1234 picture sarav1234  路  3Comments

flaurida picture flaurida  路  3Comments