UNPKG

@mui/x-data-grid-pro

Version:

The Pro plan edition of the MUI X Data Grid components.

75 lines (70 loc) 3.1 kB
"use strict"; var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard").default; Object.defineProperty(exports, "__esModule", { value: true }); exports.useGridTreeData = void 0; var React = _interopRequireWildcard(require("react")); var _xDataGrid = require("@mui/x-data-grid"); var _internals = require("@mui/x-data-grid/internals"); var _gridTreeDataGroupColDef = require("./gridTreeDataGroupColDef"); var _treeDataReorderValidator = require("./treeDataReorderValidator"); const useGridTreeData = (apiRef, props) => { const handleCellKeyDown = React.useCallback((params, event) => { const cellParams = apiRef.current.getCellParams(params.id, params.field); if (cellParams.colDef.field === _gridTreeDataGroupColDef.GRID_TREE_DATA_GROUPING_FIELD && (event.key === ' ' || event.key === 'Enter') && !event.shiftKey) { if (params.rowNode.type !== 'group') { return; } if (props.dataSource && !params.rowNode.childrenExpanded) { apiRef.current.dataSource.fetchRows(params.id); return; } apiRef.current.setRowChildrenExpansion(params.id, !params.rowNode.childrenExpanded); } }, [apiRef, props.dataSource]); const isValidRowReorderProp = props.isValidRowReorder; const isRowReorderValid = React.useCallback((initialValue, { sourceRowId, targetRowId, dropPosition, dragDirection }) => { if ((0, _xDataGrid.gridRowMaximumTreeDepthSelector)(apiRef) === 1 || !props.treeData) { return initialValue; } const expandedSortedRowIndexLookup = (0, _xDataGrid.gridExpandedSortedRowIndexLookupSelector)(apiRef); const expandedSortedRowIds = (0, _xDataGrid.gridExpandedSortedRowIdsSelector)(apiRef); const rowTree = (0, _xDataGrid.gridRowTreeSelector)(apiRef); const targetRowIndex = expandedSortedRowIndexLookup[targetRowId]; const sourceNode = rowTree[sourceRowId]; const targetNode = rowTree[targetRowId]; const prevNode = targetRowIndex > 0 ? rowTree[expandedSortedRowIds[targetRowIndex - 1]] : null; const nextNode = targetRowIndex < expandedSortedRowIds.length - 1 ? rowTree[expandedSortedRowIds[targetRowIndex + 1]] : null; // Basic validity checks if (!sourceNode || !targetNode) { return false; } // Create context object const context = { apiRef, sourceNode, targetNode, prevNode, nextNode, dropPosition, dragDirection }; // First apply internal validation let isValid = _treeDataReorderValidator.treeDataReorderValidator.validate(context); // If internal validation passes AND user provided additional validation if (isValid && isValidRowReorderProp) { // Apply additional user restrictions isValid = isValidRowReorderProp(context); } return isValid; }, [apiRef, props.treeData, isValidRowReorderProp]); (0, _internals.useGridRegisterPipeProcessor)(apiRef, 'isRowReorderValid', isRowReorderValid); (0, _xDataGrid.useGridEvent)(apiRef, 'cellKeyDown', handleCellKeyDown); }; exports.useGridTreeData = useGridTreeData;