Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React material-ui TextField text align and hide cursor

I'd like to make Material-ui TextField read-only (not disabled) with text aligned to center and hidden cursos. I use

style={{ textAlign: 'center', cursor: 'none' }}

but this doesn't have effect.

Thank you for solutions.

like image 972
CodeBy Avatar asked Oct 06 '16 09:10

CodeBy


4 Answers

InputStyle is indeed not part of the API anymore, but it seems you can supply the props to the input element for example for text align using:

inputProps={{ style: {textAlign: 'center'} }}

I suspect this will allow you do to the same as inputStyle used to.

like image 144
Fredrik Vatsendvik Avatar answered Oct 04 '22 08:10

Fredrik Vatsendvik


I was struggling with the same problem. The centering is the hardest part. What finally worked for me was the following combination of inline styles:

<TextField
  inputStyle={{ textAlign: 'center' }}
  hintStyle={{ textAlign: 'center', width: '100%' }}
  floatingLabelStyle={{ textAlign: 'center', width: '100%', transformOrigin: 'center top 0px' }}
  {...otherProps}
/>

If you want to make the text field disabled, you can simply add disabled flag to it, i.e.

<TextField disabled />
like image 40
Tomasz Lenarcik Avatar answered Oct 04 '22 06:10

Tomasz Lenarcik


To style the actual HTML input element, use the inputStyle property instead of style.

inputStyle={{ textAlign: 'center', cursor: 'none' }}

Ref: http://www.material-ui.com/#/components/text-field

To make it read only, you just need to set the "value" property to the desired text. If you don't handle the onChange event, TextField won't let you modify the text.

like image 40
Jeff McCloud Avatar answered Oct 04 '22 08:10

Jeff McCloud


With MUI version 5 - If you want to remove the blinking cursor you can do it like this:

<TextField
  InputProps={{ readOnly: true }}
/>
like image 43
Guy Engel Avatar answered Oct 04 '22 06:10

Guy Engel