【问题标题】:mui v5 styled Dialog not accepting styled widthmui v5 样式对话框不接受样式宽度
【发布时间】:2021-11-29 04:30:07
【问题描述】:

我有 mui v5 对话框,我无法使用 style() 组件设置其宽度。

import {
  Dialog,
  DialogContent,
  DialogTitle,
  Paper,
  Typography,
} from "@mui/material";
import { Close } from "@mui/icons-material";
import { styled } from "@mui/material/styles";
import ActionButton from "./ActionButton";

import React from "react";

const StyledDialog = styled(Dialog)(({ theme }) => ({
  fullWidth: true, // it's not taking effect
  maxWidth: "lg",  // it's not taking effect
  padding: theme.spacing(2),
  position: "absolute",
  top: theme.spacing(5),
  "& MuiDialog-paper": {
    padding: theme.spacing(2),
    position: "absolute",
    top: theme.spacing(5),
  },
  "& .MuiTypography-h6": {
    paddingRight: "0px",
  },
}));

export default function Popup(props) {
  const { title, children, openPopup, setOpenPopup } = props;
  return (
    <StyledDialog open={openPopup} onClose={() => setOpenPopup(false)}>
      <DialogTitle>
        <div style={{ display: "flex" }}>
          <Typography variant="h6" component="div" style={{ flexGrow: 1 }}>
            {title}
          </Typography>
          <ActionButton
            color="secondary"
            onClick={() => setOpenPopup(false)}
          >
            <Close />
          </ActionButton>
        </div>
      </DialogTitle>
      <DialogContent dividers>{children}</DialogContent>
    </StyledDialog>

但是,如果我直接在其中列出道具,它就可以工作。

<StyledDialog fullWidth="true" maxWidth="lg">

</StyledDialog>

设置宽度和最大宽度的正确方法是什么。

【问题讨论】:

    标签: javascript reactjs typescript material-ui


    【解决方案1】:

    在 styledDialog 声明上试试这个:

    const StyledDialog = styled((props) => (
        <Dialog
            fullWidth={true}
            maxWidth={'lg'}
            {...props}
        />
    ))(({ theme }) => ({
        // Your code to style the dialog goes here
    }));
    

    您的代码的问题是您没有将属性 fullWidth 和 maxWidth 传递给组件。

    【讨论】:

    • 它有效,但用于验证。你能在主题旁边传递道具而不是 styled(props)
    【解决方案2】:

    您正在尝试像 css 一样使用 maxWidth: lgfullWidth: true,但它们仅适用于 Dialog API

    所以你可以在组件中使用maxWidth 作为 API,比如

    <Dialog maxWidth="lg" fullWidth ... >
    

    或者您可以使用主题将其添加为 css 样式。

    const StyledDialog = styled(Dialog)(({ theme }) => ({
      maxWidth: theme.breakpoints.values.lg, // there's no styling such fullWidth
      ...
    }));
    

    你可以看看default theme

    【讨论】:

    • 它不起作用。
    • @Moe,什么不起作用?能不能具体一点?
    • 根据你需要全宽工作的文档,它有多大取决于 maxWidth。转到全角,但 max 是 maxWidth。以您的方式实施它不会改变窗口大小。它不会扩展或任何东西。
    • 您可以检查浏览器的实际工作方式。否则,请向我提供您的沙箱或 git 存储库,以便我查看。
    猜你喜欢
    • 2022-10-05
    • 2022-01-25
    • 2022-11-23
    • 1970-01-01
    • 2022-11-10
    • 2022-01-13
    • 2022-07-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多