index.tsx 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. import React, { PureComponent } from 'react';
  2. import cls from 'classnames';
  3. import PropTypes, { string } from 'prop-types';
  4. import { cssClasses } from '@douyinfe/semi-foundation/highlight/constants';
  5. import { getHighLightTextHTML } from '../_utils/index';
  6. import '@douyinfe/semi-foundation/highlight/highlight.scss';
  7. export interface HighlightProps {
  8. autoEscape?: boolean;
  9. caseSensitive?: boolean;
  10. sourceString?: string;
  11. searchWords?: Array<string>;
  12. highlightStyle?: React.CSSProperties;
  13. highlightClassName?: string;
  14. component?: string
  15. }
  16. const prefixCls = cssClasses.PREFIX;
  17. class Highlight extends PureComponent<HighlightProps> {
  18. static propTypes = {
  19. style: PropTypes.object,
  20. className: PropTypes.string,
  21. autoEscape: PropTypes.bool,
  22. caseSensitive: PropTypes.bool,
  23. sourceString: PropTypes.string,
  24. searchWords: PropTypes.arrayOf(PropTypes.string),
  25. highlightStyle: PropTypes.object,
  26. highlightClassName: PropTypes.string,
  27. component: PropTypes.string
  28. };
  29. static defaultProps = {
  30. component: 'mark',
  31. autoEscape: true,
  32. caseSensitive: false,
  33. sourceString: '',
  34. };
  35. render() {
  36. const {
  37. searchWords,
  38. sourceString,
  39. component,
  40. highlightClassName,
  41. highlightStyle,
  42. caseSensitive,
  43. autoEscape,
  44. } = this.props;
  45. const tagCls = cls({
  46. [`${prefixCls}-tag`]: true,
  47. }, highlightClassName);
  48. const option = {
  49. highlightTag: component,
  50. highlightClassName: tagCls,
  51. highlightStyle,
  52. caseSensitive,
  53. autoEscape,
  54. };
  55. return (
  56. getHighLightTextHTML({ sourceString, searchWords, option })
  57. );
  58. }
  59. }
  60. export default Highlight;