-
-
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.
- Loading branch information
Showing
5 changed files
with
89 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,35 @@ | ||
# `useAdopt` | ||
|
||
Extracts a values from multiple render-prop or FaCC components. | ||
This hook is similar to [`useRenderProp`](./useRenderProp.md), but | ||
it allows to specify a named map of multiple render-prop elements | ||
from which to extract values. | ||
|
||
|
||
## Usage | ||
|
||
```jsx | ||
import {useAdopt} from 'react-use'; | ||
|
||
const FaCC = ({children}) => { | ||
return children('VALUE-FaCC'); | ||
}; | ||
const RenderProp = ({render}) => { | ||
return render('VALUE-RenderProp'); | ||
}; | ||
|
||
const Demo = () => { | ||
const [fragment, result] = useAdopt({ | ||
facc: <FaCC/>, | ||
renderProp: <RenderProp/>, | ||
}); | ||
|
||
return ( | ||
<> | ||
{fragment} | ||
<div>FaCC: {result.facc[0]}</div> | ||
<div>Render prop: {result.renderProp[0]}</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,32 @@ | ||
import {storiesOf} from '@storybook/react'; | ||
import * as React from 'react'; | ||
import {useAdopt} from '..'; | ||
import ShowDocs from '../util/ShowDocs'; | ||
|
||
const FaCC = ({children}) => { | ||
return children('VALUE-FaCC'); | ||
}; | ||
const RenderProp = ({render}) => { | ||
return render('VALUE-RenderProp'); | ||
}; | ||
|
||
const Demo = () => { | ||
const [fragment, result] = useAdopt({ | ||
facc: <FaCC/>, | ||
renderProp: <RenderProp/>, | ||
}); | ||
|
||
return ( | ||
<> | ||
{fragment} | ||
<div>FaCC: {result.facc[0]}</div> | ||
<div>Render prop: {result.renderProp[0]}</div> | ||
</> | ||
); | ||
}; | ||
|
||
storiesOf('useAdopt', module) | ||
.add('Docs', () => <ShowDocs md={require('../../docs/useAdopt.md')} />) | ||
.add('Demo', () => | ||
<Demo/> | ||
) |
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,19 @@ | ||
import * as React from 'react'; | ||
import useRenderProp from './useRenderProp'; | ||
|
||
const useAdopt = <T extends {[key: string]: any[]}>(map: {[key in keyof T]: React.ReactElement<any>}): [React.ReactElement<any>, T] => { | ||
const keys = Object.keys(map); | ||
const fragments: React.ReactElement<any>[] = []; | ||
const result: T = {} as T; | ||
|
||
keys.sort(); | ||
for (const key of keys) { | ||
const [fragment, value] = useRenderProp(map[key]); | ||
fragments.push(React.cloneElement(fragment, {key})); | ||
result[key] = value; | ||
} | ||
|
||
return [React.createElement(React.Fragment, null, ...fragments), result]; | ||
}; | ||
|
||
export default useAdopt; |