@mui/x-data-grid-premium
Version:
The Premium plan edition of the MUI X Data Grid Components.
56 lines (54 loc) • 3.28 kB
JavaScript
import * as React from 'react';
import { gridRowTreeSelector, gridExpandedSortedRowIdsSelector, gridRowNodeSelector, gridRowMaximumTreeDepthSelector } from '@mui/x-data-grid-pro';
import { gridExpandedSortedRowIndexLookupSelector, useGridRowsOverridableMethods as useGridRowsOverridableMethodsCommunity, useGridSelector } from '@mui/x-data-grid-pro/internals';
import { rowGroupingReorderExecutor } from "../rowReorder/reorderExecutor.js";
export const useGridRowsOverridableMethods = (apiRef, props) => {
const {
processRowUpdate,
onProcessRowUpdateError
} = props;
const {
setRowIndex: setRowIndexPlain
} = useGridRowsOverridableMethodsCommunity(apiRef);
const flatTree = useGridSelector(apiRef, gridRowMaximumTreeDepthSelector) === 1;
const setRowIndex = React.useCallback(async (sourceRowId, targetOriginalIndex) => {
const sortedFilteredRowIds = gridExpandedSortedRowIdsSelector(apiRef);
const sortedFilteredRowIndexLookup = gridExpandedSortedRowIndexLookupSelector(apiRef);
const rowTree = gridRowTreeSelector(apiRef);
const sourceNode = 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.`);
}
/**
* 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,
placeholderIndex: targetOriginalIndex,
sortedFilteredRowIds,
sortedFilteredRowIndexLookup,
rowTree,
apiRef,
processRowUpdate,
onProcessRowUpdateError
};
await rowGroupingReorderExecutor.execute(executionContext);
}, [apiRef, processRowUpdate, onProcessRowUpdateError]);
return {
setRowIndex: flatTree ? setRowIndexPlain : setRowIndex
};
};