@nosferatu500/react-sortable-tree
Advanced tools
Comparing version 6.0.0-alpha.4 to 6.0.0-alpha.5
@@ -1332,3 +1332,3 @@ import { jsx, jsxs } from 'react/jsx-runtime'; | ||
}; | ||
this.listRef = React.createRef(); | ||
this.listRef = props.virtuosoRef || React.createRef(); | ||
const { dndType, nodeContentRenderer, treeNodeRenderer, slideRegionSize } = mergeTheme(props); | ||
@@ -1335,0 +1335,0 @@ this.treeId = `rst__${treeIdCounter}`; |
@@ -1,2 +0,3 @@ | ||
/// <reference types="react" /> | ||
import React from "react"; | ||
import { VirtuosoHandle } from "react-virtuoso"; | ||
import "./react-sortable-tree.css"; | ||
@@ -62,2 +63,3 @@ declare type SearchParams = { | ||
className?: string; | ||
virtuosoRef?: React.Ref<VirtuosoHandle>; | ||
innerStyle?: any; | ||
@@ -64,0 +66,0 @@ slideRegionSize?: number; |
321
index.js
@@ -22,3 +22,2 @@ 'use strict'; | ||
}) => treeIndex; | ||
const getReactElementText = parent => { | ||
@@ -28,14 +27,10 @@ if (typeof parent === "string") { | ||
} | ||
if (parent === undefined || typeof parent !== "object" || !parent.props || !parent.props.children || typeof parent.props.children !== "string" && typeof parent.props.children !== "object") { | ||
return ""; | ||
} | ||
if (typeof parent.props.children === "string") { | ||
return parent.props.children; | ||
} | ||
return parent.props.children.map(child => getReactElementText(child)).join(""); | ||
}; | ||
const stringSearch = (key, searchQuery, node, path, treeIndex) => { | ||
@@ -49,10 +44,7 @@ if (typeof node[key] === "function") { | ||
} | ||
if (typeof node[key] === "object") { | ||
return getReactElementText(node[key]).includes(searchQuery); | ||
} | ||
return node[key] && String(node[key]).includes(searchQuery); | ||
}; | ||
const defaultSearchMethod = ({ | ||
@@ -81,3 +73,2 @@ node, | ||
})] : []; | ||
if (currentIndex === targetIndex) { | ||
@@ -90,3 +81,2 @@ return { | ||
} | ||
if (!node.children || ignoreCollapsed && node.expanded !== true) { | ||
@@ -97,6 +87,4 @@ return { | ||
} | ||
let childIndex = currentIndex + 1; | ||
const childCount = node.children.length; | ||
for (let i = 0; i < childCount; i += 1) { | ||
@@ -112,10 +100,7 @@ const result = getNodeDataAtTreeIndexOrNextIndex({ | ||
}); | ||
if (result.node) { | ||
return result; | ||
} | ||
childIndex = result.nextIndex; | ||
} | ||
return { | ||
@@ -125,3 +110,2 @@ nextIndex: childIndex | ||
}; | ||
const getDescendantCount = ({ | ||
@@ -139,3 +123,2 @@ node, | ||
}; | ||
const walkDescendants = ({ | ||
@@ -163,6 +146,4 @@ callback, | ||
}; | ||
if (!isPseudoRoot) { | ||
const callbackResult = callback(selfInfo); | ||
if (callbackResult === false) { | ||
@@ -172,10 +153,7 @@ return false; | ||
} | ||
if (!node.children || node.expanded !== true && ignoreCollapsed && !isPseudoRoot) { | ||
return currentIndex; | ||
} | ||
let childIndex = currentIndex; | ||
const childCount = node.children.length; | ||
if (typeof node.children !== "function") { | ||
@@ -193,3 +171,2 @@ for (let i = 0; i < childCount; i += 1) { | ||
}); | ||
if (childIndex === false) { | ||
@@ -200,6 +177,4 @@ return false; | ||
} | ||
return childIndex; | ||
}; | ||
const mapDescendants = ({ | ||
@@ -216,3 +191,4 @@ callback, | ||
}) => { | ||
const nextNode = { ...node | ||
const nextNode = { | ||
...node | ||
}; | ||
@@ -230,3 +206,2 @@ const selfPath = isPseudoRoot ? [] : [...path, getNodeKey({ | ||
}; | ||
if (!nextNode.children || nextNode.expanded !== true && ignoreCollapsed && !isPseudoRoot) { | ||
@@ -238,6 +213,4 @@ return { | ||
} | ||
let childIndex = currentIndex; | ||
const childCount = nextNode.children.length; | ||
if (typeof nextNode.children !== "function") { | ||
@@ -259,3 +232,2 @@ nextNode.children = nextNode.children.map((child, i) => { | ||
} | ||
return { | ||
@@ -266,3 +238,2 @@ node: callback(selfInfo), | ||
}; | ||
const getVisibleNodeCount = ({ | ||
@@ -275,6 +246,4 @@ treeData | ||
} | ||
return 1 + node.children.reduce((total, currentNode) => total + traverse(currentNode), 0); | ||
}; | ||
return treeData.reduce((total, currentNode) => total + traverse(currentNode), 0); | ||
@@ -290,3 +259,2 @@ }; | ||
} | ||
const result = getNodeDataAtTreeIndexOrNextIndex({ | ||
@@ -304,7 +272,5 @@ targetIndex, | ||
}); | ||
if (result.node) { | ||
return result; | ||
} | ||
return undefined; | ||
@@ -321,3 +287,2 @@ }; | ||
} | ||
walkDescendants({ | ||
@@ -345,3 +310,2 @@ callback, | ||
} | ||
return mapDescendants({ | ||
@@ -368,3 +332,4 @@ callback, | ||
node | ||
}) => ({ ...node, | ||
}) => ({ | ||
...node, | ||
expanded | ||
@@ -386,3 +351,2 @@ }), | ||
const RESULT_MISS = "RESULT_MISS"; | ||
const traverse = ({ | ||
@@ -400,3 +364,2 @@ isPseudoRoot = false, | ||
} | ||
if (pathIndex >= path.length - 1) { | ||
@@ -408,9 +371,6 @@ return typeof newNode === "function" ? newNode({ | ||
} | ||
if (!node.children) { | ||
throw new Error("Path referenced children of node with no children."); | ||
} | ||
let nextTreeIndex = currentTreeIndex + 1; | ||
for (let i = 0; i < node.children.length; i += 1) { | ||
@@ -422,15 +382,14 @@ const result = traverse({ | ||
}); | ||
if (result !== RESULT_MISS) { | ||
if (result) { | ||
return { ...node, | ||
return { | ||
...node, | ||
children: [...node.children.slice(0, i), result, ...node.children.slice(i + 1)] | ||
}; | ||
} | ||
return { ...node, | ||
return { | ||
...node, | ||
children: [...node.children.slice(0, i), ...node.children.slice(i + 1)] | ||
}; | ||
} | ||
nextTreeIndex += 1 + getDescendantCount({ | ||
@@ -441,6 +400,4 @@ node: node.children[i], | ||
} | ||
return RESULT_MISS; | ||
}; | ||
const result = traverse({ | ||
@@ -454,7 +411,5 @@ node: { | ||
}); | ||
if (result === RESULT_MISS) { | ||
throw new Error("No node found at the given path."); | ||
} | ||
return result.children; | ||
@@ -476,2 +431,3 @@ }; | ||
}; | ||
const removeNode = ({ | ||
@@ -512,3 +468,2 @@ treeData, | ||
let foundNodeInfo; | ||
try { | ||
@@ -531,4 +486,4 @@ changeNodeAtPath({ | ||
}); | ||
} catch {} | ||
} catch { | ||
} | ||
return foundNodeInfo; | ||
@@ -554,3 +509,2 @@ }; | ||
} | ||
let insertedTreeIndex; | ||
@@ -568,28 +522,23 @@ let hasBeenAdded = false; | ||
const key = path ? path[path.length - 1] : undefined; | ||
if (hasBeenAdded || key !== parentKey) { | ||
return node; | ||
} | ||
hasBeenAdded = true; | ||
const parentNode = { ...node | ||
const parentNode = { | ||
...node | ||
}; | ||
if (expandParent) { | ||
parentNode.expanded = true; | ||
} | ||
if (!parentNode.children) { | ||
insertedTreeIndex = treeIndex + 1; | ||
return { ...parentNode, | ||
return { | ||
...parentNode, | ||
children: [newNode] | ||
}; | ||
} | ||
if (typeof parentNode.children === "function") { | ||
throw new TypeError("Cannot add to children defined by a function"); | ||
} | ||
let nextTreeIndex = treeIndex + 1; | ||
for (let i = 0; i < parentNode.children.length; i += 1) { | ||
@@ -601,6 +550,6 @@ nextTreeIndex += 1 + getDescendantCount({ | ||
} | ||
insertedTreeIndex = nextTreeIndex; | ||
const children = addAsFirstChild ? [newNode, ...parentNode.children] : [...parentNode.children, newNode]; | ||
return { ...parentNode, | ||
return { | ||
...parentNode, | ||
children | ||
@@ -610,7 +559,5 @@ }; | ||
}); | ||
if (!hasBeenAdded) { | ||
throw new Error("No node found with the given key."); | ||
} | ||
return { | ||
@@ -621,3 +568,2 @@ treeData: changedTreeData, | ||
}; | ||
const addNodeAtDepthAndIndex = ({ | ||
@@ -641,3 +587,2 @@ targetDepth, | ||
})]; | ||
if (currentIndex >= minimumTreeIndex - 1 || isLastChild && !(node.children && node.children.length > 0)) { | ||
@@ -650,3 +595,4 @@ if (typeof node.children === "function") { | ||
} : {}; | ||
const nextNode = { ...node, | ||
const nextNode = { | ||
...node, | ||
...extraNodeProps, | ||
@@ -664,3 +610,2 @@ children: node.children ? [newNode, ...node.children] : [newNode] | ||
} | ||
if (currentDepth >= targetDepth - 1) { | ||
@@ -673,7 +618,5 @@ if (!node.children || typeof node.children === "function" || node.expanded !== true && ignoreCollapsed && !isPseudoRoot) { | ||
} | ||
let childIndex = currentIndex + 1; | ||
let insertedTreeIndex; | ||
let insertIndex; | ||
for (let i = 0; i < node.children.length; i += 1) { | ||
@@ -685,3 +628,2 @@ if (childIndex >= minimumTreeIndex) { | ||
} | ||
childIndex += 1 + getDescendantCount({ | ||
@@ -692,3 +634,2 @@ node: node.children[i], | ||
} | ||
if (insertIndex === null || insertIndex === undefined) { | ||
@@ -701,8 +642,7 @@ if (childIndex < minimumTreeIndex && !isLastChild) { | ||
} | ||
insertedTreeIndex = childIndex; | ||
insertIndex = node.children.length; | ||
} | ||
const nextNode = { ...node, | ||
const nextNode = { | ||
...node, | ||
children: [...node.children.slice(0, insertIndex), newNode, ...node.children.slice(insertIndex)] | ||
@@ -718,3 +658,2 @@ }; | ||
} | ||
if (!node.children || typeof node.children === "function" || node.expanded !== true && ignoreCollapsed && !isPseudoRoot) { | ||
@@ -726,3 +665,2 @@ return { | ||
} | ||
let insertedTreeIndex; | ||
@@ -733,3 +671,2 @@ let pathFragment; | ||
let newChildren = node.children; | ||
if (typeof newChildren !== "function") { | ||
@@ -740,3 +677,2 @@ newChildren = newChildren.map((child, i) => { | ||
} | ||
const mapResult = addNodeAtDepthAndIndex({ | ||
@@ -763,3 +699,2 @@ targetDepth, | ||
} | ||
childIndex = mapResult.nextIndex; | ||
@@ -769,4 +704,4 @@ return mapResult.node; | ||
} | ||
const nextNode = { ...node, | ||
const nextNode = { | ||
...node, | ||
children: newChildren | ||
@@ -778,3 +713,2 @@ }; | ||
}; | ||
if (insertedTreeIndex !== null && insertedTreeIndex !== undefined) { | ||
@@ -785,6 +719,4 @@ result.insertedTreeIndex = insertedTreeIndex; | ||
} | ||
return result; | ||
}; | ||
const insertNode = ({ | ||
@@ -810,3 +742,2 @@ treeData, | ||
} | ||
const insertResult = addNodeAtDepthAndIndex({ | ||
@@ -827,7 +758,5 @@ targetDepth, | ||
}); | ||
if (!("insertedTreeIndex" in insertResult)) { | ||
throw new Error("No suitable position found to insert."); | ||
} | ||
const treeIndex = insertResult.insertedTreeIndex; | ||
@@ -852,3 +781,2 @@ return { | ||
} | ||
const flattened = []; | ||
@@ -874,8 +802,5 @@ walk({ | ||
} | ||
const childrenToParents = {}; | ||
for (const child of flatData) { | ||
const parentKey = getParentKey(child); | ||
if (parentKey in childrenToParents) { | ||
@@ -887,20 +812,17 @@ childrenToParents[parentKey].push(child); | ||
} | ||
if (!(rootKey in childrenToParents)) { | ||
return []; | ||
} | ||
const trav = parent => { | ||
const parentKey = getKey(parent); | ||
if (parentKey in childrenToParents) { | ||
return { ...parent, | ||
return { | ||
...parent, | ||
children: childrenToParents[parentKey].map(child => trav(child)) | ||
}; | ||
} | ||
return { ...parent | ||
return { | ||
...parent | ||
}; | ||
}; | ||
return childrenToParents[rootKey].map(child => trav(child)); | ||
@@ -915,7 +837,5 @@ }; | ||
} | ||
if (typeof node.children === "function") { | ||
return depth + 1; | ||
} | ||
return node.children.reduce((deepest, child) => Math.max(deepest, getDepth(child, depth + 1)), depth); | ||
@@ -933,3 +853,2 @@ }; | ||
let matchCount = 0; | ||
const trav = ({ | ||
@@ -953,4 +872,4 @@ isPseudoRoot = false, | ||
const hasChildren = node.children && typeof node.children !== "function" && node.children.length > 0; | ||
if (!isPseudoRoot && searchMethod({ ...extraInfo, | ||
if (!isPseudoRoot && searchMethod({ | ||
...extraInfo, | ||
node, | ||
@@ -962,11 +881,9 @@ searchQuery | ||
} | ||
matchCount += 1; | ||
isSelfMatch = true; | ||
} | ||
let childIndex = currentIndex; | ||
const newNode = { ...node | ||
const newNode = { | ||
...node | ||
}; | ||
if (hasChildren) { | ||
@@ -979,3 +896,2 @@ newNode.children = newNode.children.map(child => { | ||
}); | ||
if (mapResult.node.expanded) { | ||
@@ -986,10 +902,7 @@ childIndex = mapResult.treeIndex; | ||
} | ||
if (mapResult.matches.length > 0 || mapResult.hasFocusMatch) { | ||
matches = [...matches, ...mapResult.matches]; | ||
if (mapResult.hasFocusMatch) { | ||
hasFocusMatch = true; | ||
} | ||
if (expandAllMatchPaths && mapResult.matches.length > 0 || (expandAllMatchPaths || expandFocusMatchPaths) && mapResult.hasFocusMatch) { | ||
@@ -999,19 +912,17 @@ newNode.expanded = true; | ||
} | ||
return mapResult.node; | ||
}); | ||
} | ||
if (!isPseudoRoot && !newNode.expanded) { | ||
matches = matches.map(match => ({ ...match, | ||
matches = matches.map(match => ({ | ||
...match, | ||
treeIndex: undefined | ||
})); | ||
} | ||
if (isSelfMatch) { | ||
matches = [{ ...extraInfo, | ||
matches = [{ | ||
...extraInfo, | ||
node: newNode | ||
}, ...matches]; | ||
} | ||
return { | ||
@@ -1024,3 +935,2 @@ node: matches.length > 0 ? newNode : node, | ||
}; | ||
const result = trav({ | ||
@@ -1053,5 +963,5 @@ node: { | ||
}; | ||
const NodeRendererDefault = function (props) { | ||
props = { ...defaultProps$3, | ||
props = { | ||
...defaultProps$3, | ||
...props | ||
@@ -1084,3 +994,2 @@ }; | ||
let handle; | ||
if (canDrag) { | ||
@@ -1103,3 +1012,2 @@ handle = typeof node.children === "function" && node.expanded ? jsxRuntime.jsx("div", { | ||
} | ||
const isDraggedDescendant = draggedNode && isDescendant(draggedNode, node); | ||
@@ -1164,5 +1072,5 @@ const isLandingPadActive = !didDrop && isDragging; | ||
}; | ||
const PlaceholderRendererDefault = function (props) { | ||
props = { ...defaultProps$2, | ||
props = { | ||
...defaultProps$2, | ||
...props | ||
@@ -1186,6 +1094,6 @@ }; | ||
}; | ||
class TreeNodeComponent extends React.Component { | ||
render() { | ||
const props = { ...defaultProps$1, | ||
const props = { | ||
...defaultProps$1, | ||
...this.props | ||
@@ -1216,6 +1124,4 @@ }; | ||
const scaffold = []; | ||
for (const [i, lowerSiblingCount] of lowerSiblingCounts.entries()) { | ||
let lineClass = ""; | ||
if (lowerSiblingCount > 0) { | ||
@@ -1234,3 +1140,2 @@ if (listIndex === 0) { | ||
} | ||
scaffold.push(jsxRuntime.jsx("div", { | ||
@@ -1242,6 +1147,4 @@ style: { | ||
}, `pre_${1 + i}`)); | ||
if (treeIndex !== listIndex && i === swapDepth) { | ||
let highlightLineClass = ""; | ||
if (listIndex === swapFrom + swapLength - 1) { | ||
@@ -1254,3 +1157,2 @@ highlightLineClass = "rst__highlightBottomLeftCorner"; | ||
} | ||
const style = { | ||
@@ -1266,3 +1168,2 @@ width: scaffoldBlockPxWidth, | ||
} | ||
const style = { | ||
@@ -1272,8 +1173,7 @@ left: scaffoldBlockPxWidth * scaffoldBlockCount | ||
let calculatedRowHeight = rowHeight; | ||
if (typeof rowHeight === "function") { | ||
calculatedRowHeight = rowHeight(treeIndex, _node, _path); | ||
} | ||
return connectDropTarget(jsxRuntime.jsxs("div", { ...otherProps, | ||
return connectDropTarget(jsxRuntime.jsxs("div", { | ||
...otherProps, | ||
style: { | ||
@@ -1295,3 +1195,2 @@ height: `${calculatedRowHeight}px` | ||
} | ||
} | ||
@@ -1303,5 +1202,5 @@ | ||
}; | ||
const TreePlaceholder = props => { | ||
props = { ...defaultProps, | ||
props = { | ||
...defaultProps, | ||
...props | ||
@@ -1317,3 +1216,4 @@ }; | ||
return connectDropTarget(jsxRuntime.jsx("div", { | ||
children: React.Children.map(children, child => React.cloneElement(child, { ...otherProps | ||
children: React.Children.map(children, child => React.cloneElement(child, { | ||
...otherProps | ||
})) | ||
@@ -1324,3 +1224,2 @@ })); | ||
let rafId = 0; | ||
const nodeDragSourcePropInjection = (connect, monitor) => ({ | ||
@@ -1332,3 +1231,2 @@ connectDragSource: connect.dragSource(), | ||
}); | ||
const wrapSource = (el, startDrag, endDrag, dndType) => { | ||
@@ -1357,3 +1255,2 @@ const nodeDragSource = { | ||
}; | ||
const propInjection = (connect, monitor) => { | ||
@@ -1368,3 +1265,2 @@ const dragged = monitor.getItem(); | ||
}; | ||
const wrapPlaceholder = (el, treeId, drop, dndType) => { | ||
@@ -1392,7 +1288,5 @@ const placeholderDropTarget = { | ||
}; | ||
const getTargetDepth = (dropTargetProps, monitor, component, canNodeHaveChildren, treeId, maxDepth) => { | ||
let dropTargetDepth = 0; | ||
const rowAbove = dropTargetProps.getPrevRow(); | ||
if (rowAbove) { | ||
@@ -1406,16 +1300,11 @@ const { | ||
const aboveNodeCannotHaveChildren = !canNodeHaveChildren(node); | ||
if (aboveNodeCannotHaveChildren) { | ||
path = path.slice(0, -1); | ||
} | ||
dropTargetDepth = Math.min(path.length, dropTargetProps.path.length); | ||
} | ||
let blocksOffset; | ||
let dragSourceInitialDepth = (monitor.getItem().path || []).length; | ||
if (monitor.getItem().treeId !== treeId) { | ||
dragSourceInitialDepth = 0; | ||
if (component) { | ||
@@ -1431,5 +1320,3 @@ const relativePosition = component.node.getBoundingClientRect(); | ||
} | ||
let targetDepth = Math.min(dropTargetDepth, Math.max(0, dragSourceInitialDepth + blocksOffset - 1)); | ||
if (typeof maxDepth !== "undefined" && maxDepth !== undefined) { | ||
@@ -1440,6 +1327,4 @@ const draggedNode = monitor.getItem().node; | ||
} | ||
return targetDepth; | ||
}; | ||
const canDrop = (dropTargetProps, monitor, canNodeHaveChildren, treeId, maxDepth, treeRefcanDrop) => { | ||
@@ -1449,3 +1334,2 @@ if (!monitor.isOver()) { | ||
} | ||
if (monitor.getItem().treeId !== treeId && dropTargetProps.disableDropFromOutside) return false; | ||
@@ -1456,7 +1340,5 @@ const rowAbove = dropTargetProps.getPrevRow(); | ||
const targetDepth = getTargetDepth(dropTargetProps, monitor, undefined, canNodeHaveChildren, treeId, maxDepth); | ||
if (targetDepth >= abovePath.length && typeof aboveNode.children === "function") { | ||
return false; | ||
} | ||
if (typeof treeRefcanDrop === "function") { | ||
@@ -1476,6 +1358,4 @@ const { | ||
} | ||
return true; | ||
}; | ||
const wrapTarget = (el, canNodeHaveChildren, treeId, maxDepth, treeRefcanDrop, drop, dragHover, dndType) => { | ||
@@ -1498,16 +1378,14 @@ const nodeDropTarget = { | ||
const draggedNode = monitor.getItem().node; | ||
const needsRedraw = dropTargetProps.node !== draggedNode || targetDepth !== dropTargetProps.path.length - 1; | ||
const needsRedraw = | ||
dropTargetProps.node !== draggedNode || | ||
targetDepth !== dropTargetProps.path.length - 1; | ||
if (!needsRedraw) { | ||
return; | ||
} | ||
cancelAnimationFrame(rafId); | ||
rafId = requestAnimationFrame(() => { | ||
const item = monitor.getItem(); | ||
if (!item || !monitor.isOver()) { | ||
return; | ||
} | ||
dragHover({ | ||
@@ -1538,3 +1416,2 @@ node: draggedNode, | ||
const argsArray = keysArray.map(key => args[key]); | ||
if (argsArray.length !== savedArgsArray.length || argsArray.some((arg, index) => arg !== savedArgsArray[index]) || keysArray.some((key, index) => key !== savedKeysArray[index])) { | ||
@@ -1545,7 +1422,5 @@ savedArgsArray = argsArray; | ||
} | ||
return savedResult; | ||
}; | ||
}; | ||
const memoizedInsertNode = memoize(insertNode); | ||
@@ -1556,9 +1431,11 @@ const memoizedGetFlatDataFromTree = memoize(getFlatDataFromTree); | ||
let treeIdCounter = 1; | ||
const mergeTheme = props => { | ||
const merged = { ...props, | ||
style: { ...props.theme.style, | ||
const merged = { | ||
...props, | ||
style: { | ||
...props.theme.style, | ||
...props.style | ||
}, | ||
innerStyle: { ...props.theme.innerStyle, | ||
innerStyle: { | ||
...props.theme.innerStyle, | ||
...props.innerStyle | ||
@@ -1575,3 +1452,2 @@ } | ||
}; | ||
for (const propKey of Object.keys(overridableDefaults)) { | ||
@@ -1582,6 +1458,4 @@ if (props[propKey] === undefined) { | ||
} | ||
return merged; | ||
}; | ||
class ReactSortableTree extends React.Component { | ||
@@ -1601,3 +1475,2 @@ static search(props, state, seekIndex, expand, singleSearch) { | ||
} = state; | ||
if (!searchQuery && !searchMethod) { | ||
@@ -1607,3 +1480,2 @@ if (searchFinishCallback) { | ||
} | ||
return { | ||
@@ -1613,3 +1485,2 @@ searchMatches: [] | ||
} | ||
const newState = { | ||
@@ -1633,3 +1504,2 @@ instanceProps: {} | ||
}); | ||
if (expand) { | ||
@@ -1639,13 +1509,9 @@ newState.instanceProps.ignoreOneTreeUpdate = true; | ||
} | ||
if (searchFinishCallback) { | ||
searchFinishCallback(searchMatches); | ||
} | ||
let searchFocusTreeIndex; | ||
if (seekIndex && searchFocusOffset !== undefined && searchFocusOffset < searchMatches.length) { | ||
searchFocusTreeIndex = searchMatches[searchFocusOffset].treeIndex; | ||
} | ||
newState.searchMatches = searchMatches; | ||
@@ -1655,3 +1521,2 @@ newState.searchFocusTreeIndex = searchFocusTreeIndex; | ||
} | ||
static loadLazyChildren(props, state) { | ||
@@ -1681,3 +1546,5 @@ const { | ||
node: oldNode | ||
}) => oldNode === node ? { ...oldNode, | ||
}) => | ||
oldNode === node ? { | ||
...oldNode, | ||
children: childrenArray | ||
@@ -1692,6 +1559,5 @@ } : oldNode, | ||
} | ||
constructor(props) { | ||
super(props); | ||
this.listRef = React__default["default"].createRef(); | ||
this.listRef = props.virtuosoRef || React__default["default"].createRef(); | ||
const { | ||
@@ -1716,7 +1582,10 @@ dndType, | ||
} = props; | ||
return jsxRuntime.jsx(reactVirtuoso.Virtuoso, { | ||
ref: this.listRef, | ||
...otherProps | ||
}); | ||
return ( | ||
jsxRuntime.jsx(reactVirtuoso.Virtuoso, { | ||
ref: this.listRef, | ||
...otherProps | ||
}) | ||
); | ||
})); | ||
this.vStrength = withScrolling.createVerticalStrength(slideRegionSize); | ||
@@ -1748,3 +1617,2 @@ this.hStrength = withScrolling.createHorizontalStrength(slideRegionSize); | ||
} | ||
componentDidMount() { | ||
@@ -1756,3 +1624,2 @@ ReactSortableTree.loadLazyChildren(this.props, this.state); | ||
} | ||
static getDerivedStateFromProps(nextProps, prevState) { | ||
@@ -1765,3 +1632,2 @@ const { | ||
instanceProps.treeData = nextProps.treeData; | ||
if (!isTreeDataEqual) { | ||
@@ -1775,3 +1641,2 @@ if (instanceProps.ignoreOneTreeUpdate) { | ||
} | ||
newState.draggingTreeData = undefined; | ||
@@ -1787,6 +1652,6 @@ newState.draggedNode = undefined; | ||
} | ||
instanceProps.searchQuery = nextProps.searchQuery; | ||
instanceProps.searchFocusOffset = nextProps.searchFocusOffset; | ||
newState.instanceProps = { ...instanceProps, | ||
newState.instanceProps = { | ||
...instanceProps, | ||
...newState.instanceProps | ||
@@ -1796,3 +1661,2 @@ }; | ||
} | ||
componentDidUpdate(prevProps, prevState) { | ||
@@ -1806,10 +1670,7 @@ if (this.state.dragging !== prevState.dragging && this.props.onDragStateChanged) { | ||
} | ||
componentWillUnmount() { | ||
this.clearMonitorSubscription(); | ||
} | ||
handleDndMonitorChange() { | ||
const monitor = this.props.dragDropManager.getMonitor(); | ||
if (!monitor.isDragging() && this.state.draggingTreeData) { | ||
@@ -1821,3 +1682,2 @@ setTimeout(() => { | ||
} | ||
getRows(treeData) { | ||
@@ -1830,3 +1690,2 @@ return memoizedGetFlatDataFromTree({ | ||
} | ||
startDrag = ({ | ||
@@ -1862,3 +1721,2 @@ path | ||
} | ||
this.setState(({ | ||
@@ -1888,3 +1746,4 @@ draggingTreeData, | ||
node | ||
}) => ({ ...node, | ||
}) => ({ | ||
...node, | ||
expanded: true | ||
@@ -1903,3 +1762,2 @@ }), | ||
} = this.state; | ||
if (!dropResult) { | ||
@@ -1920,3 +1778,2 @@ this.setState({ | ||
let shouldCopy = this.props.shouldCopyOnOutsideDrop; | ||
if (typeof shouldCopy === "function") { | ||
@@ -1929,5 +1786,3 @@ shouldCopy = shouldCopy({ | ||
} | ||
let treeData = this.state.draggingTreeData || instanceProps.treeData; | ||
if (shouldCopy) { | ||
@@ -1939,3 +1794,4 @@ treeData = changeNodeAtPath({ | ||
node: copyNode | ||
}) => ({ ...copyNode | ||
}) => ({ | ||
...copyNode | ||
}), | ||
@@ -1945,3 +1801,2 @@ getNodeKey: this.props.getNodeKey | ||
} | ||
this.props.onChange(treeData); | ||
@@ -1967,10 +1822,7 @@ this.props.onMoveNode({ | ||
} = this.props; | ||
if (canNodeHaveChildren) { | ||
return canNodeHaveChildren(node); | ||
} | ||
return true; | ||
}; | ||
toggleChildrenVisibility({ | ||
@@ -1988,3 +1840,4 @@ node: targetNode, | ||
node | ||
}) => ({ ...node, | ||
}) => ({ | ||
...node, | ||
expanded: !node.expanded | ||
@@ -2002,3 +1855,2 @@ }), | ||
} | ||
moveNode({ | ||
@@ -2037,3 +1889,2 @@ node, | ||
} | ||
renderRow(row, { | ||
@@ -2108,3 +1959,2 @@ listIndex, | ||
} | ||
render() { | ||
@@ -2131,3 +1981,2 @@ const { | ||
let swapLength; | ||
if (draggedNode && draggedMinimumTreeIndex !== undefined) { | ||
@@ -2151,5 +2000,3 @@ const addedResult = memoizedInsertNode({ | ||
} | ||
const matchKeys = {}; | ||
for (const [i, { | ||
@@ -2160,3 +2007,2 @@ path | ||
} | ||
if (searchFocusTreeIndex !== undefined) { | ||
@@ -2168,6 +2014,4 @@ this.listRef.current.scrollToIndex({ | ||
} | ||
let containerStyle = style; | ||
let list; | ||
if (rows.length === 0) { | ||
@@ -2205,3 +2049,2 @@ const Placeholder = this.treePlaceholderRenderer; | ||
} | ||
return jsxRuntime.jsx("div", { | ||
@@ -2213,5 +2056,3 @@ className: classnames("rst__tree", className), | ||
} | ||
} | ||
ReactSortableTree.defaultProps = { | ||
@@ -2247,3 +2088,2 @@ canDrag: true, | ||
}; | ||
const SortableTreeWithoutDndContext = function (props) { | ||
@@ -2253,3 +2093,4 @@ return jsxRuntime.jsx(reactDnd.DndContext.Consumer, { | ||
dragDropManager | ||
}) => dragDropManager === undefined ? undefined : jsxRuntime.jsx(ReactSortableTree, { ...props, | ||
}) => dragDropManager === undefined ? undefined : jsxRuntime.jsx(ReactSortableTree, { | ||
...props, | ||
dragDropManager: dragDropManager | ||
@@ -2259,3 +2100,2 @@ }) | ||
}; | ||
const SortableTree = function (props) { | ||
@@ -2265,11 +2105,12 @@ return jsxRuntime.jsx(reactDnd.DndProvider, { | ||
backend: reactDndHtml5Backend.HTML5Backend, | ||
children: jsxRuntime.jsx(SortableTreeWithoutDndContext, { ...props | ||
children: jsxRuntime.jsx(SortableTreeWithoutDndContext, { | ||
...props | ||
}) | ||
}); | ||
}; | ||
const SortableTreeWithExternalManager = function (props) { | ||
return jsxRuntime.jsx(reactDnd.DndProvider, { | ||
manager: props.dragDropManager, | ||
children: jsxRuntime.jsx(SortableTreeWithoutDndContext, { ...props | ||
children: jsxRuntime.jsx(SortableTreeWithoutDndContext, { | ||
...props | ||
}) | ||
@@ -2276,0 +2117,0 @@ }); |
{ | ||
"name": "@nosferatu500/react-sortable-tree", | ||
"version": "6.0.0-alpha.4", | ||
"version": "6.0.0-alpha.5", | ||
"description": "Drag-and-drop sortable component for nested data and hierarchies", | ||
@@ -49,3 +49,3 @@ "main": "./index.js", | ||
"react-dnd-html5-backend": "^14.1.0", | ||
"react-virtuoso": "^2.19.1" | ||
"react-virtuoso": "^3.1.0" | ||
}, | ||
@@ -52,0 +52,0 @@ "peerDependencies": { |
@@ -1,2 +0,3 @@ | ||
/// <reference types="react" /> | ||
import React from "react"; | ||
import { VirtuosoHandle } from "react-virtuoso"; | ||
import "./react-sortable-tree.css"; | ||
@@ -62,2 +63,3 @@ declare type SearchParams = { | ||
className?: string; | ||
virtuosoRef?: React.Ref<VirtuosoHandle>; | ||
innerStyle?: any; | ||
@@ -64,0 +66,0 @@ slideRegionSize?: number; |
Sorry, the diff of this file is not supported yet
235100
7020
+ Addedreact-virtuoso@3.1.5(transitive)
- Removedreact-virtuoso@2.19.1(transitive)
Updatedreact-virtuoso@^3.1.0