UNPKG

wix-style-react

Version:
146 lines (113 loc) 5.18 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); var _class, _temp; var _react = require('react'); var _react2 = _interopRequireDefault(_react); var _WixComponent2 = require('../BaseComponents/WixComponent'); var _WixComponent3 = _interopRequireDefault(_WixComponent2); var _propTypes = require('prop-types'); var _propTypes2 = _interopRequireDefault(_propTypes); var _escapeRegExp = require('lodash/escapeRegExp'); var _escapeRegExp2 = _interopRequireDefault(_escapeRegExp); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; } function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } var HighlightedItem = (_temp = _class = function (_WixComponent) { _inherits(HighlightedItem, _WixComponent); function HighlightedItem() { _classCallCheck(this, HighlightedItem); return _possibleConstructorReturn(this, (HighlightedItem.__proto__ || Object.getPrototypeOf(HighlightedItem)).apply(this, arguments)); } _createClass(HighlightedItem, [{ key: 'renderElement', value: function renderElement() { var _props = this.props, children = _props.children, match = _props.match; if (match) { var matchRegExp = this.getMatchRegExp(); return this.highlightChildren(children, matchRegExp); } return children; } }, { key: 'getMatchRegExp', value: function getMatchRegExp() { var _props2 = this.props, match = _props2.match, caseSensitive = _props2.caseSensitive; return new RegExp((0, _escapeRegExp2.default)(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-' + 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 _react2.default.createElement( 'span', { key: key }, plainString ); } }, { key: 'renderHighlight', value: function renderHighlight(matchString, key) { return _react2.default.createElement('strong', { key: key }, matchString); } }, { key: 'render', value: function render() { return _react2.default.createElement( 'span', null, this.renderElement() ); } }]); return HighlightedItem; }(_WixComponent3.default), _class.propTypes = { match: _propTypes2.default.string, caseSensitive: _propTypes2.default.bool }, _temp); exports.default = HighlightedItem;