简体   繁体   English

在 Material-UI 选项卡中使用分隔符

[英]Using Dividers inside Material-UI Tabs

If I want to use a Divider or something else that isn't a Tab inside Material-UI Tabs, I get DOM warnings in the console.如果我想使用 Divider 或其他不是 Material-UI 选项卡中的选项卡的东西,我会在控制台中收到 DOM 警告。

<Tabs ...>
  //...
  <Divider />
  //...
</Tabs>

A workaround for this is to create a middleman-kind class like this:一种解决方法是创建一个中间人类型的 class ,如下所示:

<Tabs ...>
   //...
   <MDivider />
   //...
</Tabs>

function MDivider(props) {
  return <Divider />;
}

But I was thinking if this is the best solution to solve the issue or if there are other, better ways to stop getting the warning.但我在想这是否是解决问题的最佳解决方案,或者是否有其他更好的方法来停止收到警告。

codesandbox with error here此处有错误的代码框
codesandbox with fix here在此处修复的代码沙盒

Ok, so I think I found the best fix based on how the MUI Tabs are meant to be used.好的,所以我想我找到了基于 MUI 选项卡的使用方式的最佳解决方案。 If Tabs are only meant to have MUI Tab children inside, then the MUI-intended way to do this would be to add the Divider like this:如果 Tabs 只是为了在其中包含 MUI Tab 子项,那么 MUI 打算这样做的方法是添加 Divider,如下所示:

<Tab label="" icon={<Divider />} disabled />

, give it a className and style it accordingly. ,给它一个 className 并相应地设置样式。 The Tab component is a button with stuff inside, so you would need to override some paddings and min-heights in css. Tab 组件是一个里面有东西的按钮,所以你需要在 css 中覆盖一些 paddings 和 min-heights。

Using CSS to add a border to the top of the tab seems to work well for me.使用 CSS 在选项卡顶部添加边框似乎对我来说效果很好。

const useStyles = (theme) => ({
    withDivider: {
        borderTop: `1px solid ${theme.palette.divider}`,
    },
});
<Tabs>
    <Tab>...<Tab/>
    <Tab>...<Tab/>
    <Tab className={classes.withDivider}>...<Tab/>
    <Tab>...<Tab/>
</Tabs>

Just for anyone wondering why the divider doesn´t show up, add the orientation property and set it to "vertical" so the divider can be visible in horizontal Tabs.只是对于任何想知道为什么分隔线不显示的人,添加方向属性并将其设置为“垂直”,以便分隔线可以在水平选项卡中可见。

<Tab label="" icon={<Divider orientation="vertical" />}  disabled />

you can use the Dividers in between each Tab as follows:您可以在每个选项卡之间使用分隔符,如下所示:

        <Box
          style={{
            display: "flex",
            justifyContent: "flex-end",
            marginRight: 20,
          }}
        >
          <Tabs
            sx={{ backgroundColor: "#EAEBEF", borderRadius: 4 }}
            value={tab}
            onChange={(e, v) => setTab(v)}
          >
            <Tab label="Item One" />
            <Divider
              orientation="vertical"
              style={{ height: 30, alignSelf: "center" }}
            />
            <Tab label="Item Two" />
            <Divider
              orientation="vertical"
              style={{ height: 30, alignSelf: "center" }}
            />
            <Tab label="Item Three" />
          </Tabs>
        </Box>

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

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