insomnia/packages/insomnia-app/app/ui/components/base/highlight.js

40 lines
904 B
JavaScript
Raw Normal View History

// @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';
type Props = {|
search: string,
text: string,
|};
@autobind
class Highlight extends React.PureComponent<Props> {
2018-06-25 17:42:50 +00:00
render() {
const { search, text, ...otherProps } = this.props;
// Match loose here to make sure our highlighting always works
const result = fuzzyMatch(search, text, { splitSpace: true, loose: true });
if (!result) {
2018-06-25 17:42:50 +00:00
return <span {...otherProps}>{text}</span>;
}
2018-05-25 01:44:49 +00:00
return (
<span
{...otherProps}
dangerouslySetInnerHTML={{
__html: fuzzySort.highlight(
result,
2018-05-25 01:44:49 +00:00
'<strong style="font-style: italic; text-decoration: underline;">',
'</strong>',
),
2018-05-25 01:44:49 +00:00
}}
/>
);
}
}
export default Highlight;