如何设置材料-ui文本字段的样式

And*_*cio 22 javascript reactjs material-ui

我一直试图找出如何设置一个material-ui.next文本域组件(React JS)的样式.

<TextField
    id="email"
    label="Email"
    className={classes.textField}
    value={this.state.form_email}
    onChange={this.handle_change('form_email')}
    margin="normal"
/>
Run Code Online (Sandbox Code Playgroud)

我的课程创建如下(我已附上相关部分):

const styles = theme => ({
    textField: {
        width: '90%',
        marginLeft: 'auto',
        marginRight: 'auto',
        color: 'white',
        paddingBottom: 0,
        marginTop: 0,
        fontWeight: 500
    },
});
Run Code Online (Sandbox Code Playgroud)

我的问题是,我似乎无法将文本字段的颜色更改为白色.我似乎能够在整个文本字段中应用样式(因为宽度样式工作等)...但我认为问题是我没有在链中进一步应用样式并进入实际输入.

我试图查看处理传递inputProps的其他答案,但没有成功.

尽我所能尽力尝试了一切,但我想我是否需要问是否有人知道我做错了什么.

提前感谢您的时间.问候.安德烈

它目前的样子

它目前的样子

eha*_*hab 51

这里的所有答案都展示了如何使用 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.
       }
    }}
/>
Run Code Online (Sandbox Code Playgroud)

这是一个显示上述想法的有效代码和框


Cra*_*les 19

您需要将InputProps属性添加到TextField.

const styles = theme => ({
    textField: {
        width: '90%',
        marginLeft: 'auto',
        marginRight: 'auto',            
        paddingBottom: 0,
        marginTop: 0,
        fontWeight: 500
    },
    input: {
        color: 'white'
    }
});
Run Code Online (Sandbox Code Playgroud)

JSX:

<TextField
    id="email"
    label="Email"
    className={classes.textField}
    value={this.state.form_email}
    onChange={this.handle_change('form_email')}
    margin="normal"
    InputProps={{
        className: classes.input,
    }}
/>
Run Code Online (Sandbox Code Playgroud)

另外,您还可以设置标签样式或使用此处所述的覆盖.

  • 谢谢,这效果很好。Material-UI 文档并不清楚是否让您知道必须使用“InputProps”来设置文本字段的样式。 (2认同)

小智 12

我建议将您的风格保持在一个主题中。

const theme = createMuiTheme({
  overrides: {
    MuiInputBase: {
      input: {
        background: "#fff",
      },
    },
  },
});
Run Code Online (Sandbox Code Playgroud)


Jon*_*n R 6

这是内联样式的解决方案:

<TextField
    style={{
        backgroundColor: "blue"
    }}
    InputProps={{
        style: {
            color: "red"
        }
    }}
/>
Run Code Online (Sandbox Code Playgroud)


Pet*_*lin 5

这真的取决于你到底想改变什么。

该文档有一堆关于自定义 TextFields 的示例,请在此处查看它们:

https://material-ui.com/demos/text-fields/#customized-inputs

可以在此处找到有关自定义的更多信息:

https://material-ui.com/customization/overrides/

https://material-ui.com/customization/themes/

您是否尝试过使用 !important 来更改颜色?当我尝试为轮廓 TextField 的边框设置默认颜色时遇到了同样的问题

看看这个:https : //stackblitz.com/edit/material-ui-custom-outline-color