react-native-tree-multi-select
Version:
A super-fast, customizable tree view component for React Native with multi-selection, checkboxes, and search filtering capabilities.
169 lines (159 loc) • 7.46 kB
JavaScript
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.ScrollToNodeHandler = void 0;
var _react = _interopRequireDefault(require("react"));
var _expandCollapse = require("../helpers/expandCollapse.helper");
var _treeView = require("../store/treeView.store");
var _shallow = require("zustand/react/shallow");
var _typedMemo = require("../utils/typedMemo");
var _fastIsEqual = require("fast-is-equal");
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
/**
* ScrollToNodeHandler Component
*
* This component provides an imperative handle to scroll to a specified node within a tree view.
* The scrolling action is orchestrated via a two-step "milestone" mechanism that ensures the target
* node is both expanded in the tree and that the rendered list reflects this expansion before the scroll
* is performed.
*
* The two key milestones tracked by the `expandAndScrollToNodeQueue` state are:
* 1. EXPANDED: Indicates that the expansion logic for the target node has been initiated.
* 2. RENDERED: Indicates that the list has re-rendered with the expanded node included.
*
* When the `scrollToNodeID` method is called:
* - The scroll parameters (target node ID, animation preferences, view offset/position) are stored in a ref.
* - The target node's expansion is triggered via the `expandNodes` helper.
* - The `expandAndScrollToNodeQueue` state is updated to mark that expansion has begun.
*
* As the component re-renders (e.g., after the node expansion changes the rendered list):
* - A useEffect monitors changes to the list, and once it detects the expansion has occurred,
* it updates the queue to include the RENDERED milestone.
*
* A layout effect then waits for both conditions to be met:
* - The target node is confirmed to be in the expanded set.
* - The `expandAndScrollToNodeQueue` exactly matches the expected milestones ([EXPANDED, RENDERED]).
*
* Once both conditions are satisfied:
* - The index of the target node is determined within the latest flattened node list.
* - The flash list is scrolled to that index.
* - The queued scroll parameters and milestone queue are reset.
*
* This design ensures that the scroll action is performed only after the target node is fully present
* in the UI, thus preventing issues with attempting to scroll to an element that does not exist yet.
*/
// Enum representing the two milestones needed before scrolling
var ExpandQueueAction = /*#__PURE__*/function (ExpandQueueAction) {
ExpandQueueAction[ExpandQueueAction["EXPANDED"] = 0] = "EXPANDED";
ExpandQueueAction[ExpandQueueAction["RENDERED"] = 1] = "RENDERED";
return ExpandQueueAction;
}(ExpandQueueAction || {});
function _innerScrollToNodeHandler(props, ref) {
const {
storeId,
flashListRef,
flattenedFilteredNodes,
setInitialScrollIndex,
initialScrollNodeID
} = props;
const {
expanded,
childToParentMap
} = (0, _treeView.useTreeViewStore)(storeId)((0, _shallow.useShallow)(state => ({
expanded: state.expanded,
childToParentMap: state.childToParentMap
})));
_react.default.useImperativeHandle(ref, () => ({
scrollToNodeID: params => {
queuedScrollToNodeParams.current = params;
// Mark that expansion is initiated.
setExpandAndScrollToNodeQueue([ExpandQueueAction.EXPANDED]);
// Trigger expansion logic (this may update the store and subsequently re-render the list).
(0, _expandCollapse.expandNodes)(storeId, [queuedScrollToNodeParams.current.nodeId], !queuedScrollToNodeParams.current.expandScrolledNode);
}
}), [storeId]);
// Ref to store the scroll parameters for the queued action.
const queuedScrollToNodeParams = _react.default.useRef(null);
// State to track progression: first the expansion is triggered, then the list is rendered.
const [expandAndScrollToNodeQueue, setExpandAndScrollToNodeQueue] = _react.default.useState([]);
const latestFlattenedFilteredNodesRef = _react.default.useRef(flattenedFilteredNodes);
/* When the rendered node list changes, update the ref.
If an expansion was triggered, mark that the list is now rendered. */
_react.default.useEffect(() => {
setExpandAndScrollToNodeQueue(prevQueue => {
if (prevQueue.includes(ExpandQueueAction.EXPANDED)) {
latestFlattenedFilteredNodesRef.current = flattenedFilteredNodes;
return [ExpandQueueAction.EXPANDED, ExpandQueueAction.RENDERED];
} else {
return prevQueue;
}
});
}, [flattenedFilteredNodes]);
/* Once the target node is expanded and the list is updated (milestones reached),
perform the scroll using the latest node list. */
_react.default.useLayoutEffect(() => {
if (queuedScrollToNodeParams.current === null) return;
if (!(0, _fastIsEqual.fastIsEqual)(expandAndScrollToNodeQueue, [ExpandQueueAction.EXPANDED, ExpandQueueAction.RENDERED])) {
return;
}
// If node is set to not expand
if (!queuedScrollToNodeParams.current.expandScrolledNode) {
let parentId;
// Get the parent's id of the node to scroll to
if (childToParentMap.has(queuedScrollToNodeParams.current.nodeId)) {
parentId = childToParentMap.get(queuedScrollToNodeParams.current.nodeId);
}
// Ensure if the parent is expanded before proceeding to scroll to the node
if (parentId && !expanded.has(parentId)) return;
}
// If node is set to expand
else {
if (!expanded.has(queuedScrollToNodeParams.current.nodeId)) return;
}
const {
nodeId,
animated,
viewOffset,
viewPosition
} = queuedScrollToNodeParams.current;
function scrollToItem() {
const index = latestFlattenedFilteredNodesRef.current.findIndex(item => item.id === nodeId);
if (index !== -1 && flashListRef.current) {
// Scroll to the target index.
flashListRef.current.scrollToIndex({
index,
animated,
viewOffset,
viewPosition
});
} else {
if (__DEV__) {
console.info("Cannot find the item of the mentioned id to scroll in the rendered tree view list data!");
}
}
// Clear the queued parameters and reset the expansion/render queue.
queuedScrollToNodeParams.current = null;
setExpandAndScrollToNodeQueue([]);
}
scrollToItem();
}, [childToParentMap, expanded, flashListRef, expandAndScrollToNodeQueue]);
////////////////////////////// Handle Initial Scroll /////////////////////////////
/* On first render, if an initial scroll target is provided, determine its index.
This is done only once. */
const initialScrollDone = _react.default.useRef(false);
_react.default.useLayoutEffect(() => {
if (initialScrollDone.current) return;
const index = flattenedFilteredNodes.findIndex(item => item.id === initialScrollNodeID);
setInitialScrollIndex(index);
if (index !== -1) {
initialScrollDone.current = true;
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [flattenedFilteredNodes, initialScrollNodeID]);
/////////////////////////////////////////////////////////////////////////////////
return null;
}
const _ScrollToNodeHandler = /*#__PURE__*/_react.default.forwardRef(_innerScrollToNodeHandler);
const ScrollToNodeHandler = exports.ScrollToNodeHandler = (0, _typedMemo.typedMemo)(_ScrollToNodeHandler);
//# sourceMappingURL=ScrollToNodeHandler.js.map
;