ReactJS allows the display of placeholder text for input fields only when they are actively selected

After modifying the BootstrapInput code snippet to the following:

<BootstrapInput 
    id="age-customized-input" 
    placeholder="Search Title"
/>

I noticed that the placeholder text only appears when the input field is activated.

Does anyone know how to keep the placeholder text visible at all times?

The code snippet I'm referring to is from the Material-UI example below.

import React from 'react';
import { makeStyles, withStyles } from '@material-ui/core/styles';
import InputLabel from '@material-ui/core/InputLabel';
import MenuItem from '@material-ui/core/MenuItem';
import FormControl from '@material-ui/core/FormControl';
import Select from '@material-ui/core/Select';
import NativeSelect from '@material-ui/core/NativeSelect';
import InputBase from '@material-ui/core/InputBase';

const BootstrapInput = withStyles(theme => ({
  root: {
    'label + &': {
      marginTop: theme.spacing(3),
    },
  },
  input: {
    borderRadius: 4,
    position: 'relative',
    backgroundColor: theme.palette.background.paper,
    border: '1px solid #ced4da',
    fontSize: 16,
    width: 'auto',
    padding: '10px 26px 10px 12px',
    transition: theme.transitions.create(['border-color', 'box-shadow']),
    // Use the system font instead of the default Roboto font.
    fontFamily: [
      '-apple-system',
      'BlinkMacSystemFont',
      '"Segoe UI"',
      'Roboto',
      '"Helvetica Neue"',
      'Arial',
      'sans-serif',
      '"Apple Color Emoji"',
      '"Segoe UI Emoji"',
      '"Segoe UI Symbol"',
    ].join(','),
    '&:focus': {
      borderRadius: 4,
      borderColor: '#80bdff',
      boxShadow: '0 0 0 0.2rem rgba(0,123,255,.25)',
    },
  },
}))(InputBase);

const useStyles = makeStyles(theme => ({
  root: {
    display: 'flex',
    flexWrap: 'wrap',
  },
  margin: {
    margin: theme.spacing(1),
  },
}));

export default function CustomizedSelects() {
  const classes = useStyles();
  const [age, setAge] = React.useState('');
  const handleChange = event => {
    setAge(event.target.value);
  };
  return (
    <form className={classes.root} autoComplete="off">
      <FormControl className={classes.margin}>
        <InputLabel htmlFor="age-customized-input">Age</InputLabel>
        <BootstrapInput id="age-customized-input" />
      </FormControl>
      <FormControl className={classes.margin}>
        <InputLabel htmlFor="age-customized-select">Age</InputLabel>
        <Select
          value={age}
          onChange={handleChange}
          input={<BootstrapInput name="age" id="age-customized-select" />}
        >
          <MenuItem value="">
            <em>None</em>
          </MenuItem>
          <MenuItem value={10}>Ten</MenuItem>
          <MenuItem value={20}>Twenty</MenuItem>
          <MenuItem value={30}>Thirty</MenuItem>
        </Select>
      </FormControl>
      <FormControl className={classes.margin}>
        <InputLabel htmlFor="age-customized-native-simple">Age</InputLabel>
        <NativeSelect
          value={age}
          onChange={handleChange}
          input={<BootstrapInput name="age" id="age-customized-native-simple" />}
        >
          <option value="" />
          <option value={10}>Ten</option>
          <option value={20}>Twenty</option>
          <option value={30}>Thirty</option>
        </NativeSelect>
      </FormControl>
    </form>
  );
}

Explore the Material-UI example here

Answer №1

The label is being hidden by the backgroundColor property. If you disable this line of code, the labels will be visible. You can see the code sandbox example below:

  //backgroundColor: theme.palette.background.paper,

https://codesandbox.io/s/material-demo-p2f7h

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

Having trouble getting the header div to span the entire screen

I'm currently working on a project where I have set the header div to 100% width and the main container to also be 100% width. However, I am facing an issue where the right and left sides of the header are not filling the entire space. I managed to fi ...

Using Flickity to select cells in React

I have a dropdown menu where each option is linked to an image ID. The goal is to display the corresponding image when an option is selected. Here's the code snippet I'm using: const myCustomNext = () => { flkty.selectCell(somevar) }; ...

Conceal a div while revealing the other div

I am currently trying to achieve a unique visual effect. I have three divs named div1, div2, and div3. The objective is for div1 to slide up and disappear when clicked, while div2 simultaneously slides up and becomes visible. Similarly, when div2 is click ...

Preserving focus in React when clicking on another element

In the table I'm working on, there are two columns. The right column consists of input fields. When a user focuses on an input field (as shown in the image below), a non-focusable button appears on the right side of the table. Upon clicking this butto ...

Are you encountering an issue with your React application while attempting to retrieve JSON data?

I recently developed a Home.js component and integrated it within the App.js file of my React application. However, I am encountering an error when trying to fetch JSON data. The error message is displayed at this link: https://i.stack.imgur.com/hGkN3.png ...

Using React JS: Navigate to a different page when a button is clicked along with the data from a table

I am attempting to redirect from the 'SalesList.jsx' page to 'SalesInvoice.jsx' when a button is clicked, but unfortunately, I have been unsuccessful in doing so. Although I am able to retrieve all the props upon clicking the button, I ...

How to automatically set a default bootstrap tab upon page load using an MVC JsonResult responseData

I'm looking to enhance the user experience on my dashboard by allowing users to set their preferred default tab view upon logging in. Using an AJAX call to retrieve the value from the database in MVC, I am working on the JS/Razor side of things to mak ...

Cookies are storing the access token, however, the validation process fails to detect it within react.js using axios. Strangely, it works perfectly

Recently, I created a login function that generates an access token in cookies. However, I encountered an issue where the page did not detect the cookie when performing validation for a particular page. Interestingly, it works fine in Postman but fails to ...

What is the best way to remove this .click function?

My goal is to create a switch function where clicking the "on" button changes its CSS, and if the user then clicks on the "off" button, the CSS returns to normal. I also need the switch to default to the "on" position, but my attempts so far have been unsu ...

"The boundary of suspense was updated before completing hydration." This error occurs when utilizing suspense and dynamic import within Next.js

I am currently working on lazy loading a list of data using suspense and dynamic import in Nextjs. However, I encountered the following error: Error: This Suspense boundary received an update before it finished hydrating. This caused the boundary to switch ...

Removing characters from a string with regular expressions

I need to eliminate instances of << any words #_ from the given text. stringVal = "<<Start words#_ I <<love#_ kind <<man>>, <<john#_ <<kind man>> is really <<great>> <<end words#_ "; The d ...

Oops! We encountered an issue with your request, resulting in a status code of 400. This error occurred while utilizing Axios with

Having trouble posting to a mongoDB database while creating a user authentication site using MERN stack. Despite trying multiple solutions, the issue persists with various error messages. Backend is configured using Node.js, Express.js, and MongoDB. Assist ...

I am facing an issue with MUI DatePicker that is causing issues with rendering in my project. I have also included a

Check out my codesandbox example showcasing MUI's Datepicker in action: https://codesandbox.io/s/proud-pond-fb4rm?file=/src/App.js *Update: I've also included my index.tsx code snippet: import React from 'react'; import ReactDOM from ...

The onClick functionality for the IconComponent (dropdown-arrow) in React Material UI is not functioning properly when selecting

I have encountered a problem in my code snippet. The issue arises when I attempt to open the Select component by clicking on IconComponent(dropdown-arrow). <Select IconComponent={() => ( <ExpandMore className="dropdown-arrow" /> )} ...

Is it feasible to automate the cropping process with selenium?

Exploring a new challenge: simulating a cropping feature. I understand that replicating human cropping is impossible, but the image I intend to crop remains a fixed size each time I run the test. My goal is to establish the cropping WebElement at a constan ...

Angular animation not firing on exit

I am encountering an issue with my tooltip component's animations. The ":enter" animation is working as expected, but the ":leave" animation never seems to trigger. For reference, here is a link to stackblitz: https://stackblitz.com/edit/building-too ...

Issues with the webpack-dev-server and React

I was just reading up on Webpack and the moment came for me to start using it, but I ran into an error. npm ERR! code ELIFECYCLE npm ERR! errno 1 npm ERR! <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="6a09180b471e1f1e ...

How to customize the appearance of an HTML checkbox using custom images for a unique

Does anyone know how to change the default style for: <input type=checkbox...> I want it to use my own styled pictures instead of the default style. Can anyone help? Thank you! ...

Organizing list items into vertical columns

Could you help me with UL/LI syntax and how to create a wrapping list? For example, I would like to display: 1 2 3 4 5 6 As: 1 4 2 5 3 6 Currently, I have a header, content, and footer all set with specified heights. I want the list to wrap when ...

Ways to identify the visible elements on a webpage using JavaScript

I'm working on a nextjs app , I am looking to dynamically update the active button in the navbar based on which section is currently visible on the page. The child elements of the page are structured like this: <div id="section1" > < ...