Skip to content

Commit 0696478

Browse files
committed
Fix forwardRef problem where TrackElements aren't draggable
On drag, I was seeing this error from react-grid-layout: * react-grid-layout/react-grid-layout#1732 * react-grid-layout/react-draggable#670 It was still present when I tried downgrading react-draggable to an older version. It turns out the issue was in how the ref for the TrackElement was being defined. The track elements are now draggable again.
1 parent 9d965b3 commit 0696478

File tree

2 files changed

+8
-4
lines changed

2 files changed

+8
-4
lines changed

src/Track.jsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ const TrackElementRef = React.forwardRef(
3636
style={style}
3737
className={className}
3838
key={key}
39-
ref={ref}
39+
myRef={ref}
4040
{...restOfProps}>
4141
{children}
4242
</TrackElement>;

src/TrackElement.jsx

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,11 @@ export default class TrackElement extends React.Component {
6868
}
6969
renderTxtThumb(data) {
7070
let maxLen = 10;
71-
if (this.el && this.el.clientWidth) {
72-
maxLen = Math.round(this.el.clientWidth / 15);
71+
if (
72+
this.myRef && this.myRef.current &&
73+
this.myRef.current.clientWidth
74+
) {
75+
maxLen = Math.round(this.myRef.current.clientWidth / 15);
7376
}
7477
return <p className="jux-media-item-middle">
7578
{ellipsis(data.source, maxLen)}
@@ -111,7 +114,7 @@ export default class TrackElement extends React.Component {
111114
cls += ' jux-item-active';
112115
}
113116
return <div data={this.props.data}
114-
ref={(el) => this.el = el}
117+
ref={this.props.myRef}
115118
data-dragging={this.props.dragging}
116119
className={cls}
117120
style={this.props.style}
@@ -130,6 +133,7 @@ export default class TrackElement extends React.Component {
130133
TrackElement.propTypes = {
131134
className: PropTypes.string,
132135
data: PropTypes.object.isRequired,
136+
myRef: PropTypes.object,
133137
dragging: PropTypes.bool,
134138
isActive: PropTypes.bool.isRequired,
135139
style: PropTypes.object,

0 commit comments

Comments
 (0)