@mui/x-tree-view
Version:
The community edition of the MUI X Tree View components.
64 lines • 2.58 kB
JavaScript
import { createSelector } from '@mui/x-internals/store';
import { isItemDisabled, TREE_VIEW_ROOT_PARENT_ID } from "./useTreeViewItems.utils.js";
const EMPTY_CHILDREN = [];
export const itemsSelectors = {
/**
* Gets the DOM structure of the Tree View.
*/
domStructure: createSelector(state => state.items.domStructure),
/**
* Checks whether the disabled items are focusable.
*/
disabledItemFocusable: createSelector(state => state.items.disabledItemsFocusable),
/**
* Gets the meta-information of all items.
*/
itemMetaLookup: createSelector(state => state.items.itemMetaLookup),
/**
* Gets the ordered children ids of all items.
*/
itemOrderedChildrenIdsLookup: createSelector(state => state.items.itemOrderedChildrenIdsLookup),
/**
* Gets the meta-information of an item.
*/
itemMeta: createSelector((state, itemId) => state.items.itemMetaLookup[itemId ?? TREE_VIEW_ROOT_PARENT_ID] ?? null),
/**
* Gets the ordered children ids of an item.
*/
itemOrderedChildrenIds: createSelector((state, itemId) => state.items.itemOrderedChildrenIdsLookup[itemId ?? TREE_VIEW_ROOT_PARENT_ID] ?? EMPTY_CHILDREN),
/**
* Gets the model of an item.
*/
itemModel: createSelector((state, itemId) => state.items.itemModelLookup[itemId]),
/**
* Checks whether an item is disabled.
*/
isItemDisabled: createSelector((state, itemId) => isItemDisabled(state.items.itemMetaLookup, itemId)),
/**
* Gets the index of an item in its parent's children.
*/
itemIndex: createSelector((state, itemId) => {
const itemMeta = state.items.itemMetaLookup[itemId];
if (itemMeta == null) {
return -1;
}
const parentIndexes = state.items.itemChildrenIndexesLookup[itemMeta.parentId ?? TREE_VIEW_ROOT_PARENT_ID];
return parentIndexes[itemMeta.id];
}),
/**
* Gets the id of an item's parent.
*/
itemParentId: createSelector((state, itemId) => state.items.itemMetaLookup[itemId]?.parentId ?? null),
/**
* Gets the depth of an item (items at the root level have a depth of 0).
*/
itemDepth: createSelector((state, itemId) => state.items.itemMetaLookup[itemId]?.depth ?? 0),
/**
* Checks whether an item can be focused.
*/
canItemBeFocused: createSelector((state, itemId) => state.items.disabledItemsFocusable || !isItemDisabled(state.items.itemMetaLookup, itemId)),
/**
* Checks whether an item is selectable based on the `isItemSelectionDisabled` prop.
*/
isItemSelectable: createSelector((state, itemId) => state.items.itemMetaLookup[itemId]?.selectable ?? true)
};