Skip to content

Childless Nodes #402

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 5 commits into from
Oct 7, 2018
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ export default class Tree extends Component {
| rowDirection | string | Adds row direction support if set to `'rtl'` Defaults to `'ltr'`. |
| canDrag | func or bool | Return false from callback to prevent node from dragging, by hiding the drag handle. Set prop to `false` to disable dragging on all nodes. Defaults to `true`. <div>`({ node: object, path: number[] or string[], treeIndex: number, lowerSiblingCounts: number[], isSearchMatch: bool, isSearchFocus: bool }): bool`</div> |
| canDrop | func | Return false to prevent node from dropping in the given location. <div>`({ node: object, prevPath: number[] or string[], prevParent: object, prevTreeIndex: number, nextPath: number[] or string[], nextParent: object, nextTreeIndex: number }): bool`</div> |
| canNodeHaveChildren | func | Function to determine whether a node can have children, useful for preventing hover preview when you have a `canDrop` condition. Default is set to a function that returns `true`. Functions should be of type `(node): bool`.
| theme | object | Set an all-in-one packaged appearance for the tree. See the [Themes](#themes) section for more information. |
| searchMethod | func | The method used to search nodes. Defaults to [`defaultSearchMethod`](https://github.com/frontend-collective/react-sortable-tree/blob/master/src/utils/default-handlers.js), which uses the `searchQuery` string to search for nodes with matching `title` or `subtitle` values. NOTE: Changing `searchMethod` will not update the search, but changing the `searchQuery` will.<div>`({ node: object, path: number[] or string[], treeIndex: number, searchQuery: any }): bool`</div> |
| searchQuery | string or any | Used by the `searchMethod` to highlight and scroll to matched nodes. Should be a string for the default `searchMethod`, but can be anything when using a custom search. Defaults to `null`. |
Expand Down
12 changes: 12 additions & 0 deletions src/react-sortable-tree.js
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,14 @@ class ReactSortableTree extends Component {
this.moveNode(dropResult);
}

canNodeHaveChildren(node) {
const { canNodeHaveChildren } = this.props;
if (canNodeHaveChildren) {
return canNodeHaveChildren(node);
}
return true;
}

// Load any children in the tree that are given by a function
// calls the onChange callback on the new treeData
static loadLazyChildren(props, state) {
Expand Down Expand Up @@ -859,6 +867,9 @@ ReactSortableTree.propTypes = {
// Determine whether a node can be dropped based on its path and parents'.
canDrop: PropTypes.func,

// Determine whether a node can have children
canNodeHaveChildren: PropTypes.func,

// When true, or a callback returning true, dropping nodes to react-dnd
// drop targets outside of this tree will not remove them from this tree
shouldCopyOnOutsideDrop: PropTypes.oneOfType([
Expand All @@ -884,6 +895,7 @@ ReactSortableTree.propTypes = {
ReactSortableTree.defaultProps = {
canDrag: true,
canDrop: null,
canNodeHaveChildren: () => true,
className: '',
dndType: null,
generateNodeProps: null,
Expand Down
8 changes: 7 additions & 1 deletion src/utils/dnd-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,15 @@ export default class DndManager {

const rowAbove = dropTargetProps.getPrevRow();
if (rowAbove) {
let { path } = rowAbove;
const aboveNodeCannotHaveChildren = !this.treeRef.canNodeHaveChildren(rowAbove.node);
if (aboveNodeCannotHaveChildren) {
path = path.slice(0, path.length - 1);
}

// Limit the length of the path to the deepest possible
dropTargetDepth = Math.min(
rowAbove.path.length,
path.length,
dropTargetProps.path.length
);
}
Expand Down
Loading