gpt4 book ai didi

javascript - Material UI Input 的焦点和不焦点时无法更改边框颜色

转载 作者:行者123 更新时间:2023-12-04 10:10:13 24 4
gpt4 key购买 nike

我不知道为什么我不能让它工作。我选择 MuiInputBase-root元素,告诉它选择字段并将边框颜色设置为蓝色,并在焦点上将边框颜色设置为红色。我在这里做错了什么?

Codesandbox

import React from "react";
import "./styles.css";

import { makeStyles } from "@material-ui/core/styles";
import FormControl from "@material-ui/core/FormControl";
import OutlinedInput from "@material-ui/core/OutlinedInput";
import InputLabel from "@material-ui/core/InputLabel";

const useStyles = makeStyles(theme => ({
root: {
width: "20rem"
},
label: {
background: "white",
paddingRight: theme.spacing(0.5),
"&.Mui-focused": {
color: theme.palette.secondary.main
}
},
closeIcon: {
color: theme.palette.grey[400],
"&.hidden": {
display: "none"
}
},
searchIcon: {
color: theme.palette.primary.main
}
}));

const useOutlinedInputStyles = makeStyles({
root: {
"& .MuiInputBase-root": {
"& fieldset": {
borderColor: "blue"
},
"&.Mui-focused fieldset": {
borderColor: "red"
}
}
}
});

export default function App() {
const classes = useStyles();
const outlinedInputStyles = useOutlinedInputStyles();
return (
<div className="App">
<FormControl className={classes.root} variant="outlined">
<InputLabel className={classes.label} htmlFor="search-input">
placeholder
</InputLabel>
<OutlinedInput
classes={outlinedInputStyles}
id="search-input"
labelWidth={70}
/>
</FormControl>
</div>
);
}

img

最佳答案

问题是 .MuiInputBase-root不是根元素(.MuiOutlinedInput-root 元素)的子元素,它实际上是完全相同的元素,因此不需要该层。此外,类型选择器(例如 fieldset )具有 lower specificity比类选择器,所以 &.Mui-focused fieldset没有足够的特异性覆盖 default focused styles .而不是 fieldset你可以使用类选择器.MuiOutlinedInput-notchedOutline .

所以而不是:

root: {
"& .MuiInputBase-root": {
"& fieldset": {
borderColor: "blue"
},
"&.Mui-focused fieldset": {
borderColor: "red"
}
}
}

你应该有:
  root: {
"& .MuiOutlinedInput-notchedOutline": {
borderColor: "blue"
},
"&.Mui-focused .MuiOutlinedInput-notchedOutline": {
borderColor: "red"
}
}

Edit OutlinedInput border

相关答案: Change border color on Material-UI TextField

关于javascript - Material UI Input 的焦点和不焦点时无法更改边框颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61366553/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com