Skip to content

fix(Table): fix react key error on sibling cells with same string content #4198

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import React from 'react'
import { Table } from 'semantic-ui-react'

const headerRow = ['Name', 'Status', 'Notes']
const renderBodyRow = ({ name, status, notes }, index) => ({
key: index,
cells: [name || { key: 0 }, status || { key: 1 }, notes || { key: 2 }],
})

const tableData = [
{ name: undefined, status: 'Repeat', notes: 'Repeat' },
{ name: 'Jimmy', status: 'Requires Action', notes: undefined },
{ name: 'Jamie', status: undefined, notes: 'Hostile' },
{ name: 'Jill', status: undefined, notes: undefined },
]

const TableExampleWithTableData = () => (
<Table
tableData={tableData}
headerRow={headerRow}
renderBodyRow={renderBodyRow}
/>
)

export default TableExampleWithTableData
12 changes: 12 additions & 0 deletions docs/src/examples/collections/Table/Variations/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,18 @@ const Variations = () => (
examplePath='collections/Table/Variations/TableExampleSmall'
/>
<ComponentExample examplePath='collections/Table/Variations/TableExampleLarge' />

<ComponentExample
title='TableData'
description='A table can receive a JSON object to create body rows.'
examplePath='collections/Table/Variations/TableExampleTableData'
>
<Message info>
Using the <code>tableData</code> attribute requires also to define the
Tables
<code>renderBodyRow</code> attribute.
</Message>
</ComponentExample>
</ExampleSection>
)

Expand Down
12 changes: 11 additions & 1 deletion src/collections/Table/TableRow.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,17 @@ function TableRow(props) {

return (
<ElementType {...rest} className={classes}>
{_.map(cells, (cell) => TableCell.create(cell, { defaultProps: { as: cellAs } }))}
{_.map(cells, (cell, idx) => {
const defaultProps = { as: cellAs }

// generate a default react key for any string content created cells by this HOC
// required due the fact that without this, sibling cells with same content will result in React Same Key Error
if (typeof cell === 'string') {
defaultProps.key = `${idx}-${cell.toLocaleLowerCase().replace(' ', '-')}`
}

return TableCell.create(cell, { defaultProps })
})}
</ElementType>
)
}
Expand Down
15 changes: 15 additions & 0 deletions test/specs/collections/Table/Table-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,13 @@ describe('Table', () => {
{ name: 'Jill', lastName: undefined, status: undefined, notes: undefined },
]

const tableDataWithSameCellContentInSiblingCells = [
{ name: undefined, status: 'repeat', notes: 'repeat' },
{ name: 'Jimmy', status: 'Requires Action', notes: undefined },
{ name: 'Jamie', status: undefined, notes: 'Hostile' },
{ name: 'Jill', status: undefined, notes: undefined },
]

const renderBodyRowWithSpan = ({ name, lastName, status, notes }, index) => ({
key: index,
cells: [
Expand Down Expand Up @@ -170,5 +177,13 @@ describe('Table', () => {
tfoot.find('tr').should.have.lengthOf(1)
tfoot.find('tr').find('td').should.have.lengthOf(footerRow.length)
})

it('renders table data with same string content in sibling cells', () => {
wrapperMount({
headerRow,
renderBodyRow,
tableData: tableDataWithSameCellContentInSiblingCells,
})
})
})
})