Text Area in material-ui

JavascriptReactjsMaterial Ui

Javascript Problem Overview


Could someone help me making a TextField personalization into a TextArea, using material-ui library? I am not finding any parameter that should personalize it into a TextArea: https://github.com/callemall/material-ui/blob/v1-beta/src/TextField/TextField.d.ts

This is the TextArea:https://material.io/guidelines/components/text-fields.html#text-fields-field-types (CMD/Ctrl + F 'Text area').

> Text areas are taller than text fields and wrap overflow text onto a > new line. They scroll vertically when the cursor reaches the bottom of > the field.

Javascript Solutions


Solution 1 - Javascript

To make TextField work like a textarea you can use multiline prop. You can read more about TextFied and its props here.

Example

<TextField
  placeholder="MultiLine with rows: 2 and rowsMax: 4"
  multiline
  rows={2}
  maxRows={4}
/>

You can set maxRows={Infinity} if you want to scale your multiline input box with your content (regardless of the content length).

Solution 2 - Javascript

We can use outlined multiline <TextField> for text area
and it could be implemented by creating a theme to apply globally anywhere inside <App/>

theme.js
import { createMuiTheme} from '@material-ui/core/styles';
const theme = createMuiTheme({
 overrides: {
    MuiOutlinedInput: {
        multiline: {
            fontWeight: 'bold',
            fontSize: '20px',
            color: 'purple',
            width: '50vw'
        }
    }
}
  
});
export default theme;
app.js
...
import { ThemeProvider } from '@material-ui/core/styles';
import Theme from './relevant-path-where-the-above-theme.js-file-is-saved/Theme';
...
...
render() {
    
    return (
      <ThemeProvider theme={Theme}>
        <div className="App"> 
            <Routes/>
        </div>
      </ThemeProvider>
        
    );
  }
SomeComponent.js
...
<TextField
  id="outlined-multiline-static"
  label="Multiline"
  multiline
  rows={10}
  variant="outlined"
/>
...

Now the style for outlined multiline <TextField> will be applied globally enter image description here

Solution 3 - Javascript

You should use TextareaAutosize API available in material UI.

import TextareaAutosize from '@material-ui/core/TextareaAutosize';

or

import { TextareaAutosize } from '@material-ui/core';

The following example has all the attributes of TextareaAutosize: https://material-ui.com/components/textarea-autosize/

You can learn more about the difference between the two imports by reading this guide.

Solution 4 - Javascript

If you want something simple instead of the whole material-ui component, just use this code:

textarea.style.height = textarea.scrollHeight+'px'

Where scrollHeight is inner height of textarea and style.height is outer (if outer < inner, scrollbar shows)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJohann GomesView Question on Stackoverflow
Solution 1 - JavascriptbennygenelView Answer on Stackoverflow
Solution 2 - JavascriptSampath kumarView Answer on Stackoverflow
Solution 3 - JavascriptRishav KumarView Answer on Stackoverflow
Solution 4 - JavascriptVityaSchelView Answer on Stackoverflow