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(data-table): do not overwrite row.cells property #1336

Merged
merged 3 commits into from
Jun 5, 2022
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
36 changes: 21 additions & 15 deletions src/DataTable/DataTable.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -146,11 +146,13 @@
.map(({ key }, i) => ({ key, id: key }))
.reduce((a, c) => ({ ...a, [c.key]: c.id }), {})
);
const resolvePath = (object, path) =>
path
const resolvePath = (object, path) => {
if (path in object) return object[path];
return path
.split(/[\.\[\]\'\"]/)
.filter((p) => p)
.reduce((o, p) => (o && typeof o === "object" ? o[p] : o), object);
};

setContext("DataTable", {
sortHeader,
Expand Down Expand Up @@ -195,14 +197,18 @@
$: if (radio || batchSelection) selectable = true;
$: tableSortable.set(sortable);
$: headerKeys = headers.map(({ key }) => key);
$: $tableRows = rows.map((row) => ({
...row,
cells: headerKeys.map((key, index) => ({
key,
value: resolvePath(row, key),
display: headers[index].display,
})),
}));
$: tableCellsByRowId = rows.reduce(
(rows, row) => ({
...rows,
[row.id]: headerKeys.map((key, index) => ({
key,
value: resolvePath(row, key),
display: headers[index].display,
})),
}),
{}
);
$: $tableRows = rows;
$: sortedRows = [...$tableRows];
$: ascending = $sortHeader.sortDirection === "ascending";
$: sortKey = $sortHeader.key;
Expand All @@ -213,11 +219,11 @@
} else {
sortedRows = [...$tableRows].sort((a, b) => {
const itemA = ascending
? resolvePath(a, sortKey, "")
: resolvePath(b, sortKey, "");
? resolvePath(a, sortKey)
: resolvePath(b, sortKey);
const itemB = ascending
? resolvePath(b, sortKey, "")
: resolvePath(a, sortKey, "");
? resolvePath(b, sortKey)
: resolvePath(a, sortKey);

if ($sortHeader.sort) return $sortHeader.sort(itemA, itemB);

Expand Down Expand Up @@ -464,7 +470,7 @@
{/if}
</td>
{/if}
{#each row.cells as cell, j (cell.key)}
{#each tableCellsByRowId[row.id] as cell, j (cell.key)}
{#if headers[j].empty}
<td class:bx--table-column-menu="{headers[j].columnMenu}">
<slot
Expand Down
2 changes: 1 addition & 1 deletion src/DataTable/ToolbarSearch.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
if (shouldFilterRows === true) {
rows = rows.filter((row) => {
return Object.entries(row)
.filter(([key]) => !["cells", "id"].includes(key))
.filter(([key]) => key !== "id")
.some(([key, _value]) => {
if (typeof _value === "string" || typeof _value === "number") {
return (_value + "")
Expand Down