1
1
import React , { Component } from "react" ;
2
- import ReactDOM from "react-dom" ;
3
2
import PropTypes from "prop-types" ;
4
3
import Container from "@zeecoder/container-query" ;
5
4
6
- export default class ContainerQuery extends Component {
5
+ class ContainerQuery extends Component {
7
6
constructor ( props ) {
8
7
super ( props ) ;
9
8
10
- this . state = { width : 0 , height : 0 } ;
11
-
12
- this . containerOptions = { ...this . props . options } ;
9
+ this . options = { ...this . props . options } ;
13
10
14
11
// Listen to size changes only if needed
15
12
if ( typeof this . props . children === "function" ) {
16
- this . containerOptions . handleResize = this . handleResize ;
13
+ this . options . handleResize = this . handleResize ;
17
14
}
18
15
}
19
16
17
+ state = { width : 0 , height : 0 } ;
18
+
19
+ ref = React . createRef ( ) ;
20
+
20
21
handleResize = size => {
21
22
if ( this . __willUnmount ) {
22
23
return ;
@@ -25,33 +26,33 @@ export default class ContainerQuery extends Component {
25
26
this . setState ( size ) ;
26
27
} ;
27
28
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
-
40
29
componentDidMount ( ) {
41
- this . instantiateContainer ( ) ;
42
- }
43
-
44
- componentDidUpdate ( ) {
45
- this . instantiateContainer ( ) ;
30
+ new Container ( this . ref . current , this . props . meta , this . options ) ;
46
31
}
47
32
48
33
componentWillUnmount ( ) {
49
34
this . __willUnmount = true ;
50
35
}
51
36
52
37
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 ( ) {
53
54
if ( typeof this . props . children === "function" ) {
54
- return this . props . children ( this . state . size ) ;
55
+ return this . props . children ( this . state ) ;
55
56
} else if ( this . props . children ) {
56
57
return this . props . children ;
57
58
}
@@ -61,12 +62,15 @@ export default class ContainerQuery extends Component {
61
62
}
62
63
63
64
ContainerQuery . defaultProps = {
64
- meta : null ,
65
- options : { }
65
+ options : { } ,
66
+ as : "div"
66
67
} ;
67
68
68
69
ContainerQuery . propTypes = {
69
- meta : PropTypes . object ,
70
70
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
72
74
} ;
75
+
76
+ export default ContainerQuery ;
0 commit comments