@hi-ui/highlighter 4.0.9 → 4.1.0-alpha.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -35,10 +35,12 @@ var Highlighter = /*#__PURE__*/React.forwardRef(function (_a, ref) {
35
35
  _a$children = _a.children,
36
36
  children = _a$children === void 0 ? null : _a$children,
37
37
  rest = tslib.__rest(_a, ["prefixCls", "className", "keyword", "children"]);
38
+ var startIndex = React.useRef(0);
38
39
  if (!keyword) return children;
39
40
  if (typeof children !== 'string') return children;
40
41
  // 支持多个匹配高亮
41
42
  var parts = children.split(keyword);
43
+ var matches = children.match(keyword);
42
44
  // 未匹配到
43
45
  if (parts.length < 2) return children;
44
46
  var lastPart = parts.pop();
@@ -48,11 +50,14 @@ var Highlighter = /*#__PURE__*/React.forwardRef(function (_a, ref) {
48
50
  ref: ref,
49
51
  className: cls
50
52
  }, rest), parts.reduce(function (acc, part, index) {
53
+ var matched = typeof keyword === 'string' ? keyword : matches[index];
54
+ startIndex.current += part.length;
51
55
  acc.push(part);
52
56
  acc.push( /*#__PURE__*/React__default["default"].createElement("span", {
53
57
  key: index,
54
58
  className: prefixCls + "--matched"
55
- }, keyword));
59
+ }, children.substring(startIndex.current, startIndex.current + matched.length)));
60
+ startIndex.current += matched.length;
56
61
  return acc;
57
62
  }, []), lastPart);
58
63
  });
@@ -8,7 +8,7 @@
8
8
  * LICENSE file in the root directory of this source tree.
9
9
  */
10
10
  import { __rest } from 'tslib';
11
- import React, { forwardRef } from 'react';
11
+ import React, { forwardRef, useRef } from 'react';
12
12
  import { getPrefixCls, cx } from '@hi-ui/classname';
13
13
  import { __DEV__ } from '@hi-ui/env';
14
14
  var HIGHLIGHTER_PREFIX = getPrefixCls('highlighter');
@@ -23,10 +23,12 @@ var Highlighter = /*#__PURE__*/forwardRef(function (_a, ref) {
23
23
  _a$children = _a.children,
24
24
  children = _a$children === void 0 ? null : _a$children,
25
25
  rest = __rest(_a, ["prefixCls", "className", "keyword", "children"]);
26
+ var startIndex = useRef(0);
26
27
  if (!keyword) return children;
27
28
  if (typeof children !== 'string') return children;
28
29
  // 支持多个匹配高亮
29
30
  var parts = children.split(keyword);
31
+ var matches = children.match(keyword);
30
32
  // 未匹配到
31
33
  if (parts.length < 2) return children;
32
34
  var lastPart = parts.pop();
@@ -36,11 +38,14 @@ var Highlighter = /*#__PURE__*/forwardRef(function (_a, ref) {
36
38
  ref: ref,
37
39
  className: cls
38
40
  }, rest), parts.reduce(function (acc, part, index) {
41
+ var matched = typeof keyword === 'string' ? keyword : matches[index];
42
+ startIndex.current += part.length;
39
43
  acc.push(part);
40
44
  acc.push( /*#__PURE__*/React.createElement("span", {
41
45
  key: index,
42
46
  className: prefixCls + "--matched"
43
- }, keyword));
47
+ }, children.substring(startIndex.current, startIndex.current + matched.length)));
48
+ startIndex.current += matched.length;
44
49
  return acc;
45
50
  }, []), lastPart);
46
51
  });
@@ -8,5 +8,5 @@ export interface HighlighterProps extends HiBaseHTMLProps<'span'> {
8
8
  /**
9
9
  * 匹配高亮关键词
10
10
  */
11
- keyword?: string;
11
+ keyword?: string | RegExp;
12
12
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hi-ui/highlighter",
3
- "version": "4.0.9",
3
+ "version": "4.1.0-alpha.0",
4
4
  "description": "A sub-package for @hi-ui/hiui.",
5
5
  "keywords": [],
6
6
  "author": "HiUI <mi-hiui@xiaomi.com>",