【问题标题】:Cannot override Mui-selected background Color无法覆盖 Mui 选择的背景颜色
【发布时间】:2022-02-03 03:33:39
【问题描述】:

我想覆盖 Material UI 中所选分页项的背景颜色,但它无法正常工作。

有时它会变成正确的颜色,但在我刷新页面后它又变成了错误的颜色。

正确的颜色

颜色错误

这是我的代码

export default function CustomPagination (props) {
    const _screenClass = useScreenClass(); 
    const _isBigScreen = (['lg', 'xl', 'xxl'].includes(_screenClass));
    const {
        count,
        defaultPage,
        currentPage,
    } = props;
    return <Pagination
        defaultPage={1}
        count={count}
        sx = {{
        '& .Mui-selected': {
            backgroundColor: "#f16037",
            color:'white',
            borderColor: 'transparent',
            fontSize: 16,
            height: "32px",
            width: "32px",
            borderRadius: "8px",
            },
        }}
        boundaryCount={_isBigScreen ? 2 : 1}
        siblingCount={0}
        renderItem={(item) => {
            return <PaginationItem
            components={{ previous: KeyboardDoubleArrowLeftIcon, next: KeyboardDoubleArrowRightIcon }}
            selected = {item.page == currentPage}
            {...item}
            />;
        }}
    />
}

【问题讨论】:

    标签: reactjs pagination material-ui styles


    【解决方案1】:
    1. 你应该将你的风格道具应用到PaginationItem
    2. &amp; 和目标类名之间不需要空格:"&amp;.Mui-selected"

    因此,您的代码应如下所示:

          <Pagination
            count={10}
            renderItem={(item) => (
              <PaginationItem
                components={{ previous: ArrowBackIcon, next: ArrowForwardIcon }}
                {...item}
                sx={{
                  "&.Mui-selected": {
                    backgroundColor: "#f16037",
                    color: "white",
                    borderColor: "transparent",
                    fontSize: 16,
                    height: "32px",
                    width: "32px",
                    borderRadius: "8px"
                  }
                }}
              />
            )}
          />
    

    Working Demo

    【讨论】:

    • 谢谢,我的错。现在可以了,非常感谢
    • 不客气,如果它解决了您的问题,您可以接受答案。
    猜你喜欢
    • 2022-01-23
    • 1970-01-01
    • 2013-02-09
    • 2022-10-22
    • 1970-01-01
    • 2018-10-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多