Skip to content
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

default handler #2

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Syntax sugar for events handlers. Kind of like the excellent, [classnames](https

## Usage

This module gives you some special syntax to make your event handlers more declarative and functional. You can create handlers for only certain keypresses, or easily attach multiple handlers to a single event.
This module gives you some special syntax to make your event handlers more declarative and functional. You can create handlers for only certain keypresses, or easily attach multiple handlers to a single event.

This example calls `updateText` with every keydown event, but also calls `submit` only when enter is pressed:

Expand All @@ -32,11 +32,22 @@ You may pass an array, object, or just a plain function, and you may also do any
var ev = require('@f/event-handler')

function render () {
return <input onKeyDown={ev[{enter: [submit, close]}, updateText]} />
return <input onKeyDown={ev([{enter: [submit, close]}, updateText])} />
}
```

This will close the input and submit when `enter` is pressed, it will also update the text on every normal keydown.
If your descriptor is an object, the 'default' key will match every event:

```js
var ev = require('@f/event-handler')

function render () {
return <input onKeyDown={ev({enter: submit, default: updateText})} />
}
```

This will submit the input when `enter` is pressed and update the text on every __other__ keydown.

## Return values

Expand Down
5 changes: 5 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,15 @@ function match (obj) {
return function (e) {
var chord = eventKey(e)
var fn = obj[chord]
var defaultFn = obj.default

if (isFunction(fn)) {
return fn(e)
}

if (isFunction(defaultFn)) {
return defaultFn(e)
}
}
}

Expand Down
15 changes: 15 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,21 @@ test('should work', function (t) {
t.end()
})

test('default handler', function (t) {
var descriptor = {
'ctrl+shift+enter': function() {
return 'combination'
},
'default': function() {
return 'default'
}
}

t.equal(ev(descriptor)(event('ctrl+shift+enter')), 'combination')
t.equal(ev(descriptor)(event('x')), 'default')
t.end()
})

/**
* Helpers
*/
Expand Down