@mui/x-tree-view
Version:
The community edition of the MUI X Tree View components.
108 lines (106 loc) • 3.51 kB
JavaScript
;
var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard").default;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.useTreeViewBuildContext = void 0;
var React = _interopRequireWildcard(require("react"));
const useTreeViewBuildContext = parameters => {
const {
plugins,
instance,
publicAPI,
store,
rootRef
} = parameters;
const runItemPlugins = React.useCallback(itemPluginProps => {
let finalRootRef = null;
let finalContentRef = null;
const pluginPropEnhancers = [];
const pluginPropEnhancersNames = {};
plugins.forEach(plugin => {
if (!plugin.itemPlugin) {
return;
}
const itemPluginResponse = plugin.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
};
}, [plugins]);
const wrapItem = React.useCallback(({
itemId,
children,
idAttribute
}) => {
let finalChildren = children;
// The wrappers are reversed to ensure that the first wrapper is the outermost one.
for (let i = plugins.length - 1; i >= 0; i -= 1) {
const plugin = plugins[i];
if (plugin.wrapItem) {
finalChildren = plugin.wrapItem({
instance,
itemId,
children: finalChildren,
idAttribute
});
}
}
return finalChildren;
}, [plugins, instance]);
const wrapRoot = React.useCallback(({
children
}) => {
let finalChildren = children;
// The wrappers are reversed to ensure that the first wrapper is the outermost one.
for (let i = plugins.length - 1; i >= 0; i -= 1) {
const plugin = plugins[i];
if (plugin.wrapRoot) {
finalChildren = plugin.wrapRoot({
children: finalChildren
});
}
}
return finalChildren;
}, [plugins]);
return React.useMemo(() => ({
runItemPlugins,
wrapItem,
wrapRoot,
instance,
publicAPI,
store,
rootRef
}), [runItemPlugins, wrapItem, wrapRoot, instance, publicAPI, store, rootRef]);
};
exports.useTreeViewBuildContext = useTreeViewBuildContext;