wix-style-react
Version:
wix-style-react
115 lines (91 loc) • 4.71 kB
JavaScript
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 _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
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; }
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
import React from 'react';
import WixComponent from '../BaseComponents/WixComponent';
import HighlightedItem from './HighlightedItem';
import PropTypes from 'prop-types';
/**
* Highlighter
*
* It highlights string type children by wrapping match
* with strong dom element.
* It remains children element structure.
*/
var childKeyGenerator = function childKeyGenerator() {
var childKey = 0;
return function () {
return 'highlighted-child-' + childKey++;
};
};
var ELEM_TYPES = {
STRING: 'string',
ARRAY: 'array',
REACT_ELEMENT: 'React_element'
};
var getElementType = function getElementType(element) {
if (Array.isArray(element)) {
return ELEM_TYPES.ARRAY;
}
if (React.isValidElement(element)) {
return ELEM_TYPES.REACT_ELEMENT;
}
if (typeof element === 'string') {
return ELEM_TYPES.STRING;
}
return '';
};
var highlight = function highlight(element, match, nextChildKey) {
var _elementTypesMap;
if (!element) {
return null;
}
var elementType = getElementType(element);
var elementTypesMap = (_elementTypesMap = {}, _defineProperty(_elementTypesMap, ELEM_TYPES.STRING, function (elem, _match) {
return React.createElement(
HighlightedItem,
{ key: nextChildKey(), match: _match },
elem
);
}), _defineProperty(_elementTypesMap, ELEM_TYPES.REACT_ELEMENT, function (elem) {
if (elem.props.children) {
return React.cloneElement(elem, _extends({}, elem.props, { key: nextChildKey() }), highlight(elem.props.children, match, nextChildKey));
}
return elem;
}), _defineProperty(_elementTypesMap, ELEM_TYPES.ARRAY, function (elem) {
return elem.map(function (el) {
return highlight(el, match, nextChildKey);
});
}), _elementTypesMap);
return elementTypesMap[elementType] ? elementTypesMap[elementType](element, match) : element;
};
var Highlighter = (_temp = _class = function (_WixComponent) {
_inherits(Highlighter, _WixComponent);
function Highlighter(props) {
_classCallCheck(this, Highlighter);
// we want to create new react keys generator for instance of highlighter
var _this = _possibleConstructorReturn(this, (Highlighter.__proto__ || Object.getPrototypeOf(Highlighter)).call(this, props));
_this.nextChildKey = childKeyGenerator();
return _this;
}
_createClass(Highlighter, [{
key: 'render',
value: function render() {
return React.createElement(
'span',
null,
highlight(this.props.children, this.props.match, this.nextChildKey)
);
}
}]);
return Highlighter;
}(WixComponent), _class.propTypes = {
/** match to highlight */
match: PropTypes.string
}, _temp);
Highlighter.displayName = 'Highlighter';
export default Highlighter;