Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use createPortal API to implement RootModal #267

Merged
merged 1 commit into from
Jan 25, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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