Add highlighting to search results as mocked.

This commit is contained in:
Tom Moor
2017-11-25 15:44:10 -08:00
parent 8b2f6f193f
commit 2d8a41a982
5 changed files with 76 additions and 13 deletions

View File

@@ -0,0 +1,34 @@
// @flow
import React from 'react';
import replace from 'string-replace-to-array';
import styled from 'styled-components';
import { color } from 'shared/styles/constants';
type Props = {
highlight: ?string,
text: string,
caseSensitive?: boolean,
};
function Highlight({ highlight, caseSensitive, text, ...rest }: Props) {
return (
<span {...rest}>
{highlight
? replace(
text,
new RegExp(
(highlight || '').replace(/[-\\^$*+?.()|[\]{}]/g, '\\$&'),
caseSensitive ? 'g' : 'gi'
),
(tag, index) => <Mark key={index}>{tag}</Mark>
)
: text}
</span>
);
}
const Mark = styled.mark`
background: ${color.yellow};
`;
export default Highlight;

View File

@@ -0,0 +1,3 @@
// @flow
import Highlight from './Highlight';
export default Highlight;