-
Notifications
You must be signed in to change notification settings - Fork 934
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looking awesome! Just a comment about docs.
README.md
Outdated
|
||
For example: | ||
|
||
``` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cool, looking good 👍 Just a few thoughts.
e => { | ||
e.defaultPrevented = true | ||
}, | ||
() => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Rather than this, could we use Jest mock functions (via jest.fn()
) and assertions? Could make the defaultCalled
cleaner as well.
README.md
Outdated
}, | ||
} | ||
``` | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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?
src/utils.js
Outdated
* 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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The last sentence of this comment is unnecessary.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks so much for iterating with me on this so well. We're almost there! Very well done!
README.md
Outdated
## 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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe we should add a simple example here:
<input {...getInputProps({onKeyDown: event => /* your handler code */ })} />
README.md
Outdated
```javascript | ||
<input | ||
{...getInputProps({ | ||
onKeyDown(event) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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. :) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.preventDownshiftDefault
in addition toevent.defaultPrevented
. The check forevent.defaultPrevented
will be removed in the next breaking change.Checklist: