Jul*_*nay 19 reactjs material-ui
我正在尝试将样式应用于ListItemText(Material-UI @next)中的文本:
const text = {
color: 'red'
}
<ListItem button><ListItemText style={text} primary="MyText" /></ListItem>
Run Code Online (Sandbox Code Playgroud)
但是<Typograhy>里面的渲染元素根本没有样式("MyText"不是红色).
查看生成的代码,似乎Typography> subheading的默认CSS规则覆盖了我的CSS.
谢谢你的帮助
编辑:在问题的第一个版本中,有一个错误("className"而不是ListItemText上的"style"道具,对不起).
小智 25
我相信现在实现这一目标的唯一方法是使用ListItemText元素的'disableTypography'道具.
<ListItemText
disableTypography
primary={<Typography type="body2" style={{ color: '#FFFFFF' }}>MyTitle</Typography>}
/>
Run Code Online (Sandbox Code Playgroud)
这使您可以使用自己想要的样式嵌入自己的文本元素.
小智 11
根据文档,该<ListItemText />组件公开了 prop primaryTypographyProps,我们可以使用它来完成您在问题中尝试的操作:
const text = {
color: "red"
};
<ListItem button>
<ListItemText primaryTypographyProps={{ style: text }} primary="MyText" />
</ListItem>
Run Code Online (Sandbox Code Playgroud)
希望有帮助!
Nea*_*arl 10
MUI v5 更新
您可以利用系统属性Typography直接在primary和secondary内的组件中添加样式道具ListItemText:
<ListItemText
primary="Photos"
secondary="Jan 9, 2014"
primaryTypographyProps={{
fontSize: 22,
color: 'primary.main',
}}
secondaryTypographyProps={{
fontSize: 15,
color: 'green',
}}
/>
Run Code Online (Sandbox Code Playgroud)
styled如果您想ListItemText在多个地方重复使用,也可以使用:
import MuiListItemText from '@mui/material/ListItemText';
import { styled } from '@mui/material/styles';
const ListItemText = styled(MuiListItemText)({
'& .MuiListItemText-primary': {
color: 'orange',
},
'& .MuiListItemText-secondary': {
color: 'gray',
},
});
Run Code Online (Sandbox Code Playgroud)
事实证明,有一种更好的方法可以做到这一点:
const styles = {
selected: {
color: 'green',
background: 'red',
},
}
const DashboardNagivationItems = props => (
<ListItemText
classes={{ text: props.classes.selected }}
primary="Scheduled Calls"
/>
)
export default withStyles(styles)(DashboardNagivationItems)
Run Code Online (Sandbox Code Playgroud)
您可以在此处详细了解如何完成此操作:https://material-ui-next.com/customization/overrides/#overriding-with-classes
<ListItem >
<Avatar style={{ backgroundColor: "#ff6f00" }}>
<LabIcon />
</Avatar>
<ListItemText
primary={<Typography variant="h6" style={{ color: '#ff6f00' }}>Lab</Typography>}
secondary="Experiments" />
</ListItem>
Run Code Online (Sandbox Code Playgroud)
这是一个很好的选择,您可以在不禁用排版的情况下实现
<ListItemText
classes={{ primary: this.props.classes.whiteColor }}
primary="MyTitle"
/>
Run Code Online (Sandbox Code Playgroud)
如果您使用的是 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);
Run Code Online (Sandbox Code Playgroud)
在primary属性上设置所有与文本相关的样式。可悲的是它隐藏在文档中如此之深。https://material-ui.com/api/list-item/
const textColor = {
color: "red"
};
<ListItemText primaryTypographyProps={{ style: textColor }} primary="BlodyLogic" />
Run Code Online (Sandbox Code Playgroud)
对于次要文本
const secondaryColor = {
color: 'green'
}
<ListItemText secondaryTypographyProps={{ style: secondaryColor }}
secondary="If you say that you" />
Run Code Online (Sandbox Code Playgroud)
<ListItemText
primary={
<Typography variant="caption" display="block" gutterBottom>
caption text
</Typography>
}
/>
Run Code Online (Sandbox Code Playgroud)
定制设计:
const useStyles = makeStyles({
text: {
color: 'red',
fontSize: 49
},
});
/.....// all make classes
<ListItemText
primary={
<Typography className={classes.text}>
caption text
</Typography>
}
/>
Run Code Online (Sandbox Code Playgroud)
我想向 @SundaramRavi 添加一些关于以下方面的内容:
Whatever.styles.js
const styles = theme => ({
white: {
color: theme.palette.common.white
}
});
exports.styles = styles;
Run Code Online (Sandbox Code Playgroud)
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);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
20130 次 |
| 最近记录: |