Change the Label in a TextField Component with Material UI

How do I customize the label of a TextField and prevent a grey background color from appearing after selecting an item in Material UI?

To view the solution, please visit this CodeSandbox link: CLICK HERE

Label Customization Issue: https://i.sstatic.net/1Nqbi.jpg

Grey Background Color Problem: https://i.sstatic.net/bIF3U.jpg

<TextField
  variant="outlined"
  label="Choose"
  style={{
    background: "#fff"
  }}
  InputProps={{
    className: classes.input
  }}
  fullWidth
  select
>
  {results.map((result, index) => (
    <MenuItem key={index} value={result.id}>
      {result.likes}
    </MenuItem>
  ))}
</TextField>

Answer №1

Here's a method you can try: Adjust the margin of the TextField to create space below it without affecting the position of the Label. Then, enclose the TextField within a <div></div> and raise it by the same distance.

import { TextField } from "@material-ui/core";
import { makeStyles } from "@material-ui/styles";
import React from "react";
import MenuItem from "@material-ui/core/MenuItem";

const useStyles = makeStyles((theme) => ({
  input: {
    height: "35px",
    marginTop: '8px',
  }
}));

export default function Test() {
  const classes = useStyles();
  const results = [
    {
      id: 1,
      name: "John Jomes",
      likes: "Food"
    },
    {
      id: 2,
      name: "John Jomes",
      likes: "Food"
    },
    {
      id: 3,
      name: "John Jomes",
      likes: "Food"
    }
  ];

  return (
    <div style={{marginTop: '8px'}}>
      <TextField
      variant="outlined"
      label="Choose"
      InputProps={{
        className: classes.input
      }}
      fullWidth
      select
      >
      {results.map((result, index) => (
        <MenuItem key={index} value={result.id}>
          {result.likes}
        </MenuItem>
      ))}
      </TextField>
    </div>
  );
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Can you please provide the appropriate PropTypes for a dictionary in a ReactJS project?

Looking for something along the lines of y = {"key0": [value0, value1], "key1":[value2]} What is the proper way to define the proptypes for y? ...

Unexpected behavior in ReactJS when using Material UI

In my Webpack + ReactJS project, I am working on creating a table using Material UI. I am trying to implement an 'icon menu' feature with a 'menu item' that allows users to delete a specific line along with its associated data. Below i ...

Moving TextField label orientation

I'm having trouble adjusting the position of a label within a TextField without having to change the entire project layout to right-to-left (RTL) like I've seen in other solutions. I simply want to customize the style for this specific component. ...

Creating a dynamic and flexible div using CSS

I am currently working on a small webpage and encountering an issue with responsiveness when scaling the browser window in and out. The design looks perfect at 320x568 resolution, but when I scale it down, a black box (`.transbox`) overlaps the image, whic ...

Stop jQuery popups from wrapping text when resizing the browser window

Whenever a link is clicked, a jQuery popup appears. The popup functions properly in terms of opening and closing, but I would like to ensure that its position remains fixed when resizing the browser window to avoid any wrapping issues. Typically, I use a ...

What is the best way to calculate the total of a field with matching Project and Employee names?

My task involves grouping data from an Array of objects by Project Name and Employee Name, whereby existing projects and employees have their hours added together. projects = [ { "project": { "id": 1, "name": "M ...

The justify-content-sm-center behavior in Bootstrap seems to be malfunctioning

I am facing an issue with justify-content-sm-center in Bootstrap 5. It is not functioning as intended when trying to center a specific div on smaller screens (below 578px). I have applied the justify-content-sm-center class to the div, but it does not work ...

Configure remotePatterns in the next.config.js file on localhost to retrieve images from a local API source

Hey there, I'm currently working on building a random image API. The idea is that when I visit the page http://localhost:3000/random-img, I should get a new random image each time. My goal is to use the URL in this format <Image src="http//lo ...

Does renaming a page to index.html have any impact?

After restarting my laptop, everything was back to normal. Thank you for the help and replies. The issue I'm facing is with two identical pages that have the same code. The only difference between them is the file names - index and index2. However, f ...

Activate or deactivate a button depending on the input value of a TextField in React.js

I am currently designing a form similar to the one displayed in this image. I want the "Add" button to become active as soon as the user starts typing in the "Tags" input field. For this project, I am using material-ui and I have created an Input compone ...

Adjust dimensions of 3D Object in React

This is my initial attempt at using Three.js / @react-three/fiber / @react-three/drei within a React project. However, I encountered an issue with a 3D model I downloaded from Sketchfab.com, as it appears too large on my website. I've been searching ...

There was no package discovered matching the designated pattern: D:a 1a***.zip

After creating a build definition in VSTS with npm build and copying the build folder to the drop location, the build pipeline is functioning correctly. Next, when setting up the release definition, the files are downloading successfully: A series of tim ...

Learn how to retrieve information using a React app connecting to a Node.js and Express server, specifically when the React app is initiated from the

I am currently facing an issue with my react-app that is being called from my node-server. The problem arises when the react-app tries to fetch data from the server, but it seems to encounter a hurdle. This seems to be connected to a specific piece of code ...

AngularJS is used to vertically collapse three different div elements. This feature

I am facing an issue with the design of my page that contains three panels. Two of these panels have tables, and when viewing the page on a smaller display, it disrupts the layout. I am wondering if there is a way to add collapsible buttons to the panels s ...

When a td element is clicked, the Textbox will also be clicked

Similar Question: Dealing with jQuery on() when clicking a div but not its child $(oTrPlanning).prev().children('td').each(function () { this.onclick = setCountClick; }); When a TD element is clicked, the function setCountClick() i ...

Is it possible to stack images on top of each other on a webpage?

I'm looking to create a dress-up game website where users can click on different accessories to layer them on top of each other. It's a bit tricky to explain, but I've found some examples that demonstrate what I have in mind: This site is ...

Enhance your breadcrumb experience with MUI Breadcrumbs in React Router by customizing the labels

I have successfully implemented breadcrumbs using MUI and React Router. import React from "react"; import { Breadcrumbs as AppBreadcrumbs, Link, Typography, } from "@material-ui/core"; import { withRouter } from "react-router ...

participate in the eventChannel by performing all actions triggered instead of simply receiving the output

I have been attempting to subscribe to firebase onAuthStateChanged utilizing the event channel in redux-saga. I have created a sandbox showcasing the current status. https://codesandbox.io/s/42kwzv2324 On line 71 and 72 in sagas/sagas.js, I am logging th ...

Hosting React on IIS without refreshing the DOM

Currently developing a React Application with 4 pages. The routing works perfectly when running locally, all pages are rendered properly. However, the same cannot be said for IIS. I have set up a 'dist' folder and mapped it to the IIS directory. ...

Thymeleaf: Automating the Adjustment of Content Fragments

In my project using Spring MVC and Thymeleaf, there is a default fragment that sets up the page structure. Here's what it looks like: <!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org" xmlns:layout="http://www.ultraq.net.nz/thymeleaf/ ...