mirror of
https://github.com/carbon-design-system/carbon-components-svelte.git
synced 2025-09-15 10:21:05 +00:00
fix(data-table): do not overwrite row.cells
property (#1336)
* refactor(data-table): remove unneeded third argument from resolvePath call * fix(data-table): do not overwrite `cells` property (#667) * perf(data-table): early return if path in object when resolving path
This commit is contained in:
parent
260bf4e040
commit
d2cdb8eb0f
2 changed files with 22 additions and 16 deletions
|
@ -146,11 +146,13 @@
|
||||||
.map(({ key }, i) => ({ key, id: key }))
|
.map(({ key }, i) => ({ key, id: key }))
|
||||||
.reduce((a, c) => ({ ...a, [c.key]: c.id }), {})
|
.reduce((a, c) => ({ ...a, [c.key]: c.id }), {})
|
||||||
);
|
);
|
||||||
const resolvePath = (object, path) =>
|
const resolvePath = (object, path) => {
|
||||||
path
|
if (path in object) return object[path];
|
||||||
|
return path
|
||||||
.split(/[\.\[\]\'\"]/)
|
.split(/[\.\[\]\'\"]/)
|
||||||
.filter((p) => p)
|
.filter((p) => p)
|
||||||
.reduce((o, p) => (o && typeof o === "object" ? o[p] : o), object);
|
.reduce((o, p) => (o && typeof o === "object" ? o[p] : o), object);
|
||||||
|
};
|
||||||
|
|
||||||
setContext("DataTable", {
|
setContext("DataTable", {
|
||||||
sortHeader,
|
sortHeader,
|
||||||
|
@ -195,14 +197,18 @@
|
||||||
$: if (radio || batchSelection) selectable = true;
|
$: if (radio || batchSelection) selectable = true;
|
||||||
$: tableSortable.set(sortable);
|
$: tableSortable.set(sortable);
|
||||||
$: headerKeys = headers.map(({ key }) => key);
|
$: headerKeys = headers.map(({ key }) => key);
|
||||||
$: $tableRows = rows.map((row) => ({
|
$: tableCellsByRowId = rows.reduce(
|
||||||
...row,
|
(rows, row) => ({
|
||||||
cells: headerKeys.map((key, index) => ({
|
...rows,
|
||||||
key,
|
[row.id]: headerKeys.map((key, index) => ({
|
||||||
value: resolvePath(row, key),
|
key,
|
||||||
display: headers[index].display,
|
value: resolvePath(row, key),
|
||||||
})),
|
display: headers[index].display,
|
||||||
}));
|
})),
|
||||||
|
}),
|
||||||
|
{}
|
||||||
|
);
|
||||||
|
$: $tableRows = rows;
|
||||||
$: sortedRows = [...$tableRows];
|
$: sortedRows = [...$tableRows];
|
||||||
$: ascending = $sortHeader.sortDirection === "ascending";
|
$: ascending = $sortHeader.sortDirection === "ascending";
|
||||||
$: sortKey = $sortHeader.key;
|
$: sortKey = $sortHeader.key;
|
||||||
|
@ -213,11 +219,11 @@
|
||||||
} else {
|
} else {
|
||||||
sortedRows = [...$tableRows].sort((a, b) => {
|
sortedRows = [...$tableRows].sort((a, b) => {
|
||||||
const itemA = ascending
|
const itemA = ascending
|
||||||
? resolvePath(a, sortKey, "")
|
? resolvePath(a, sortKey)
|
||||||
: resolvePath(b, sortKey, "");
|
: resolvePath(b, sortKey);
|
||||||
const itemB = ascending
|
const itemB = ascending
|
||||||
? resolvePath(b, sortKey, "")
|
? resolvePath(b, sortKey)
|
||||||
: resolvePath(a, sortKey, "");
|
: resolvePath(a, sortKey);
|
||||||
|
|
||||||
if ($sortHeader.sort) return $sortHeader.sort(itemA, itemB);
|
if ($sortHeader.sort) return $sortHeader.sort(itemA, itemB);
|
||||||
|
|
||||||
|
@ -464,7 +470,7 @@
|
||||||
{/if}
|
{/if}
|
||||||
</td>
|
</td>
|
||||||
{/if}
|
{/if}
|
||||||
{#each row.cells as cell, j (cell.key)}
|
{#each tableCellsByRowId[row.id] as cell, j (cell.key)}
|
||||||
{#if headers[j].empty}
|
{#if headers[j].empty}
|
||||||
<td class:bx--table-column-menu="{headers[j].columnMenu}">
|
<td class:bx--table-column-menu="{headers[j].columnMenu}">
|
||||||
<slot
|
<slot
|
||||||
|
|
|
@ -50,7 +50,7 @@
|
||||||
if (shouldFilterRows === true) {
|
if (shouldFilterRows === true) {
|
||||||
rows = rows.filter((row) => {
|
rows = rows.filter((row) => {
|
||||||
return Object.entries(row)
|
return Object.entries(row)
|
||||||
.filter(([key]) => !["cells", "id"].includes(key))
|
.filter(([key]) => key !== "id")
|
||||||
.some(([key, _value]) => {
|
.some(([key, _value]) => {
|
||||||
if (typeof _value === "string" || typeof _value === "number") {
|
if (typeof _value === "string" || typeof _value === "number") {
|
||||||
return (_value + "")
|
return (_value + "")
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue