Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. 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 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',
}
like image 856
Hrutvik Patel Avatar asked Jun 07 '19 19:06

Hrutvik Patel


People also ask

What is the difference between center right and left text alignment?

left: It aligns text along the left side of a page or containing element. It is the default value. right: It aligns text along the right side of a page or containing element. center: Text is aligned around a midpoint. justify: To make sure that both edges of each line are aligned with both margins, space is added between words.

What is the use of typography in material UI?

The Typography component of Material UI is used to present your text and content as clearly and efficiently as possible. import Typography from '@material-ui/core/Typography'; // OR import { Typography } from '@material-ui/core'; Syntax: It sets the alignment property. left: It aligns text along the left side of a page or containing element.

What is muitextfield in WPF?

The name MuiTextField can be used when providing default props or style overrides in the theme. Props of the FormControl component are also available. This prop helps users to fill forms faster, especially on mobile devices. The name can be confusing, as it's more like an autofill. You can learn more about it following the specification.

Should I use muitextfield or formcontrol?

Consider either: The name MuiTextField can be used when providing default props or style overrides in the theme. Props of the FormControl component are also available. This prop helps users to fill forms faster, especially on mobile devices. The name can be confusing, as it's more like an autofill.


Video Answer


2 Answers

UPDATE Material UI 5

With the new version of Material UI, the inputProps is one level deeper.

<TextField 
    InputProps={{
        inputProps: {
            style: { textAlign: "right" },
        }
    }}
/>
like image 152
ThomasP1988 Avatar answered Sep 18 '22 09:09

ThomasP1988


Change JSX a little:

<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.

You need to use it as style: {} inside inputProps the same way as InputStyle before.

like image 25
Andrei Konstantinov Avatar answered Sep 20 '22 09:09

Andrei Konstantinov