-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
UIBULKED-341 Separate Item notes in different columns based on the no…
…te type
- Loading branch information
Showing
5 changed files
with
90 additions
and
0 deletions.
There are no files selected for viewing
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
48 changes: 48 additions & 0 deletions
48
src/components/BulkEditList/BulkEditListResult/Preview/FormattedNotes/FormattedNotes.js
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import React from 'react'; | ||
|
||
const tableStyles = { | ||
width: '100%', | ||
borderCollapse: 'collapse' | ||
} | ||
|
||
const cellStyles = { | ||
border: '1px solid', | ||
padding: '0 4px', | ||
maxWidth: '200px', | ||
} | ||
|
||
const ignoredContent = ['false']; | ||
|
||
export const FormattedNotes = ({ notes }) => { | ||
if (!notes) return null; | ||
|
||
|
||
const tableData = notes.split('|') | ||
.map(i => i.split(';').filter(i => !ignoredContent.includes(i))) | ||
.reduce((acc, elem) => { | ||
const [head, ...content] = elem; | ||
|
||
acc[head] = content.join(';'); | ||
|
||
return acc; | ||
}, {}); | ||
|
||
const renderCell = (item, key, tag) => { | ||
return React.createElement(tag, { key, style: cellStyles }, item); | ||
}; | ||
|
||
const renderHead = () => { | ||
return Object.keys(tableData).map((item, key) => renderCell(item, key, 'th')); | ||
} | ||
|
||
const renderBody = () => { | ||
return Object.values(tableData).map((item, key) => renderCell(item, key, 'td')); | ||
}; | ||
|
||
return ( | ||
<table style={tableStyles}> | ||
<thead><tr>{renderHead()}</tr></thead> | ||
<tbody><tr>{renderBody()}</tr></tbody> | ||
</table> | ||
) | ||
}; |
37 changes: 37 additions & 0 deletions
37
src/components/BulkEditList/BulkEditListResult/Preview/FormattedNotes/FormattedNotes.test.js
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import React from 'react'; | ||
import { render } from '@testing-library/react'; | ||
import { FormattedNotes } from './FormattedNotes'; | ||
|
||
describe('FormattedNotes', () => { | ||
const notes = "Note;Missing pages; p 10-13;false|Action note;My action note;false"; | ||
|
||
it('renders nothing when notes prop is not provided', () => { | ||
const { container } = render(<FormattedNotes />); | ||
expect(container.firstChild).toBeNull(); | ||
}); | ||
|
||
it('renders a table with the correct structure when notes are provided', () => { | ||
const { getByText } = render(<FormattedNotes notes={notes} />); | ||
|
||
expect(getByText('Note')).toBeInTheDocument(); | ||
expect(getByText('Missing pages; p 10-13')).toBeInTheDocument(); | ||
expect(getByText('Action note')).toBeInTheDocument(); | ||
expect(getByText('My action note')).toBeInTheDocument(); | ||
}); | ||
|
||
it('ignores content specified in ignoredContent array', () => { | ||
const { queryByText } = render(<FormattedNotes notes={notes} />); | ||
|
||
expect(queryByText('false')).toBeNull(); | ||
}); | ||
|
||
it('renders a table with the correct number of rows', () => { | ||
const { container } = render(<FormattedNotes notes={notes} />); | ||
|
||
const headRows = container.querySelectorAll('thead tr'); | ||
const rows = container.querySelectorAll('tbody tr'); | ||
|
||
expect(headRows.length).toBe(1); | ||
expect(rows.length).toBe(1); | ||
}); | ||
}); |
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
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