Skip to content

Make components stateful for compatibility with react@16 / react-dnd. #73

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

Merged
merged 1 commit into from
Mar 23, 2017
Merged
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
244 changes: 124 additions & 120 deletions src/node-renderer-default.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { PropTypes } from 'react';
import React, { Component, PropTypes } from 'react';
import { getIEVersion } from './utils/browser-utils';
import baseStyles from './node-renderer-default.scss';
import { isDescendant } from './utils/tree-data-utils';
Expand All @@ -15,139 +15,143 @@ if (getIEVersion < 10) {
};
}

const NodeRendererDefault = ({
scaffoldBlockPxWidth,
toggleChildrenVisibility,
connectDragPreview,
connectDragSource,
isDragging,
canDrop,
node,
draggedNode,
path,
treeIndex,
isSearchMatch,
isSearchFocus,
buttons,
className,
style = {},
didDrop,
isOver: _isOver, // Not needed, but preserved for other renderers
parentNode: _parentNode, // Needed for drag-and-drop utils
endDrag: _endDrag, // Needed for drag-and-drop utils
startDrag: _startDrag, // Needed for drag-and-drop utils
...otherProps,
}) => {
let handle;
if (typeof node.children === 'function' && node.expanded) {
// Show a loading symbol on the handle when the children are expanded
// and yet still defined by a function (a callback to fetch the children)
handle = (
<div className={styles.loadingHandle}>
<div className={styles.loadingCircle}>
<div className={styles.loadingCirclePoint} />
<div className={styles.loadingCirclePoint} />
<div className={styles.loadingCirclePoint} />
<div className={styles.loadingCirclePoint} />
<div className={styles.loadingCirclePoint} />
<div className={styles.loadingCirclePoint} />
<div className={styles.loadingCirclePoint} />
<div className={styles.loadingCirclePoint} />
<div className={styles.loadingCirclePoint} />
<div className={styles.loadingCirclePoint} />
<div className={styles.loadingCirclePoint} />
<div className={styles.loadingCirclePoint} />
</div>
</div>
);
} else {
// Show the handle used to initiate a drag-and-drop
handle = connectDragSource((
<div className={styles.moveHandle} />
), { dropEffect: 'copy' });
}
class NodeRendererDefault extends Component {
render() {
const {
scaffoldBlockPxWidth,
toggleChildrenVisibility,
connectDragPreview,
connectDragSource,
isDragging,
canDrop,
node,
draggedNode,
path,
treeIndex,
isSearchMatch,
isSearchFocus,
buttons,
className,
style = {},
didDrop,
isOver: _isOver, // Not needed, but preserved for other renderers
parentNode: _parentNode, // Needed for drag-and-drop utils
endDrag: _endDrag, // Needed for drag-and-drop utils
startDrag: _startDrag, // Needed for drag-and-drop utils
...otherProps,
} = this.props;

const isDraggedDescendant = draggedNode && isDescendant(draggedNode, node);
const isLandingPadActive = !didDrop && isDragging;
let handle;
if (typeof node.children === 'function' && node.expanded) {
// Show a loading symbol on the handle when the children are expanded
// and yet still defined by a function (a callback to fetch the children)
handle = (
<div className={styles.loadingHandle}>
<div className={styles.loadingCircle}>
<div className={styles.loadingCirclePoint} />
<div className={styles.loadingCirclePoint} />
<div className={styles.loadingCirclePoint} />
<div className={styles.loadingCirclePoint} />
<div className={styles.loadingCirclePoint} />
<div className={styles.loadingCirclePoint} />
<div className={styles.loadingCirclePoint} />
<div className={styles.loadingCirclePoint} />
<div className={styles.loadingCirclePoint} />
<div className={styles.loadingCirclePoint} />
<div className={styles.loadingCirclePoint} />
<div className={styles.loadingCirclePoint} />
</div>
</div>
);
} else {
// Show the handle used to initiate a drag-and-drop
handle = connectDragSource((
<div className={styles.moveHandle} />
), { dropEffect: 'copy' });
}

return (
<div
style={{ height: '100%' }}
{...otherProps}
>
{toggleChildrenVisibility && node.children && node.children.length > 0 && (
<div>
<button
aria-label={node.expanded ? 'Collapse' : 'Expand'}
className={node.expanded ? styles.collapseButton : styles.expandButton}
style={{ left: -0.5 * scaffoldBlockPxWidth }}
onClick={() => toggleChildrenVisibility({node, path, treeIndex})}
/>
const isDraggedDescendant = draggedNode && isDescendant(draggedNode, node);
const isLandingPadActive = !didDrop && isDragging;

{node.expanded && !isDragging &&
<div
style={{ width: scaffoldBlockPxWidth }}
className={styles.lineChildren}
return (
<div
style={{ height: '100%' }}
{...otherProps}
>
{toggleChildrenVisibility && node.children && node.children.length > 0 && (
<div>
<button
aria-label={node.expanded ? 'Collapse' : 'Expand'}
className={node.expanded ? styles.collapseButton : styles.expandButton}
style={{ left: -0.5 * scaffoldBlockPxWidth }}
onClick={() => toggleChildrenVisibility({node, path, treeIndex})}
/>
}
</div>
)}

<div className={styles.rowWrapper}>
{/* Set the row preview to be used during drag and drop */}
{connectDragPreview(
<div
className={styles.row +
(isLandingPadActive ? ` ${styles.rowLandingPad}` : '') +
(isLandingPadActive && !canDrop ? ` ${styles.rowCancelPad}` : '') +
(isSearchMatch ? ` ${styles.rowSearchMatch}` : '') +
(isSearchFocus ? ` ${styles.rowSearchFocus}` : '') +
(className ? ` ${className}` : '')
{node.expanded && !isDragging &&
<div
style={{ width: scaffoldBlockPxWidth }}
className={styles.lineChildren}
/>
}
style={{
opacity: isDraggedDescendant ? 0.5 : 1,
...style,
}}
>
{handle}
</div>
)}

<div className={styles.rowContents}>
<div className={styles.rowLabel}>
<span
className={styles.rowTitle +
(node.subtitle ? ` ${styles.rowTitleWithSubtitle}` : '')
}
>
{typeof node.title === 'function' ?
node.title({node, path, treeIndex }) :
node.title
}
</span>
<div className={styles.rowWrapper}>
{/* Set the row preview to be used during drag and drop */}
{connectDragPreview(
<div
className={styles.row +
(isLandingPadActive ? ` ${styles.rowLandingPad}` : '') +
(isLandingPadActive && !canDrop ? ` ${styles.rowCancelPad}` : '') +
(isSearchMatch ? ` ${styles.rowSearchMatch}` : '') +
(isSearchFocus ? ` ${styles.rowSearchFocus}` : '') +
(className ? ` ${className}` : '')
}
style={{
opacity: isDraggedDescendant ? 0.5 : 1,
...style,
}}
>
{handle}

{node.subtitle &&
<span className={styles.rowSubtitle}>
{typeof node.subtitle === 'function' ?
node.subtitle({node, path, treeIndex }) :
node.subtitle
<div className={styles.rowContents}>
<div className={styles.rowLabel}>
<span
className={styles.rowTitle +
(node.subtitle ? ` ${styles.rowTitleWithSubtitle}` : '')
}
>
{typeof node.title === 'function' ?
node.title({node, path, treeIndex }) :
node.title
}
</span>
}
</div>

<div className={styles.rowToolbar}>
{buttons && buttons.map((btn, index) => (
<div key={index} className={styles.toolbarButton}>
{btn}
</div>
))}
{node.subtitle &&
<span className={styles.rowSubtitle}>
{typeof node.subtitle === 'function' ?
node.subtitle({node, path, treeIndex }) :
node.subtitle
}
</span>
}
</div>

<div className={styles.rowToolbar}>
{buttons && buttons.map((btn, index) => (
<div key={index} className={styles.toolbarButton}>
{btn}
</div>
))}
</div>
</div>
</div>
</div>
)}
)}
</div>
</div>
</div>
);
};
);
}
}

NodeRendererDefault.propTypes = {
node: PropTypes.object.isRequired,
Expand Down
Loading