Activate a pointer cursor when hovering over a TextField in Material-ui design

Hey there! I'm new to Material-ui/ReactJS and I have a question. I'm trying to change the cursor to a pointer when hovering over a Material-ui TextField, but it's proving to be quite challenging. The default behavior is 'cursor: text', and even though I've successfully changed the background color on hover, adding "cursor: pointer !important" doesn't seem to work. I've experimented with className, class, inline styles, but I must be missing something. Material-ui provides a demo showcasing how to style textfields on hover and focus at [https://codesandbox.io/s/p7uwn?file=/demo.js][1]. I tried to apply the same logic to change the cursor to a pointer on hover, but no luck so far. Any help or guidance would be greatly appreciated.

import React from 'react';
import styled from 'styled-components';
import { TextField, NoSsr } from '@material-ui/core';

const StyledTextField = styled(TextField)`
  label.Mui-focused {
    color: green;
  }
  .MuiOutlinedInput-root {
    fieldset {
      border-color: red;
    }
    &:hover fieldset {
      border-color: yellow;
      cursor: pointer !important;
    }
    &.Mui-focused fieldset {
      border-color: green;
    }
  }
`;

export default function GlobalClassName() {
  return (
    <NoSsr>
      <StyledTextField label="Deterministic" variant="outlined" id="deterministic-outlined-input" />
    </NoSsr>
  );
}


 

Answer №1

After experimenting with the code, I discovered that using

<TextField sx={{ cursor: 'pointer' }} />

was not effective for achieving the desired result. Instead, I found success by specifying it as

<TextField sx={{ input: { cursor: 'pointer' } }}

This adjustment made the desired change take effect successfully.

Answer №2

Simply include cursor:pointer in the CSS of your component, whether it be inline like

<Component style={{cursor: 'pointer'}}>
, through
<Component sx={{cursor: 'pointer'}}>
, or within its styled component CSS. This will automatically change the mouse behavior on hover without going overboard with unnecessary complexity. Just remember to add cursor: 'pointer' to the component's CSS.

Answer №3

After a brief examination of the browser, we were able to identify the necessary CSS component to target. This is

.MuiOutlinedInput-input

Simply adding the following style will resolve your issue:

cursor: pointer;

For reference, here is the code snippet:

import React from 'react';
import styled from 'styled-components';
import { TextField, NoSsr } from '@material-ui/core';

const StyledTextField = styled(TextField)`
  label.Mui-focused {
    color: green;
  }
  .MuiOutlinedInput-input {
    cursor: pointer;
  }
  .MuiOutlinedInput-root {
    fieldset {
      border-color: red;
    }
    &:hover fieldset {
      border-color: blue;
      cursor: pointer;
    }
    &.Mui-focused fieldset {
      border-color: green;
    }
  }
`;

export default function GlobalClassName() {
  return (
    <NoSsr>
      <StyledTextField label="Deterministic" variant="outlined" id="deterministic-outlined-input" />
    </NoSsr>
  );
}

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

Adjusting the dimension of a bootstrap button based on a value using React

Just getting started in the world of React and could use some assistance achieving this specific design: https://i.stack.imgur.com/E8PAr.png The buttons/widgets displayed above represent a certain number of items. I'm looking to adjust the size of t ...

javascript implementing optional chaining for a single parameter

Is it possible to implement optional chaining on a single parameter? setAllProperties( Object.values(users).flatMap(({ properties }) => Object.values(properties) ) ); I am looking for a way to ensure that the properties folder exists in ...

The issue arises when attempting to use input alongside debounce, event.persist(), and storing the value at the parent component

Is there a way to implement an input field with debounced search where the value is passed from the parent component? Currently, when I pass the value from the parent component it doesn't work as expected. What would be the correct approach to make th ...

Finding the Solution: Unraveling an Image String with Python

I need help decoding an image string. For example, I have a string that represents the Google logo. How can I determine the encoding used and decode it correctly? Example of google logo : (Here you will find instructions on styling properties followed by ...

Tips for altering the visibility of a different class on hover using jss

Exploring the features of material ui react Below is my scss code snippet (when hovering over .content, the .replyBtn becomes visible): .content { &:hover { .replyBtn { visibility: visible } } } .replyBtn { visibility: hidden; } ...

Expand the width and include a placeholder in mat input field for Angular version 5

Currently utilizing a mat input with the code provided, presenting a similar appearance to the screenshot below. I am looking to add a placeholder that disappears once the user begins typing. However, in my current setup, the text shifts upward and floats ...

The sub-menu positioning feature functions properly in Chrome and Safari, but is not compatible with Internet Explorer and Firefox

Working on a website for a gaming community, known as http://www.quad-gaming.com/. All is running smoothly except for the dropdown menu under "Login" on the right side of the navigation bar. It behaves properly in Chrome and Safari, but in IE and Firefox, ...

Show 404 error for Express.js routes only in production environment

I've been working on creating a video conferencing application based on this guide. While the tutorial was helpful during development, I encountered issues when trying to push the application to production. Unfortunately, my familiarity with these to ...

Error in React Component: Array objects in response are not in correct order

My React app is receiving data via websocket, with a big object that contains game information. One of the properties is an array of player objects: { propertyX: "X", players: [{player1}, {player2}, {player3}], propertyY: "Y" } The issue I'm f ...

Error occurred: Undefined module imported

CounterDisplay.js import React from 'react'; const CounterDisplay = <div> <h1>{this.state.counter}</h1> <button onClick={this.handleDecrement}>-</button> <button onClick={this.handleIncrement}>+ ...

Disregard the CSS styling within the modal window

Currently, I am working with ReactJS and ANTD. My modal has been adjusted with paddings to center-align the text, but now I want to enhance the design. Here is the desired look: https://i.stack.imgur.com/qef7n.png This is how it appears at the moment: htt ...

Error occurred during SCSS rendering process: [Internal Error: Unable to locate or read the specified file.]

When trying to create a website using scss, I encounter an error every time I save the scss file. Strangely, after saving it multiple times, the file will render properly. Although this issue is minor, it really bothers me. I have searched for a solution ...

Changing a p element to a div and adding a class in Bootstrap 4.5

To maintain consistency, I prefer using <div class='something'>BIO</div> over <p>BIO</p>. In Bootstrap 4.5, is there a similar class that achieves the same result? I'm not very skilled in CSS, so please forgive me i ...

Utilizing Role-Based Access Control to Restrict Routes in React

Need help with setting up 2 routes for different types of users. I already have authentication in place, but unsure how to differentiate between the two. Any suggestions or links to relevant documentation would be greatly appreciated as I've been rese ...

Utilizing Jquery to implement active state and unique IDs during mouseover operation

I found this great example on Stack Overflow that demonstrates Jquery Show/Hide functionality when hovering over links. It works perfectly by showing the content of the next div when the corresponding link is hovered over. However, I'm facing an issu ...

javascript alter css property display to either visible or hidden

I am struggling with a CSS id that hides visibility and uses display: none. The issue arises when I want the element to be visible upon clicking a button, but removing the display:none property is causing design problems due to it being an invisible elemen ...

React CRUD API: Error - Request method is invalid 405

I've recently configured a project using DRF with Postgre as the backend and React-admin for the frontend. class CreateTracker(generics.ListCreateAPIView): """ This allows users to create a tracker """ queryset = Trac ...

Ways to align two elements within the same level in the DOM?

Is it really impossible to have two elements render parallel to each other on the z-axis? Even with the same z-index, they are treated as being on different levels based on their order in the DOM. This can be a challenge when trying to display nearby eleme ...

List items out of place because of CSS incompatibility in Firefox and Chrome

After creating a list of names displayed in an <ul>, I implemented some CSS styling only to encounter browser-specific issues. In Chrome: The list element is shifted by one row. In Firefox: All list items are collapsing into one item. Here&apo ...

Using Express to host the webpack-dev server

I have been using webpack-dev-server to run my project, but now I want to switch to running it through an express app. What steps do I need to take for this transition? My Package.json details: { "name": "react-redux-template", "version": "1.0.0" ...