-
Notifications
You must be signed in to change notification settings - Fork 4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(Table): add shorthand for generating tables #565
Comments
Edit I wound up pseudo-coding way more than I intended (see the bottom section) haha. If you think this makes sense I can take a pass at implementing this. Might be easier to talk about once there's actual code written. The tricky part here is going to be the body row since it's going to be based on data for a given row. The header/footer rows are just going to be static, which makes them slightly easier. What about this: // Table usage.
<Table
headerRow={rowShorthand}
bodyRow={functionThatReturnsRowShorthand}
footerRow={rowShorthand}
/> Not setting This would mean a Row shorthand TableRow.create = createShorthandFactory(TableRow, items => ({ items })) So you could theoretically do this: <Table
headerRow={['name', 'status', 'notes']}
bodyRow={(props) => [props.name, props.status, props.notes]}
footerRow={[{ colspan: 3, content: 'Some footer content' }]}
/> Same example, but adding props to the TableRow for each row of the body: <Table
headerRow={['name', 'status', 'notes']}
bodyRow={(props) => ({ positive: status === 'paid', items: [props.name, props.status, props.notes] })}
footerRow={[{ colspan: 3, content: 'Some footer content' }]}
/> I'm using Pseudo code for the implementation: // Table
renderHeader () {
return (
<TableHead>
TableRow.create(this.props.headerRow, { itemAs: 'th' })
</TableHead>
)
}
renderBody () {
return (
<TableBody>
{this.props.tableData.map((item, index) => TableRow.create(this.props.bodyRow(item, index)))}
</TableBody>
)
}
renderFooter() {
return (
<TableFooter>
TableRow.create(this.props.FooterRow)
</TableFooter>
)
}
render () {
<Table>
{headerRow && this.renderHeader()}
{this.renderBody()}
{footerRow && this.renderFooter()}
</Table>
}
// TableRow
renderItems () {
return this.props.items.map((item) => TableCell.create(item, {
// Make 'itemAs' a tableRow prop to avoid needing to add the prop to each
// list item individually. If the user passed an 'as' prop already within
// 'item' it would still win.
as: itemAs
}))
}
render () {
if (children) {
return <ElementType {...rest} className={classes}>{children}</ElementType>
}
<ElementType {...rest} className={classes}>{this.renderItems()}</ElementType>
} |
* TableCell shorthand * TableRow shorthand * Table shorthand * Add example to test table shorthand
* TableCell shorthand * TableRow shorthand * Table shorthand * Add example to test table shorthand
@levithomason - I wound up implementing this and I think it came out really well. Check out the PR referenced above ^ |
* TableCell shorthand * TableRow shorthand * Table shorthand * Add example to test table shorthand
* TableCell shorthand * TableRow shorthand * Table shorthand * Add example to test table shorthand
* feat(Table): Table shorthand (#565) * TableCell shorthand * TableRow shorthand * Table shorthand * Add example to test table shorthand * Fix specs * Add table shorthand specs * feat(table): Rename Table props - TableRow: items => cells - Table: bodyRow => renderBodyRow
The Table has been updated to our v1 API in #451. This was done with the sub component API only. We now need to add shorthand for generating tables.
Our previous
TableColumn
implementation was inspired by Facebook's fixed-data-table. @jcarbo has also put forth many great starter ideas in this comment.Though it is more work, I am leaning toward the suggested row based approach (last example from jcarbo) because it offers more control. Or at least providing this as one of the shorthand options. With the old
TableColumn
approach, here at TA we ran into issues by not being able to control the actual row or cell that was rendered.I think providing "renderer" props that expect table elements to be returned is the way to go. We could even provide multiple "renderer" props that would allow varying levels of control. Even along side a
columns
prop that let you define columns like we used to. This way, you can have several approaches to accomplish the most common goals.The text was updated successfully, but these errors were encountered: