简体   繁体   English

在material-ui中使用TextField对象的inputProps时使用带有连字符的css属性

[英]use css attribute with hyphen while using inputProps of TextField object in material-ui

so im trying to customize a TextField component in material-ui, and i read about the inputProps attribute which can help me center the text, unfortunately i tried something like this:所以我试图在 material-ui 中自定义一个 TextField 组件,我阅读了可以帮助我居中文本的 inputProps 属性,不幸的是我尝试了这样的事情:

<TextField
  required
  id="userName"
  label="User name:"
  defaultValue="enter your user name"
  margin="normal"
  fullWidth={true}
  inputProps={{ textAlign: 'center', }}
/>

but than i get an error about textAlign prop, it won't convert from camelCase into a hyphen case text-align, and i can't write text-align instead because its invalid... what can i do?但是比我收到关于 textAlign 道具的错误,它不会从驼峰式大小写转换为连字符大小写文本对齐,而且我不能写文本对齐,因为它无效......我能做什么?

heres the error: React does not recognize the textAlign prop on a DOM element.这是错误:React 无法识别 DOM 元素上的textAlign If you intentionally want it to appear in the DOM as a custom attribute, spell it as lowercase textalign instead.如果您有意希望它作为自定义属性出现在 DOM 中,请将其拼写为小写的textalign If you accidentally passed it from a parent component, remove it from the DOM element.如果您不小心从父组件传递了它,请将其从 DOM 元素中删除。

thanks alot!多谢!

You can use a quoted property name:您可以使用带引号的属性名称:

<TextField
     required
     id="userName"
     label="User name:"
     defaultValue="enter your user name"
     margin="normal"
     fullWidth={true}
     inputProps={{"text-align": 'center',}}
 />

Notice the {"text-align": 'center',} on the last line.注意最后一行的{"text-align": 'center',} The left-hand side (name) of a property initializer can be quoted (single or double quotes) if the name won't be a valid identifier (in your case, because of the hyphen).如果名称不是有效标识符(在您的情况下,由于连字符),可以引用(单引号或双引号)属性初始值设定项的左侧(名称)。

Simpler example:更简单的例子:

 var obj = { "name-with-hyphen": 42 }; console.log(obj["name-with-hyphen"]); // 42

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM