Created
December 25, 2020 18:45
-
-
Save DarkoTrpevski/9eec0bc7d0a732ad45fc619a095500dc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Sample Array Data | |
const sampleData = [ | |
{ name: "Bill", score: 15 }, | |
{ name: "Jane", score: 18 }, | |
{ name: "Fred", score: 10 } | |
] | |
<List data = {sampleData} onRowClick={row => console.log(row)} /> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React from 'react' | |
interface Props<T> { | |
data: T[]; | |
onRowClick: (item: T) => void; | |
} | |
const List = <T extends object>({ data, onRowClick }: Props<T>) => { | |
if(data.length === 0) return null; | |
return ( | |
<table> | |
<thead> | |
Some Title/Head | |
</thead> | |
<tbody> | |
{ | |
data.map((item: T, index: number) => ( | |
<tr key={index} onClick={() => onRowClick(item)}> | |
{ | |
(Object.keys(data[0]) as Array<keyof T>).map(key => ( | |
<td key={key.toString()}>{item[key]}</td>) | |
) | |
} | |
</tr> | |
)) | |
} | |
</tbody> | |
</table> | |
); | |
} | |
export default List; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment