reactjs Ant设计菜单,react中的项目自定义样式

lc8prwob  于 2023-01-30  发布在  React
关注(0)|答案(2)|浏览(221)

大家好,我学习Ant Design与React+ typescript。我需要覆盖菜单中的一些默认样式。项目组件。但我有一个问题,删除右边框样式时,用户单击该菜单。项目使用styleComponent。我不知道确切的方式,但我检查该组件,我从检查中选择类名,并使右边框:3px实心透明
但它仍然没有为我工作这里是我下面附上的代码

import { Layout, Menu } from "antd";
import React from "react";
import styled from "styled-components";
import { Flex } from "../styleComponents/commonUtilsStyles";
import {
  MenuUnfoldOutlined,
  MenuFoldOutlined,
  UserOutlined,
  VideoCameraOutlined,
  UploadOutlined,
} from "@ant-design/icons";
import SubMenu from "antd/lib/menu/SubMenu";

const MenuItem = styled(Menu.Item)`
  .ant-menu-vertical .ant-menu-item::after,
  .ant-menu-vertical-left .ant-menu-item::after,
  .ant-menu-vertical-right .ant-menu-item::after,
  .ant-menu-inline .ant-menu-item::after {
    border-right: 3px solid transparent !important;
  }
`;

const { Header, Sider, Content } = Layout;

const FlexContainer = styled(Flex)`
  background-color: white;
  box-shadow: 6px 6px 32px #cccccc, -6px -6px 32px #f4f4f4;
`;

const HeaderContainer: React.FC = () => {
  return (
    <>
      <Layout>
        <Sider>
          <Menu mode="inline">
            <SubMenu key="submenu" title="number">
              <MenuItem className="no-border" key="1">
                one
              </MenuItem>
              <MenuItem className="no-border" key="2">
                two
              </MenuItem>
              <MenuItem className="no-border" key="3">
                three
              </MenuItem>
            </SubMenu>
            <MenuItem className="no-border" key="11">
              one 1
            </MenuItem>
            <MenuItem className="no-border" key="21">
              two 1
            </MenuItem>
            <MenuItem className="no-border" key="31">
              three 1
            </MenuItem>
          </Menu>
        </Sider>
      </Layout>
      <h1>hello</h1>
    </>
  );
};

export default HeaderContainer;

输出

但我需要到删除的边界线样式时,用户单击该菜单它

nuypyhwy

nuypyhwy1#

创建一个css文件,并将border-right属性设置为0px,以删除以下类的右边框。
index.css

.ant-menu-vertical .ant-menu-item::after,
.ant-menu-vertical-left .ant-menu-item::after,
.ant-menu-vertical-right .ant-menu-item::after,
.ant-menu-inline .ant-menu-item::after {
  border-right: 0px;
}

截图:

liwlm1x9

liwlm1x92#

我发现最好的办法就是用情感React

import styled from "@emotion/styled";
import { Menu } from "antd";

然后覆盖样式(您需要检查以了解antd css类)

/**
 * ant-menu-item:hover::after
 * ant-menu-item-selected
 * ant-menu-item-selected:after
 */

const CustomMenu = styled(AntMenu)`
    && .ant-menu-item:hover::after {
        border-bottom: 0px solid transparent;
    }
    
    && .ant-menu-item-selected {
        background-color: #EEF6F7;
        border-top: 4px solid #B80012;
        border-radius: 0px;
        color: #B80012;
    }    

    && .ant-menu-item-selected:after {
        border-bottom-width: 0px;        
        border-bottom-color: transparent;
    }
`;

最后,将其用作组件

return (
    <ConfigProvider theme={theme}>
        <CustomMenu
            onClick={onClick}
            selectedKeys={[current]}
            mode="horizontal"
            items={menuItems}
            {...props}
        />                
    </ConfigProvider>
);

这是我在Storybook Custom AntDesign Horizontal menu中进行测试的方式

    • 解释**为什么我们可以这样做?
  • 首先,情感样式化组件'styled可以样式化任何组件,只要它接受classNameprop.https://emotion.sh/docs/styled
  • 其次,让我们查看Ant设计源代码,转到菜单组件,我们将在import InternalMenu from './menu';这一行中看到index.tsx导入InternalMenu
  • 然后,我们转到menu.tsx,它导出InternalMenu组件,并在这里看到它的属性const InternalMenu = forwardRef<RcMenuRef, InternalMenuProps>((props, ref) => {...}
  • 向下滚动,在重构部分,它有const { prefixCls: customizePrefixCls, className,...restProps} = props;

=〉这里你可以看到它接受classNameprops,这意味着你可以应用EmotionJS提供的样式化组件方法。
好好玩!
平张:)

相关问题