Created
June 18, 2020 14:06
-
-
Save noudadrichem/ba3ac1466e09376b270a416ef34767bd 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
import React from 'react'; | |
interface TableProps { | |
data: any[]; | |
headings?: string[] | |
} | |
function Table(props: TableProps): JSX.Element { | |
const { data = [], headings } = props; | |
const cssGridWidth = { | |
gridTemplateColumns: `repeat(${data[0] !== undefined ? Object.keys(data[0]).length : 0}, 1fr)` | |
}; | |
return ( | |
<div className="table"> | |
<div className='table-heading' style={cssGridWidth}> | |
{ data.length > 0 && | |
Object.keys(data[0]).map((key: string) => ( | |
<div className="table-column" key={key}>{key}</div> | |
)) | |
} | |
</div> | |
{ | |
data.map((object: any, idx: number) => { | |
return ( | |
<div className='table-row' key={idx} style={cssGridWidth}> | |
{ | |
Object.keys(object).map((key: string) => { | |
const value = object[key]; | |
return ( | |
<div className="table-column" key={`${key}-${value}`}> | |
{value} | |
</div> | |
) | |
}) | |
} | |
</div> | |
) | |
}) | |
} | |
</div> | |
) | |
} | |
export default Table; |
Author
noudadrichem
commented
Jun 18, 2020
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment