downshift icon indicating copy to clipboard operation
downshift copied to clipboard

Cannot prevent default keyDown handling and weird debugging issue

Open GonchuB opened this issue 7 years ago • 8 comments

#666 😈

  • downshift version: 3.2.2
  • node version: codesandbox
  • npm (or yarn) version: codesandbox

Relevant code or config

Snippet 1

function stateReducer(state, changes) {
  // If dropdown is about to get open and there is not highlighted index, highlight the first one.
  if (
    changes.hasOwnProperty("isOpen") &&
    changes.isOpen &&
    state.highlightedIndex === null &&
    changes.type === Downshift.stateChangeTypes.clickButton
  ) {
    return {
      ...changes,
      highlightedIndex: 0
    };
  }

  // FIXME: This one I had to add because after handling the previous change
  // another one got dispatched immediately after with `changes = { highlightedIndex: null }`
  if (
    changes.hasOwnProperty("highlightedIndex") &&
    changes.highlightedIndex === null &&
    changes.type === Downshift.stateChangeTypes.clickButton
  ) {
    return {
      ...changes,
      highlightedIndex: 0
    };
  }

  return changes;
}

Snippet 2

<button
  className="toggle"
  ref={this.dropdownRef}
  {...downshiftProps.getToggleButtonProps({
    onKeyDown: event => {
      // FIXME: This is not preventing the menu to open.
      if (event.keyCode === 13 && !downshiftProps.isOpen) {
        event.preventDownshiftDefault = true;
      }
    }
  })}
>
  ...
</button>

What you did:

Created a text dropdown with Downshift with 2 custom behaviors:

  • When dropdown gets opened and Enter key is the pressed, first option should be selected.
  • When dropdown is closed and Enter key is pressed (with focus on the dropdown) the surrounding form should be submitted (as if the form received the enter Key press)

What happened:

Implemented first custom behaviour with Snippet 1. The first statement should cover the functionality, but for some reason another set of changes is being dispatched immediately after I "correct" the next state. So I needed to patch another state change to highlight the first option.

Implemented second custom behaviour with Snippet 2. The Enter key handling should not be handled by the component in general, so I'm setting the property on the event for downshift to prevent default behaviour. The behaviour is only prevent it when I put a breakpoint in event.preventDownshiftDefault = true;. When I'm not debugging, the menu just opens up. (I would still need to check that the form is submitted even without downshift handling the keyDown)

Reproduction repository:

https://codesandbox.io/s/x3prz3l224

Problem description:

  • Can't seem to prevent downshift from handling keyDown events
  • Can't seem to override state change in a straightforward way

GonchuB avatar Feb 12 '19 08:02 GonchuB

Hello. Let me answer to your points.

  1. From what I can see in your state change code, it seems that you can achieve that functionality by using defaultHighlightedIndex prop and pass 0 to it :). Not sure why the reducer is called again, maybe something in your code triggering it, but just use that prop I suggested and remove that custom stateReducer.
  2. Right now I am not really familiar with preventing the Downshift event from happening, but from reading the docs, the syntax is a bit different from your example:
const ui = (
  <Downshift>
    {({getInputProps}) => (
      <input
        {...getInputProps({
          onKeyDown: event => {
            if (event.key === 'Enter') {
              // Prevent Downshift's default 'Enter' behavior.
              event.nativeEvent.preventDownshiftDefault = true

              // your handler code
            }
          },
        })}
      />
    )}
  </Downshift>
)

Try with the nativeEvent and see what happens. Let me know if both your problems get solved, thanks!

silviuaavram avatar Feb 12 '19 16:02 silviuaavram

@silviuavram for 1, you are right, the defaultHightlightedIndex prop works as expected (I guess I had a typo when I tried it out 😢). Still, would be nice to understand how and why the stateReducer is being called.

As for 2, its the same, downshift code checks for both (neither is working for me): https://github.com/downshift-js/downshift/blob/f2b7b6f3de493352b2d006b10ca897588592cf67/src/utils.js#L93..L97

GonchuB avatar Feb 12 '19 16:02 GonchuB

  1. stateReducer is called in our internalSetState and is used in case you want to change something before the state is set.
  2. you still need to preventDefault on the onClick since hitting Enter on a button not only triggers a onKeyDown, but also a onClick. We love Javascript :)

silviuaavram avatar Feb 12 '19 16:02 silviuaavram

@silviuavram

  1. I understand that, but I can’t see why I’m receiving a null highlighted index in the second call (and no other change, with the same changes.type that it was called before)

  2. I was afraid I would need to prevent the default given at the end it is a button click. The downside is that I can’t get the normal behavior of “submitting a form with enter key” that I would get with a submit button if the toggle is getting the click and preventing default :(.

What I would love is to have natural propagation of the event, as if it was not being handled by downshift or the component for that matter. That is the behavior you would get with a normal select (that is inside a form with a submit button) when receiving the Enter keypress.

Note: I could do 2 if React allowed to dispatch synthethic events. Just prevent default and dispatch to the appropriate component

GonchuB avatar Feb 12 '19 17:02 GonchuB

  1. at the moment, we have 2 state changes for toggle. I don't think this is really required, maybe you can help out. in toggleMenu first there is a setState with a new isOpen then there is a callback to set the highlightedIndex. As it's a toggle and there is no defaultHighlightedIndex, that ends as being null. So if you can help improving this, that could be very nice :).

  2. I still don't understand why you need form related stuff on a dropdown toggle button.

silviuaavram avatar Feb 13 '19 14:02 silviuaavram

@silviuavram

  1. Clear. I will take a look and see if I can work it out.

  2. Maybe I did not make myself clear. Want I would like to achieve is the same behavior as having a native