Solution 1 :

Use inputComponent inside InputProps to achieve customized text field inside the TextField

InputProps={{
  inputComponent: () => (
    <div style={{ color: "yellow" }}>XXXXXXXXXXXXXXXXX</div>
  )
}}

enter image description here


Try it online:

Edit funny-curran-yw54t

Solution 2 :

You don’t have to do use div’s inside value attribute. You want the text to be styled, use InputProps

import React from "react";
import TextField from "@material-ui/core/TextField";

export default function App() {
  return (
    <>
      <TextField
        id="outlined-multiline-static"
        fullWidth
        rows="12"
        value="Hi"
        variant="outlined"
        InputProps={{
          style: {
            color: "yellow"
          }
        }}
      />
    </>
  );
}

Problem :

I tried below code and while I was expecting to get a yellowish Hi, I got [object Object] instead.

Is there a way to fix it? maybe using InputProps helps but unfortunately I couldn’t find any detailed tutorial about it.

<TextField
    id="outlined-multiline-static"
    label="Multiline"
    multiline
    fullWidth
    rows="22"
    value={<div> Hi <div style={{ color: "yellow" }}>There</div></div>}
    variant="outlined"
  />

Edit:
I just simplified the problem and don’t want the whole of the text to be yellow.

https://codesandbox.io/s/hopeful-bush-gfi9m?fontsize=14&hidenavigation=1&theme=dark

Comments

Comment posted by joy08

you want the entire field to be yellow?

Comment posted by Me.

No, I just simplifed the problem. sorry I forgot to mention this in the question.

Comment posted by Me.

yes, I edit the question. thank you

Comment posted by Me.

It destroys rows=”22″ style.

Comment posted by keikai

The reason why it’s not multiline is that you customized that field with

Comment posted by Me.

I think it just make whole of the text yellow. sorry I forgot to mention I simplified the problem and don’t want whole of the text to be same color please check edit part

Comment posted by joy08

Did not get this edit part. Can you be a more specific

Comment posted by Me.

lets assume the value is

Hi

there

Comment posted by Me.

I mean what if we want to show {black}Hi + {yellow}there.

Comment posted by Me.

Codesandbox also edited to this text, sorry for my mistake.

By