Created
June 3, 2022 16:18
-
-
Save horaciosystem/3fb1a20cc55f57d0098c0e5eb21cccba to your computer and use it in GitHub Desktop.
Implementation idea for Table components
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
//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