Skip to content

Instantly share code, notes, and snippets.

@horaciosystem
Created June 3, 2022 16:18
Show Gist options
  • Save horaciosystem/3fb1a20cc55f57d0098c0e5eb21cccba to your computer and use it in GitHub Desktop.
Save horaciosystem/3fb1a20cc55f57d0098c0e5eb21cccba to your computer and use it in GitHub Desktop.
Implementation idea for Table components
//Configuration object that can be used to build the table:
const trackRowSchema = [
{
field: 'expand-column',
renderValue: (value) => <Button aria-label="expand-icon"><icon /></Button>,
},
{
field: 'name',
renderValue: (value) => <FancyLabel>{value.name}</FancyLabel>,
},
{
field: 'status',
renderValue: (value) => <ComplexStatusComponent>{value.status}</ComplexStatusComponent>
},
]
//Table component agnostic of specific business logic, or entity/model
function Table({ entityPath, tableRowSchema }) {
const [entityList] = useFetch(entityPath);
entityList.map(entity => <TableRow track={entity} rowSchema={tableRowSchema} />)
}
//TableRow component just gets the entity and the row schema adn renders the row
function TableRow({ entity, rowSchema }) {
return (
<tr>
{rowSchema.map(({ field, renderValue }) => (
<td key={field}>
{renderValue(entity)}
</td>
))}
</tr>
);
}
<Table entityPath="/tracks" tableRowSchema={trackRowSchema} />
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment