how to make read only mui textfield?

Hi Friends ๐Ÿ‘‹,

Welcome To aGuideHub! โค๏ธ

Make read only mui textfield

To make read only mui textfield in react js, use the inputProps prop, in this prop we can set the readOnly flag to handle mui textfield read-only feature.

User canโ€™t able to change the text of the input field when you set readOnly to true.

Today, I am going to show you. how to make read-only mui textfield.

<TextField
  type="text"
  id="outlined-basic"
  label="Outlined"
  variant="outlined"
  onChange={(e) => setText(e.target.value)}
  value={text}
  InputProps={{
    readOnly: true,
  }}
/>

Installation

Emotion library is required to create react components that have styles attached to them.

npm install @mui/material @emotion/react @emotion/styled

MUI material textfield readOnly example

To set readOnly true in mui textfield, pass readOnly true in inputProps prop it will make mui text field read-only.

App.js

import * as React from "react";
import Box from "@mui/material/Box";
import TextField from "@mui/material/TextField";

export default function BasicTextFields() {
  const [text, setText] = React.useState("infinitbility.com");

  return (
    <Box component="form">
      <TextField
        type="text"
        id="outlined-basic"
        label="Outlined"
        variant="outlined"
        onChange={(e) => setText(e.target.value)}
        value={text}
        InputProps={{
          readOnly: true
        }}
      />
    </Box>
  );
}

In the above code example, I set readOnly to true, you can also try it in the below sandbox.

Check the output of the above code example.

All the best ๐Ÿ‘

codesandbox

Premium Content

You can get all the below premium content directly in your mail when you subscribe us

Books

Interview Questions

Soon You will get CSS, JavaScript, React Js, and TypeScript So Subscribe to it.

Portfolio Template

View | Get Source Code

Cheat Sheets

Cheat Sheets Books are basically Important useful notes which we use in our day-to-day life.

Related Posts