Skip to content

Commit

Permalink
Fix/results table key error (#22)
Browse files Browse the repository at this point in the history
* Add reformatted ResultsTable code

- Add spacing to a large line of code to increase readability

* Add fix for key error in ResultsTable

- If there are multiple matches for the highlight method, a key error would occur due to duplicate keys, fix this by using an index counter as part of the key
- An ESLint key rule had to be disabled, however as the UBRN is used as part of the key it shouldn't be an issue
  • Loading branch information
ONS-Tom authored and ONS-Anthony committed Dec 14, 2017
1 parent a71df5a commit 91f77c1
Showing 1 changed file with 17 additions and 5 deletions.
22 changes: 17 additions & 5 deletions src/components/ResultsTable.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,26 @@ import { downloadCSV, downloadJSON } from '../utils/export';

const ResultsTable = ({ results, showFilter, showPagination, defaultPageSize, convertBands, businessName }) => {
// https://stackoverflow.com/questions/29652862/highlight-text-using-reactjs
function getHighlightedText(text, higlight) {
function getHighlightedText(row, higlight) {
// Split text on higlight term, include term itself into parts, ignore case
try {
const parts = text.split(new RegExp(`(${higlight})`, 'gi'));
return (<span key={text}>{parts.map(part => part.toLowerCase() === higlight.toLowerCase() ? <span key={`${text}-span`} style={{ backgroundColor: '#FFFF00' }}>{part}</span> : part)}</span>);
const parts = row.value.split(new RegExp(`(${higlight})`, 'gi'));
// We can use the array index as a key as we already use the UBRN as part of the key
/* eslint react/no-array-index-key: "off" */
return (
<span key={row.original.id}>
{parts.map((part, i) => (
part.toLowerCase() === higlight.toLowerCase() ?
(<span key={`${row.original.id}-${i}`} style={{ backgroundColor: '#FFFF00' }}>
{part}
</span>)
: part),
)
}
</span>);
} catch (e) {
// Catch the invalid regular expressions
return (<span>{text}</span>);
return (<span key={row.original.id}>{row.value}</span>);
}
}
return (
Expand All @@ -36,7 +48,7 @@ const ResultsTable = ({ results, showFilter, showPagination, defaultPageSize, co
id: 'businessName',
accessor: d => d.businessName,
Cell: row => (
(businessName !== '') ? getHighlightedText(row.value, businessName) : row.value
(businessName !== '') ? getHighlightedText(row, businessName) : row.value
),
},
{
Expand Down

0 comments on commit 91f77c1

Please sign in to comment.