Created
April 19, 2017 16:18
-
-
Save pavel-lens/1a0d1c94df21e6b6c67cf3feb3477cb6 to your computer and use it in GitHub Desktop.
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
renderWrappedChildren(children) { | |
// Traverse through all children with pretty functional way :-) | |
return React.Children.map(children, (child) => { | |
// This is support for non-node elements (eg. pure text), they have no props | |
if (!child.props) { | |
return child | |
} | |
// If current component has additional children, traverse through them as well! | |
if (child.props.children) { | |
// You have to override also children here | |
return React.cloneElement(child, { | |
children: this.renderWrappedChildren(child.props.children), | |
onChange: this.createHandleChange(child.props.onChange), | |
}) | |
} | |
// Return new component with overridden `onChange` callback | |
return React.cloneElement(child, { | |
onChange: this.createHandleChange(child.props.onChange), | |
}) | |
}, | |
) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment