1
1
/* eslint react/no-find-dom-node: 0 */
2
2
3
- import { Component } from 'react'
4
- import ReactDOM from 'react-dom'
3
+ import { Component , Children , createRef , cloneElement } from 'react'
5
4
import PropTypes from 'prop-types'
6
5
import ScrollSyncContext from './support/ScrollSyncContext'
7
6
@@ -19,7 +18,10 @@ export default class ScrollSyncPane extends Component {
19
18
20
19
static propTypes = {
21
20
children : PropTypes . node . isRequired ,
22
- attachTo : PropTypes . object ,
21
+ attachTo : PropTypes . oneOfType ( [
22
+ PropTypes . func ,
23
+ PropTypes . shape ( { current : PropTypes . any } )
24
+ ] ) ,
23
25
group : PropTypes . oneOfType ( [ PropTypes . string , PropTypes . arrayOf ( PropTypes . string ) ] ) ,
24
26
enabled : PropTypes . bool
25
27
}
@@ -29,34 +31,63 @@ export default class ScrollSyncPane extends Component {
29
31
enabled : true
30
32
}
31
33
32
- static contextTypes = {
33
- registerPane : PropTypes . func ,
34
- unregisterPane : PropTypes . func
34
+ constructor ( props ) {
35
+ super ( props )
36
+ this . childRef = createRef ( )
35
37
}
36
38
37
39
componentDidMount ( ) {
38
40
if ( this . props . enabled ) {
39
- this . node = this . props . attachTo || ReactDOM . findDOMNode ( this )
40
- this . context . registerPane ( this . node , this . toArray ( this . props . group ) )
41
+ this . updateNode ( )
42
+ if ( this . node ) {
43
+ this . context . registerPane ( this . node , this . toArray ( this . props . group ) )
44
+ }
41
45
}
42
46
}
43
47
44
48
componentDidUpdate ( prevProps ) {
45
- if ( this . props . enabled && this . props . group !== prevProps . group ) {
49
+ if ( this . props . attachTo !== prevProps . attachTo ) {
50
+ if ( this . node ) {
51
+ this . context . unregisterPane ( this . node , this . toArray ( prevProps . group ) )
52
+ }
53
+ this . updateNode ( )
54
+ if ( this . node ) {
55
+ this . context . registerPane ( this . node , this . toArray ( prevProps . group ) )
56
+ }
57
+ }
58
+ if ( this . node && this . props . enabled !== prevProps . enabled ) {
59
+ if ( this . props . enabled ) {
60
+ this . context . registerPane ( this . node , this . toArray ( prevProps . group ) )
61
+ } else {
62
+ this . context . unregisterPane ( this . node , this . toArray ( prevProps . group ) )
63
+ }
64
+ }
65
+ if ( this . node && this . props . enabled && this . props . group !== prevProps . group ) {
46
66
this . context . unregisterPane ( this . node , this . toArray ( prevProps . group ) )
47
67
this . context . registerPane ( this . node , this . toArray ( this . props . group ) )
48
68
}
49
69
}
50
70
51
71
componentWillUnmount ( ) {
52
- if ( this . props . enabled ) {
72
+ if ( this . node && this . props . enabled ) {
53
73
this . context . unregisterPane ( this . node , this . toArray ( this . props . group ) )
54
74
}
55
75
}
56
76
57
77
toArray = groups => [ ] . concat ( groups )
58
78
79
+ updateNode = ( ) => {
80
+ if ( this . props . attachTo ) {
81
+ this . node = this . props . attachTo . current
82
+ } else {
83
+ this . node = this . childRef . current
84
+ }
85
+ }
86
+
59
87
render ( ) {
60
- return this . props . children
88
+ if ( this . props . attachTo ) {
89
+ return this . props . children
90
+ }
91
+ return cloneElement ( Children . only ( this . props . children ) , { ref : this . childRef } )
61
92
}
62
93
}
0 commit comments