11/* eslint react/no-find-dom-node: 0 */
22
3- import { Component } from 'react'
4- import ReactDOM from 'react-dom'
3+ import { Component , Children , createRef , cloneElement } from 'react'
54import PropTypes from 'prop-types'
65import ScrollSyncContext from './support/ScrollSyncContext'
76
@@ -19,7 +18,10 @@ export default class ScrollSyncPane extends Component {
1918
2019 static propTypes = {
2120 children : PropTypes . node . isRequired ,
22- attachTo : PropTypes . object ,
21+ attachTo : PropTypes . oneOfType ( [
22+ PropTypes . func ,
23+ PropTypes . shape ( { current : PropTypes . any } )
24+ ] ) ,
2325 group : PropTypes . oneOfType ( [ PropTypes . string , PropTypes . arrayOf ( PropTypes . string ) ] ) ,
2426 enabled : PropTypes . bool
2527 }
@@ -29,34 +31,63 @@ export default class ScrollSyncPane extends Component {
2931 enabled : true
3032 }
3133
32- static contextTypes = {
33- registerPane : PropTypes . func ,
34- unregisterPane : PropTypes . func
34+ constructor ( props ) {
35+ super ( props )
36+ this . childRef = createRef ( )
3537 }
3638
3739 componentDidMount ( ) {
3840 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+ }
4145 }
4246 }
4347
4448 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 ) {
4666 this . context . unregisterPane ( this . node , this . toArray ( prevProps . group ) )
4767 this . context . registerPane ( this . node , this . toArray ( this . props . group ) )
4868 }
4969 }
5070
5171 componentWillUnmount ( ) {
52- if ( this . props . enabled ) {
72+ if ( this . node && this . props . enabled ) {
5373 this . context . unregisterPane ( this . node , this . toArray ( this . props . group ) )
5474 }
5575 }
5676
5777 toArray = groups => [ ] . concat ( groups )
5878
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+
5987 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 } )
6192 }
6293}
0 commit comments