【问题标题】:ThemeProvider affecting non wrapped componentsThemeProvider 影响非包装组件
【发布时间】:2020-04-18 07:20:01
【问题描述】:

我正在使用 Material UI 和 ThemeProvider 来覆盖一些样式,但是,被覆盖的样式正在影响我没有包含在 ThemeProvider 标签中的组件。另外,我不得不使用 !important 来覆盖某些样式,是否有原因它仍在使用默认样式而不是使用 !important 来覆盖我的覆盖样式?

我的代码如下。我只想覆盖下拉组件的样式,但 CreateNewFolder 组件也受到了影响。

const theme = createMuiTheme({
    overrides: {
        MuiOutlinedInput: {
            input: {
                padding: "8px 14px !important"
            }
        },
        MuiInputLabel: {
            formControl: {
                transform: "none !important",
                top: "-6px !important",
                left: "45px !important",
                backgroundColor: "white",
                fontSize: "0.8rem",
                padding: "0 6px"
            }
        },
        MuiSelect: {
            select: {
                "&:focus": {
                    backgroundColor: "white"
                }
            }
        }
    }
});
return (
        <>
            <div
                style={{
                    display: "flex",
                    justifyContent: "space-between",
                    padding: "25px 95px 15px 75px",
                    height: "10vh"
                }}
            >
                <div>
                    <Button
                        variant="contained"
                        onClick={handleNewAdunit}
                        style={{ marginRight: "15px" }}
                    >
                        <AddIcon /> &nbsp; Adunit
                    </Button>
                    <Button onClick={handleFolder} variant="outlined">
                        <AddIcon /> &nbsp; Folder
                    </Button>
                </div>
                <div style={{ display: "flex", paddingLeft: "30px" }}>
                    <SearchBar
                        inputProps={{
                            value: "",
                            onChange: () => {},
                            style: {
                                width: "300px"
                            }
                        }}
                        results={[]}
                        displayKey={"component"}
                    />
                    <ThemeProvider theme={theme}>
                        <Dropdown
                            label="Sort By"
                            variant="outlined"
                            onChange={() => {}}
                            menuItems={[
                                {
                                    label: "Last modified",
                                    key: "last modified"
                                },
                                { label: "Option 2", key: "option 2" },
                                { label: "Option 3", key: "option 3" }
                            ]}
                            style={{
                                width: "150px",
                                marginLeft: "30px"
                            }}
                        />
                        <Dropdown
                            label="Show"
                            variant="outlined"
                            onChange={() => {}}
                            menuItems={[
                                { label: "My files", key: "my files" },
                                { label: "Option 2", key: "option 2" },
                                { label: "Option 3", key: "option 3" }
                            ]}
                            style={{
                                width: "150px",
                                marginLeft: "30px"
                                // height: "35px"
                            }}
                        />
                    </ThemeProvider>
                </div>
            </div>
            <div>
                <CreateNewFolder ref={folderRef} />
                <Snackbar
                    ref={adunitRef}
                    variant="info"
                    message="Stay tuned. New feature coming soon!"
                />
            </div>
        </>

【问题讨论】:

    标签: reactjs material-ui


    【解决方案1】:

    我可能错了,但主题似乎是全球性的。您可以使用useTheme 钩子访问它,这意味着主题不仅可以作为在树中传递的道具来访问,因此主题也是全局级别的。再次,它可能是错误的

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-22
      • 1970-01-01
      • 2019-11-21
      相关资源
      最近更新 更多