Repository navigation
feat(handlers): prevent default behavior with preventDownshiftDefault - #358
Conversation
Now users can provide their own event handlers and set `event.preventDownshiftDefault = true` to completely disable the default behavior. Also: * Tests * Docs
kentcdodds
left a comment
There was a problem hiding this comment.
Looking awesome! Just a comment about docs.
|
|
||
| For example: | ||
|
|
||
| ``` |
There was a problem hiding this comment.
Let's add javascript here so we get syntax highlighting.
Also, could we add an example that shows that you can completely override the built-in prop by adding the prop after spreading the prop getter?
Also use javascript syntax highlighting for code blocks.
Codecov Report
@@ Coverage Diff @@
## master #358 +/- ##
=====================================
Coverage 100% 100%
=====================================
Files 4 4
Lines 330 330
Branches 84 85 +1
=====================================
Hits 330 330
Continue to review full report at Codecov.
|
kentcdodds
left a comment
There was a problem hiding this comment.
Cool, looking good 👍 Just a few thoughts.
| e => { | ||
| e.defaultPrevented = true | ||
| }, | ||
| () => { |
There was a problem hiding this comment.
Rather than this, could we use Jest mock functions (via jest.fn()) and assertions? Could make the defaultCalled cleaner as well.
| }, | ||
| } | ||
| ``` | ||
|
|
There was a problem hiding this comment.
Cool, thanks for the update. Could we make these examples more practical so folks who may be more beginner will understand. So like:
<input {...getInputProps()} onKeyDown={event => { /* my own stuff */} />And the same for the example above?
| * They are executed in order until one of them sets | ||
| * `event.preventDownshiftDefault = true`. This allows the | ||
| * normal event to maintain its intended `event.defaultPrevented` | ||
| * state. |
There was a problem hiding this comment.
The last sentence of this comment is unnecessary.
kentcdodds
left a comment
There was a problem hiding this comment.
Thanks so much for iterating with me on this so well. We're almost there! Very well done!
| ## EventHandlers | ||
|
|
||
| You can provide your own event handlers to Downshift which will be called before the default handlers. | ||
|
|
There was a problem hiding this comment.
Maybe we should add a simple example here:
<input {...getInputProps({onKeyDown: event => /* your handler code */ })} />| ```javascript | ||
| <input | ||
| {...getInputProps({ | ||
| onKeyDown(event) { |
There was a problem hiding this comment.
Let's change this to an arrow function just in case someone new copy/pastes this and tries to use this and doesn't understand why it's not working.
|
|
||
| test('call default handler when defaultDownshiftPrevented and defaultPrevented are false', () => { | ||
| const customHandler = jest.fn(() => {}) | ||
| const defaultHandler = jest.fn(() => {}) |
There was a problem hiding this comment.
If you don't care about the implementation you can just do: jest.fn() and that's the same as what you have here.
Other than that, these tests are awesome 👍
Thanks for the feedback! Better to get this done right. :) |
kentcdodds
left a comment
There was a problem hiding this comment.
This is excellent! Thank you so much for your help here!
|
Thanks so much for your help! I've added you as a collaborator on the project. Please make sure that you review the |
There was an issue with a minor release, so this manual-releases.md change is to release a new minor version. No idea what went wrong... Reference: #358
downshift-js#358) * chore(contrib): add @ericedem * feat(handlers): prevent default behavior with preventDownshiftDefault Now users can provide their own event handlers and set `event.preventDownshiftDefault = true` to completely disable the default behavior. Also: * Tests * Docs * docs(eventHandlers): provide override example Also use javascript syntax highlighting for code blocks. * refactor(composeEventHandlers): use jest for testing * docs(eventHandlers): make examples new user friendly * fix(composeEventHandlers): remove extraneous doc comment sentence. * refactor(composeEventHandler): use parameter-less jest.fn() * docs(eventHandlers): add basic example * Update README.md Closes downshift-js#353 Closes downshift-js#330
There was an issue with a minor release, so this manual-releases.md change is to release a new minor version. No idea what went wrong... Reference: downshift-js#358
What: allow event handlers to prevent default downshift behavior
This is the non-breaking change for #353.
Why: Previously, you could only prevent default behavior by calling event.preventDefault(). This worked, but it would also prevent events from triggering other default behaviors like form submission.
How: composeEventHandlers() now checks for
event.preventDownshiftDefaultin addition toevent.defaultPrevented. The check forevent.defaultPreventedwill be removed in the next breaking change.Checklist: