简体   繁体   English

我有一个 Button.tsx 文件,我在其中使用情感样式来设置 3 个按钮的样式

[英]I have a Button.tsx file where I am using emotion styled to style 3 buttons

type SubPortalButtonProps = {
  name: JSX.Element;
  buttonType?: "clear" | "search" | "availabilty";
  onClick?: React.MouseEventHandler<HTMLButtonElement>;
};

type SubPortalButtonStyledProps = {
  typeOfButton?: "clear" | "search" | "availabilty";
};

const SubPortalButtonStyled = styled.button`
${(props:SubPortalButtonStyledProps) =>
  props.typeOfButton ==="clear" ? {
          background: "linear-gradient( #eaf0f7 30%,#ecf2f8  , #cadbeb 60%)",
          width: "7.5rem",
          height: "1.6rem",
          borderRadius: ".1rem",
          marginLeft:"1rem",
          fontSize: "1rem",
          textAlign: "center",
          borderWidth: ".01px",

  } : props.typeOfButton ==="search" ? {
      background: "linear-gradient( #659efa 30%,#3b85fc  , #0d68fe 60%)",
      color: "white",
      width: "10rem",
      height: "2.3rem",
      float: "right",
      margin: ".6rem 1rem 2rem 1rem",
      borderRadius: ".1rem",
      fontSize: "1.3rem",
      borderWidth: ".01px",
  } : props.typeOfButton == "availabilty" ? {
      color: "#306767",
      width: "11.5rem",
      paddingTop: ".3rem",
      paddingBottom: "1.5rem",
      marginLeft: "0.1rem",
      background: "linear-gradient(rgb(203, 225, 236) 10%,#fff 40%, rgb(203, 225, 236))",
      borderWidth: ".01px",
      height: "1.3rem",
      paddingLeft: "5px",
      textAlign: "left",
      top: ".5rem",
      fontSize: ".90rem",
      fontWeight: "900",
      transitionDuration: "0.4s",
      ":hover": {
        background:
          "linear-gradient(rgb(166, 214, 238) 10%,#fff 40%, rgb(177, 203, 216))",
        color: "#113a3a",
      },
  } : null   
}:`

Here I am getting the below error when I provide ":null" as in the above ternary operator.在这里,当我像上面的三元运算符一样提供“:null”时,出现以下错误。

     No overload matches this call.
      Overload 1 of 2, '(...styles: Interpolation<SubPortalButtonStyledProps & { theme: object; }> 
[]): StyledComponent<DetailedHTMLProps<ButtonHTMLAttributes<HTMLButtonElement>, HTMLButtonElement>, SubPortalButtonStyledProps, object>', gave the following error.
Argument of type 'TemplateStringsArray' is not assignable to parameter of type 'Interpolation<SubPortalButtonStyledProps & { theme: object; }>'.
  Type 'TemplateStringsArray' is not assignable to type 'ObjectInterpolation<SubPortalButtonStyledProps & { theme: object; }>'.
    Types of property 'filter' are incompatible.
      Type '{ <S extends string>(predicate: (value: string, index: number, array: readonly string[]) => value is S, thisArg?: any): S[]; (predicate: (value: string, index: number, array: readonly string[]) => unknown, thisArg?: any): string[]; }' is not assignable to type 'string | string[] | undefined'.
        Type '{ <S extends string>(predicate: (value: string, index: number, array: readonly string[]) => value is S, thisArg?: any): S[]; (predicate: (value: string, index: number, array: readonly string[]) => unknown, thisArg?: any): string[]; }' is missing the following properties from type 'string[]': pop, push, concat, join, and 28 more.
      Overload 2 of 2, '(template: TemplateStringsArray, ...styles: Interpolation<SubPortalButtonStyledProps & { theme: object; }>[]): StyledComponent<...>', gave the following error.
Argument of type '(props: SubPortalButtonStyledProps) => { background: string; width: string; height: string; borderRadius: string; marginLeft: string; fontSize: string; textAlign: "center"; borderWidth: string; color?: undefined; float?: undefined; ... 7 more ...; ":hover"?: undefined; } | { ...; } | { ...; } | null' is not assignable to parameter of type 'Interpolation<SubPortalButtonStyledProps & { theme: object; }>'.
  Type '(props: SubPortalButtonStyledProps) => { background: string; width: string; height: string; borderRadius: string; marginLeft: string; fontSize: string; textAlign: "center"; borderWidth: string; color?: undefined; float?: undefined; ... 7 more ...; ":hover"?: undefined; } | { ...; } | { ...; } | null' is not assignable to type 'FunctionInterpolation<SubPortalButtonStyledProps & { theme: object; }>'.
    Type '{ background: string; width: string; height: string; borderRadius: string; marginLeft: string; fontSize: string; textAlign: "center"; borderWidth: string; color?: undefined; float?: undefined; ... 7 more ...; ":hover"?: undefined; } | { ...; } | { ...; } | null' is not assignable to type 'Interpolation<SubPortalButtonStyledProps & { theme: object; }>'.
      Type '{ color: string; width: string; paddingTop: string; paddingBottom: string; marginLeft: string; background: string; borderWidth: string; height: string; paddingLeft: string; textAlign: "left"; top: string; ... 6 more ...; margin?: undefined; }' is not assignable to type 'Interpolation<SubPortalButtonStyledProps & { theme: object; }>'.
        Type '{ color: string; width: string; paddingTop: string; paddingBottom: string; marginLeft: string; background: string; borderWidth: string; height: string; paddingLeft: string; textAlign: "left"; top: string; ... 6 more ...; margin?: undefined; }' is not assignable to type 'ObjectInterpolation<SubPortalButtonStyledProps & { theme: object; }>'.
          Types of property 'fontWeight' are incompatible.
            Type '"900"' is not assignable to type 'FontWeightProperty | FontWeightProperty[] | (Globals | "bold" | "normal" | "bolder" | "lighter")[] | undefined'.ts(2769) 

Also while I use the same code in another projects there are no issues and the code runs smoothly.此外,当我在另一个项目中使用相同的代码时,没有问题并且代码运行顺利。 How do fix this?如何解决这个问题? Is it because of some project cache or something?是因为一些项目缓存还是什么? If so how do I clear it?如果是这样,我该如何清除它?

In this case, I recommend using css functions, see how easy it is to use it, and your code will become cleaner and readable:在这种情况下,我推荐使用 css 函数,看看它是多么容易使用,你的代码会变得更干净和可读:

import css, { styled } from 'styled-components';

type SubPortalButtonProps = {
  name: JSX.Element;
  buttonType?: 'clear' | 'search' | 'availabilty';
  onClick?: React.MouseEventHandler<HTMLButtonElement>;
};

export const buttonVariant: Record<NonNullable<SubPortalButtonProps['buttonType']>, ReturnType<typeof css>> = {
  clear: css`
    background: "linear-gradient( #eaf0f7 30%,#ecf2f8  , #cadbeb 60%)",
    width: "7.5rem",
    height: "1.6rem",
    borderRadius: ".1rem",
    marginLeft:"1rem",
    fontSize: "1rem",
    textAlign: "center",
    borderWidth: ".01px",
  `,
  search: css`
    background: "linear-gradient( #659efa 30%,#3b85fc  , #0d68fe 60%)",
    color: "white",
    width: "10rem",
    height: "2.3rem",
    float: "right",
    margin: ".6rem 1rem 2rem 1rem",
    borderRadius: ".1rem",
    fontSize: "1.3rem",
    borderWidth: ".01px",
  `,
  availabilty: css`
    color: "#306767",
    width: "11.5rem",
    paddingTop: ".3rem",
    paddingBottom: "1.5rem",
    marginLeft: "0.1rem",
    background: "linear-gradient(rgb(203, 225, 236) 10%,#fff 40%, rgb(203, 225, 236))",
    borderWidth: ".01px",
    height: "1.3rem",
    paddingLeft: "5px",
    textAlign: "left",
    top: ".5rem",
    fontSize: ".90rem",
    fontWeight: "900",
    transitionDuration: "0.4s",

    ":hover": {
      background:
        "linear-gradient(rgb(166, 214, 238) 10%,#fff 40%, rgb(177, 203, 216))",
      color: "#113a3a",
    },
  `,
};

export const Button = styled.button<SubPortalButtonProps>`
  ${({ buttonType }) => buttonVariant[buttonType]};
  // someDuplicateCSS: values;
`; 

and using in tsx file:并在 tsx 文件中使用:

<>
 <Button buttonType="clear" name="clearButton" />
 <Button buttonType="search" name="searchButton" />
 <Button buttonType="availability" name="availabilityButton" />
</>

I hope this makes sense.我希望这是有道理的。 some duplicate properties can be taken out as a function in the initialization of the style component itself在样式组件本身的初始化中可以取出一些重复的属性作为function

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM