-
-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: 🎸 add createRenderProp function for creating render-props
- Loading branch information
Showing
4 changed files
with
38 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import createRenderProp from '../util/createRenderProp'; | ||
import useKey from '../useKey'; | ||
|
||
const UseKey = createRenderProp(useKey, ({filter, fn, deps, ...rest}) => [filter, fn, rest, deps]); | ||
|
||
export default UseKey; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import {storiesOf} from '@storybook/react'; | ||
import * as React from 'react'; | ||
import UseKey from '../UseKey'; | ||
|
||
storiesOf('Components|<UseKey>', module) | ||
.add('Demo', () => | ||
<div> | ||
Press "q" key! | ||
<UseKey filter='q' fn={() => alert('Q pressed!')} /> | ||
</div> | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
const defaultMapPropsToArgs = props => [props]; | ||
|
||
const createRenderProp = (hook, mapPropsToArgs = defaultMapPropsToArgs) => { | ||
const RenderProp = (props) => { | ||
const state = hook(...mapPropsToArgs(props)); | ||
const {children, render = children} = props; | ||
return render ? render(state) || null : null; | ||
}; | ||
|
||
return RenderProp; | ||
}; | ||
|
||
export default createRenderProp; |