当使用Material-UI(反应)将复选框集中在全局级别时,尝试更改其样式。
目前,只有默认样式和悬停样式有效:
MuiCheckbox: { colorSecondary: { color: 'green', '&:hover': { color: 'blue !important', border: '1px solid rgba(0,0,0,0.54)', outline: '2px auto rgba(19,124,189,.6)' } }, }
我正在尝试做的,但是没有用:
MuiCheckbox: { colorSecondary: { '&$focused': { color: 'blue', border: '1px solid rgba(0,0,0,0.54)', outline: '2px auto rgba(19,124,189,.6)' }, '&$focusVisible': { color: 'blue', border: '1px solid rgba(0,0,0,0.54)', outline: '2px auto rgba(19,124,189,.6)' }, } }
下面的示例显示了控制Checkbox一系列可能状态的颜色。Material- UI不会管理的focused状态Checkbox,而仅管理一个状态focusVisible,因此Checkbox通过键盘将焦点移至会触发该状态。focused在下面的示例中,我通过focused使用的onFocus和onBlur属性显式管理状态来显示样式Checkbox。
focused
Checkbox
focusVisible
onFocus
onBlur
import React from "react"; import ReactDOM from "react-dom"; import { createMuiTheme, ThemeProvider } from "@material-ui/core/styles"; import Checkbox from "@material-ui/core/Checkbox"; const theme = createMuiTheme({ overrides: { MuiCheckbox: { colorSecondary: { color: "green", "&:hover": { color: "blue" }, "&$checked": { color: "purple", "&:hover": { color: "lightblue" }, "&.Mui-focusVisible": { color: "red" } }, "&.Mui-focusVisible": { color: "orange" }, "&.focused:not(.Mui-focusVisible):not($checked)": { color: "pink" } } } } }); function App() { const [focused, setFocused] = React.useState(false); return ( <ThemeProvider theme={theme}> <div className="App"> <Checkbox className={focused ? "focused" : ""} onFocus={() => setFocused(true)} onBlur={() => setFocused(false)} /> <input value="somewhere to move focus" /> </div> </ThemeProvider> ); } const rootElement = document.getElementById("root"); ReactDOM.render(<App />, rootElement);