Skip to content

Commit

Permalink
Use createPortal API to implement RootModal
Browse files Browse the repository at this point in the history
  • Loading branch information
patw0929 committed Jan 24, 2019
1 parent ea768de commit b05cc98
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 17 deletions.
26 changes: 12 additions & 14 deletions src/components/RootModal.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { Component } from 'react';
import React, { Component, Fragment } from 'react';
import PropTypes from 'prop-types';
import ReactDOM from 'react-dom';

Expand All @@ -7,29 +7,27 @@ export default class RootModal extends Component {
children: PropTypes.node,
};

componentDidMount() {
constructor(props) {
super(props);

this.modalTarget = document.createElement('div');
this.modalTarget.className = 'intl-tel-input iti-container';
document.body.appendChild(this.modalTarget);
this._render();
}

shouldComponentUpdate() {
this._render();

return false;
componentDidMount() {
document.body.appendChild(this.modalTarget);
}

componentWillUnmount() {
ReactDOM.unmountComponentAtNode(this.modalTarget);
document.body.removeChild(this.modalTarget);
}

_render() {
ReactDOM.render(<div>{this.props.children}</div>, this.modalTarget);
}

render() {
return <noscript />;
return ReactDOM.createPortal(
<Fragment>
{this.props.children}
</Fragment>,
this.modalTarget
);
}
}
6 changes: 3 additions & 3 deletions src/components/__tests__/RootModal.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ describe('RootModal', function() {
this.makeSubject = () => {
return mount(
<RootModal>
<div>foo</div>
<div className="root">foo</div>
</RootModal>,
{
attachTo: document.body,
Expand All @@ -19,10 +19,10 @@ describe('RootModal', function() {
};
});

it('should has a noscript tag', () => {
it('should has a div.root tag', () => {
const subject = this.makeSubject();

expect(subject.find('noscript').length).toBeTruthy();
expect(subject.find('div.root').length).toBeTruthy();
});

it('should has parent element which has specific className', () => {
Expand Down

0 comments on commit b05cc98

Please sign in to comment.