@awsui/components-react
Version:
AWS UI is a collection of [React](https://reactjs.org/) components that help create intuitive, responsive, and accessible user experiences for web applications. It is developed by Amazon Web Services (AWS). This work is available under the terms of the [A
33 lines (32 loc) • 1.32 kB
JavaScript
import React from 'react';
import styles from './styles.css.js';
import clsx from 'clsx';
var splitOnFiltering = function (str, highlightText) {
var filteringPattern = highlightText.replace(/[-[\]/{}()*+?.\\^$|]/g, '\\$&');
var regexp = new RegExp(filteringPattern, 'gi');
var noMatches = str.split(regexp);
var matches = str.match(regexp);
return { noMatches: noMatches, matches: matches };
};
var Highlight = function (_a) {
var str = _a.str;
return str ? React.createElement("span", { className: clsx(styles['filtering-match-highlight']) }, str) : null;
};
export default function HighlightMatch(_a) {
var str = _a.str, highlightText = _a.highlightText;
if (!str || !highlightText) {
return React.createElement(React.Fragment, null, str);
}
if (str === highlightText) {
return React.createElement(Highlight, { str: str });
}
var _b = splitOnFiltering(str, highlightText), noMatches = _b.noMatches, matches = _b.matches;
var highlighted = [];
noMatches.forEach(function (noMatch, idx) {
highlighted.push(noMatch);
if (matches && idx < matches.length) {
highlighted.push(React.createElement(Highlight, { key: idx, str: matches[idx] }));
}
});
return React.createElement("span", null, highlighted);
}