Skip to content
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

rowKey function for table rows #1781

Merged
merged 2 commits into from
Sep 4, 2023
Merged
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
30 changes: 18 additions & 12 deletions assets/js/components/Table/Table.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
import CollapsibleTableRow from './CollapsibleTableRow';
import Pagination from './Pagination';
import EmptyState from './EmptyState';
import { defaultRowKey } from './defaultRowKey';

const defaultCellRender = (content) => (
<p className="text-gray-900 whitespace-no-wrap">{content}</p>
Expand Down Expand Up @@ -61,6 +62,7 @@ function Table({
setSearchParams,
emptyStateText = 'No data available',
withPadding = true,
rowKey = defaultRowKey,
}) {
const {
columns,
Expand Down Expand Up @@ -181,18 +183,22 @@ function Table({
emptyStateText={emptyStateText}
/>
) : (
renderedData.map((item, index) => (
<CollapsibleTableRow
item={item}
key={index}
collapsibleDetailRenderer={collapsibleDetailRenderer}
renderCells={renderCells}
columns={columns}
colSpan={columns.length}
className={rowClassName}
collapsedRowClassName={collapsedRowClassName}
/>
))
renderedData.map((item, index) => {
const key = rowKey(item, index);

return (
<CollapsibleTableRow
item={item}
key={key}
collapsibleDetailRenderer={collapsibleDetailRenderer}
renderCells={renderCells}
columns={columns}
colSpan={columns.length}
className={rowClassName}
collapsedRowClassName={collapsedRowClassName}
/>
);
})
)}
</tbody>
</table>
Expand Down
28 changes: 27 additions & 1 deletion assets/js/components/Table/Table.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ describe('Table component', () => {
});
});

test('should return empty state message when data is empty', () => {
it('should return empty state message when data is empty', () => {
const data = [];
const emptyStateText = faker.random.words(5);
render(
Expand All @@ -157,4 +157,30 @@ describe('Table component', () => {
expect(tableCell).toHaveTextContent(emptyStateText);
});
});

describe('rowKey function', () => {
it('is used to determine keys for rows', async () => {
const rowKey = jest.fn((_item, index) => index);
const data = tableDataFactory.buildList(5);

render(
<Table
config={tableConfig}
data={data}
setSearchParams={() => {}}
rowKey={rowKey}
/>
);

await waitFor(() => {
const table = screen.getByRole('table');
expect(table.querySelectorAll('tbody > tr')).toHaveLength(5);
});

expect(rowKey).toHaveBeenCalledTimes(5);
expect(rowKey).toHaveBeenCalledWith(data[0], 0);
expect(rowKey).toHaveBeenCalledWith(data[1], 1);
expect(rowKey).toHaveBeenCalledWith(data[2], 2);
});
});
});
3 changes: 3 additions & 0 deletions assets/js/components/Table/defaultRowKey.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { get } from 'lodash';

export const defaultRowKey = (item, index) => get(item, 'key', index);