Last active
September 7, 2017 18:52
-
-
Save Magellol/81ab9765fe7af4ad24969b7728bbb5b1 to your computer and use it in GitHub Desktop.
Give the developer the ability to have more flexibility on the layout if a function is passed as children.
This file contains hidden or 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
const propTypes = { | |
children: PropTypes.oneOfType([PropTypes.func, PropTypes.node]), | |
}; | |
const defaultProps = { | |
children: null, | |
}; | |
const SaveButton = () => ... | |
const ActionsContainer = ({ children }) => ... | |
const ConfigurationPanel = ({ children, onSave }) => | |
<div> | |
{typeof children === 'function' | |
? children({ | |
ActionsContainer, | |
SaveButton: () => <SaveButton handleClick={onSave} />, | |
}) | |
: <div> | |
{children} | |
<ActionsContainer> | |
<SaveButton handleClick={onSave} /> | |
</ActionsContainer> | |
</div>} | |
</div>; | |
// Simple usage, less flexiblity on the rendered layout. | |
const App = () => ( | |
<ConfigurationPanel> | |
<div>Some children as node here</div> | |
</ConfigurationPanel> | |
); | |
// Advanced usage, more flexiblity on the rendered layout. | |
const App = () => ( | |
<ConfigurationPanel> | |
{({ ActionsContainer, SaveButton }) => ( | |
<div>Some children></div> | |
<ActionsContainer> | |
<CancelButton /> | |
<SaveButton /> | |
</ActionsContainer> | |
)} | |
</ConfigurationPanel> | |
); | |
ReactDOM.render(<App />, document.getElementById('root')); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment