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.
[added] Portal component; replaces OverlayMixin
- Loading branch information
Showing
5 changed files
with
167 additions
and
13 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,34 @@ | ||
import React from 'react'; | ||
import CustomPropTypes from './utils/CustomPropTypes'; | ||
import { OverlayMixin } from './OverlayMixin'; | ||
|
||
let Portal = React.createClass({ | ||
|
||
displayName: 'Portal', | ||
|
||
propTypes: { | ||
/** | ||
* The DOM Node that the Component will render it's children into | ||
*/ | ||
container: CustomPropTypes.mountable | ||
}, | ||
|
||
// we use the mixin for now, to avoid duplicating a bunch of code. | ||
// when the deprecation is removed we need to move the logic here from OverlayMixin | ||
mixins: [ OverlayMixin ], | ||
|
||
renderOverlay() { | ||
if (!this.props.children) { | ||
return null; | ||
} | ||
|
||
return React.Children.only(this.props.children); | ||
}, | ||
|
||
render() { | ||
return null; | ||
} | ||
}); | ||
|
||
|
||
export default Portal; |
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,78 @@ | ||
import React from 'react'; | ||
import ReactTestUtils from 'react/lib/ReactTestUtils'; | ||
import Portal from '../src/Portal'; | ||
|
||
describe('Portal', function () { | ||
let instance; | ||
|
||
let Overlay = React.createClass({ | ||
render() { | ||
return ( | ||
<div> | ||
<Portal ref='p' {...this.props}>{this.props.overlay}</Portal> | ||
</div> | ||
); | ||
}, | ||
getOverlayDOMNode(){ | ||
return this.refs.p.getOverlayDOMNode(); | ||
} | ||
}); | ||
|
||
afterEach(function() { | ||
if (instance && ReactTestUtils.isCompositeComponent(instance) && instance.isMounted()) { | ||
React.unmountComponentAtNode(React.findDOMNode(instance)); | ||
} | ||
}); | ||
|
||
it('Should render overlay into container (DOMNode)', function() { | ||
let container = document.createElement('div'); | ||
|
||
instance = ReactTestUtils.renderIntoDocument( | ||
<Overlay container={container} overlay={<div id="test1" />} /> | ||
); | ||
|
||
assert.equal(container.querySelectorAll('#test1').length, 1); | ||
}); | ||
|
||
it('Should render overlay into container (ReactComponent)', function() { | ||
let Container = React.createClass({ | ||
render() { | ||
return <Overlay container={this} overlay={<div id="test1" />} />; | ||
} | ||
}); | ||
|
||
instance = ReactTestUtils.renderIntoDocument( | ||
<Container /> | ||
); | ||
|
||
assert.equal(React.findDOMNode(instance).querySelectorAll('#test1').length, 1); | ||
}); | ||
|
||
it('Should not render a null overlay', function() { | ||
let Container = React.createClass({ | ||
render() { | ||
return <Overlay ref='overlay' container={this} overlay={null} />; | ||
} | ||
}); | ||
|
||
instance = ReactTestUtils.renderIntoDocument( | ||
<Container /> | ||
); | ||
|
||
assert.equal(instance.refs.overlay.getOverlayDOMNode(), null); | ||
}); | ||
|
||
it('Should render only an overlay', function() { | ||
let OnlyOverlay = React.createClass({ | ||
render() { | ||
return <Portal ref='p' {...this.props}>{this.props.overlay}</Portal>; | ||
} | ||
}); | ||
|
||
let overlayInstance = ReactTestUtils.renderIntoDocument( | ||
<OnlyOverlay overlay={<div id="test1" />} /> | ||
); | ||
|
||
assert.equal(overlayInstance.refs.p.getOverlayDOMNode().nodeName, 'DIV'); | ||
}); | ||
}); |