forked from react-bootstrap/react-bootstrap
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request react-bootstrap#644 from taion/overlay-context
Add context-forwarding trigger factory methods
- Loading branch information
Showing
5 changed files
with
187 additions
and
39 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
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,48 @@ | ||
import React from 'react'; | ||
|
||
/** | ||
* Creates new trigger class that injects context into overlay. | ||
*/ | ||
export default function createContextWrapper(Trigger, propName) { | ||
return function (contextTypes) { | ||
class ContextWrapper extends React.Component { | ||
getChildContext() { | ||
return this.props.context; | ||
} | ||
|
||
render() { | ||
// Strip injected props from below. | ||
const {wrapped, ...props} = this.props; | ||
delete props.context; | ||
|
||
return React.cloneElement(wrapped, props); | ||
} | ||
} | ||
ContextWrapper.childContextTypes = contextTypes; | ||
|
||
class TriggerWithContext { | ||
render() { | ||
const props = {...this.props}; | ||
props[propName] = this.getWrappedOverlay(); | ||
|
||
return ( | ||
<Trigger {...props}> | ||
{this.props.children} | ||
</Trigger> | ||
); | ||
} | ||
|
||
getWrappedOverlay() { | ||
return ( | ||
<ContextWrapper | ||
context={this.context} | ||
wrapped={this.props[propName]} | ||
/> | ||
); | ||
} | ||
} | ||
TriggerWithContext.contextTypes = contextTypes; | ||
|
||
return TriggerWithContext; | ||
}; | ||
} |
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