0

我是材料 UI 的新手。在这里,我正在尝试创建一个样式化的组件,它将是一个Typography. 所以,我尝试的是,

import styled from 'styled-components';


 import {
  FormGroup,
  FormControlLabel,
  Tooltip,
  FormControl,
  Select,
  Radio,
  Typography,
  Button
} from '@material-ui/core'
const StyledTypography = styled.Typography<Itest>(({ marginLeft, marginRight }) => ({
}))

但这给了我一个编译时错误。

Property 'Typography' does not exist on type 'ThemedStyledInterface<ThemeInterface>'

谁能帮我这个 ?

我使用了以下方式

const StyledTypography = styled(Typography)<ISortBySelector>(({ fontSize }) => ({
  && {
  fontFamily: 'Roboto',
  fontSize: fontSize ? fontSize : '10px',
  fontWeight: 'normal',
  fontStretch: 'normal',
  fontStyle: 'normal',
  lineHeight: 'normal',
  letterSpacing: fontSize ? '0.14px' : '0.07px',
  width: fontSize ? '50px' : '35px',
  height: fontSize ? '19px' : '14px',
  color: '#000000',
  cursor: 'pointer'
  }
}))
4

2 回答 2

5

我不确定您是否在那里使用了正确的语法,但这是我通常将组件传递到样式组件的方式(注意我没有使用点表示法)。此外,您可以使用通常的 CSS 语法,而不是较低的驼峰格式。

import Typography from '@material-ui/core/Typography';
import styled from 'styled-components';

const StyledTypography = styled(Typography)<Itest>`
   && {
     font-family: Roboto;
     // customise your styles here
   }
`;

如果您希望将其他道具传递给StyledTypography,例如,设置variantcaption

<StyledTypography variant='caption'>
  Some text
</StyledTypography>
于 2020-04-09T04:25:19.483 回答
1

我不认为使用点符号是你的问题。我认为这就是您要实现的目标:

import { styled } from '@mui/material/styles'
import Typography from '@mui/material/Typography'

export const StyledTypography = styled(Typography)(({ theme }) => ({
  '&': {
    color: theme.palette.common.black,
    // ...and so on
  },
})) as typeof Typography

如果您不使用主题中的变量,则另一个答案是可以的。另外,请注意,当前component特定组件上的道具存在错误,因此as typeof Typography可能需要。

于 2021-12-02T11:59:28.047 回答