ness_cons
ness_cons

Reputation: 31

How to format text in Material UI TextField

I would like the text in the MUI TextField to be formatted. But when I write something like this:

<TextField
    label="Size"
    value={Hello, <i>world</i>}
    variant="outlined"
  />

I get this:

Are there any ways to format text in the textfield?

Upvotes: 0

Views: 2884

Answers (3)

Tanzeel Khan
Tanzeel Khan

Reputation: 46

Something like this:

import { TextField } from "@mui/material";
    
    const App = () => {
      return (
        <TextField
                  error={error}
                  defaultValue=""
                  id="standard-select"
                  label="Select Options"
                  variant="outlined"
                  value="Hello"
         </TextField>
      );
    };
    
    export default App;

Upvotes: 0

Mr. Polywhirl
Mr. Polywhirl

Reputation: 48723

You can use styled components:

import { TextField } from "@mui/material";
import { styled } from "@mui/material/styles";

const StyledTextField = styled(TextField)`
  & > .MuiInputBase-root > input {
    font-style: italic;
  }
`;

const App = () => {
  return (
    <div className="App">
      <StyledTextField
        InputLabelProps={{ shrink: true }}
        id="outlined-size-normal"
        label="Size"
        value="Hello"
        variant="outlined"
      />
    </div>
  );
};

export default App;

Note: You will need to install @emotion/react and @emotion/styled.


Demo here: https://codesandbox.io/s/laughing-goodall-6q6n7j?file=/src/App.jsx

Upvotes: 0

Dobromir Kirov
Dobromir Kirov

Reputation: 1042

Something like this:

<TextField
    placeholder="your placeholder"
    value={"hello"}
    inputProps={{
      sx: {
        fontStyle: "italic"
      },
    }}
  />

Upvotes: 2

Related Questions