UNPKG

@mui/x-tree-view

Version:

The community edition of the MUI X Tree View components.

94 lines (91 loc) 3.31 kB
"use strict"; var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard").default; Object.defineProperty(exports, "__esModule", { value: true }); exports.useTreeViewBuildContext = void 0; var React = _interopRequireWildcard(require("react")); var _useRefWithInit = require("@base-ui/utils/useRefWithInit"); const useTreeViewBuildContext = parameters => { const { store, apiRef, rootRef } = parameters; const publicAPI = (0, _useRefWithInit.useRefWithInit)(() => store.buildPublicAPI()).current; initializeInputApiRef(publicAPI, apiRef); const runItemPlugins = React.useCallback(itemPluginProps => { let finalRootRef = null; let finalContentRef = null; const pluginPropEnhancers = []; const pluginPropEnhancersNames = {}; store.itemPluginManager.listPlugins().forEach(itemPlugin => { const itemPluginResponse = itemPlugin({ props: itemPluginProps, rootRef: finalRootRef, contentRef: finalContentRef }); if (itemPluginResponse?.rootRef) { finalRootRef = itemPluginResponse.rootRef; } if (itemPluginResponse?.contentRef) { finalContentRef = itemPluginResponse.contentRef; } if (itemPluginResponse?.propsEnhancers) { pluginPropEnhancers.push(itemPluginResponse.propsEnhancers); // Prepare a list of all the slots which are enhanced by at least one plugin Object.keys(itemPluginResponse.propsEnhancers).forEach(propsEnhancerName => { pluginPropEnhancersNames[propsEnhancerName] = true; }); } }); const resolvePropsEnhancer = currentSlotName => currentSlotParams => { const enhancedProps = {}; pluginPropEnhancers.forEach(propsEnhancersForCurrentPlugin => { const propsEnhancerForCurrentPluginAndSlot = propsEnhancersForCurrentPlugin[currentSlotName]; if (propsEnhancerForCurrentPluginAndSlot != null) { Object.assign(enhancedProps, propsEnhancerForCurrentPluginAndSlot(currentSlotParams)); } }); return enhancedProps; }; const propsEnhancers = Object.fromEntries(Object.keys(pluginPropEnhancersNames).map(propEnhancerName => [propEnhancerName, resolvePropsEnhancer(propEnhancerName)])); return { contentRef: finalContentRef, rootRef: finalRootRef, propsEnhancers }; }, [store]); const wrapItem = React.useCallback(({ itemId, children, idAttribute }) => { let finalChildren = children; const itemsWrapper = store.itemPluginManager.listWrappers(); // The wrappers are reversed to ensure that the first wrapper is the outermost one. for (let i = itemsWrapper.length - 1; i >= 0; i -= 1) { const itemWrapper = itemsWrapper[i]; finalChildren = itemWrapper({ store: store, itemId, children: finalChildren, idAttribute }); } return finalChildren; }, [store]); return React.useMemo(() => ({ runItemPlugins, wrapItem, publicAPI, store, rootRef }), [runItemPlugins, wrapItem, publicAPI, store, rootRef]); }; exports.useTreeViewBuildContext = useTreeViewBuildContext; function initializeInputApiRef(publicAPI, apiRef) { if (apiRef != null && apiRef.current == null) { apiRef.current = publicAPI; } }