Skip to content

Commit 60092bc

Browse files
committed
fix: fix circular JSON structure when rendering components in table
1 parent 731ba5c commit 60092bc

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

packages/components/data-table/src/index.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import React, { useMemo } from "react";
22
import { Table, type TableProps } from "@react-ck/table";
33
import * as CC from "change-case";
4+
import { stringFromObject } from "./utils/string-from-object";
45

56
// TODO: add pagination
67
// TODO: add section table
@@ -66,7 +67,7 @@ export const DataTable = <T extends TableData>({
6667
)}
6768
<tbody>
6869
{data.map((row) => (
69-
<tr key={JSON.stringify(row)}>
70+
<tr key={stringFromObject(row)}>
7071
{keys.map((key) => (
7172
<td key={key}>{row[key]}</td>
7273
))}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/** Destructs an object into a plain string, has depth limitation to preserve performance */
2+
3+
export const stringFromObject = (obj: object, initialDepth = 0): string => {
4+
const maxDepth = 5;
5+
const depth = initialDepth + 1;
6+
7+
const res = Object.entries(obj)
8+
.flatMap(([key, value]) => {
9+
let converted: string;
10+
11+
if (typeof value === "string") {
12+
converted = value;
13+
} else if (typeof value === "function") {
14+
converted = String(value);
15+
} else if (value === null || value === undefined) {
16+
converted = "";
17+
} else if (typeof value === "object") {
18+
converted = depth < maxDepth ? stringFromObject(value, depth) : "";
19+
} else {
20+
converted = String(value);
21+
}
22+
23+
return [key, converted];
24+
})
25+
.join(" ");
26+
27+
return res;
28+
};

0 commit comments

Comments
 (0)