@mui/x-data-grid-pro
Version:
The Pro plan edition of the MUI X Data Grid components.
60 lines (59 loc) • 3.65 kB
JavaScript
import _formatErrorMessage from "@mui/x-internals/formatErrorMessage";
import * as React from 'react';
import { gridRowTreeSelector, gridExpandedSortedRowIdsSelector, gridRowNodeSelector, gridExpandedSortedRowIndexLookupSelector } from '@mui/x-data-grid';
import { useGridRowsOverridableMethodsCommunity } from '@mui/x-data-grid/internals';
import { treeDataReorderExecutor } from "../treeData/treeDataReorderExecutor.mjs";
export const useGridRowsOverridableMethods = (apiRef, props) => {
const {
processRowUpdate,
onProcessRowUpdateError,
setTreeDataPath,
treeData
} = props;
const {
setRowIndex: setRowIndexFlat,
setRowPosition: setRowPositionFlat
} = useGridRowsOverridableMethodsCommunity(apiRef);
const setRowPosition = React.useCallback(async (sourceRowId, targetRowId, position) => {
const sortedFilteredRowIds = gridExpandedSortedRowIdsSelector(apiRef);
const sortedFilteredRowIndexLookup = gridExpandedSortedRowIndexLookupSelector(apiRef);
const rowTree = gridRowTreeSelector(apiRef);
const sourceNode = gridRowNodeSelector(apiRef, sourceRowId);
const targetNode = gridRowNodeSelector(apiRef, targetRowId);
if (!sourceNode) {
throw new Error(process.env.NODE_ENV !== "production" ? `MUI X Data Grid: No row with id "${sourceRowId}" found. ` + 'The source row for reordering does not exist in the grid. ' + 'Verify the row id is correct and the row exists.' : _formatErrorMessage(128, sourceRowId));
}
if (!targetNode) {
throw new Error(process.env.NODE_ENV !== "production" ? `MUI X Data Grid: No row with id "${targetRowId}" found. ` + 'The target row for reordering does not exist in the grid. ' + 'Verify the row id is correct and the row exists.' : _formatErrorMessage(129, targetRowId));
}
if (sourceNode.type === 'footer') {
throw new Error(process.env.NODE_ENV !== "production" ? 'MUI X Data Grid: Row reordering does not support reordering of footer rows. ' + 'Footer rows are special rows that cannot be moved. ' + 'Only data rows can be reordered.' : _formatErrorMessage(130));
}
// Get the target index from the targetRowId using the lookup selector
const targetIndexUnadjusted = sortedFilteredRowIndexLookup[targetRowId];
if (targetIndexUnadjusted === undefined) {
throw new Error(process.env.NODE_ENV !== "production" ? `MUI X Data Grid: Target row with id "${targetRowId}" not found in current view. ` + 'The target row may be filtered out or not visible. ' + 'Ensure the target row is visible in the current grid view.' : _formatErrorMessage(131, targetRowId));
}
const targetIndex = position === 'below' ? targetIndexUnadjusted + 1 : targetIndexUnadjusted;
const executionContext = {
sourceRowId,
dropPosition: position,
placeholderIndex: targetIndex,
sortedFilteredRowIds,
sortedFilteredRowIndexLookup,
rowTree,
apiRef,
processRowUpdate,
onProcessRowUpdateError,
setTreeDataPath
};
return treeDataReorderExecutor.execute(executionContext);
}, [apiRef, processRowUpdate, onProcessRowUpdateError, setTreeDataPath]);
const setRowIndex = React.useCallback(async () => {
throw new Error(process.env.NODE_ENV !== "production" ? 'MUI X Data Grid: setRowIndex() is not supported for tree data. ' + 'Tree data uses hierarchical positioning which requires setRowPosition(). ' + 'Use setRowPosition() instead to reorder rows in tree data mode.' : _formatErrorMessage(132));
}, []);
return {
setRowIndex: treeData ? setRowIndex : setRowIndexFlat,
setRowPosition: treeData ? setRowPosition : setRowPositionFlat
};
};