File tree Expand file tree Collapse file tree 2 files changed +30
-1
lines changed
packages/components/data-table/src Expand file tree Collapse file tree 2 files changed +30
-1
lines changed Original file line number Diff line number Diff line change 11import React , { useMemo } from "react" ;
22import { Table , type TableProps } from "@react-ck/table" ;
33import * 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 ) ) }
Original file line number Diff line number Diff line change 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+ } ;
You can’t perform that action at this time.
0 commit comments