Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React-bootstrap-daterangepicker with input behaves weirdly

I'm trying to use react-bootstrap-datepicker with inputs inside of it, and I ran into an unpleasant bug: each time I click on one of the inputs or press any key, calendar is toggled (shown if it was hidden and vice versa). I reproduced it on codesandbox: https://codesandbox.io/s/4zx1l0n6p4 You can also see the versions of the dependencies I'm using there. I'm not sure if the problem is in the package or in my code, so I'd appreciate any help!

like image 703
Enk Avatar asked Apr 28 '26 09:04

Enk


1 Answers

Update: I managed to get it to work by installing version 3.4.0 and replacing two inputs with single input: https://codesandbox.io/s/kwy30z9rl3. The bug I described is probably caused by the addition of the wrapper div in one of the later versions. In the latest version react-bootstrap-daterangepicker wraps any children you pass to it into a container:

render() {
const { children, containerStyles, containerClass } = this.props;
return (
  <div
    ref={picker => {
      this.$picker = $(picker);
    }}
    className={containerClass}
    style={containerStyles}
  >
    {children}
  </div>
);

}

And bootstrap-daterangepicker checks if the parent of the datepicker is an input/button and apllies the necessary event handlers:

if (this.element.is('input') || this.element.is('button')) {
this.element.on({
    'click.daterangepicker': $.proxy(this.show, this),
    'focus.daterangepicker': $.proxy(this.show, this),
    'keyup.daterangepicker': $.proxy(this.elementChanged, this),
    'keydown.daterangepicker': $.proxy(this.keydown, this) //IE 11 compatibility
});
} else {
    this.element.on('click.daterangepicker', $.proxy(this.toggle, this));
    this.element.on('keydown.daterangepicker', $.proxy(this.toggle, this));
}

And since the element in question is a plain div, every click or keypress on the input triggers toggle.

like image 108
Enk Avatar answered Apr 29 '26 23:04

Enk



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!