当前位置: 首页 > 知识库问答 >
问题:

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

游乐池
2023-03-14

我一直在试图找出如何样式的材料ui TextField组件

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

我的类创建如下:

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

我的问题是,我似乎无法让文本字段的颜色变成白色。我似乎能够将样式应用于整个文本字段(因为宽度样式工作等)...但是我认为问题是我没有将样式应用到链的更下游和实际输入中。

我曾试图寻找其他关于传递输入道具的答案,但没有成功。

我已经尽了我最大的努力,但我想我需要问问是否有人知道我做错了什么。

它现在看起来是什么样子

共有3个答案

卫念
2023-03-14

这是一个具有内联样式的解决方案:

<TextField
    style={{
        backgroundColor: "blue"
    }}
    InputProps={{
        style: {
            color: "red"
        }
    }}
/>
司徒斌
2023-03-14

这里的所有答案都显示了如何使用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.
       }
    }}
/>

这是一个工作代码框,显示了上面的想法。

澹台岳
2023-03-14

您需要将InputProps属性添加到文本字段。

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

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,
    }}
/>

另外,您还可以设置标签的样式或使用此处所述的替代。

 类似资料:
  • 如何在单击按钮后聚焦文本字段。我尝试使用自动对焦,但没有成功:示例沙盒

  • 我想显示现在说唱文字,比如...我怎么能改变它通过使用材料UI 它像那样显示“身体1”。Lorem ipsum dolor坐在这里,奉献给我们。克罗斯·布兰迪特..." 但我想表现得像“身体1.洛雷姆·伊普苏姆·多洛·希特·阿梅特,奉献给精英们。库斯·布兰迪。。。더보기" 你能帮我吗?

  • Cesium中为几何形状设置材质有多种方法 第一种方法 Material 直接构建Cesium.Material对象,通过设置Material的属性来进行控制,官方示例和API描述的比较清楚, API说明 材质示例 第二种方法 MaterialProperty 今天介绍通过MaterialProperty设置: Cesium 材质相关的类为 MaterialProperty,它有以下几个子类: –

  • 我创建表单,我有几个TextField, DropDownMenu材料UI组件包括,问题是我如何可以收集所有的数据从所有的TextFields, DropDownMenus在一个obj和发送到服务器上。对于TextField,它具有TextField.getValue()返回输入的值。但是我不明白如何使用它。 我想把它发送到服务器上_handleClick方法。

  • 我试图通过编程将焦点设置在(激活)材质ui中菜单组件内的一个菜单项上。我可以通过切换到它来手动完成,但我需要通过编程来响应按键关闭事件。

  • 我认为这在HTML/CSS中只是一个简单的任务,但在MaterialUI中,如何在MaterialUI中为TextField设置不同的字体大小? 以下代码不起作用: