UNPKG

@mui/x-tree-view

Version:

The community edition of the MUI X Tree View components.

119 lines (117 loc) 3.75 kB
"use strict"; var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard").default; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default; Object.defineProperty(exports, "__esModule", { value: true }); exports.useTreeView = void 0; exports.useTreeViewApiInitialization = useTreeViewApiInitialization; var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends")); var React = _interopRequireWildcard(require("react")); var _useForkRef = _interopRequireDefault(require("@mui/utils/useForkRef")); var _corePlugins = require("../corePlugins"); var _useExtractPluginParamsFromProps = require("./useExtractPluginParamsFromProps"); var _useTreeViewBuildContext = require("./useTreeViewBuildContext"); var _TreeViewStore = require("../utils/TreeViewStore"); function initializeInputApiRef(inputApiRef) { if (inputApiRef.current == null) { inputApiRef.current = {}; } return inputApiRef; } function useTreeViewApiInitialization(inputApiRef) { const fallbackPublicApiRef = React.useRef({}); if (inputApiRef) { return initializeInputApiRef(inputApiRef); } return fallbackPublicApiRef; } let globalId = 0; /** * This is the main hook that sets the plugin system up for the tree-view. * * It manages the data used to create the tree-view. * * @param plugins All the plugins that will be used in the tree-view. * @param props The props passed to the tree-view. * @param rootRef The ref of the root element. */ const useTreeView = ({ plugins: inPlugins, rootRef, props }) => { const plugins = React.useMemo(() => [..._corePlugins.TREE_VIEW_CORE_PLUGINS, ...inPlugins], [inPlugins]); const { pluginParams, forwardedProps, apiRef } = (0, _useExtractPluginParamsFromProps.useExtractPluginParamsFromProps)({ plugins, props }); const instanceRef = React.useRef({}); const instance = instanceRef.current; const publicAPI = useTreeViewApiInitialization(apiRef); const innerRootRef = React.useRef(null); const handleRootRef = (0, _useForkRef.default)(innerRootRef, rootRef); const storeRef = React.useRef(null); if (storeRef.current == null) { globalId += 1; const initialState = { cacheKey: { id: globalId } }; plugins.forEach(plugin => { if (plugin.getInitialState) { Object.assign(initialState, plugin.getInitialState(pluginParams)); } }); storeRef.current = new _TreeViewStore.TreeViewStore(initialState); } const contextValue = (0, _useTreeViewBuildContext.useTreeViewBuildContext)({ plugins, instance, publicAPI: publicAPI.current, store: storeRef.current, rootRef: innerRootRef }); const rootPropsGetters = []; const runPlugin = plugin => { const pluginResponse = plugin({ instance, params: pluginParams, rootRef: innerRootRef, plugins, store: storeRef.current }); if (pluginResponse.getRootProps) { rootPropsGetters.push(pluginResponse.getRootProps); } if (pluginResponse.publicAPI) { Object.assign(publicAPI.current, pluginResponse.publicAPI); } if (pluginResponse.instance) { Object.assign(instance, pluginResponse.instance); } }; plugins.forEach(runPlugin); const getRootProps = (otherHandlers = {}) => { const rootProps = (0, _extends2.default)({ role: 'tree' }, forwardedProps, otherHandlers, { ref: handleRootRef }); rootPropsGetters.forEach(rootPropsGetter => { Object.assign(rootProps, rootPropsGetter(otherHandlers)); }); return rootProps; }; return { getRootProps, rootRef: handleRootRef, contextValue }; }; exports.useTreeView = useTreeView;