UNPKG

react-bootstrap-typeahead

Version:
93 lines (68 loc) 2.78 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); Object.defineProperty(exports, "__esModule", { value: true }); exports["default"] = void 0; var _inheritsLoose2 = _interopRequireDefault(require("@babel/runtime/helpers/inheritsLoose")); var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty")); var _propTypes = _interopRequireDefault(require("prop-types")); var _react = _interopRequireDefault(require("react")); var _utils = require("../utils"); var propTypes = { children: _propTypes["default"].string.isRequired, highlightClassName: _propTypes["default"].string, search: _propTypes["default"].string.isRequired }; var defaultProps = { highlightClassName: 'rbt-highlight-text' }; /** * Stripped-down version of https://github.com/helior/react-highlighter * * Results are already filtered by the time the component is used internally so * we can safely ignore case and diacritical marks for the purposes of matching. */ var Highlighter = /*#__PURE__*/function (_React$PureComponent) { (0, _inheritsLoose2["default"])(Highlighter, _React$PureComponent); function Highlighter() { return _React$PureComponent.apply(this, arguments) || this; } var _proto = Highlighter.prototype; _proto.render = function render() { var _this$props = this.props, children = _this$props.children, highlightClassName = _this$props.highlightClassName, search = _this$props.search; if (!search || !children) { return children; } var matchCount = 0; var remaining = children; var highlighterChildren = []; while (remaining) { var bounds = (0, _utils.getMatchBounds)(remaining, search); // No match anywhere in the remaining string, stop. if (!bounds) { highlighterChildren.push(remaining); break; } // Capture the string that leads up to a match. var nonMatch = remaining.slice(0, bounds.start); if (nonMatch) { highlighterChildren.push(nonMatch); } // Capture the matching string. var match = remaining.slice(bounds.start, bounds.end); highlighterChildren.push( /*#__PURE__*/_react["default"].createElement("mark", { className: highlightClassName, key: matchCount }, match)); matchCount += 1; // And if there's anything left over, continue the loop. remaining = remaining.slice(bounds.end); } return highlighterChildren; }; return Highlighter; }(_react["default"].PureComponent); (0, _defineProperty2["default"])(Highlighter, "propTypes", propTypes); (0, _defineProperty2["default"])(Highlighter, "defaultProps", defaultProps); var _default = Highlighter; exports["default"] = _default;