tdesign-react
Version:
TDesign Component for React
79 lines (70 loc) • 2.5 kB
JavaScript
/**
* tdesign v1.15.1
* (c) 2025 tdesign
* @license MIT
*/
;
var React = require('react');
var hooks_useConfig = require('../hooks/useConfig.js');
var toString = require('./dep-255ceed8.js');
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
var React__default = /*#__PURE__*/_interopDefaultLegacy(React);
/**
* Used to match `RegExp`
* [syntax characters](http://ecma-international.org/ecma-262/7.0/#sec-patterns).
*/
var reRegExpChar = /[\\^$.*+?()[\]{}|]/g,
reHasRegExpChar = RegExp(reRegExpChar.source);
/**
* Escapes the `RegExp` special characters "^", "$", "\", ".", "*", "+",
* "?", "(", ")", "[", "]", "{", "}", and "|" in `string`.
*
* @static
* @memberOf _
* @since 3.0.0
* @category String
* @param {string} [string=''] The string to escape.
* @returns {string} Returns the escaped string.
* @example
*
* _.escapeRegExp('[lodash](https://lodash.com/)');
* // => '\[lodash\]\(https://lodash\.com/\)'
*/
function escapeRegExp(string) {
string = toString.toString(string);
return string && reHasRegExpChar.test(string) ? string.replace(reRegExpChar, '\\$&') : string;
}
var HighlightOption = function HighlightOption(props) {
var _useConfig = hooks_useConfig["default"](),
classPrefix = _useConfig.classPrefix;
var content = props.content,
keyword = props.keyword;
var words = React.useMemo(function () {
var _content$match;
if (!content) return {
list: []
};
if (typeof content !== "string" || !keyword) return {
list: [content]
};
var regExp = new RegExp(escapeRegExp(keyword), "i");
var splitKeyword = (_content$match = content.match(regExp)) === null || _content$match === void 0 ? void 0 : _content$match[0];
return {
list: content.split(splitKeyword),
keyword: splitKeyword
};
}, [content, keyword]);
return /* @__PURE__ */React__default["default"].createElement("div", {
className: "".concat(classPrefix, "-select-option__highlight-item")
}, words.list.map(function (item, index) {
if (!index) return item;
return [/* @__PURE__ */React__default["default"].createElement("b", {
className: "".concat(classPrefix, "-is-highlight"),
key: item + words.keyword
}, words.keyword), item];
}));
};
HighlightOption.displayName = "HighlightOption";
exports.HighlightOption = HighlightOption;
exports.escapeRegExp = escapeRegExp;
//# sourceMappingURL=dep-5883d975.js.map