MUI - ListItemText 中的样式文本

MUI - Styling text inside ListItemText

我正在尝试将样式应用于 ListItemText (MUI) 中的文本:

const text = {
  color: 'red'
}

<ListItem button><ListItemText style={text} primary="MyText" /></ListItem>

但是内部呈现的 Typograhy 元素根本没有样式(“MyText”不是红色)。

查看生成的代码,似乎 Typography > 副标题的默认 CSS 规则覆盖了我的 CSS.

感谢您的帮助

编辑:在问题的第一个版本中,有一个错误(ListItemText 上的“className”而不是“style”属性,对此感到抱歉)。

我相信现在实现此目的的唯一方法是使用 ListItemText 元素的 'disableTypography' 属性。

 <ListItemText
        disableTypography
        primary={<Typography type="body2" style={{ color: '#FFFFFF' }}>MyTitle</Typography>}
      />

这让您可以在自己的文本元素中嵌入任何您想要的样式。

Material v1.0

我会向@SundaramRavi 添加一些关于:

  • 他使用样式元素的方式不像 Material v1.0 那样好(阅读 v0.16+ and v1.0.
  • 之间非常重要的区别
  • 为了更好地分离关注点而构建文件的方式。

Whatever.styles.js

const styles = theme => ({
  white: {
    color: theme.palette.common.white
  }
});

exports.styles = styles;

Whatever.js

const React = require('react');
const PropTypes = require('prop-types');
const {styles} = require('./Whatever.styles');

class Whatever extends React.Component {
  constructor(props) {
    super(props);
  }

  render() {
    const {classes} = this.props;
    return (
      <div>
        <ListItemText
          disableTypography
          primary={
            <Typography type="body2" style={{body2: classes.white}}>
              MyTitle
            </Typography>
          }
        />
      </div>
    );
  }
}

Whatever.propTypes = {
  classes: PropTypes.object.isRequired,
  theme: PropTypes.object.isRequired
};

exports.Whatever = withStyles(styles, {withTheme: true})(Whatever);

事实证明,还有一种更好的方法可以做到这一点:

const styles = {
  selected: {
    color: 'green',
    background: 'red',
  },
}

const DashboardNagivationItems = props => (
  <ListItemText
    classes={{ text: props.classes.selected }}
    primary="Scheduled Calls"
  />
)

export default withStyles(styles)(DashboardNagivationItems)

您可以在此处阅读有关如何完成此操作的更多信息:https://material-ui-next.com/customization/overrides/#overriding-with-classes

this 很好,你可以在不禁用排版的情况下实现

<ListItemText 
   classes={{ primary: this.props.classes.whiteColor }}
   primary="MyTitle"
/>
            <ListItem >
                    <Avatar style={{ backgroundColor: "#ff6f00" }}>
                      <LabIcon />
                    </Avatar>
                    <ListItemText 
                     primary={<Typography variant="h6" style={{ color: '#ff6f00' }}>Lab</Typography>}
                     secondary="Experiments" />
                  </ListItem>

如果您使用的是 material-ui 3.x,这是这样做的:

import { withStyles } from '@material-ui/core/styles';

const styles = {
  listItemText: {
    color: 'white',
  },
}

class YourComponent extends Component {
...
render() {
    const { classes } = this.props; // this is magically provided by withStyles HOC.
    return (
          <ListItem button>
            <ListItemIcon>
              <DashboardIcon />
            </ListItemIcon>
            <ListItemText classes={{ primary: classes.listItemText }} primary="My Bookings" />
          </ListItem>
    );
...

}
export default withStyles(styles)(YourComponent);

primary 属性 上设置所有与文本相关的样式。遗憾的是它在文档中隐藏得如此之深。 https://material-ui.com/api/list-item/

根据 documentation<ListItemText /> 组件公开了道具 primaryTypographyProps,我们可以使用它来完成您在问题中尝试的内容:

const text = {
    color: "red"
};

<ListItem button>
    <ListItemText primaryTypographyProps={{ style: text }} primary="MyText" />
</ListItem>

希望对您有所帮助!

您可以使用 & span

轻松设置文本样式
const useStyles = makeStyles(theme => ({
    listItem: {
        "& span": {
            color: "red"
        }

    }
}));
..
..
..

<ListItem button>
    <ListItemIcon>
        <SendIcon />
    </ListItemIcon>
    <ListItemText className={classes.listItem} primary="Sent mail"/>
</ListItem>

方法一

const textColor = {
    color: "red"
};

<ListItemText primaryTypographyProps={{ style: textColor }} primary="BlodyLogic" />
对于次要文本
const secondaryColor = {
   color: 'green'
}

<ListItemText secondaryTypographyProps={{ style: secondaryColor }} 
     secondary="If you say that you" />

方法二

<ListItemText
          primary={
            <Typography variant="caption" display="block" gutterBottom>
              caption text
            </Typography>
          }
 />

定制设计:

const useStyles = makeStyles({
      text: {
        color: 'red',
        fontSize: 49
      },
    });
        
/.....// all make classes

<ListItemText
              primary={
                <Typography className={classes.text}>
                  caption text
                </Typography>
              }
     />

Offical Docs

如果您使用的是 "@material-ui/core": "^4.11.4"(4.X.X 或更高版本) 那么这很简单:

#1st 步骤:像这样定义样式:

const useStyles = makeStyles((theme: Theme) =>
  createStyles({
    // Other Styling if you wish to use it
    root: {
      width: '100%',
      maxWidth: '36ch',
      backgroundColor: theme.palette.background.paper
    },
    // Other Styling if you wish to use it
    inline: {
      display: 'inline'
    },
    // Styling that you asked for
    textColor: {
        color: 'red'
    }
  }),
);

#2nd 步骤:定义一个常量使用 特定样式 像这样:

const AlignItemsList = (props: any) => {
    const classes = useStyles(); // Like this one
    ......
}

#3rd 步骤:在您的 ListItemText 组件中这样做:

const AlignItemsList = (props: any) => {
    const classes = useStyles();
    ......
    <ListItemText
         primary="Your Text Goes Here"
         classes={{ primary: classes.textColor }} // Like this one
         ...
   />
};

#4th & 最后一步:只需正常导出您的组件,无需任何其他内容,如下所示:

export default AlignItemsList;

MUI v5 更新

您可以利用 Typography 中的 system properties 直接在 ListItemText 中的 primarysecondary 组件中添加样式道具:

<ListItemText
  primary="Photos"
  secondary="Jan 9, 2014"
  primaryTypographyProps={{
    fontSize: 22,
    color: 'primary.main',
  }}
  secondaryTypographyProps={{
    fontSize: 15,
    color: 'green',
  }}
/>

如果你想在多个地方重复使用ListItemText,你也可以使用styled

import MuiListItemText from '@mui/material/ListItemText';
import { styled } from '@mui/material/styles';

const ListItemText = styled(MuiListItemText)({
  '& .MuiListItemText-primary': {
    color: 'orange',
  },
  '& .MuiListItemText-secondary': {
    color: 'gray',
  },
});

现场演示