react-datepicker icon indicating copy to clipboard operation
react-datepicker copied to clipboard

react-datepicker interferes with react-select

Open gilbertkaradja opened this issue 9 years ago • 30 comments

Noticed a weird behavior. I use react-datepicker and react-select. When the select is opened, if i click on the datepicker, the select field closes and the datepicker opens as expected.

But when the datepicker is open, if i try to click on the select, the select opens and the datepicker closes BUT almost instantly the select closes and datepicker opens again.

gilbertkaradja avatar Feb 03 '17 08:02 gilbertkaradja

I'm also seeing this....not sure which one to blame here but at least seems to be related to these lines: https://github.com/Hacker0x01/react-datepicker/blob/master/src/datepicker.jsx#L156-L157

Without that deferFocusInput() works for me. Not really sure why that is needed or what it is actually achieving so can't really comment on how to fix it. If someone has some pointers of fixing this I could try submitting PR. Didn't yet check anything on react-select side so there might be also something.

tume avatar Feb 06 '17 16:02 tume

Added example repository for reproducing this: https://github.com/tume/select-datepicker-conflict

Just react-datepicker example + empty react-select component. You can see the weird behaviour when opening calendar and then trying to open the select component.

I was digging this a bit deeper and my understanding now is that onBlur is being called before onClickOutside which means the calendar is still open when onBlur is called. And this seems to happen because react-select listens to mouseDown event and is calling focus in that handler and by my speculation is then calling onBlur in react-datepicker.

I'm not so familiar with all these events and their correct calling order so would appreciate if someone can point out that should this be fixed in this component or is this more to do with react-select ?

tume avatar Feb 07 '17 18:02 tume

@tume The lines u referenced here: https://github.com/Hacker0x01/react-datepicker/blob/master/src/datepicker.jsx#L156-L157 , removing them fixes the issue?

gilbertkaradja avatar Feb 07 '17 19:02 gilbertkaradja

@gilbertKaradja I was testing by calling this.props.onBlur(event) always so skipping that deferFocusInput().

tume avatar Feb 07 '17 19:02 tume

And that fixed the flickering?

gilbertkaradja avatar Feb 07 '17 20:02 gilbertkaradja

Yeah in my testing but it's not really a proper fix.

tume avatar Feb 07 '17 20:02 tume

Sure, but have you noticed any side-effects? I haven't had time to properly investigate this, might do that tomorrow

gilbertkaradja avatar Feb 07 '17 20:02 gilbertkaradja

If I recall correctly, removing those lines would cause the input to not refocus after the calendar is clicked.

rafeememon avatar Feb 07 '17 20:02 rafeememon

@rafeememon: If that's all it does I am willing to sacrifice that feature to get rid of the flicker-bug

gilbertkaradja avatar Feb 07 '17 20:02 gilbertkaradja

Unfortunately that'd be breaking behavior that I'm not sure others would be willing to sacrifice =)

rafeememon avatar Feb 07 '17 20:02 rafeememon

@rafeememon: I did't mean remove the functionality entirely. I will employ the "fix" until I or someone else finds a proper solution

gilbertkaradja avatar Feb 07 '17 20:02 gilbertkaradja

Any update on this?

gilbertkaradja avatar Mar 06 '17 15:03 gilbertkaradja

@gilbertKaradja, nope, any help creating a proper fix would be great

rafeememon avatar Mar 06 '17 15:03 rafeememon

Same problem

ibcWillJust42 avatar Mar 07 '17 08:03 ibcWillJust42

+1

kevinrue avatar Mar 07 '17 17:03 kevinrue

One way that worked for me: <DatePicker onChange={ this.onDateValueChange } selected={this.state.date} ref="picker" onClickOutside={this.clickOutside}/>;

And clickOutside method:

clickOutside() {
    this.refs.picker.cancelFocusInput();
    this.refs.picker.setOpen(false);
}

shkumbin avatar Mar 27 '17 10:03 shkumbin

https://github.com/JedWatson/react-select/issues/1539

swbullis avatar Mar 27 '17 12:03 swbullis

I made a proof of concept PR that removes onclickoutside almost entirely and instead uses blur to hide the calendar component. I personally believe onclickoutside is inherently flawed and that any other component on the page should be able to call stopPropagation without breaking another.

https://github.com/malectro/react-datepicker/pull/1

Unfortunately this changes some basic behavior of react-datepicker and breaks 4 tests (mostly related to the year and month dropdowns and how it responds to blur events that aren't due to clicks). But I don't feel these changes to be degradations.

Thoughts? There's a lot more work to do fixing up the tests and possibly removing onclickoutside entirely, but I wanted to get feedback before proceeding.

malectro avatar May 15 '17 18:05 malectro

Workaround, that works for me until the issue will be fixed:

import DatePicker from "react-datepicker";

class CustomDatePicker extends DatePicker {
  constructor(props) {
    super(props);
  }

  deferFocusInput = () => {
    this.cancelFocusInput();
  }
}

export default CustomDatePicker;

and then you could use CustomDatePicker instead of DatePicker

Alexey-Lukin avatar Jul 12 '17 08:07 Alexey-Lukin

From what I can tell, the problem is that the handleBlur method is blindly refocusing the input when it is blurred, if the calendar was open. When react-select is clicked, it is focused (and the menu opens) but the calendar input immediately "steals" focus back again.

I can't work out what the actual purpose of calling the deferFocusInput method is (or actually trace how the component detects that something else has been focused, for example tabbing through the inputs in the example) so if someone can shed light on this I'd be happy to help work out how both components can play nicely together.

JedWatson avatar Sep 12 '17 12:09 JedWatson