2018-05-23 06:39:01 +00:00
|
|
|
// @flow
|
|
|
|
import * as React from 'react';
|
|
|
|
import autobind from 'autobind-decorator';
|
2018-05-25 01:44:49 +00:00
|
|
|
import fuzzySort from 'fuzzysort';
|
2018-06-25 17:42:50 +00:00
|
|
|
import { fuzzyMatch } from '../../../common/misc';
|
2018-05-23 06:39:01 +00:00
|
|
|
|
|
|
|
type Props = {|
|
|
|
|
search: string,
|
2018-12-12 17:36:11 +00:00
|
|
|
text: string,
|
2018-05-23 06:39:01 +00:00
|
|
|
|};
|
|
|
|
|
|
|
|
@autobind
|
|
|
|
class Highlight extends React.PureComponent<Props> {
|
2018-06-25 17:42:50 +00:00
|
|
|
render() {
|
|
|
|
const { search, text, ...otherProps } = this.props;
|
2018-05-23 06:39:01 +00:00
|
|
|
|
2018-06-27 05:13:48 +00:00
|
|
|
// Match loose here to make sure our highlighting always works
|
|
|
|
const result = fuzzyMatch(search, text, { splitSpace: true, loose: true });
|
2018-05-23 06:39:01 +00:00
|
|
|
|
2018-06-27 05:13:48 +00:00
|
|
|
if (!result) {
|
2018-06-25 17:42:50 +00:00
|
|
|
return <span {...otherProps}>{text}</span>;
|
2018-05-23 06:39:01 +00:00
|
|
|
}
|
|
|
|
|
2018-05-25 01:44:49 +00:00
|
|
|
return (
|
|
|
|
<span
|
|
|
|
{...otherProps}
|
|
|
|
dangerouslySetInnerHTML={{
|
|
|
|
__html: fuzzySort.highlight(
|
2018-06-27 05:13:48 +00:00
|
|
|
result,
|
2018-05-25 01:44:49 +00:00
|
|
|
'<strong style="font-style: italic; text-decoration: underline;">',
|
2018-12-12 17:36:11 +00:00
|
|
|
'</strong>',
|
|
|
|
),
|
2018-05-25 01:44:49 +00:00
|
|
|
}}
|
|
|
|
/>
|
|
|
|
);
|
2018-05-23 06:39:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default Highlight;
|