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

Uxd 1984 Data Table updates on data change #1233

Merged
merged 9 commits into from
Feb 23, 2022

Conversation

tristanjasper
Copy link
Contributor

@tristanjasper tristanjasper commented Feb 22, 2022

Purpose 🚀

Currently when the source data prop of a table changes:
a) A dataTable without the Infinite loader sub component will not show a vertical scrollbar
b) A dataTable with the Infinite loader will not evaluate the correct row heights, making long text strings get cut off.

This pr is a proposal to have the dataTable update when data changes and

  1. Re-evaluate the row heights based on row data changes, leading to long strings being cut off.
  2. Resize the outer container div meaning additional rows will not be hidden

This pr ensures that DataTable resizes and re-renders correctly when the source data is changed.

Note
Additionally discovered a bug which i'll create an issue for-
On the Data change story
Click "Add Item" button three times
Then click "Save"
The DataTable with InfiniteLoader renders with a vertical scrollbar even though it is below the maxHeight value.

Notes ✏️

details of code change / secondary purposes of this PR

Updates 📦

If you have changed a component's source code (not stories, specs, or docs), before merging your branch run yarn changeset. This will prompt you to:

  • indicate if changes were patch/minor/major for each modified package
  • enter a release message

Storybook 📕

http://storybooks.highbond-s3.com/paprika/your-branch-name

Screenshots 📸

optional but highly recommended

References 🔗

relevant Jira ticket / GitHub issues

@changeset-bot
Copy link

changeset-bot bot commented Feb 22, 2022

🦋 Changeset detected

Latest commit: 875d358

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@paprika/data-table Minor

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@tristanjasper tristanjasper changed the title Uxd 1984 Data Table updates on data change Uxd 1984 Data Table updates on data change - WIP Feb 22, 2022
width: ${`${width}px`};
${fontSize()}

overflow: ${hasInfiniteLoader ? `hidden;` : `auto;`}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

ensures the vertical scroll bar is showing when data changes and rendering without the hasInfiniteLoader sub component

const clearRowHeights = React.useCallback(
(indexes: number[]) => indexes.forEach(index => delete rowHeights.current[index]),
[]
);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

only re-evaluating the data row heights for rows that have changed

}
}
prevData.current = data;
isLoadingMoreItemsRef.current = false;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ensuring there are not unnecessary calculations being done when additional rows are being added by using this flag when necessary

if (changedIndexes.length > 0) {
clearRowHeights(changedIndexes);
listRef.current.resetAfterIndex(changedIndexes[0]);
setTimeout(resetDimension, 0);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Finding i've had to use setTimeout here to ensure the resize gets applied after the react table has finished resetting, I don't see a way to avoid it

@tristanjasper tristanjasper removed the WIP label Feb 23, 2022
@tristanjasper tristanjasper changed the title Uxd 1984 Data Table updates on data change - WIP Uxd 1984 Data Table updates on data change Feb 23, 2022
@tristanjasper tristanjasper marked this pull request as ready for review February 23, 2022 20:13
listRef.current.resetAfterIndex(changedIndexes[0]);
setTimeout(resetDimension, 0);
} else if (data.length !== prevData.current.length) {
resetDimension();
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Originally I only call this if the height of the table is less that then maxHeight (as that is when it should only need to be called), however I discovered a bug (detailed in description) where the height of the react-table isn't always evaluated the same how is is rendered (Issue will be added)

@@ -72,5 +72,5 @@ export default function useBestTableDimensions({
resize: resetDimension,
}));

return dimensions;
return { dimensions, resetDimension };
Copy link
Contributor

Choose a reason for hiding this comment

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

Since we're adding exports here, maybe we can move shouldResizeWithViewport out, so like { dimensions, resetDimension, shouldResizeWithViewport }

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Well the shouldResizeWithViewport prop value is passed into this hook in dataTable.tsx. I'm not sure why we would want to expose it here as it is already available in that component. Can you expand?

isItemLoaded={index => items[index] !== undefined}
loadMoreItems={async () => {
console.log("callign loadmore items");
// do nothing
Copy link
Contributor

Choose a reason for hiding this comment

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

Loading more items is kind of a data change event as well, can we enable it here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Well this story is about the data data object changing outside of the dataComponent. The loadMoreItems call is only triggered when the user scrolls down the table and a call to get new items can be made so isn't quite the same scenario, so I'm not sure there would be value adapting this.

if (!isEqual(row, data[index])) {
changedIndexes.push(index);
}
});
Copy link
Contributor

@allison-c allison-c Feb 23, 2022

Choose a reason for hiding this comment

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

I just feel these calculations are quite expensive, e.g. the data from the data[index] might not always be useful. Some columns might be hidden, but the data for those columns are still in the data object passed into DataTable.

Another example is, let's say only the data of row number 2 is changed, but since we have 200 rows, we still need to check if the data for row number 3 - row number 200 have been changed

So I'd still prefer using useImperativeHandle to let the consumer decide which indexes they updated, or from which index the data are newly updated. Open for discussions!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, It is expensive but my thoughts are if we don't do it here we will still need to evaluate which indexes have changed in the consumer. I would have thought letting the dataTable deal with that would be easier for developers using it.

@tristanjasper
Copy link
Contributor Author

Added an issue for the height bug as mentioned in the description here

@tristanjasper tristanjasper merged commit d3c2e23 into master Feb 23, 2022
@tristanjasper tristanjasper deleted the uxd-1984-data-table-change-on-data branch February 23, 2022 22:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants