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

Table data assignment bug #2300

Merged
merged 7 commits into from
Jul 31, 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "patch",
"comment": "Fix bug in nimble-table to avoid requiring a full copy of the data to be made when hierarchy is enabled",
"packageName": "@ni/nimble-components",
"email": "20542556+mollykreis@users.noreply.github.com",
"dependentChangeType": "patch"
}
5 changes: 4 additions & 1 deletion packages/nimble-components/src/table/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,10 @@ export class Table<

public async setData(newData: readonly TData[]): Promise<void> {
await this.processPendingUpdates();
const tanstackUpdates = this.calculateTanStackData(newData);
// Make a shallow clone of the record to avoid holding a reference to the client's data object.
// This also ensures that a data update will always result in a row's record being a new object.
const clonedData = newData.map(record => ({ ...record }));
const tanstackUpdates = this.calculateTanStackData(clonedData);
this.updateTableOptions(tanstackUpdates);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,15 @@ export class DataHierarchyManager<TData extends TableRecord> {
} catch {
this.isDataFlat = true;
this._hierarchicalData = records.map((record, index) => ({
clientRecord: { ...record },
clientRecord: record,
originalIndex: index
}));
this._parentIdConfigurationValid = false;
}
} else {
this.isDataFlat = true;
this._hierarchicalData = records.map((record, index) => ({
clientRecord: { ...record },
clientRecord: record,
originalIndex: index
}));
this._parentIdConfigurationValid = true;
Expand Down
28 changes: 27 additions & 1 deletion packages/nimble-components/src/table/tests/table.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,7 @@ describe('Table', () => {
verifyRenderedData(simpleTableData);
});

it('can update a record without making a copy of the data', async () => {
it('can update a record without making a copy of the data without hierarchy enabled', async () => {
await connect();
await waitForUpdatesAsync();

Expand Down Expand Up @@ -965,6 +965,32 @@ describe('Table', () => {
parentId3: 'Parent 2'
}
];

it('can update a record without making a copy of the data with hierarchy enabled', async () => {
element.idFieldName = 'id';
element.parentIdFieldName = 'parentId';
await connect();
await waitForUpdatesAsync();

const data: SimpleTableRecord[] = hierarchicalData.map(x => ({
...x
}));
await element.setData(data);
await waitForUpdatesAsync();
const currentFieldValue = data[0]!.stringData;
expect(pageObject.getRenderedCellTextContent(0, 0)).toEqual(
currentFieldValue
);

const updatedFieldValue = `${currentFieldValue} - updated value`;
data[0]!.stringData = updatedFieldValue;
await element.setData(data);
await waitForUpdatesAsync();
expect(pageObject.getRenderedCellTextContent(0, 0)).toEqual(
updatedFieldValue
);
});

it('shows collapse all button with hierarchical data', async () => {
await connect();
element.idFieldName = 'id';
Expand Down