import React from 'react'; import PropTypes from 'prop-types'; import classNames from 'classnames'; export const Highlight = ({ cx, value, highlightedTagName, isHighlighted, nonHighlightedTagName, }) => { const TagName = isHighlighted ? highlightedTagName : nonHighlightedTagName; const className = isHighlighted ? 'highlighted' : 'nonHighlighted'; return {value}; }; Highlight.propTypes = { cx: PropTypes.func.isRequired, value: PropTypes.string.isRequired, isHighlighted: PropTypes.bool.isRequired, highlightedTagName: PropTypes.string.isRequired, nonHighlightedTagName: PropTypes.string.isRequired, }; const Highlighter = ({ cx, hit, attribute, highlight, highlightProperty, tagName, nonHighlightedTagName, separator, className, }) => { const parsedHighlightedValue = highlight({ hit, attribute, highlightProperty, }); return ( {parsedHighlightedValue.map((item, i) => { if (Array.isArray(item)) { const isLast = i === parsedHighlightedValue.length - 1; return ( {item.map((element, index) => ( ))} {!isLast && {separator}} ); } return ( ); })} ); }; Highlighter.propTypes = { cx: PropTypes.func.isRequired, hit: PropTypes.object.isRequired, attribute: PropTypes.oneOfType([ PropTypes.arrayOf(PropTypes.string), PropTypes.string, ]).isRequired, highlight: PropTypes.func.isRequired, highlightProperty: PropTypes.string.isRequired, tagName: PropTypes.string, nonHighlightedTagName: PropTypes.string, className: PropTypes.string, separator: PropTypes.node, }; Highlighter.defaultProps = { tagName: 'em', nonHighlightedTagName: 'span', className: '', separator: ', ', }; export default Highlighter;