Skip to content

Commit

Permalink
[EuiDataGrid] Memoization & perf improvements (#7556)
Browse files Browse the repository at this point in the history
Co-authored-by: Kevin Qualters <kevin.qualters@elastic.co>
  • Loading branch information
cee-chen and kqualters-elastic committed Mar 12, 2024
1 parent f9ff62e commit 88911d7
Show file tree
Hide file tree
Showing 45 changed files with 2,505 additions and 2,567 deletions.
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
6 changes: 5 additions & 1 deletion src-docs/src/views/datagrid/toolbar/additional_controls.tsx
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

0 comments on commit 88911d7

Please sign in to comment.