Troubleshooting Material UI Widget Components

After working diligently on the autocomplete component, I am pleased with how the feature turned out. However, when this component is rendered as a widget on other pages, I'm encountering some style issues due to global styling overrides or additions on the page that hosts my component.

Here is how it appears in my local environment: https://i.sstatic.net/JUC5U.png

And this is how it looks once deployed and checked on one of the pages: https://i.sstatic.net/26J5c.png https://i.sstatic.net/vPWAx.png

Despite spending the entire day trying to resolve these issues without success, I have identified the specific styles causing problems for my component:

https://i.sstatic.net/YJWNS.png

https://i.sstatic.net/lZ3q7.png

These styles are used to hide the outlined style of the textfield

const useStyles = makeStyles(theme => ({
  root: {
    "& .MuiOutlinedInput-root .MuiOutlinedInput-notchedOutline": {
      border: 'none !important',
      outline: 'none'
    },
    "& .MuiOutlinedInput-root.Mui-focused .MuiOutlinedInput-notchedOutline": {
      border: 'none !important',
      outline: 'none'
    }
  }
}));

Below is an insight into how my autocomplete components are structured:

<Autocomplete
          id="listings-filter"
          multiple
          disableCloseOnSelect={true}
          freeSolo
          clearOnBlur={false}
          limitTags={5}
          disableCloseOnSelect={true}
          blurOnSelect={false}
          options={options}
          groupBy={(option) => option.key }
          onInputChange={handleInputChange}
          onChange={handleOptionSelection}
          disableCloseOnSelect
          getOptionLabel={(option) => option.value}
          renderOption={(option, { selected }) => (
            <React.Fragment>
              <Checkbox
                icon={icon}
                checkedIcon={checkedIcon}
                style={{ marginRight: 8 }}
                checked={selected}
              />
            {option.value}
            </React.Fragment>
          )}
          style={{ width: "100%" }}
          renderInput={(params) => (
            <TextField
              id='autocomplete-input'
              {...params}
              margin={'dense'}
              className={autocompleteClasses.root}
              InputLabelProps={{
                shrink: true
              }}
              variant='outlined'
              label="Search listings by address, MLS or property name"
              placeholder='Type the address, MLS or property name'
            />
          )}
        />

I attempted to apply inputProps to the textfield and adjust styles there, but this did not yield positive results. Similarly, I tried adding styles within the makeStyles section, yet I struggle to pinpoint the exact class for MUI style overrides. The nature of this issue, seemingly related to a generic input component rather than a Material UI component, further complicates matters.

I am unsure if resolving this via React is feasible or if resorting to a CSS file is necessary to mitigate this behavior. Any guidance or assistance would be greatly appreciated!

EDIT: In addition to referencing another StackOverflow question and utilizing the inputProps of the TextField component, I encountered an error where the autocomplete component crashes upon clicking the input - error message: Uncaught TypeError: Cannot read property 'focus' of null

const useStyles = makeStyles(theme => ({
  root: {
    "& .MuiOutlinedInput-root .MuiOutlinedInput-notchedOutline": {
      border: 'none !important',
      outline: 'none'
    },
    "& .MuiOutlinedInput-root.Mui-focused .MuiOutlinedInput-notchedOutline": {
      border: 'none !important',
      outline: 'none'
    }
  },
  input: {
    border: '10 !important',
    borderColor: 'red',
    boxShadow: 'unset !important',
    color: 'red'
  }
}));

renderInput={(params) => (
            <TextField
              id='autocomplete-input'
              {...params}
              margin={'dense'}
              className={autocompleteClasses.root}
              InputLabelProps={{
                ...params.InputLabelProps,
                shrink: true
              }}
              inputProps={{
                ...params.InputProps,
                classes:{
                  root: autocompleteClasses.input,
                }
              }}
              variant='outlined'
              label="Search listings by address, MLS or property name"
              placeholder='Type the address, MLS or property name'
            />
          )}

Answer №1

My solution involved creating a scss file to address the issue:

.custom-autocomplete > div > div > input {
  border: 0px !important;
  border-color: white !important;
  box-shadow: unset !important;
  -webkit-box-shadow: unset !important
}

I then applied this as a className on the autocomplete component:

import './customStyles.scss'

<Autocomplete
          id="listings-filter"
          className={'custom-autocomplete'}
          multiple
          disableCloseOnSelect={true}
          freeSolo
          clearOnBlur={false}
          limitTags={5}
          disableCloseOnSelect={true}
          blurOnSelect={false}
          options={options}
          groupBy={(option) => option.key }
          onInputChange={handleInputChange}
          onChange={handleOptionSelection}
          disableCloseOnSelect
... />

Hopefully, this solution can be helpful to others facing a similar issue!

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

Only CSS creates tooltips within text seamlessly without any line breaks

I have been exploring ways to incorporate multiple tooltips into text on my website. After experimenting with both span and div tags, I was able to create a tooltip using CSS only. However, I am facing challenges with positioning the tooltiptext correctly. ...

Join and Navigate in Angular 2

Attempting to retrieve information from a JSON file has been an issue for me. Here is the code snippet: ngOnInit() { this.http.get('assets/json/buildings.json', { responseType: 'text'}) .map(response => response) .subsc ...

Include dropdown lists for selecting the year, month, and day on a web page

Is there a way to implement a dropdown style date selector that dynamically updates the number of days based on the selected year and month using JavaScript? For example, February 2008 has 29 days, April has 30 days, and June has 31 days. Any suggestions ...

show textboxes positioned in the middle of a slanted rectangle

Is there a way to place centered textboxes inside each of the colorful boxes in the image below? Update: I've posted my feeble attempt below. Admittedly, design is not my forte and I'm struggling with this task. p.s. To those who downvoted, tha ...

When using console.log in Node.js, an empty object may be displayed as the

I'm intrigued by the way Node.js displays objects when using console.log(object). In the file constructor.js, I found the following code snippet (taken from the book Learning Javascript Design Patterns): var defineProp = function(obj, key, value){ ...

The error "relative error: invalid property value" occurs when setting the width and height of a parent element

I'm having trouble getting my h2 text to appear above my images when hovered over. I keep receiving an error message in Chrome Developer Tools saying that "display: relative" is not a valid property value, but I can't figure out why. I've se ...

The state change in React-Redux does not trigger a re-render

I successfully developed a large react-redux application with an undo feature in one component. While the state updates properly and re-renders the component along with its children, I noticed that the component's position on the page remains unchange ...

Is there a way to display x squared as a superscript inside a button element?

In my reactjs project, I am developing a basic calculator. One of the buttons is supposed to calculate x squared, and I tried using the <sup> HTML element for this purpose but it did not work as expected. <Button name = "x<sup>2</sup> ...

SVG Error: You might require a suitable loader to manage this file format, as there are currently no configured loaders to handle it

Encountered an issue with my application - everything was functioning properly until I executed npm run audit fix force. Now, I am receiving the following error message related to an SVG file. The error indicates that there may be a need for a loader to h ...

Trying to get a jQuery click function to trigger only once in CoffeeScript code

I've been searching high and low for a solution but nothing seems to be working. The issue I'm encountering is that I have a function that is supposed to post content to either a Facebook wall or a Twitter feed, but the click function only seems ...

How to Spin an Image in the Canvas Using HTML5

I am having trouble rotating an image inside a canvas after researching similar questions on Stack Overflow. Here's what I've learned: It seems I cannot rotate a single object inside the canvas. I can only rotate the entire canvas itself. ...

Halting a function within a specific namespace

Is there a way to prevent certain characters from being entered during a keypress event for inputs when using a namespace in JavaScript? I noticed that even if the function returns false when the character is detected, the character still gets entered. How ...

Ways to display a random selection of six items in a ReactJS array using React Hooks and then shuffle the array

Currently working on developing a hangman game and using an array with 6 items for each round. Each item will be shown once without repetition in a single round. However, I've encountered a problem with my code where it keeps looping through the array ...

Best Practices for Organizing Enums in Your React Application

In my React project, I am working with multiple enums. How should I organize them within my application? Currently, I have created an enums.js file located in the config folder where I define enums as follows: export const USER_TYPES = { USER: "user", ...

Bringing Typescript classes into React-scripts does not act as a constructor

I am currently working on integrating a Typescript file into my existing app using Yarn and React-scripts. Encountered error: Module not found, unable to resolve './DiamondNodeModel' import {DiamondNodeModel} from './DiamondNodeModel&ap ...

Interactive calendar feature displaying events upon hovering over a date

I need some assistance with displaying a drop-down list on full calendar events when hovering over the events. Can someone provide guidance? Here is a glimpse of what I currently have: https://i.sstatic.net/fZXMH.png I've attempted setting the z-in ...

Verify whether a specific value is present within a nested array in MongoDB

Looking to verify whether a value sent in a request already exists within an array associated with a particular User. The data structure is as follows: { "_id": "63233972df0f14076e027106", "firstName": "mako" ...

Troubleshooting issues with JSON compatibility within the OnChange event

Initially, I wrote this code to retrieve a single data point. However, I realized that I needed more fields returned when the drop-down select menu triggered an onchange event. So, I switched to using JSON, but now it's not returning any data. The dro ...

How can I deselect the 'select-all' checkbox when any of the child checkboxes are unchecked?

Here is the code provided where clicking on the select-all checkbox will check or uncheck all child checkboxes. If any child checkbox is deselected, the 'select-all' checkbox should also be unchecked. How can this be achieved? $(document).read ...

Learn how to pass an id from the query parameters to the getInitialProps function in Next.js

Looking to create a basic website that displays content from a database? Utilizing the getInitialProps function from Next.js documentation can help with server-side rendering: https://nextjs.org/docs/api-reference/data-fetching/getInitialProps#getinitialpr ...