首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何覆盖自定义组件的MUI 5输入标签背景色

问如何覆盖自定义组件的MUI 5输入标签背景色
EN

Stack Overflow用户
提问于 2021-12-21 12:44:26
回答 1查看 981关注 0票数 0

我一直在为如何覆盖MUI 5输入标签的背景色而苦苦挣扎。我能够覆盖背景色,并在我的_theme.js文件中将其设置为蓝色,但是这个更改是全局的。有谁知道我将如何覆盖它,并使我的KeywordSearchTextField在我的index.js文件中的背景色红色遵循MUI 5的最佳实践?任何帮助都是非常感谢的!请看我在https://codesandbox.io/s/mui-5-styling-uqt9m?file=/pages/index.js的沙箱。下面的代码片段..。

代码语言:javascript
复制
import Box from "@mui/material/Box";
import TextField from "@mui/material/TextField";
import Autocomplete from "@mui/material/Autocomplete";
import { styled, createTheme, useTheme } from "@mui/material/styles";

const styles = {
  baseBoxStyle: {
    maxWidth: "350px",
    margin: "20px",
    padding: "10px"
  }
};

const KeywordSearchContainer = styled("div")(() => ({}));

const KeywordSearchTextField = styled(TextField)(() => ({}));

const keywordOptions = [
  { label: "Generic keyword 1", value: "Generic keyword 1" },
  { label: "Generic keyword 2", value: "Generic keyword 2" },
  { label: "Generic keyword 3", value: "Generic keyword 3" }
];

export default function Index() {
  return (
    <Box sx={{ ...styles.baseBoxStyle }}>
      <KeywordSearchContainer>
        <Autocomplete
          id="free-solo-demo"
          options={keywordOptions.map((option) => option.label)}
          renderInput={(params) => (
            <KeywordSearchTextField
              {...params}
              label="freeSolo"
              InputLabelProps={{
                classes: {
                  focused: "focused"
                }
              }}
            />
          )}
        />
      </KeywordSearchContainer>
    </Box>
  );
}```
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-12-21 13:24:24

将样式添加到目标巢式类(组件-槽组合),如下所示:也请参阅我的分叉码箱。

代码语言:javascript
复制
<KeywordSearchTextField
  {...params}
  sx={{
    '& .MuiInputBase-input:focus': {
      backgroundColor: 'red',
    },
  }}
  label="freeSolo"
  InputLabelProps={{
    classes: {
      focused: 'focused',
    },
  }}
/>
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70435685

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档