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

feat(Datagrid): add support for dynamic sub rows #5735

Conversation

matthewgallo
Copy link
Member

@matthewgallo matthewgallo commented Jul 26, 2024

Closes #5600

This PR adds support for dynamic sub rows/nested rows. To use this functionality, provide getAsyncSubRows to the useDatagrid hook and resolve a promise upon receiving/fetching sub rows and updating your data.

const datagridState = useDatagrid(
    {
      columns,
      data,
      getAsyncSubRows: async (row) => {
        // ONLY update/simulate fetch of new sub rows
        // if the current row doesn't already have any
        if (row.subRows.length) return;
        await new Promise((resolve) => {
          setTimeout(() => {
            // For the sake of this example, we will show
            // a skeleton for subRows fetching dynamic subRows
            // but won't update data with anything
            if (row.depth > 0) {
              return resolve();
            }
            const newRows = makeData(2, { id: 'test-id' });
            const clonedData = [...data];
            const rowIndexToUpdate = clonedData.findIndex(
              (r) => r.id === row.original.id
            );
            clonedData[rowIndexToUpdate].subRows = newRows;
            setData(clonedData);
            resolve();
          }, 1000);
        });
      },
    },
    useNestedRows
  );

What did you change?

packages/ibm-products-styles/src/components/Datagrid/styles/_useNestedRows.scss
packages/ibm-products/src/components/Datagrid/Datagrid/DatagridRow.tsx
packages/ibm-products/src/components/Datagrid/Datagrid/addons/stateReducer.js
packages/ibm-products/src/components/Datagrid/Extensions/NestedRows/NestedRows.stories.jsx
packages/ibm-products/src/components/Datagrid/types/index.ts
packages/ibm-products/src/components/Datagrid/useNestedRowExpander.js
packages/ibm-products/src/components/Datagrid/useRowRenderer.js
packages/ibm-products/src/components/Datagrid/utils/makeData.js

How did you test and verify your work?

Storybook

Copy link

netlify bot commented Jul 26, 2024

Deploy Preview for carbon-for-ibm-products ready!

Name Link
🔨 Latest commit fddb86f
🔍 Latest deploy log https://app.netlify.com/sites/carbon-for-ibm-products/deploys/66a3c59d9bd277000894c142
😎 Deploy Preview https://deploy-preview-5735--carbon-for-ibm-products.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

@matthewgallo matthewgallo marked this pull request as ready for review July 26, 2024 18:51
@matthewgallo matthewgallo requested a review from a team as a code owner July 26, 2024 18:51
@matthewgallo matthewgallo requested review from kennylam and ariellalgilmore and removed request for a team July 26, 2024 18:51
Copy link
Contributor

@devadula-nandan devadula-nandan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@ariellalgilmore ariellalgilmore added this pull request to the merge queue Aug 9, 2024
Merged via the queue into carbon-design-system:main with commit b5d7e1f Aug 9, 2024
27 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Dynamic loading of child nodes in the datagrid with selectableNestedRows
3 participants