Skip to content

Update react-sortable-tree to compatible with react-dnd v9 #507

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,17 +62,17 @@
"dependencies": {
"frontend-collective-react-dnd-scrollzone": "^1.0.2",
"lodash.isequal": "^4.5.0",
"prop-types": "^15.6.1",
"react-dnd": "^7.3.0",
"react-dnd-html5-backend": "^7.0.1",
"prop-types": "^15.7.2",
"react-dnd": "^9.0.0",
"react-dnd-html5-backend": "^9.0.0",
"react-lifecycles-compat": "^3.0.4",
"react-sortable-tree": "^2.6.0",
"react-virtualized": "^9.19.1"
},
"peerDependencies": {
"react": "^16.3.0",
"react-dnd": "^7.3.0",
"react-dom": "^16.3.0"
"react": "^16.8.6",
"react-dnd": "^9.0.0",
"react-dom": "^16.8.6"
},
"devDependencies": {
"@babel/cli": "^7.1.5",
Expand Down Expand Up @@ -104,11 +104,11 @@
"jest-enzyme": "^7.0.1",
"parcel-bundler": "^1.11.0",
"prettier": "^1.13.3",
"react": "^16.3.0",
"react": "^16.8.6",
"react-addons-shallow-compare": "^15.6.2",
"react-dnd-test-backend": "^7.0.1",
"react-dnd-touch-backend": "^0.6.0",
"react-dom": "^16.3.0",
"react-dnd-test-backend": "^9.0.0",
"react-dnd-touch-backend": "^9.0.0",
"react-dom": "^16.8.6",
"react-hot-loader": "^4.3.0",
"react-sortable-tree-theme-file-explorer": "^1.1.2",
"react-test-renderer": "^16.4.0",
Expand Down
19 changes: 8 additions & 11 deletions src/react-sortable-tree.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { Component } from 'react';
import React, { Component, useContext } from 'react';
import PropTypes from 'prop-types';
import { AutoSizer, List } from 'react-virtualized';
import isEqual from 'lodash.isequal';
Expand All @@ -7,7 +7,7 @@ import withScrolling, {
createVerticalStrength,
createHorizontalStrength,
} from 'frontend-collective-react-dnd-scrollzone';
import { DragDropContextConsumer } from 'react-dnd';
import { DndContext } from 'react-dnd';
import { polyfill } from 'react-lifecycles-compat';
import 'react-virtualized/styles.css';
import TreeNode from './tree-node';
Expand Down Expand Up @@ -935,15 +935,12 @@ ReactSortableTree.defaultProps = {

polyfill(ReactSortableTree);

const SortableTreeWithoutDndContext = props => (
<DragDropContextConsumer>
{({ dragDropManager }) =>
dragDropManager === undefined ? null : (
<ReactSortableTree {...props} dragDropManager={dragDropManager} />
)
}
</DragDropContextConsumer>
);
const SortableTreeWithoutDndContext = props => {
const { dragDropManager } = useContext(DndContext);
return (
<ReactSortableTree {...props} dragDropManager={dragDropManager} />
);
};

// Export the tree component without the react-dnd DragDropContext,
// for when component is used with other components using react-dnd.
Expand Down
27 changes: 10 additions & 17 deletions src/react-sortable-tree.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import PropTypes from 'prop-types';
import renderer from 'react-test-renderer';
import { mount } from 'enzyme';
import { List } from 'react-virtualized';
import { DragDropContext } from 'react-dnd';
import { DndProvider } from 'react-dnd';
import HTML5Backend from 'react-dnd-html5-backend';
import TouchBackend from 'react-dnd-touch-backend';
import SortableTree, {
Expand Down Expand Up @@ -401,35 +401,28 @@ describe('<SortableTree />', () => {
});

it('loads using SortableTreeWithoutDndContext', () => {
const HTML5Wrapped = DragDropContext(HTML5Backend)(
SortableTreeWithoutDndContext
);
const TouchWrapped = DragDropContext(TouchBackend)(
SortableTreeWithoutDndContext
);

expect(
mount(<HTML5Wrapped treeData={[{ title: 'a' }]} onChange={() => {}} />)
mount(<DndProvider backend={HTML5Backend}>
<SortableTreeWithoutDndContext treeData={[{ title: 'a' }]} onChange={() => {}} />
</DndProvider>)
).toBeDefined();
expect(
mount(<TouchWrapped treeData={[{ title: 'a' }]} onChange={() => {}} />)
mount(<DndProvider backend={TouchBackend}>
<TouchWrapped treeData={[{ title: 'a' }]} onChange={() => {}} />
</DndProvider>)
).toBeDefined();
});

it('loads using SortableTreeWithoutDndContext', () => {
const TestWrapped = DragDropContext(HTML5Backend)(
SortableTreeWithoutDndContext
);

const onDragStateChanged = jest.fn();
const treeData = [{ title: 'a' }, { title: 'b' }];
const wrapper = mount(
<TestWrapped
const wrapper = mount(<DndProvider backend={HTML5Backend}>
<SortableTreeWithoutDndContext
treeData={treeData}
onDragStateChanged={onDragStateChanged}
onChange={() => {}}
/>
);
</DndProvider>);

// Obtain a reference to the backend
const backend = wrapper
Expand Down
10 changes: 7 additions & 3 deletions src/utils/dnd-manager.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {
DragDropContext as dragDropContext,
DragSource as dragSource,
DropTarget as dropTarget,
DndProvider,
} from 'react-dnd';
import HTML5Backend from 'react-dnd-html5-backend';
import { findDOMNode } from 'react-dom';
Expand All @@ -13,8 +13,12 @@ export default class DndManager {
this.treeRef = treeRef;
}

static wrapRoot(el) {
return dragDropContext(HTML5Backend)(el);
static wrapRoot(Comp) {
return ((props) => (
<DndProvider backend={HTML5Backend}>
<Comp {...props} />
</DndProvider>
));
}

get startDrag() {
Expand Down
9 changes: 4 additions & 5 deletions stories/drag-out-to-remove.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-disable react/no-multi-comp */
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import { DragDropContext, DropTarget } from 'react-dnd';
import { DropTarget, DndProvider } from 'react-dnd';
import HTML5Backend from 'react-dnd-html5-backend';
import { SortableTreeWithoutDndContext as SortableTree } from '../src';
// In your own app, you would need to use import styles once in the app
Expand Down Expand Up @@ -54,7 +54,7 @@ const TrashAreaComponent = DropTarget(
trashAreaCollect
)(trashAreaBaseComponent);

class UnwrappedApp extends Component {
class App extends Component {
constructor(props) {
super(props);

Expand All @@ -69,7 +69,7 @@ class UnwrappedApp extends Component {
}

render() {
return (
return (<DndProvider backend={HTML5Backend}>
<div>
<TrashAreaComponent>
<div style={{ height: 250 }}>
Expand All @@ -81,9 +81,8 @@ class UnwrappedApp extends Component {
</div>
</TrashAreaComponent>
</div>
);
</DndProvider>);
}
}

const App = DragDropContext(HTML5Backend)(UnwrappedApp);
export default App;
9 changes: 4 additions & 5 deletions stories/external-node.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-disable react/no-multi-comp */
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import { DragDropContext, DragSource } from 'react-dnd';
import { DragSource, DndProvider } from 'react-dnd';
import HTML5Backend from 'react-dnd-html5-backend';
import { SortableTreeWithoutDndContext as SortableTree } from '../src';
// In your own app, you would need to use import styles once in the app
Expand Down Expand Up @@ -55,7 +55,7 @@ const YourExternalNodeComponent = DragSource(
externalNodeCollect
)(externalNodeBaseComponent);

class UnwrappedApp extends Component {
class App extends Component {
constructor(props) {
super(props);

Expand All @@ -65,7 +65,7 @@ class UnwrappedApp extends Component {
}

render() {
return (
return (<DndProvider backend={HTML5Backend}>
<div>
<div style={{ height: 300 }}>
<SortableTree
Expand All @@ -76,9 +76,8 @@ class UnwrappedApp extends Component {
</div>
<YourExternalNodeComponent node={{ title: 'Baby Rabbit' }} />← drag this
</div>
);
</DndProvider>);
}
}

const App = DragDropContext(HTML5Backend)(UnwrappedApp);
export default App;
11 changes: 4 additions & 7 deletions stories/touch-support.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-disable import/no-extraneous-dependencies */
import React, { Component } from 'react';
import { DragDropContext } from 'react-dnd';
import { DndProvider } from 'react-dnd';
import HTML5Backend from 'react-dnd-html5-backend';
import TouchBackend from 'react-dnd-touch-backend';
import { SortableTreeWithoutDndContext as SortableTree } from '../src';
Expand All @@ -10,7 +10,7 @@ import { SortableTreeWithoutDndContext as SortableTree } from '../src';
// https://stackoverflow.com/a/4819886/1601953
const isTouchDevice = !!('ontouchstart' in window || navigator.maxTouchPoints);

class UnwrappedApp extends Component {
class App extends Component {
constructor(props) {
super(props);

Expand All @@ -22,7 +22,7 @@ class UnwrappedApp extends Component {
}

render() {
return (
return (<DndProvider backend={isTouchDevice ? TouchBackend : HTML5Backend}>
<div>
<span>
This is {!isTouchDevice && 'not '}a touch-supporting browser
Expand All @@ -35,11 +35,8 @@ class UnwrappedApp extends Component {
/>
</div>
</div>
);
</DndProvider>);
}
}

const App = DragDropContext(isTouchDevice ? TouchBackend : HTML5Backend)(
UnwrappedApp
);
export default App;