Skip to content

Commit bfbd0a9

Browse files
vanntrongTrong Vo
andauthored
fix: fix ref on ScrollSyncPanel not working (#90)
Co-authored-by: Trong Vo <[email protected]>
1 parent dcdcf37 commit bfbd0a9

File tree

1 file changed

+23
-10
lines changed

1 file changed

+23
-10
lines changed

src/ScrollSyncPane.js

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import ScrollSyncContext from './support/ScrollSyncContext'
1212
* @example ./example.md
1313
*/
1414

15-
1615
export default class ScrollSyncPane extends Component {
1716
static contextType = ScrollSyncContext;
1817

@@ -22,18 +21,26 @@ export default class ScrollSyncPane extends Component {
2221
PropTypes.func,
2322
PropTypes.shape({ current: PropTypes.any })
2423
]),
25-
group: PropTypes.oneOfType([PropTypes.string, PropTypes.arrayOf(PropTypes.string)]),
26-
enabled: PropTypes.bool
27-
}
24+
group: PropTypes.oneOfType([
25+
PropTypes.string,
26+
PropTypes.arrayOf(PropTypes.string)
27+
]),
28+
enabled: PropTypes.bool,
29+
innerRef: PropTypes.oneOfType([// Either a function
30+
PropTypes.func,
31+
// Or the instance of a DOM native element (see the note about SSR)
32+
PropTypes.shape({ current: PropTypes.instanceOf(Element) })])
33+
};
2834

2935
static defaultProps = {
3036
group: 'default',
3137
enabled: true
32-
}
38+
};
3339

3440
constructor(props) {
3541
super(props)
36-
this.childRef = createRef()
42+
43+
this.childRef = props.innerRef ? props.innerRef : createRef()
3744
}
3845

3946
componentDidMount() {
@@ -62,7 +69,11 @@ export default class ScrollSyncPane extends Component {
6269
this.context.unregisterPane(this.node, this.toArray(prevProps.group))
6370
}
6471
}
65-
if (this.node && this.props.enabled && this.props.group !== prevProps.group) {
72+
if (
73+
this.node &&
74+
this.props.enabled &&
75+
this.props.group !== prevProps.group
76+
) {
6677
this.context.unregisterPane(this.node, this.toArray(prevProps.group))
6778
this.context.registerPane(this.node, this.toArray(this.props.group))
6879
}
@@ -74,20 +85,22 @@ export default class ScrollSyncPane extends Component {
7485
}
7586
}
7687

77-
toArray = groups => [].concat(groups)
88+
toArray = groups => [].concat(groups);
7889

7990
updateNode = () => {
8091
if (this.props.attachTo) {
8192
this.node = this.props.attachTo.current
8293
} else {
8394
this.node = this.childRef.current
8495
}
85-
}
96+
};
8697

8798
render() {
8899
if (this.props.attachTo) {
89100
return this.props.children
90101
}
91-
return cloneElement(Children.only(this.props.children), { ref: this.childRef })
102+
return cloneElement(Children.only(this.props.children), {
103+
ref: this.childRef
104+
})
92105
}
93106
}

0 commit comments

Comments
 (0)