简体   繁体   English

如何覆盖 MUI 父组件 css 以显示:反应中按钮的“块”

[英]How to override MUI parent component css to display: 'block' for buttons in react

I have a modal that requires a change for our mobile breakpoint in which the buttons should display as a stack above each other.我有一个模式需要更改我们的移动断点,其中按钮应显示为彼此上方的堆栈。 Each button should take up the width of the modal, something like the attached picture.每个按钮都应该占据模态框的宽度,类似于附图。 I am trying to figure out how to override the component to change the css.我试图弄清楚如何覆盖组件以更改 css。

Here's the parent component:这是父组件:

<StyledDialogFooter sx={{ padding: 0, pt: 2 }}>
            {(secondaryButtonProps || secondaryButton) && (
              <Button
                variant="contained"
                color="secondary" 
                size="small"
                {...secondaryButtonProps}
                onClick={debouncedSecondaryClick}
                data-testid={noButtonTestId}
              >
                {secondaryButtonProps?.children ?? 'No'}
              </Button>
            )}
            {(primaryButtonProps || primaryButton) && (
              <Button
                variant="contained"
                color="primary"
                size="small"
                {...primaryButtonProps}
                onClick={debouncedPrimaryClick}
                data-testid={yesButtonTestId}
              >
                {primaryButtonProps?.children ?? 'Yes'}
              </Button>
            )}
          </StyledDialogFooter>

Component that calls on the parent, accepts button props:调用父级的组件,接受按钮道具:

<Dialog
      open={open}
      loading={waitingForResponse}
      title={'Manage sharing'}
      onClose={onClose}
      paperSx={{ width: '380px !important' }}
      primaryButton
      primaryButtonProps={{
        onClick: onSave,
        variant: 'contained',
        color: 'primary',
        children: 'Save',
        disabled: saveButtonDisabled,
        sx: {
          ...(breakpoint === 'sm' && {
            width: '100%',
            display: 'block',
          }),
        },
      }}
      secondaryButton
      secondaryButtonProps={{
        onClick: onCancel,
        variant: 'contained',
        color: 'secondary',
        children: 'Cancel',
        sx: {
          ...(breakpoint === 'sm' && {
            width: '100%',
            display: 'block',
          }),
        },
      }}
    >

I have tried adding custom css with the sx prop, but it it still showing as the buttons side by side.我尝试使用 sx 道具添加自定义 css,但它仍然并排显示为按钮。 Any ideas how to override this?任何想法如何覆盖它? 按钮图片

How it is now:现在怎么样了: 在此处输入图像描述

My initial thought for this definitely involves using Grid.我对此的最初想法肯定涉及使用 Grid。

However, not the baked in MUI Grid but instead, take the time to learn the CSS solution for grid as it's far more robust.但是,不是MUI Grid中的烘焙,而是花时间学习网格的 CSS 解决方案,因为它更加健壮。

I've taken advantage of both MUI breakpoints and regular CSS grid together:我同时利用了 MUI 断点和常规 CSS 网格:

<Dialog>
      <DialogContent>
        <Box
          sx={{
            display: "grid",
            gridTemplateColumns: "repeat(12, 1fr)",
            gap: 2,
          }}
        >
          <Button
            sx={{
              gridColumn: {
                xs: "span 12",
                sm: "span 12",
                md: "span 6",
                lg: "span 6",
                xl: "span 6",
              },
            }}
          >
            Button_1
          </Button>
          <Button
            sx={{
              gridColumn: {
                xs: "span 12",
                sm: "span 12",
                md: "span 6",
                lg: "span 6",
                xl: "span 6",
              },
            }}
          >
            Button_1
          </Button>
        </Box>
      </DialogContent>
    </Dialog>

The MUI box is simply a div so that's why I put the Grid on the Box. MUI 框只是一个 div,所以这就是我将 Grid 放在 Box 上的原因。 This would conveniently utilize MUI breakpoints on your buttons so they span the whole width of the container on xs and sm screens.这将方便地利用按钮上的 MUI 断点,以便它们跨越xssm屏幕上容器的整个宽度。 Regardless, take the time to learn more about Grid (if you haven't) - I find it to be a life saver when trying pin down layouts.无论如何,花点时间了解更多关于 Grid 的信息(如果你还没有的话)——我发现它在尝试固定布局时可以挽救生命。

you should use Stack:你应该使用堆栈:

<Stack
  direction={{ sm: 'column', md: 'row' }}
  spacing={{ xs: 1, sm: 2, md: 4 }}
>
  <Item>Item 1</Item>
  <Item>Item 2</Item>
  <Item>Item 3</Item>
</Stack>

and replace evry Item with Button or any Component并用 Button 或任何组件替换 evry Item

You should take a different approach to achieve the result you want using the components you have from material-ui.您应该采用不同的方法来使用您在 material-ui 中拥有的组件来实现您想要的结果。 The button is designed in a specific way and changing the way it behaves should be your last option.该按钮以特定方式设计,更改其行为方式应该是您的最后选择。 You better put the buttons in a Stack component which you can change the direction based on the screen size.您最好将按钮放在 Stack 组件中,您可以根据屏幕大小更改方向。 Here is how you can do it:您可以这样做:

<Stack spacing={2} direction={{xs: 'column', sm: 'row', md: 'row'}}>
  <Button>Button 1</Button>
  <Button>Button 2</Button>
</Stack>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM