UNPKG

@mui/x-tree-view

Version:

The community edition of the MUI X Tree View components.

125 lines (123 loc) 5.17 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.selectionSelectors = void 0; var _store = require("@mui/x-internals/store"); var _selectors = require("../items/selectors"); const selectedItemsSelector = (0, _store.createSelectorMemoized)(state => state.selectedItems, selectedItemsRaw => { if (Array.isArray(selectedItemsRaw)) { return selectedItemsRaw; } if (selectedItemsRaw != null) { return [selectedItemsRaw]; } return []; }); const selectedItemsMapSelector = (0, _store.createSelectorMemoized)(selectedItemsSelector, selectedItems => { const selectedItemsMap = new Map(); selectedItems.forEach(id => { selectedItemsMap.set(id, true); }); return selectedItemsMap; }); const isItemSelectableSelector = (0, _store.createSelector)((state, itemId) => state.itemMetaLookup[itemId]?.selectable ?? true); const isItemSelectedSelector = (0, _store.createSelector)(selectedItemsMapSelector, (selectedItemsMap, itemId) => selectedItemsMap.has(itemId)); const canItemBeSelectedSelector = (0, _store.createSelector)(_selectors.itemsSelectors.isItemDisabled, isItemSelectableSelector, state => !state.disableSelection, (isItemDisabled, isItemSelectable, isSelectionEnabled, _itemId) => isSelectionEnabled && !isItemDisabled && isItemSelectable); const propagationRulesSelector = (0, _store.createSelector)(state => state.selectionPropagation); const itemSelectionStatusSelector = (0, _store.createSelector)((state, itemId) => { if (isItemSelectedSelector(state, itemId)) { return 'selected'; } // Only the descendants can make an unselected item `indeterminate`, so leaves can be resolved // without traversing the tree. if (_selectors.itemsSelectors.itemOrderedChildrenIds(state, itemId).length === 0) { return 'unselected'; } let hasSelectedDescendant = false; let hasUnSelectedDescendant = false; const traverseDescendants = itemToTraverseId => { if (itemToTraverseId !== itemId) { if (canItemBeSelectedSelector(state, itemToTraverseId)) { if (isItemSelectedSelector(state, itemToTraverseId)) { hasSelectedDescendant = true; } else { hasUnSelectedDescendant = true; } } } _selectors.itemsSelectors.itemOrderedChildrenIds(state, itemToTraverseId).forEach(traverseDescendants); }; traverseDescendants(itemId); const shouldSelectBasedOnDescendants = propagationRulesSelector(state).parents; if (shouldSelectBasedOnDescendants) { if (hasSelectedDescendant && hasUnSelectedDescendant) { return 'indeterminate'; } if (hasSelectedDescendant && !hasUnSelectedDescendant) { return 'selected'; } return 'unselected'; } if (hasSelectedDescendant) { return 'indeterminate'; } return 'unselected'; }); const selectionSelectors = exports.selectionSelectors = { /** * Gets the selected items as provided to the component. */ selectedItemsRaw: (0, _store.createSelector)(state => state.selectedItems), /** * Gets the selected items as an array. */ selectedItems: selectedItemsSelector, /** * Gets the selected items as a Map. */ selectedItemsMap: selectedItemsMapSelector, /** * Checks whether selection is enabled. */ enabled: (0, _store.createSelector)(state => !state.disableSelection), /** * Checks whether multi selection is enabled. */ isMultiSelectEnabled: (0, _store.createSelector)(state => state.multiSelect), /** * Checks whether checkbox selection is enabled. */ isCheckboxSelectionEnabled: (0, _store.createSelector)(state => state.checkboxSelection), /** * Gets the selection propagation rules. */ propagationRules: propagationRulesSelector, /** * Checks whether an item is selected. */ isItemSelected: isItemSelectedSelector, /** * Gets the selection status of an item. * An item that is not selected is `indeterminate` when some of its selectable descendants are selected. * When `selectionPropagation.parents` is enabled, an item whose selectable descendants are all selected is `selected`. */ itemSelectionStatus: itemSelectionStatusSelector, /** * Checks whether an item is indeterminate (it is not selected but some of its selectable descendants are). */ isItemIndeterminate: (0, _store.createSelector)(itemSelectionStatusSelector, (selectionStatus, _itemId) => selectionStatus === 'indeterminate'), /** * Checks whether the selection feature is enabled for an item. * Returns `true` when selection is enabled on the Tree View and the item is selectable (even if the item is disabled). */ isFeatureEnabledForItem: (0, _store.createSelector)(isItemSelectableSelector, state => !state.disableSelection, (isItemSelectable, isSelectionEnabled, _itemId) => isSelectionEnabled && isItemSelectable), /** * Checks whether an item can be selected (if selection is enabled, if the item is not disabled, and if the item is selectable). */ canItemBeSelected: canItemBeSelectedSelector, /** * Checks whether an item is selectable based on the `isItemSelectionDisabled` prop. */ isItemSelectable: isItemSelectableSelector };