reactjs - 如何使徽章出现在 Material UI 中的卡片标题旁边

标签 reactjs material-ui

如何让徽章出现在标题旁边。 CardActions 中出现的图标可以在右侧。有什么方法可以让徽章出现在标题旁边而不使用绝对定位?

https://codesandbox.io/embed/material-ui-card-styling-example-s9ko1

import React from "react";
import ReactDOM from "react-dom";
import { withStyles } from "@material-ui/core/styles";
import Avatar from "@material-ui/core/Avatar";
import Card from "@material-ui/core/Card";
import CardMedia from "@material-ui/core/CardMedia";
import CardContent from "@material-ui/core/CardContent";
import Divider from "@material-ui/core/Divider";
import Typography from "@material-ui/core/Typography";
import MoreVertIcon from "@material-ui/icons/MoreVert";
import CardHeader from "@material-ui/core/CardHeader";
import IconButton from "@material-ui/core/IconButton";
import Badge from "@material-ui/core/Badge";

import "./styles.css";

const faces = [
  "http://i.pravatar.cc/300?img=1",
  "http://i.pravatar.cc/300?img=2",
  "http://i.pravatar.cc/300?img=3",
  "http://i.pravatar.cc/300?img=4"
];

const styles = muiBaseTheme => ({
  card: {
    maxWidth: 300,
    margin: "auto",
    transition: "0.3s",
    boxShadow: "0 8px 40px -12px rgba(0,0,0,0.3)",
    "&:hover": {
      boxShadow: "0 16px 70px -12.125px rgba(0,0,0,0.3)"
    }
  },
  media: {
    paddingTop: "56.25%"
  },
  content: {
    textAlign: "left",
    padding: muiBaseTheme.spacing.unit * 3
  },
  divider: {
    margin: `${muiBaseTheme.spacing.unit * 3}px 0`
  },
  heading: {
    fontWeight: "bold"
  },
  subheading: {
    lineHeight: 1.8
  },
  avatar: {
    display: "inline-block",
    border: "2px solid white",
    "&:not(:first-of-type)": {
      marginLeft: -muiBaseTheme.spacing.unit
    }
  }
});

function App({ classes }) {
  return (
    <div className="App">
      <Card className={classes.card}>
        <CardHeader
          action={
            <div>
              <Badge badgeContent={"2"} color="secondary">
                {" "}
              </Badge>
              <IconButton aria-label="settings">
                <MoreVertIcon />
              </IconButton>
            </div>
          }
          title="Shrimp"
          subheader="September 14, 2016"
        />
        <CardMedia
          className={classes.media}
          image={
            "https://image.freepik.com/free-photo/river-foggy-mountains-landscape_1204-511.jpg"
          }
        />
        <CardContent className={classes.content}>
          <Typography
            className={"MuiTypography--heading"}
            variant={"h6"}
            gutterBottom
          >
            Nature Around Us
          </Typography>
          <Typography
            className={"MuiTypography--subheading"}
            variant={"caption"}
          >
            We are going to learn different kinds of species in nature that live
            together to form amazing environment.
          </Typography>
          <Divider className={classes.divider} light />
          {faces.map(face => (
            <Avatar className={classes.avatar} key={face} src={face} />
          ))}
        </CardContent>
      </Card>
    </div>
  );
}

const WrappedApp = withStyles(styles)(App);

const rootElement = document.getElementById("root");
ReactDOM.render(<WrappedApp />, rootElement);

最佳答案

包含 Badge 似乎工作正常在 title属性(property)。

        <CardHeader
          action={
            <div>
              <IconButton aria-label="settings">
                <MoreVertIcon />
              </IconButton>
            </div>
          }
          title={
            <Badge badgeContent={"2"} color="secondary">
              Shrimp
            </Badge>
          }
          subheader="September 14, 2016"
        />

Edit Material-UI Card Styling Example

关于reactjs - 如何使徽章出现在 Material UI 中的卡片标题旁边,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57414297/

相关文章:

reactjs - 通过排序/分页向表格添加骨架效果

javascript - ReactJS keyevent 不起作用

css - 如何在 material ui REACTjs 中覆盖 menuItem 中的选定类?

css - Material 界面 : How can I change the border color of the Select component?

javascript - 将 defaultProps 与 withStyles 一起使用

visual-studio-code - 找不到 Visual Studio Code 的 ESLint 配置

javascript - 导致 React 组件重新渲染的事件列表

reactjs - axios嵌套请求函数返回

javascript - React - 过滤商店中的对象会导致递归/无限循环

javascript - Material-ui v.1 - MenuItem 传递外部索引 onClick