0

I am using reactjs and material ui and i am not being able to change the background color of selected items in several components, for example in the SelectField.

<SelectField
   floatingLabelText="Choose a sport"
   value={this.state.value}
   onChange={this.handleChange.bind(this)}
   menuStyle={{color:'red'}}
   menuItemStyle={{color:'black', borderBottom:'1px solid white'}}
   listStyle={{backgroundColor:'rgb(0, 188, 212)'}}
   labelStyle={{color:'black'}}

But i don't know how to add a hover functionality or change the selected item color.

Any experiences on this?

Thanks!

3 Answers 3

1

Material-UI uses JSS for compiling style. You can see more about that in Material-UI's documentation.

CSS selectors are done as another property, so you can just use &:hover

button: {
  fontSize: 12,
  '&:hover': {
    background: 'blue'
  }
},
Sign up to request clarification or add additional context in comments.

Comments

0

If you want to make hover in your component, you can use css styles:

    <div>
        <style>
          {`
              .menuItem:hover {
                background-color: red !important;
              }

              .menuItem {
                background-color: transparent !important;
              }      
          `}
        </style>
        <MuiThemeProvider>
          <SelectField
             id="field"
            floatingLabelText="Choose a sport"
            menuStyle={{ color: 'red' }}
            menuItemStyle={{ color: 'black', borderBottom: '1px solid white' }}
            listStyle={{ backgroundColor: 'rgb(0, 188, 212)' }}
            labelStyle={{ color: 'black' }} >
            <MenuItem className="menuItem" value={1} primaryText="Never" />
            <MenuItem className="menuItem" value={2} primaryText="Every Night" />
            <MenuItem className="menuItem" value={3} primaryText="Weeknights" />
            <MenuItem className="menuItem" value={4} primaryText="Weekends" />
            <MenuItem className="menuItem" value={5} primaryText="Weekly" />
          </SelectField>
        </MuiThemeProvider>
      </div>

https://codesandbox.io/s/w7q276lk08

3 Comments

This does not work. If you add this: <SelectField id="field" floatingLabelText="Choose a sport" menuStyle={{ color: 'red' }} menuItemStyle={{ color: 'black', borderBottom: '1px solid white' }} listStyle={{ backgroundColor: 'rgb(0, 188, 212)' }} labelStyle={{ color: 'black' }} > <MenuItem>a</MenuItem> <MenuItem>a</MenuItem> </SelectField> the hover only applies on the select field, not the menu items
@LucasMilotich, sure, this hover was for the select, but not menu items. I've edited the code and the sndbox link for menu items hover.
@LucasMilotich, glad to help :) Mark as right answer then.
0

There is selectedMenuItemStyle property you can apply style like below.

 <SelectField
  floatingLabelText="Choose a sport"
  value={this.state.value}
  onChange={this.handleChange.bind(this)}
  menuStyle={{color:'red'}}
  menuItemStyle={{color:'black', borderBottom:'1px solid white'}}
  listStyle={{backgroundColor:'rgb(0, 188, 212)'}}
  labelStyle={{color:'black'}}
  selectedMenuItemStyle={{color:'red'}}

>

1 Comment

This only works with the selected option, I would like to change the background color on mouse over (for example)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.