Skip to content

Commit

Permalink
Don't allow to call connect* methods on the composite component elements
Browse files Browse the repository at this point in the history
  • Loading branch information
gaearon committed Oct 14, 2015
1 parent f37dc94 commit 96d4c30
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 6 deletions.
19 changes: 16 additions & 3 deletions src/bindConnectorMethod.js
@@ -1,7 +1,7 @@
import shallowEqual from './utils/shallowEqual';
import cloneWithRef from './utils/cloneWithRef';
import { Disposable, SerialDisposable } from 'disposables';
import { findDOMNode, isValidElement } from 'react';
import { isValidElement } from 'react';

export default function bindConnectorMethod(handlerId, connect) {
const disposable = new SerialDisposable();
Expand All @@ -14,12 +14,25 @@ export default function bindConnectorMethod(handlerId, connect) {
// This helps us achieve a neat API where user doesn't even know that refs
// are being used under the hood.
if (isValidElement(nextWhatever)) {
// Custom components can no longer be wrapped directly in React DnD 2.0
// so that we don't need to depend on findDOMNode() from react-dom.
if (typeof nextWhatever.type !== 'string') {
const displayName = nextWhatever.type.displayName ||
nextWhatever.type.name ||
'the component';
throw new Error(
`Only native element nodes can now be passed to ${connect.name}(). ` +
`You can either wrap ${displayName} into a <div>, or turn it into a ` +
`drag source or a drop target itself.`
);
}

const nextElement = nextWhatever;
return cloneWithRef(nextElement, inst => ref(inst, nextOptions));
}

// At this point we can only receive components or DOM nodes.
const nextNode = findDOMNode(nextWhatever);
// At this point we can only receive DOM nodes.
const nextNode = nextWhatever;

// If nothing changed, bail out of re-connecting the node to the backend.
if (nextNode === currentNode && shallowEqual(currentOptions, nextOptions)) {
Expand Down
8 changes: 6 additions & 2 deletions src/createSourceConnector.js
@@ -1,6 +1,10 @@
export default function createSourceConnector(backend) {
return {
dragSource: backend.connectDragSource.bind(backend),
dragPreview: backend.connectDragPreview.bind(backend)
dragSource: function connectDragSource(...args) {
return backend.connectDragSource(...args);
},
dragPreview: function connectDragPreview(...args) {
return backend.connectDragPreview(...args);
}
};
}
4 changes: 3 additions & 1 deletion src/createTargetConnector.js
@@ -1,5 +1,7 @@
export default function createTargetConnector(backend) {
return {
dropTarget: backend.connectDropTarget.bind(backend)
dropTarget: function connectDropTarget(...args) {
return backend.connectDropTarget(...args);
}
};
}

0 comments on commit 96d4c30

Please sign in to comment.