index.tsx 2.18 KB
Newer Older
dechen lin's avatar
dechen lin committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import { Popover, Tooltip } from 'antd';
import React, { useRef, useState } from 'react';
import styles from './index.module.scss';

import { RefObject } from 'react';
import { useIsomorphicLayoutEffect, useMemoizedFn } from 'ahooks';

export function useResizeEffect<T extends HTMLElement>(effect: (target: T) => void, targetRef: RefObject<T>) {
  const fn = useMemoizedFn(effect);
  useIsomorphicLayoutEffect(() => {
    const target = targetRef.current;
    if (!target) return;
    if (window.ResizeObserver) {
      let animationFrame: number;
      const observer = new ResizeObserver(() => {
        animationFrame = window.requestAnimationFrame(() => fn(target));
      });
      observer.observe(target);
      return () => {
        window.cancelAnimationFrame(animationFrame);
        observer.disconnect();
      };
    } else {
      fn(target);
    }
  }, [targetRef]);
}

interface ITextTooltip {
  style?: React.CSSProperties;
  str: string;
  suffix?: React.ReactNode | string;
  trigger?: 'hover' | 'click';
  handleClick?: () => void;
}

export const TextTooltip = (props: ITextTooltip) => {
  const { style = {}, str, trigger = 'click', suffix, handleClick } = props;
  const rootRef = useRef<HTMLDivElement>(null);
  const tooltipRef = useRef<HTMLDivElement>(null);
  const [clickable, setClickable] = useState(false);
  function calcEllipsised() {
    // 没有被截断
    if (tooltipRef!?.current!?.scrollWidth > tooltipRef!?.current!?.clientWidth) {
      setClickable(true);
    } else {
      setClickable(false);
    }
  }
  useResizeEffect(calcEllipsised, rootRef);

  return (
    <Tooltip
      title={<div className="bg-black/[0.85] text-white p-[6px]">{str}</div>}
      trigger={clickable ? trigger : ('' as 'click')}
      overlayClassName={styles.textTooltip}
      style={{ width: '100%' }}
      zIndex={999999}
      placement="right"
      align={{
        offset: [72, 0]
      }}
    >
      <div style={{ width: '100%', ...style }} className="flex" ref={rootRef}>
        <div className="text-ellipsis overflow-hidden whitespace-nowrap" ref={tooltipRef}>
          <span onClick={() => handleClick?.()}>{str}</span>
        </div>
        {suffix}
      </div>
    </Tooltip>
  );
};