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
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
5 changes: 5 additions & 0 deletions .changeset/sharp-gorillas-search.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@itwin/itwinui-react": patch
---

Fixed a bug in `Table` where `initialState.columnOrder` was not being respected.
23 changes: 23 additions & 0 deletions packages/itwinui-react/src/core/Table/Table.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3685,6 +3685,29 @@ it.each([
},
);

it('should respect initialState.columnOrder', () => {
const mockColumns = columns();
const columnOrder = ['description', 'view', 'name'];

const { container } = render(
<Table
columns={mockColumns}
data={mockedData()}
emptyTableContent='Empty table'
initialState={{ columnOrder }}
/>,
);

// DOM order should match columnOrder
container
.querySelectorAll<HTMLDivElement>('[role=columnheader]')
.forEach((cell, index) =>
expect(cell.textContent).toBe(
mockColumns.find((c) => c.id === columnOrder[index])?.Header,
),
);
});

it('should not have `draggable` attribute on columns with `disableReordering` enabled', () => {
const columns: Column<TestDataType>[] = [
{
Expand Down
5 changes: 4 additions & 1 deletion packages/itwinui-react/src/core/Table/Table.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -717,7 +717,10 @@ export const Table = <
// This is to avoid the old columnOrder from affecting the new columns' columnOrder
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;
Comment on lines 718 to 726
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.

Expand Down
Loading