UNPKG

wix-style-react

Version:
130 lines (109 loc) 4.53 kB
import _classCallCheck from "@babel/runtime/helpers/classCallCheck"; import _createClass from "@babel/runtime/helpers/createClass"; import _inherits from "@babel/runtime/helpers/inherits"; import _possibleConstructorReturn from "@babel/runtime/helpers/possibleConstructorReturn"; import _getPrototypeOf from "@babel/runtime/helpers/getPrototypeOf"; import _defineProperty from "@babel/runtime/helpers/defineProperty"; function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; } function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } } import React from 'react'; import PropTypes from 'prop-types'; import escapeRegExp from 'lodash/escapeRegExp'; var HighlightedItem = /*#__PURE__*/function (_React$PureComponent) { _inherits(HighlightedItem, _React$PureComponent); var _super = _createSuper(HighlightedItem); function HighlightedItem() { _classCallCheck(this, HighlightedItem); return _super.apply(this, arguments); } _createClass(HighlightedItem, [{ key: "renderElement", value: function renderElement() { var _this$props = this.props, children = _this$props.children, match = _this$props.match; if (match) { var matchRegExp = this.getMatchRegExp(); return this.highlightChildren(children, matchRegExp); } return children; } }, { key: "getMatchRegExp", value: function getMatchRegExp() { var _this$props2 = this.props, match = _this$props2.match, caseSensitive = _this$props2.caseSensitive; return new RegExp(escapeRegExp(match), caseSensitive ? '' : 'i'); } }, { key: "getMatchBoundaries", value: function getMatchBoundaries(subject, matchRegExp) { var matches = matchRegExp.exec(subject); if (matches) { return { first: matches.index, last: matches.index + matches[0].length }; } } }, { key: "getMatchReactKey", value: function getMatchReactKey(index) { return "match-index-".concat(index); } }, { key: "highlightChildren", value: function highlightChildren(children, matchRegExp) { var processedChildren = []; var matchIndex = 0; while (children) { if (!matchRegExp.test(children)) { processedChildren.push(this.renderPlain(children, this.getMatchReactKey(matchIndex++))); return processedChildren; } var boundaries = this.getMatchBoundaries(children, matchRegExp); var nonMatch = children.slice(0, boundaries.first); if (nonMatch) { processedChildren.push(this.renderPlain(nonMatch, this.getMatchReactKey(matchIndex++))); } var match = children.slice(boundaries.first, boundaries.last); if (match) { processedChildren.push(this.renderHighlight(match, this.getMatchReactKey(matchIndex++))); } children = children.slice(boundaries.last); } return processedChildren; } }, { key: "renderPlain", value: function renderPlain(plainString, key) { return /*#__PURE__*/React.createElement("span", { key: key }, plainString); } }, { key: "renderHighlight", value: function renderHighlight(matchString, key) { return /*#__PURE__*/React.createElement('strong', { key: key }, matchString); } }, { key: "render", value: function render() { var dataHook = this.props.dataHook; return /*#__PURE__*/React.createElement("span", { "data-hook": dataHook }, this.renderElement()); } }]); return HighlightedItem; }(React.PureComponent); _defineProperty(HighlightedItem, "propTypes", { /** Applied as data-hook HTML attribute that can be used in the tests */ dataHook: PropTypes.string, match: PropTypes.string, caseSensitive: PropTypes.bool }); export default HighlightedItem;