TooltipInput.tsx 3.6 KB
Newer Older
R
Rongfeng Fu 已提交
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 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
import { useEffect, useState, useRef } from 'react';
import { Input, Tooltip } from 'antd';

interface Props {
  value?: string;
  onChange?: (value?: string) => void;
  placeholder: string;
  name: string;
  isPassword: boolean;
  fieldProps: any;
}

export default ({
  value,
  onChange,
  placeholder,
  name,
  isPassword,
  fieldProps,
}: Props) => {
  const [visible, setVisible] = useState(false);
  const [currentValue, setCurrentValue] = useState(value);
  const open = useRef();
  open.current = {
    input: false,
    tooltip: false,
  };

  const onMouseEnterInput = () => {
    open.current = {
      ...(open?.current || {}),
      input: true,
    };
    setVisible(true);
  };

  const onMouseEnterTooltip = () => {
    open.current = {
      ...(open?.current || {}),
      tooltip: true,
    };
    setVisible(true);
  };

  const onMouseLeaveInput = () => {
    setTimeout(() => {
      if (!open?.current?.tooltip) {
        setVisible(false);
      }
    }, 300);
  };

  const onMouseLeaveTooltip = () => {
    setVisible(false);
  };

  const addEventTooltipOverlay = () => {
    const tooltipOverlay = document.querySelector(
      `.tooltip-input-tooltip-overlay-${name}`,
    );
    if (tooltipOverlay) {
      tooltipOverlay?.addEventListener('mouseenter', onMouseEnterTooltip);
      tooltipOverlay?.addEventListener('mouseleave', onMouseLeaveTooltip);
    } else {
      setTimeout(() => {
        addEventTooltipOverlay();
      }, 500);
    }
  };

  const addEventInputConatiner = () => {
    const inputConatiner = document.querySelector(`.tooltip-input-${name}`);
    if (inputConatiner) {
      inputConatiner?.addEventListener('mouseenter', onMouseEnterInput);
      inputConatiner?.addEventListener('mouseleave', onMouseLeaveInput);
    } else {
      setTimeout(() => {
        addEventInputConatiner();
      }, 500);
    }
  };

  useEffect(() => {
    const tooltipOverlay = document.querySelector(
      `.tooltip-input-tooltip-overlay-${name}`,
    );
    const inputConatiner = document.querySelector(`.tooltip-input-${name}`);
    addEventTooltipOverlay();
    addEventInputConatiner();
    return () => {
      tooltipOverlay?.removeEventListener('mouseenter', onMouseEnterTooltip);
      tooltipOverlay?.removeEventListener('mouseleave', onMouseLeaveTooltip);
      inputConatiner?.removeEventListener('mouseenter', onMouseEnterInput);
      inputConatiner?.removeEventListener('mouseleave', onMouseLeaveInput);
    };
  }, []);

  useEffect(() => {
    if (onChange) {
      onChange(currentValue);
    }
  }, [currentValue]);

  return (
    <Tooltip
      open={!currentValue && placeholder?.length > 48 && visible}
      title={placeholder}
      overlayClassName={`tooltip-input-tooltip-overlay-${name}`}
    >
      {isPassword ? (
        <Input.Password
          className={`tooltip-input-${name}`}
          placeholder={placeholder}
          allowClear
          value={currentValue}
          onChange={(e: any) => {
            setCurrentValue(e?.target?.value);
            setVisible(false);
          }}
          style={{ width: 448 }}
          onFocus={() => setVisible(false)}
          {...fieldProps}
        />
      ) : (
        <Input
          className={`tooltip-input-${name}`}
          placeholder={placeholder}
          allowClear
          value={currentValue}
          onChange={(e: any) => {
            setCurrentValue(e?.target?.value);
            setVisible(false);
          }}
          autoComplete="off"
          style={{ width: 448 }}
          onFocus={() => setVisible(false)}
          {...fieldProps}
        />
      )}
    </Tooltip>
  );
};