Skip to content

Commit

Permalink
fix rowSelection process
Browse files Browse the repository at this point in the history
  • Loading branch information
rdonigian committed Sep 29, 2024
1 parent f9d4cdb commit 63cb403
Showing 1 changed file with 12 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ import {
GridColDef,
GridEventListener,
GridRenderCellParams,
GridRowId,
GridToolbarColumnsButton,
GridToolbarFilterButton,
} from '@mui/x-data-grid-pro';
import { merge } from 'lodash';
import { useCallback, useEffect, useMemo } from 'react';
import { useMemo, useState } from 'react';
import {
EngagementFilters,
ProgressReportFilters,
Expand Down Expand Up @@ -51,8 +52,7 @@ export const ProgressReportsColumns: Array<
...textColumn(),
width: 200,
valueGetter: (_, row) => row.parent.project.name.value,
renderCell: ({ value, row, api }) => {
console.log(api.isRowSelected(row.id));
renderCell: ({ value, row }) => {
return (
<Box
sx={{
Expand Down Expand Up @@ -264,11 +264,6 @@ export const ProgressReportsExpanded = () => {
end: {
beforeInclusive: currentQuarter.endOf('quarter'),
},
// engagement: {
// project: {
// mine: true,
// },
// },
} satisfies ProgressReportFilters;

const [dataGridProps] = useDataGridSource({
Expand Down Expand Up @@ -296,28 +291,10 @@ export const ProgressReportsExpanded = () => {
[dataGridProps.slotProps]
);

const handleEvent: GridEventListener<'rowClick'> = useCallback(
({ id }, event, details) => {
if (event.defaultPrevented) return;

event.preventDefault();
event.stopPropagation();

const selected = details.api.isRowSelected(id);
details.api.setRowSelectionModel(selected ? [] : [id]);

details.api.resetRowHeights();
},
[]
);

useEffect(() => {
// The `subscribeEvent` method will automatically unsubscribe in the cleanup function of the `useEffect`.
return apiRef.current.subscribeEvent('rowClick', handleEvent);
}, [apiRef, handleEvent]);
const [selected, setSelected] = useState<GridRowId[]>([]);

// apiRef.current.subscribeEvent('rowClick', handleEvent);
// useGridApiEventHandler(apiRef, 'rowClick', handleEvent);
const handleRowClick: GridEventListener<'rowClick'> = ({ id }) =>
selected.length > 0 ? setSelected([]) : setSelected([id]);

return (
<Box
Expand All @@ -335,12 +312,11 @@ export const ProgressReportsExpanded = () => {
columns={ProgressReportsColumns}
hideFooter
autoHeight={false}
onRowClick={handleEvent}
getRowHeight={(params) => {
if (apiRef.current.isRowSelected(params.id)) {
return 'auto';
}
}}
onRowClick={handleRowClick}
rowSelectionModel={selected}
getRowHeight={(params) =>
apiRef.current.isRowSelected(params.id) ? 'auto' : undefined
}
sx={[
{
'&.MuiDataGrid-root .MuiDataGrid-cell': {
Expand Down Expand Up @@ -368,7 +344,7 @@ const ProgressReportsToolbar = () => (
<GridToolbarFilterButton />
<QuickFilters sx={{ flex: 1 }}>
<QuickFilterResetButton />
<QuickFilterButton {...useFilterToggle('engagement.project.isMember')}>
<QuickFilterButton {...useFilterToggle('engagement.project.mine')}>
Mine
</QuickFilterButton>
<QuickFilterButton {...useFilterToggle('engagement.project.pinned')}>
Expand Down

0 comments on commit 63cb403

Please sign in to comment.