UNPKG

baseui

Version:

A React Component library implementing the Base design language

67 lines (63 loc) 1.92 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.HighlightCellText = void 0; exports.matchesQuery = matchesQuery; exports.splitByQuery = splitByQuery; var _react = _interopRequireDefault(require("react")); var _styles = require("../styles"); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } /* Copyright (c) Uber Technologies, Inc. This source code is licensed under the MIT license found in the LICENSE file in the root directory of this source tree. */ function matchesQuery(text, query) { return text.toLowerCase().includes(query.toLowerCase()); } function splitByQuery(text, query) { const start = text.toLowerCase().indexOf(query.toLowerCase()); // query not found if (start === -1) { return [text]; } if (start === 0) { return [text.slice(0, query.length), text.slice(query.length)]; } const substrings = []; let substring = ''; for (let i = 0; i < text.length; i++) { substring = substring + text[i]; if ( // prefix i === start - 1 || // query i === start + query.length - 1 || // suffix i === text.length - 1) { // @ts-ignore substrings.push(substring); substring = ''; } } return substrings; } const HighlightCellText = props => { const [css, theme] = (0, _styles.useStyletron)(); if (!props.query) { return /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, "props.text"); } return /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, splitByQuery(props.text, props.query).map((el, i) => { if (matchesQuery(el, props.query)) { return /*#__PURE__*/_react.default.createElement("span", { className: css({ ...theme.typography.font150 }), key: i }, el); } return el; })); }; exports.HighlightCellText = HighlightCellText;