Skip to content

Change how to prevent default events #353

Closed

Description

@kentcdodds

Right now if you add a custom event handler (like onKeyDown to the getInputProps for example), you can prevent the default behavior with event.preventDefault(). This has worked pretty well, but honestly it's overloading the feature of preventing the default from events so we should come up with another way to signal preventing the default behavior.

What if instead you simply add a property to the event like: event.preventDownshiftDefault = true and then downshift references that rather than event.defaultPrevented?

I think that'd be pretty simple and straightforward and the upgrade for this breaking change would be pretty smooth. In fact, we could support both for a little while without much trouble.

Thoughts?

Activity

  1. ericedem commented on Mar 2, 2018

    @ericedem
    Collaborator

    Ok so just to clarify the particular case mentioned in #330. The new workaround will be:

    getInputProps({
      onKeyDown(event) {
        if (event.key === 'Enter') {
          // If nothing is highlighted, let the normal input event through.
          if (!isOpen || highlightedIndex === null) {
            event.preventDownshiftDefault = true;
          }
        }
      }
    })
    

    Which overrides the default behavior of calling event.preventDefault() when there is no item highlighted, but otherwise let Downshift behave normally.

  2. kentcdodds commented on Mar 2, 2018

    @kentcdodds
    MemberAuthor

    Thanks for the example @ericedem. That's exactly what I would do 👍

  3. ericedem commented on Mar 3, 2018

    @ericedem
    Collaborator

    If there is a consensus, happy to take this one on.

  4. kentcdodds commented on Mar 3, 2018

    @kentcdodds
    MemberAuthor

    So I want this to be implemented. I'd be happy to implement it myself, but I'm happy to accept a PR from anyone who gets to it before me.

    For this first iteration we should implement it as a non-breaking change and support both this and the current mechanism. It shouldn't be too much work. Probably just documentation, tests, and an update in the utils file for composeEventHandlers:

    https://github.com/paypal/downshift/blob/10d2cf6128bc008bd7984d537edd812dd2e4306e/src/utils.js#L132-L146

    Should probably just be:

    // TODO: remove everything after the || in the next breaking change
    return event.preventDownshiftDefault || event.defaultPrevented
  5. kentcdodds commented on Mar 3, 2018

    @kentcdodds
    MemberAuthor

    Perfect @ericedem 👍 Thanks!

  6. ericedem commented on Mar 3, 2018

    @ericedem
    Collaborator

    @kentcdodds I have an implementation working for this, where do you recommend putting docs, Under getInputProps perhaps? Or maybe a new section related to handlers?

  7. kentcdodds commented on Mar 3, 2018

    @kentcdodds
    MemberAuthor

    Let's add a new section for it. It applies to several prop getters

  8. k-ode commented on Mar 5, 2018

    @k-ode
    Collaborator

    Just wanted to add that this would be great. I was very confused by the current behavior. We use Downshift together with Draft.js and have to use this ugly thing to make them play nice together.

    getNewInputProps(downshiftProps) {
        const inputProps = downshiftProps.getInputProps();
        const inputOnKeyDown = inputProps.onKeyDown;
        inputProps.onKeyDown = ev => {
             ev.defaultPrevented = false;
    
             inputOnKeyDown(ev);
    
             ev.preventDefault();
    
             this.onKeyDown(ev);
         };
         return inputProps;
    }
    
  9. kentcdodds commented on Mar 5, 2018

    @kentcdodds
    MemberAuthor

    Yeah, it was kind of a last minute feature and in retrospect wasn't thought through very well 😅

  10. pcattori commented on Apr 14, 2018

    @pcattori

    @kimgronqvist any chance you can provide a link to a more complete example?

    I'm also trying to integrate draftjs with downshift, but it seems that draftjs is eating the up/down/escape events and not letting downshift see them.

  11. k-ode commented on Apr 14, 2018

    @k-ode
    Collaborator

    Sure! This example conveys the general idea: https://codesandbox.io/s/zx9z9275xm

  12. pcattori commented on Apr 14, 2018

    @pcattori

    @kimgronqvist you missed a pair of {} to destructure onKeyDown from getInputProps 😛 :

    const { onKeyDown } = getInputProps();

    link with the fix

    thanks!

  13. added a commit that references this issue on Sep 30, 2018
    e0ff5ab
  14. TLadd commented on Jul 8, 2019

    @TLadd
    Contributor

    The correct way to do this now is to set the field on the native event rather than the synthetic event, since React reuses synthetic events. The README has a correct example

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions