Javascript 如何设置材质ui文本字段的样式
我一直在努力研究如何设计组件的样式Javascript 如何设置材质ui文本字段的样式,javascript,reactjs,material-ui,Javascript,Reactjs,Material Ui,我一直在努力研究如何设计组件的样式 <TextField id="email" label="Email" className={classes.textField} value={this.state.form_email} onChange={this.handle_change('form_email')} margin="normal" /> 我的问题是,我似乎无法使文本字段的颜色变为白色。我似乎能够将样式应用于整个文本字段(
<TextField
id="email"
label="Email"
className={classes.textField}
value={this.state.form_email}
onChange={this.handle_change('form_email')}
margin="normal"
/>
我的问题是,我似乎无法使文本字段的颜色变为白色。我似乎能够将样式应用于整个文本字段(因为宽度样式可以工作等等)。。。但我认为问题在于,我没有将这些样式进一步应用到实际输入中
我曾试图寻找其他关于传递输入道具的答案,但没有成功
我已经尽了我最大的努力,但我想我需要问问是否有人知道我做错了什么
它现在看起来是什么样子
尝试使用
TextField
上的inputStyle
属性。从
inputStyle(对象)-覆盖文本字段输入的内联样式
元素。当multiLine为false时:定义输入的样式
元素。当multiLine为true时:定义容器的样式
文本区
您需要将
InputProps
属性添加到文本字段
const styles = theme => ({
textField: {
width: '90%',
marginLeft: 'auto',
marginRight: 'auto',
paddingBottom: 0,
marginTop: 0,
fontWeight: 500
},
input: {
color: 'white'
}
});
JSX:
另一方面,您还可以按照说明设置标签样式或使用覆盖。这实际上取决于您要更改的内容 文档中有一系列关于自定义文本字段的示例,请在此处查看: 有关自定义的更多信息可在此处找到: 及 你试过使用吗!重要的颜色变化?当我试图为轮廓文本字段的边框设置默认颜色时,我也遇到了同样的问题
看看这个:这是一个具有内联样式的解决方案:
<TextField
style={{
backgroundColor: "blue"
}}
InputProps={{
style: {
color: "red"
}
}}
/>
我建议将你的风格保持在一个主题之内
const theme = createMuiTheme({
overrides: {
MuiInputBase: {
input: {
background: "#fff",
},
},
},
});
这里的所有答案都显示了如何使用InputProps或InputProps设置样式,但没有人解释为什么以及它是如何工作的。没有人解释inputProps和inputProps之间的区别
<TextField
variant="outlined"
// inputProps are used to pass attributes native to the underlying
// HTML input element, e.g. name, id, style.
inputProps={{
style: { textAlign: 'center' },
}
// InputProps (capital I) passes props to the wrapper Material
// component. Can be one of the following: Input, FilledInput,
// OutlinedInput. You can pass here anything that the underlying
// Material component uses: error, value, onChange, and classes.
InputProps={{
// Usually you don't need className, the `classes` object will
// be sufficient. However, you can also use it and this will
// add your class to the div of the InputBase.
className: styles.slider_filter_input,
classes: {
root: classes.root
focused: classes.focused
// The list of keys you pass here depend on your variant
// If for example you used variant="outlined", then you need
// to check the CSS API of the OutlinedInput.
}
}}
/>
尝试使用组件而不是文本字段
。然后可以使用如下简单的内联样式:
style={{color:'white'}}
这也将使占位符文本变亮。。。万岁。您可以将样式传递给层次结构中的任何子元素:
TextField>Input>Input(HTML元素)
注意InputProps
vs.InputProps
// pass styles (or props) to the Input component
<TextField InputProps={{className: classes.input}} />
// pass styles (or props) to the inner input element
<TextField inputProps={{className: classes.input}} />
//将样式(或道具)传递给输入组件
//将样式(或道具)传递给内部输入元素
谢谢终于有人解释了。如果你能解释一下为什么风格道具有时会起作用,那就太好了(示例:,页边空白有效,但颜色无效…谢谢again@NielsDominguez您所描述的与材质Ui无关,而是与css的工作方式有关,当您向组件添加样式时,该样式不起作用,这意味着一些更具体的样式正在应用于该规则-基于css特定规则。@NielsDominguez注意到t您的示例无论如何都不起作用,因为您正在将样式作为道具传递给TextField组件,而该组件没有使用该道具执行任何操作。当将样式道具传递给诸如div之类的本机dom元素时,它们将作为内联样式应用于。当您将它们传递给React组件时,React组件需要像y其他道具。通常材质ui组件使用诸如InputProp之类的道具将样式传递给底层本机元素。这两个className
s有何不同,换句话说,为什么MUI需要不止一个?(例如,白色
颜色进入主题的文本字段
CSS规则?谢谢,这很好。当涉及到让您知道必须使用InputProps
来设置文本字段样式时,材质UI文档并不清楚。太棒了!如果您使用了许多文本字段,这是最好的解决方案。
const theme = createMuiTheme({
overrides: {
MuiInputBase: {
input: {
background: "#fff",
},
},
},
});
<TextField
variant="outlined"
// inputProps are used to pass attributes native to the underlying
// HTML input element, e.g. name, id, style.
inputProps={{
style: { textAlign: 'center' },
}
// InputProps (capital I) passes props to the wrapper Material
// component. Can be one of the following: Input, FilledInput,
// OutlinedInput. You can pass here anything that the underlying
// Material component uses: error, value, onChange, and classes.
InputProps={{
// Usually you don't need className, the `classes` object will
// be sufficient. However, you can also use it and this will
// add your class to the div of the InputBase.
className: styles.slider_filter_input,
classes: {
root: classes.root
focused: classes.focused
// The list of keys you pass here depend on your variant
// If for example you used variant="outlined", then you need
// to check the CSS API of the OutlinedInput.
}
}}
/>
// pass styles (or props) to the Input component
<TextField InputProps={{className: classes.input}} />
// pass styles (or props) to the inner input element
<TextField inputProps={{className: classes.input}} />