Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[EuiDataGrid] Memoization & perf improvements #7556

Merged
merged 20 commits into from
Mar 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
7b8bd12
[docs] Pull out all inline `renderCellValue` functions/components to …
kqualters-elastic Mar 7, 2024
871ff63
[tests] Pull out inline `renderCellValue` fns/components to static de…
kqualters-elastic Mar 7, 2024
293bf9c
Fix `useMemo` dependency arrays calling `Object.values(props)`
kqualters-elastic Mar 7, 2024
e6888c5
Add basic `useMemo` usages
kqualters-elastic Mar 7, 2024
eea719d
Add more complex `useMemo` usages
kqualters-elastic Mar 7, 2024
4ed410a
Add basic `useCallback` usages
kqualters-elastic Mar 7, 2024
e1bdcb2
Basic `memo()` wrapper around data grid subcomponents
kqualters-elastic Mar 7, 2024
48d0220
Memoize `CellWrapper` + rename it to match filename
kqualters-elastic Mar 7, 2024
30efd08
Cells - replace inline JSX with static component instead
kqualters-elastic Mar 7, 2024
aeb74d6
Merge branch 'main' into datagrid/perf
cee-chen Mar 8, 2024
9c7d2d6
Memoize focusContext
kqualters-elastic Mar 8, 2024
1f00528
Memoize `useCellPopover` further
kqualters-elastic Mar 8, 2024
7c18fe6
Memoize column width hook utilities
kqualters-elastic Mar 8, 2024
3696a75
Add new `useDeepEqual` hook service + update style overrides from `re…
kqualters-elastic Mar 8, 2024
0dbd5bb
Further memoize sorting utils
kqualters-elastic Mar 8, 2024
b1ea642
EuiDataGrid cell memoizations/cleanup
kqualters-elastic Mar 8, 2024
6982d58
Fix `cellContext` no longer triggering rerenders
cee-chen Mar 8, 2024
2527be6
Remove unnecessary anonymous function
cee-chen Mar 11, 2024
cd2dfb8
[PR feedback] Remove `sorting` prop from context
cee-chen Mar 11, 2024
8f01367
Memoize `useDataGridColumnSorting`
kqualters-elastic Mar 12, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 32 additions & 29 deletions src-docs/src/views/datagrid/advanced/custom_renderer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
EuiDataGridPaginationProps,
EuiDataGridSorting,
EuiDataGridColumnSortingConfig,
RenderCellValue,
} from '../../../../../src';

const raw_data: Array<{ [key: string]: string }> = [];
Expand Down Expand Up @@ -67,6 +68,14 @@ const columns = [
},
];

const checkboxRowCellRender: RenderCellValue = ({ rowIndex }) => (
<EuiCheckbox
id={`select-row-${rowIndex}`}
aria-label="Select row"
onChange={() => {}}
/>
);

const leadingControlColumns: EuiDataGridProps['leadingControlColumns'] = [
{
id: 'selection',
Expand All @@ -78,13 +87,7 @@ const leadingControlColumns: EuiDataGridProps['leadingControlColumns'] = [
onChange={() => {}}
/>
),
rowCellRender: ({ rowIndex }) => (
<EuiCheckbox
id={`select-row-${rowIndex}`}
aria-label="Select row"
onChange={() => {}}
/>
),
rowCellRender: checkboxRowCellRender,
},
];

Expand All @@ -103,6 +106,22 @@ const trailingControlColumns: EuiDataGridProps['trailingControlColumns'] = [
},
];

const RowCellRender: RenderCellValue = ({ setCellProps, rowIndex }) => {
setCellProps({ style: { width: '100%', height: 'auto' } });

const firstName = raw_data[rowIndex].name.split(', ')[1];
const isGood = faker.datatype.boolean();
return (
<>
{firstName}&apos;s account has {isGood ? 'no' : ''} outstanding fees.{' '}
<EuiIcon
type={isGood ? 'checkInCircleFilled' : 'error'}
color={isGood ? 'success' : 'danger'}
/>
</>
);
};

// The custom row details is actually a trailing control column cell with
// a hidden header. This is important for accessibility and markup reasons
// @see https://fuschia-stretch.glitch.me/ for more
Expand All @@ -120,21 +139,7 @@ const rowDetails: EuiDataGridProps['trailingControlColumns'] = [

// When rendering this custom cell, we'll want to override
// the automatic width/heights calculated by EuiDataGrid
rowCellRender: ({ setCellProps, rowIndex }) => {
setCellProps({ style: { width: '100%', height: 'auto' } });

const firstName = raw_data[rowIndex].name.split(', ')[1];
const isGood = faker.datatype.boolean();
return (
<>
{firstName}&apos;s account has {isGood ? 'no' : ''} outstanding fees.{' '}
<EuiIcon
type={isGood ? 'checkInCircleFilled' : 'error'}
color={isGood ? 'success' : 'danger'}
/>
</>
);
},
rowCellRender: RowCellRender,
},
];

Expand All @@ -144,10 +149,10 @@ const footerCellValues: { [key: string]: string } = {
.toLocaleString('en-US', { style: 'currency', currency: 'USD' })}`,
};

const RenderFooterCellValue: EuiDataGridProps['renderFooterCellValue'] = ({
columnId,
setCellProps,
}) => {
const renderCellValue: RenderCellValue = ({ rowIndex, columnId }) =>
raw_data[rowIndex][columnId];

const RenderFooterCellValue: RenderCellValue = ({ columnId, setCellProps }) => {
const value = footerCellValues[columnId];

useEffect(() => {
Expand Down Expand Up @@ -298,9 +303,7 @@ export default () => {
onChangeItemsPerPage: onChangePageSize,
}}
rowCount={raw_data.length}
renderCellValue={({ rowIndex, columnId }) =>
raw_data[rowIndex][columnId]
}
renderCellValue={renderCellValue}
renderFooterCellValue={RenderFooterCellValue}
renderCustomGridBody={RenderCustomGridBody}
height={autoHeight ? undefined : 400}
Expand Down
8 changes: 5 additions & 3 deletions src-docs/src/views/datagrid/advanced/ref.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
EuiDataGridColumnSortingConfig,
EuiDataGridPaginationProps,
EuiDataGridSorting,
RenderCellValue,
} from '../../../../../src';

const raw_data: Array<{ [key: string]: string }> = [];
Expand All @@ -33,6 +34,9 @@ for (let i = 1; i < 100; i++) {
});
}

const renderCellValue: RenderCellValue = ({ rowIndex, columnId }) =>
raw_data[rowIndex][columnId];

export default () => {
const dataGridRef = useRef<EuiDataGridRefProps | null>(null);

Expand Down Expand Up @@ -219,9 +223,7 @@ export default () => {
sorting={{ columns: sortingColumns, onSort }}
inMemory={{ level: 'sorting' }}
rowCount={raw_data.length}
renderCellValue={({ rowIndex, columnId }) =>
raw_data[rowIndex][columnId]
}
renderCellValue={renderCellValue}
pagination={{
...pagination,
onChangePage: onChangePage,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
EuiDataGrid,
EuiDataGridColumnCellAction,
EuiDataGridColumn,
RenderCellValue as RenderCellValueType,
} from '../../../../../src';

const cellActions: EuiDataGridColumnCellAction[] = [
Expand Down Expand Up @@ -50,6 +51,22 @@ for (let i = 1; i < 5; i++) {
});
}

const RenderCellValue: RenderCellValueType = ({
rowIndex,
columnId,
setCellProps,
}) => {
const value = data[rowIndex][columnId];

useEffect(() => {
if (columnId === 'boolean' && value === 'false') {
setCellProps({ isExpandable: false });
}
}, [columnId, value, setCellProps]);

return value;
};

export default () => {
const [visibleColumns, setVisibleColumns] = useState(
columns.map(({ id }) => id)
Expand All @@ -61,17 +78,7 @@ export default () => {
columns={columns}
columnVisibility={{ visibleColumns, setVisibleColumns }}
rowCount={data.length}
renderCellValue={({ rowIndex, columnId, setCellProps }) => {
const value = data[rowIndex][columnId];

useEffect(() => {
if (columnId === 'boolean' && value === 'false') {
setCellProps({ isExpandable: false });
}
}, [columnId, value, setCellProps]);

return value;
}}
renderCellValue={RenderCellValue}
/>
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
EuiCopy,
EuiText,
EuiImage,
RenderCellValue as RenderCellValueType,
} from '../../../../../src';

const cellActions: EuiDataGridColumnCellAction[] = [
Expand Down Expand Up @@ -163,6 +164,9 @@ const RenderCellPopover = (props: EuiDataGridCellPopoverElementProps) => {
);
};

const renderCellValue: RenderCellValueType = ({ rowIndex, columnId }) =>
data[rowIndex][columnId];

export default () => {
const [visibleColumns, setVisibleColumns] = useState(
columns.map(({ id }) => id)
Expand All @@ -174,7 +178,7 @@ export default () => {
columns={columns}
columnVisibility={{ visibleColumns, setVisibleColumns }}
rowCount={data.length}
renderCellValue={({ rowIndex, columnId }) => data[rowIndex][columnId]}
renderCellValue={renderCellValue}
renderCellPopover={RenderCellPopover}
/>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
EuiDataGrid,
EuiDataGridColumnCellAction,
EuiDataGridColumn,
RenderCellValue as RenderCellValueType,
} from '../../../../../src/components';

const cellActions1: EuiDataGridColumnCellAction[] = [
Expand Down Expand Up @@ -79,6 +80,9 @@ for (let i = 1; i < 5; i++) {
});
}

const renderCellValue: RenderCellValueType = ({ rowIndex, columnId }) =>
data[rowIndex][columnId];

export default () => {
const [visibleColumns, setVisibleColumns] = useState(
columns.map(({ id }) => id)
Expand All @@ -90,7 +94,7 @@ export default () => {
columns={columns}
columnVisibility={{ visibleColumns, setVisibleColumns }}
rowCount={data.length}
renderCellValue={({ rowIndex, columnId }) => data[rowIndex][columnId]}
renderCellValue={renderCellValue}
/>
);
};
4 changes: 2 additions & 2 deletions src-docs/src/views/datagrid/styling/row_height_auto.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import githubData from '../_row_auto_height_data.json';

import {
EuiDataGrid,
EuiDataGridProps,
RenderCellValue as RenderCellValueType,
EuiLink,
EuiAvatar,
EuiBadge,
Expand Down Expand Up @@ -68,7 +68,7 @@ const columns = [
// instead of loading up front, generate entries on the fly
const raw_data: DataShape[] = githubData;

const RenderCellValue: EuiDataGridProps['renderCellValue'] = ({
const RenderCellValue: RenderCellValueType = ({
rowIndex,
columnId,
isDetails,
Expand Down
4 changes: 2 additions & 2 deletions src-docs/src/views/datagrid/styling/row_height_fixed.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import githubData from '../_row_auto_height_data.json';

import {
EuiDataGrid,
EuiDataGridProps,
RenderCellValue as RenderCellValueType,
EuiLink,
EuiAvatar,
EuiBadge,
Expand Down Expand Up @@ -68,7 +68,7 @@ const columns = [
// instead of loading up front, generate entries on the fly
const raw_data: DataShape[] = githubData;

const RenderCellValue: EuiDataGridProps['renderCellValue'] = ({
const RenderCellValue: RenderCellValueType = ({
rowIndex,
columnId,
isDetails,
Expand Down
4 changes: 2 additions & 2 deletions src-docs/src/views/datagrid/styling/row_line_height.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
EuiDataGrid,
EuiDataGridColumnSortingConfig,
EuiDataGridPaginationProps,
EuiDataGridProps,
RenderCellValue as RenderCellValueType,
EuiDataGridSorting,
formatDate,
} from '../../../../../src';
Expand Down Expand Up @@ -62,7 +62,7 @@ const columns = [
// instead of loading up front, generate entries on the fly
const raw_data: DataShape[] = githubData;

const RenderCellValue: EuiDataGridProps['renderCellValue'] = ({
const RenderCellValue: RenderCellValueType = ({
rowIndex,
columnId,
isDetails,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
EuiContextMenuPanel,
EuiPopover,
EuiDataGridPaginationProps,
RenderCellValue,
} from '../../../../../src';

const columns = [
Expand Down Expand Up @@ -50,6 +51,9 @@ for (let i = 1; i < 20; i++) {
});
}

const renderCellValue: RenderCellValue = ({ rowIndex, columnId }) =>
data[rowIndex][columnId];

export default () => {
const [pagination, setPagination] = useState({ pageIndex: 0 });
const [isFlyoutVisible, setIsFlyoutVisible] = useState(false);
Expand Down Expand Up @@ -127,7 +131,7 @@ export default () => {
border: 'horizontal',
header: 'underline',
}}
renderCellValue={({ rowIndex, columnId }) => data[rowIndex][columnId]}
renderCellValue={renderCellValue}
pagination={{
...pagination,
onChangeItemsPerPage: setPageSize,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
EuiFlexGroup,
EuiFlexItem,
euiScreenReaderOnly,
RenderCellValue,
} from '../../../../../src';

const raw_data: Array<{ [key: string]: string }> = [];
Expand Down Expand Up @@ -82,6 +83,9 @@ const renderCustomToolbar: EuiDataGridToolbarProps['renderCustomToolbar'] = ({
);
};

const renderCellValue: RenderCellValue = ({ rowIndex, columnId }) =>
raw_data[rowIndex][columnId];

// Some additional custom settings to show in the Display popover
const AdditionalDisplaySettings = () => {
const [exampleSettingValue, setExampleSettingValue] = useState<number>(10);
Expand Down Expand Up @@ -126,7 +130,7 @@ export default () => {
columnVisibility={{ visibleColumns, setVisibleColumns }}
sorting={{ columns: sortingColumns, onSort }}
rowCount={raw_data.length}
renderCellValue={({ rowIndex, columnId }) => raw_data[rowIndex][columnId]}
renderCellValue={renderCellValue}
gridStyle={{ border: 'none', header: 'underline' }}
renderCustomToolbar={renderCustomToolbar}
toolbarVisibility={{
Expand Down
4 changes: 4 additions & 0 deletions src/components/datagrid/body/cell/data_grid_cell.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,10 @@ describe('EuiDataGridCell', () => {
style: { top: 0, left: 0, width: 50, height: 10 },
});
});
it('cellContext', () => {
component.setProps({ cellContext: { someData: true } });
component.setProps({ cellContext: { someData: false } });
});
});

describe('when state changes:', () => {
Expand Down
Loading
Loading