简体   繁体   English

如何居中对齐 Material-ui TextField 文本并设置最小数值?

[英]How can I center-align Material-ui TextField text and also set a min number value?

I am trying to both center align the text inside and set a min number value to 0. But that is not occurring.我正在尝试将文本居中对齐并将最小数值设置为 0。但这并没有发生。 I can do either or but not both at the same time.我可以同时做其中一个,但不能同时做两个。 I looked at the material-ui page on TextField but it was no help --> here我查看了 TextField 上的 material-ui 页面,但没有帮助 -->这里

<TextField type="number" 
    id={cells.id} 
    inputProps={{min: 0}} 
    InputProps={classes.inputText}  
    className={classes.inputComponent} 
    disabled={cells.disabled} 
    defaultValue={cells.text} />

I need to set a style for the text field itself and style for the text inside.我需要为文本字段本身和里面的文本设置样式。

inputComponent: {
    height: '30px',
    width:  '71px',
    border: '1px solid #D3D4D0',
    borderRadius: '5px',
    backgroundColor: '#FFFFFF',
    boxShadow: '0 1px 0 0 rgba(170,170,170,0.01)'
}

inputText: {
    color: 'rgba(0,0,0,0.87)',
    fontSize: '16px',
    letterSpacing: '0.5px',
    lineHeight: '28px',
    textAlign: 'center',
}

Change JSX a little:稍微改变 JSX:

<TextField type="number" 
    id={cells.id} 
    inputProps={{min: 0, style: { textAlign: 'center' }}} // the change is here
    InputProps={classes.inputText}  
    className={classes.inputComponent} 
    disabled={cells.disabled} 
    defaultValue={cells.text} />

Reason原因

InputStyle is not part of the API anymore. InputStyle不再是 API 的一部分。

You need to use it as style: {} inside inputProps the same way as InputStyle before.您需要将其用作style: {}inputProps中与之前的InputStyle相同。

For MUI v5:对于 MUI v5:

<TextField
  sx={{input: {textAlign: "center"}}}
/>

UPDATE Material UI 5更新材质 UI 5

With the new version of Material UI, the inputProps is one level deeper.使用新版本的 Material UI,inputProps 更深一层。

<TextField 
    InputProps={{
        inputProps: {
            style: { textAlign: "right" },
        }
    }}
/>

for right-align右对齐

    import { styled } from '@mui/material/styles';
    const useStyles = makeStyles({
    input: {
      "& .css-1t8l2tu-MuiInputBase-input-MuiOutlinedInput-input": {
        textAlign: 'right',
      },
       "& .css-1d3z3hw-MuiOutlinedInput-notchedOutline": {
           textAlign: 'right'
       },

       "& .css-1kty9di-MuiFormLabel-root-MuiInputLabel-root": {
           transformOrigin: 'top left' ,
           left: 'auto',
           right: 8,
       },

       "& .css-14s5rfu-MuiFormLabel-root-MuiInputLabel-root" :{
           left: 'auto',
           right: 20,
       }
    }
  })

and then进而

    function ComponentNam (){ 
        const classes = useStyles();
        return(
         <TextField className={classes.input} fullWidth label="کلمه را وارد
          کنید" id="fullWidth" /> 
        )
     }

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

相关问题 如何在 material-ui 中为当前日期和时间的文本字段类型 datetime-local 设置默认值? - how can I set the default value to the textfield type datetime-local in material-ui for the current date and time? ReactJS:如何将Material-UI的占位符和文本居中 <DatePicker/> ? - ReactJS: How can I center the placeholder and text for Material-UI's <DatePicker/>? 如何在 Material UI 中为 Textfield 类型日期设置“min”属性? - How can I set 'min' attribute for a Textfield type date in Material UI? ReactJS:如何居中对齐Material-UI <Table> 在 <div> ? - ReactJS: How to center align Material-UI's <Table> in <div>? 以编程方式在 material-ui 自动完成文本字段中设置值 - Programmatically set value in material-ui Autocomplete TextField 如何在网格项目中居中对齐 Material-UI“芯片”? - How do I center align a Material-UI "Chip" within a Grid item? 如何获取 material-ui 的自动完成 Select 的值? - How can I get the value of the Autocomplete Select of material-ui? Material-ui 文本字段 null 值为零 - Material-ui Textfield null value for zero 在提交时获取 material-ui TextField 值 - getting material-ui TextField value onsubmit 在Material-UI TextField中,当输入模糊时,如何防止标签落在装饰物后面? - In a Material-UI TextField how can I prevent a label from falling behind an adornment when the input is blurred?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM