@mui/x-data-grid-pro
Version:
The Pro plan edition of the MUI X Data Grid components.
22 lines • 922 B
JavaScript
import * as React from 'react';
import { useGridEvent } from '@mui/x-data-grid';
import { GRID_TREE_DATA_GROUPING_FIELD } from "./gridTreeDataGroupColDef.js";
export const useGridTreeData = (apiRef, props) => {
/**
* EVENTS
*/
const handleCellKeyDown = React.useCallback((params, event) => {
const cellParams = apiRef.current.getCellParams(params.id, params.field);
if (cellParams.colDef.field === 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]);
useGridEvent(apiRef, 'cellKeyDown', handleCellKeyDown);
};