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

fix(datagrid): add useMemo() calls #5949

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
92 changes: 49 additions & 43 deletions packages/ibm-products/src/components/Datagrid/useRowRenderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,56 +4,62 @@
* This source code is licensed under the Apache-2.0 license found in the
* LICENSE file in the root directory of this source tree.
*/

import { useMemo } from 'react';
import DatagridRow from './Datagrid/DatagridRow';

const useRowRenderer = (hooks) => {
const useInstance = (instance) => {
const { rows, state } = instance;

let newRows = null;
if (state?.dynamicRowSkeleton) {
const depthIndexArray = state?.dynamicRowSkeleton?.id
.split('')
.filter((r) => r !== '.');
if (depthIndexArray.length > 1) {
let subRows;
let finalRow;
// This will find the subRow that requested to find dynamic rows
// and will apply the skeleton row after that row
depthIndexArray.map((depthIndex, index) => {
if (index === 0) {
subRows = rows[depthIndex].subRows;
return;
}
if (depthIndexArray.length - 1 === index) {
finalRow = subRows.find((r) => r.index === parseInt(depthIndex));
subRows.splice(finalRow.index + 1, 0, state?.dynamicRowSkeleton); // + 1 because we want to add the skeleton row after the row initiating the dynamic request
return;
}
const foundRow = subRows.find(
(r) => r.index === parseInt(depthIndex)
);
subRows = foundRow.subRows;
});
}
const rootRowIndexWithDynamicRow = rows.findIndex(
(r) => r.id === state?.dynamicRowSkeleton?.id
);
if (rootRowIndexWithDynamicRow > -1) {
newRows = [
...rows.slice(0, rootRowIndexWithDynamicRow + 1), // + 1 because we want to add the skeleton row after the row initiating the dynamic request
state?.dynamicRowSkeleton,
...rows.slice(rootRowIndexWithDynamicRow + 1),
];
const rowsWithRenderer = useMemo(() => {
let newRows = null;
if (state?.dynamicRowSkeleton) {
const depthIndexArray = state?.dynamicRowSkeleton?.id
.split('')
.filter((r) => r !== '.');
if (depthIndexArray.length > 1) {
let subRows;
let finalRow;
// This will find the subRow that requested to find dynamic rows
// and will apply the skeleton row after that row
depthIndexArray.map((depthIndex, index) => {
if (index === 0) {
subRows = rows[depthIndex].subRows;
return;
}
if (depthIndexArray.length - 1 === index) {
finalRow = subRows.find((r) => r.index === parseInt(depthIndex));
subRows.splice(finalRow.index + 1, 0, state?.dynamicRowSkeleton); // + 1 because we want to add the skeleton row after the row initiating the dynamic request
return;
}
const foundRow = subRows.find(
(r) => r.index === parseInt(depthIndex)
);
subRows = foundRow.subRows;
});
}
const rootRowIndexWithDynamicRow = rows.findIndex(
(r) => r.id === state?.dynamicRowSkeleton?.id
);
if (rootRowIndexWithDynamicRow > -1) {
newRows = [
...rows.slice(0, rootRowIndexWithDynamicRow + 1), // + 1 because we want to add the skeleton row after the row initiating the dynamic request
state?.dynamicRowSkeleton,
...rows.slice(rootRowIndexWithDynamicRow + 1),
];
}
}
}
newRows = newRows ?? rows;
const addRowRenderer = (row) =>
Object.assign(row, {
RowRenderer: DatagridRow,
subRows: (row.subRows || []).map(addRowRenderer),
});
const rowsWithRenderer = newRows.map(addRowRenderer);
newRows = newRows ?? rows;

const addRowRenderer = (row) =>
Object.assign(row, {
RowRenderer: DatagridRow,
subRows: (row.subRows || []).map(addRowRenderer),
});
return newRows.map(addRowRenderer);
}, [rows, state]);

Object.assign(instance, { rows: rowsWithRenderer });
};
hooks.useInstance.push(useInstance);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* LICENSE file in the root directory of this source tree.
*/

import React, { useLayoutEffect, useState } from 'react';
import React, { useLayoutEffect, useMemo, useState } from 'react';
import cx from 'classnames';
import { TableSelectRow } from '@carbon/react';
import { SelectAll } from './Datagrid/DatagridSelectAll';
Expand All @@ -22,7 +22,10 @@ const useSelectRows = (hooks: Hooks) => {
useHighlightSelection(hooks);
const useInstance = (instance: TableInstance) => {
const { rows } = instance;
const rowsWithSelect = rows.map((row) => ({ ...row, isSelectable: true }));
const rowsWithSelect = useMemo(
() => rows.map((row) => ({ ...row, isSelectable: true })),
[rows]
);
Object.assign(instance, { rows: rowsWithSelect });
};
hooks.useInstance.push(useInstance);
Expand Down
Loading