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 Table columnOrder resetting incorrectly #1901

Merged
merged 1 commit into from
Mar 6, 2024
Merged

Conversation

mayank99
Copy link
Contributor

@mayank99 mayank99 commented Mar 6, 2024

Changes

Fixes #1899.

The root cause was that the initial value of lastPassedColumns is [] so it was always resetting the columns. [] is never valid so I've added a simple check for it.

Testing

Tested in playground (using linked sandbox) and added unit test.

Docs

N/A

@mayank99 mayank99 self-assigned this Mar 6, 2024
@mayank99 mayank99 requested review from a team as code owners March 6, 2024 17:57
@mayank99 mayank99 requested review from r100-stack and Ben-Pusey-Bentley and removed request for a team March 6, 2024 17:57
@mayank99 mayank99 changed the title fix Table columnOrder resetting incorrectly fix Table columnOrder resetting incorrectly Mar 6, 2024
Comment on lines 718 to 726
React.useEffect(() => {
// Check if columns have changed (by value)
if (JSON.stringify(lastPassedColumns.current) !== JSON.stringify(columns)) {
if (
lastPassedColumns.current.length > 0 &&
JSON.stringify(lastPassedColumns.current) !== JSON.stringify(columns)
) {
instance.setColumnOrder([]);
}
lastPassedColumns.current = columns;
Copy link
Contributor Author

@mayank99 mayank99 Mar 6, 2024

Choose a reason for hiding this comment

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

Some comments for future:

  1. We should try to avoid stringifying columns like this. It can be expensive and error-prone. In our new Table we could require always passing an id, or find a way to compare the internal ids.

  2. We should maybe create a hook for this "previous state" pattern.

    function usePreviousState<T>(state: T, onChange: () => void): React.Ref<T>;

Copy link
Member

Choose a reason for hiding this comment

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

Sounds good, linked this comment in #1144.

@mayank99 mayank99 merged commit 7be06ec into main Mar 6, 2024
15 checks passed
@mayank99 mayank99 deleted the mayank/column-order branch March 6, 2024 18:31
mayank99 added a commit that referenced this pull request Mar 6, 2024
@imodeljs-admin imodeljs-admin mentioned this pull request Mar 6, 2024
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.

Table initial state not setting column order properly.
2 participants