Skip to content
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
5 changes: 5 additions & 0 deletions .changeset/fix-document-table-array-keys.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'outstatic': patch
---

Fix duplicate React key warnings when document table array fields contain repeated labels.
4 changes: 2 additions & 2 deletions packages/outstatic/src/components/documents-table.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,9 @@ const documentColumnLabel = (id: string): string =>

const renderCellValue = (value: unknown): ReactNode => {
if (Array.isArray(value)) {
return value.map((item: { label: string }) => (
return value.map((item: { label: string; value?: unknown }, index) => (
<span
key={item.label}
key={`${item.value ?? item.label}-${index}`}
className="bg-muted text-muted-foreground me-2 rounded px-2.5 py-0.5 text-xs font-medium"
>
{item.label}
Expand Down
46 changes: 46 additions & 0 deletions packages/outstatic/src/components/tests/documents-table.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,52 @@ describe('DocumentsTable', () => {
expect(screen.getByText(date2)).toBeInTheDocument()
})

it('renders array field values without duplicate key warnings', () => {
const consoleError = jest
.spyOn(console, 'error')
.mockImplementation(() => {})

mockUseGetDocuments.mockImplementation(() => ({
data: {
documents: [
{
slug: 'doc1',
title: 'Document 1',
tags: [
{ label: 'News', value: 'news' },
{ label: 'News', value: 'news' }
],
content: '',
collection: 'testCollection'
}
],
metadata: new Map([
['title', 'string'],
['tags', 'array'],
['slug', 'string']
])
},
refetch: jest.fn()
}))

render(
<TestWrapper>
<DocumentsTable />
</TestWrapper>
)

expect(screen.getAllByText('News')).toHaveLength(2)
expect(
consoleError.mock.calls.some((args) =>
String(args[0]).includes(
'Each child in a list should have a unique "key" prop'
)
)
).toBe(false)

consoleError.mockRestore()
})

it('renders table headers for the default visible columns', () => {
render(
<TestWrapper>
Expand Down
Loading