如何添加类型的样式组件

发布于 2025-02-06 19:17:14 字数 2305 浏览 1 评论 0原文

我正在使用antd进行工具提示,并且想将Props 添加到样式的组件中。

当我将悬停在样式组件中留下的道具时,我会遇到以下错误:

Property 'left' does not exist on type 'ThemedStyledProps<(TooltipProps & RefAttributes<unknown>) & {}, DefaultTheme>'.
  Property 'left' does not exist on type 'TooltipPropsWithTitle & RefAttributes<unknown> & ThemeProps<DefaultTheme>'.ts(2339)

import styled from 'styled-components';
import { Tooltip as TooltipAnt } from 'antd';

export const Children = styled.div`
  position: relative;
  display: inline;
  > * {
    position: relative;
    display: inline;
  }
`;

export const Tooltip = styled(TooltipAnt)`
  .ant-tooltip-content {
    position: relative;
    overflow-x: hidden;
    overflow-y: visible;
    padding-bottom: 20px;
    display: inline-block;
  }

  .ant-tooltip {
    left: ${props => props.left || 0} !important;
  }

  .ant-tooltip-arrow {
    display: none;
  }

  .ant-tooltip-inner {
    padding: 10px 15px;
    background: white;
    display: inline-block;
    border-radius: 10px;
    border: 3px solid #D04A02;
    border-bottom-right-radius: 0;
    z-index: 2;
    max-width: 200px;
    color: #000;
    box-shadow: none;
    word-wrap: break-word;
  }

  .ant-tooltip-content:before {
    content: '';
    position: absolute;
    width: 25px;
    height: 25px;
    background: white;
    bottom: 14px;
    right: -8px;
    transform: rotate(25deg);
    border-bottom: 3px solid #D04A02;
  }

  .ant-tooltip-content:after {
    content: '';
    width: 3px;
    background: #D04A02;
    height: 50%;
    position: absolute;
    right: -0.5px;
    bottom: 12px;
  }
`;

import * as Styled from './Tooltip.styles';

type Props = {
    title: string;
    children: React.ReactNode;
    left?: string;
};

export const CustomTooltip = ({ title, children, left }: Props) => (
    <Styled.Tooltip
        title={title}
        getPopupContainer={(triggerNode) => triggerNode}
        left={left}
    >
        <Styled.Children>{children}</Styled.Children>
    </Styled.Tooltip>
);

I am making tooltip using antd and I would like to add props leftto styled components.

I am getting following error when I hover left prop in styled components:

Property 'left' does not exist on type 'ThemedStyledProps<(TooltipProps & RefAttributes<unknown>) & {}, DefaultTheme>'.
  Property 'left' does not exist on type 'TooltipPropsWithTitle & RefAttributes<unknown> & ThemeProps<DefaultTheme>'.ts(2339)

import styled from 'styled-components';
import { Tooltip as TooltipAnt } from 'antd';

export const Children = styled.div`
  position: relative;
  display: inline;
  > * {
    position: relative;
    display: inline;
  }
`;

export const Tooltip = styled(TooltipAnt)`
  .ant-tooltip-content {
    position: relative;
    overflow-x: hidden;
    overflow-y: visible;
    padding-bottom: 20px;
    display: inline-block;
  }

  .ant-tooltip {
    left: ${props => props.left || 0} !important;
  }

  .ant-tooltip-arrow {
    display: none;
  }

  .ant-tooltip-inner {
    padding: 10px 15px;
    background: white;
    display: inline-block;
    border-radius: 10px;
    border: 3px solid #D04A02;
    border-bottom-right-radius: 0;
    z-index: 2;
    max-width: 200px;
    color: #000;
    box-shadow: none;
    word-wrap: break-word;
  }

  .ant-tooltip-content:before {
    content: '';
    position: absolute;
    width: 25px;
    height: 25px;
    background: white;
    bottom: 14px;
    right: -8px;
    transform: rotate(25deg);
    border-bottom: 3px solid #D04A02;
  }

  .ant-tooltip-content:after {
    content: '';
    width: 3px;
    background: #D04A02;
    height: 50%;
    position: absolute;
    right: -0.5px;
    bottom: 12px;
  }
`;

import * as Styled from './Tooltip.styles';

type Props = {
    title: string;
    children: React.ReactNode;
    left?: string;
};

export const CustomTooltip = ({ title, children, left }: Props) => (
    <Styled.Tooltip
        title={title}
        getPopupContainer={(triggerNode) => triggerNode}
        left={left}
    >
        <Styled.Children>{children}</Styled.Children>
    </Styled.Tooltip>
);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一紙繁鸢 2025-02-13 19:17:14

我曾经遇到过一个类似的问题,唯一修复的是将符号更改为包括该元素的道具:

export const Tooltip = styled(TooltipAnt)<{ left: number }>`
  .ant-tooltip-content {
    position: relative;
    overflow-x: hidden;
    overflow-y: visible;
    padding-bottom: 20px;
    display: inline-block;
  }

  .ant-tooltip {
    left: ${props => props.left || 0} !important;
  }

I ran to a similar problem once and the only thing that fixed it was changing the notation to include the prop on the element:

export const Tooltip = styled(TooltipAnt)<{ left: number }>`
  .ant-tooltip-content {
    position: relative;
    overflow-x: hidden;
    overflow-y: visible;
    padding-bottom: 20px;
    display: inline-block;
  }

  .ant-tooltip {
    left: ${props => props.left || 0} !important;
  }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文