11import React , { Component } from "react" ;
2- import ReactDOM from "react-dom" ;
32import PropTypes from "prop-types" ;
43import Container from "@zeecoder/container-query" ;
54
6- export default class ContainerQuery extends Component {
5+ class ContainerQuery extends Component {
76 constructor ( props ) {
87 super ( props ) ;
98
10- this . state = { width : 0 , height : 0 } ;
11-
12- this . containerOptions = { ...this . props . options } ;
9+ this . options = { ...this . props . options } ;
1310
1411 // Listen to size changes only if needed
1512 if ( typeof this . props . children === "function" ) {
16- this . containerOptions . handleResize = this . handleResize ;
13+ this . options . handleResize = this . handleResize ;
1714 }
1815 }
1916
17+ state = { width : 0 , height : 0 } ;
18+
19+ ref = React . createRef ( ) ;
20+
2021 handleResize = size => {
2122 if ( this . __willUnmount ) {
2223 return ;
@@ -25,33 +26,33 @@ export default class ContainerQuery extends Component {
2526 this . setState ( size ) ;
2627 } ;
2728
28- instantiateContainer ( ) {
29- if ( ! this . props . meta ) {
30- return ;
31- }
32-
33- const element = this . props . element || ReactDOM . findDOMNode ( this ) ;
34- if ( element && this . element !== element ) {
35- this . element = element ;
36- new Container ( this . element , this . props . meta , this . containerOptions ) ;
37- }
38- }
39-
4029 componentDidMount ( ) {
41- this . instantiateContainer ( ) ;
42- }
43-
44- componentDidUpdate ( ) {
45- this . instantiateContainer ( ) ;
30+ new Container ( this . ref . current , this . props . meta , this . options ) ;
4631 }
4732
4833 componentWillUnmount ( ) {
4934 this . __willUnmount = true ;
5035 }
5136
5237 render ( ) {
38+ // Removing all props only used by this component, and only passing
39+ // through the rest that was supposedly meant for the wrapped child
40+ const { options, as, meta, children, ...props } = this . props ;
41+
42+ // Needs to start with a capital letter for the jsx compiler.
43+ // @see https://stackoverflow.com/a/38823404
44+ const TagName = as ;
45+
46+ return (
47+ < TagName { ...props } ref = { this . ref } >
48+ { this . doRender ( ) }
49+ </ TagName >
50+ ) ;
51+ }
52+
53+ doRender ( ) {
5354 if ( typeof this . props . children === "function" ) {
54- return this . props . children ( this . state . size ) ;
55+ return this . props . children ( this . state ) ;
5556 } else if ( this . props . children ) {
5657 return this . props . children ;
5758 }
@@ -61,12 +62,15 @@ export default class ContainerQuery extends Component {
6162}
6263
6364ContainerQuery . defaultProps = {
64- meta : null ,
65- options : { }
65+ options : { } ,
66+ as : "div"
6667} ;
6768
6869ContainerQuery . propTypes = {
69- meta : PropTypes . object ,
7070 options : PropTypes . object ,
71- children : PropTypes . oneOfType ( [ PropTypes . element , PropTypes . func ] )
71+ as : PropTypes . string ,
72+ meta : PropTypes . object . isRequired ,
73+ children : PropTypes . oneOfType ( [ PropTypes . node , PropTypes . func ] ) . isRequired
7274} ;
75+
76+ export default ContainerQuery ;
0 commit comments