UNPKG

@mui/x-data-grid-premium

Version:

The Premium plan edition of the MUI X Data Grid Components.

118 lines (114 loc) 5.69 kB
"use strict"; var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard").default; Object.defineProperty(exports, "__esModule", { value: true }); exports.useGridRowsOverridableMethods = void 0; var React = _interopRequireWildcard(require("react")); var _xDataGridPro = require("@mui/x-data-grid-pro"); var _internals = require("@mui/x-data-grid-pro/internals"); var _rowGroupingReorderExecutor = require("../rowReorder/rowGroupingReorderExecutor"); const useGridRowsOverridableMethods = (apiRef, props) => { const { processRowUpdate, onProcessRowUpdateError } = props; const { setRowIndex: setRowIndexPlain, setRowPosition: setRowPositionPlain } = (0, _internals.useGridRowsOverridableMethodsCommunity)(apiRef); const { setRowIndex: setRowIndexTreeData, setRowPosition: setRowPositionTreeData } = (0, _internals.useGridRowsOverridableMethodsPro)(apiRef, props); const flatTree = (0, _internals.useGridSelector)(apiRef, _xDataGridPro.gridRowMaximumTreeDepthSelector) === 1; const setRowPosition = React.useCallback(async (sourceRowId, targetRowId, position) => { const sortedFilteredRowIds = (0, _xDataGridPro.gridExpandedSortedRowIdsSelector)(apiRef); const sortedFilteredRowIndexLookup = (0, _xDataGridPro.gridExpandedSortedRowIndexLookupSelector)(apiRef); const rowTree = (0, _xDataGridPro.gridRowTreeSelector)(apiRef); const sourceNode = (0, _xDataGridPro.gridRowNodeSelector)(apiRef, sourceRowId); const targetNode = (0, _xDataGridPro.gridRowNodeSelector)(apiRef, targetRowId); if (!sourceNode) { throw new Error(`MUI X: No row with id #${sourceRowId} found.`); } if (!targetNode) { throw new Error(`MUI X: No row with id #${targetRowId} found.`); } if (sourceNode.type === 'footer') { throw new Error(`MUI X: The row reordering do not support reordering of footer rows.`); } // Get the target index from the targetRowId using the lookup selector const targetIndexUnadjusted = sortedFilteredRowIndexLookup[targetRowId]; if (targetIndexUnadjusted === undefined) { throw new Error(`MUI X: Target row with id #${targetRowId} not found in current view.`); } const targetIndex = position === 'below' ? targetIndexUnadjusted + 1 : targetIndexUnadjusted; /** * Row Grouping Reordering Use Cases * ================================= * * | Case | Source Node | Target Node | Parent Relationship | Action | * | :--- | :---------- | :---------- | :------------------------ | :-------------------------------------------------------------------------- | * | A ✅ | Leaf | Leaf | Same parent | Swap positions (similar to flat tree structure) | * | B ✅ | Group | Group | Same parent | Swap positions (along with their descendants) | * | C ✅ | Leaf | Leaf | Different parents | Make source node a child of target's parent and update parent nodes in tree | * | D ✅ | Leaf | Group | Different parents | Make source a child of target, only allowed at same depth as source.parent | * | E ❌ | Leaf | Group | Target is source's parent | Not allowed, will have no difference | * | F ❌ | Group | Leaf | Any | Not allowed, will break the row grouping criteria | * | G ✅ | Group | Group | Different parents | Only allowed at same depth to preserve grouping criteria | */ const executionContext = { sourceRowId, dropPosition: position, placeholderIndex: targetIndex, sortedFilteredRowIds, sortedFilteredRowIndexLookup, rowTree, apiRef, processRowUpdate, onProcessRowUpdateError }; return _rowGroupingReorderExecutor.rowGroupingReorderExecutor.execute(executionContext); }, [apiRef, processRowUpdate, onProcessRowUpdateError]); const setRowIndex = React.useCallback(async (sourceRowId, targetOriginalIndex) => { const sortedFilteredRowIds = (0, _xDataGridPro.gridExpandedSortedRowIdsSelector)(apiRef); const sortedFilteredRowIndexLookup = (0, _xDataGridPro.gridExpandedSortedRowIndexLookupSelector)(apiRef); const rowTree = (0, _xDataGridPro.gridRowTreeSelector)(apiRef); const sourceNode = (0, _xDataGridPro.gridRowNodeSelector)(apiRef, sourceRowId); if (!sourceNode) { throw new Error(`MUI X: No row with id #${sourceRowId} found.`); } if (sourceNode.type === 'footer') { throw new Error(`MUI X: The row reordering do not support reordering of footer rows.`); } const executionContext = { sourceRowId, dropPosition: 'below', placeholderIndex: targetOriginalIndex, sortedFilteredRowIds, sortedFilteredRowIndexLookup, rowTree, apiRef, processRowUpdate, onProcessRowUpdateError }; return _rowGroupingReorderExecutor.rowGroupingReorderExecutor.execute(executionContext); }, [apiRef, processRowUpdate, onProcessRowUpdateError]); if (flatTree) { return { setRowIndex: setRowIndexPlain, setRowPosition: setRowPositionPlain }; } if (props.treeData) { return { setRowIndex: setRowIndexTreeData, setRowPosition: setRowPositionTreeData }; } return { setRowIndex, setRowPosition }; }; exports.useGridRowsOverridableMethods = useGridRowsOverridableMethods;