React-datepicker: Can i force open calendar dropdown on clicking external button?

Created on 23 Jan 2018  路  2Comments  路  Source: Hacker0x01/react-datepicker

I need to open/close calendar dropdown on click of an external button. I am maintaining a local state flag "startOpenFlag" which is false by default. And in my options i have startOpen={this.state.startOpenFlag}
On click of myButton i am setting this flag to true. But, calendar dropdown doesn't seem to open.

Any help appreciated.

Most helpful comment

You can use setOpen(bool) method. Just set ref property to DatePicker component and call setOpen, for example:

toggle = () => {
    this.component.setOpen(this.focus);
    this.focus = !this.focus;
}
...
<DatePicker
  ref={(r) => {
    this.component = r;
  }}
  autoFocus
  selected={this.state.startDate}
  onChange={this.handleChange}
/>
<button type="button" onClick={() => this.toggle()}>toggle</button>

All 2 comments

You can use setOpen(bool) method. Just set ref property to DatePicker component and call setOpen, for example:

toggle = () => {
    this.component.setOpen(this.focus);
    this.focus = !this.focus;
}
...
<DatePicker
  ref={(r) => {
    this.component = r;
  }}
  autoFocus
  selected={this.state.startDate}
  onChange={this.handleChange}
/>
<button type="button" onClick={() => this.toggle()}>toggle</button>

I added a custom input.
customInput={}

class CustomInput extends React.Component {
render () {
return (




)
}
}

This works like a charm! Thanks for your help though. :) Closing this issue.

Was this page helpful?
0 / 5 - 0 ratings