question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How do i highlight selected item in dropdown

See original GitHub issue

I want to apply a css class to the current selected item when the dropdown is open. Something like the following.

capture

As you can see the current selected item is highlighted in red when the dropdown is opened. This is done by passing the className to the option that is currently selected as follows:-

const options = data.map((item) => {
    let className = item.isDefault ?
        'highlight-selected-item ' :
        '';

    return {
        label: item.profileName,
        value: item.profileName,
        className
    };
});

I think it would be great if the Select component can take a prop like optionSelectedClassName so that we don’t have to manually see which item is selected and add a className to that.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:1
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

6reactions
harsimarriar96commented, Oct 2, 2018

This will work.

const CustomStyle = {
  option: (base, state) => ({
    ...base,
    backgroundColor: state.isSelected ? {Color1} : {Color2},
    ':active': {
      backgroundColor: state.isSelected ? {Color3} : {Color4}
    }
  })
}
<Select styles={customStyle} >
2reactions
jackblackCHcommented, Aug 7, 2019

This will work.

const CustomStyle = {
  option: (base, state) => ({
    ...base,
    backgroundColor: state.isSelected ? {Color1} : {Color2},
    ':active': {
      backgroundColor: state.isSelected ? {Color3} : {Color4}
    }
  })
}
<Select styles={customStyle} >

':active': { backgroundColor: 'hotpink' } This should be mentioned in the docs.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How can I highlight an option in a dropdown menu?
By highlight i mean either have it selected or change its background colour or something. HTML: <select id="siteId" name="siteId" onchange ...
Read more >
How to highlight currently selected item in dropdown menu
addClass('active'); // Store the index of the active li element localStorage.activeItem = $this.parent().index(); }; // Check if activeItem was ...
Read more >
How to highlight selected item in dropdown list - CodeProject
Solution 2 ; protected void ; object sender, EventArgs e){ // ; Find element by Text or value ListItem lstItem = DropDownList1.Items.FindByText( ...
Read more >
Solved: Blank Gallery - highlight selected item
Blank Gallery - highlight selected item ; Set the OnCheck property of the Checkbox as below. Set(SelectedRow, ThisItem.City)​ ; Set the Default property...
Read more >
DropDownList / How To / Highlight the matched character in ...
By using the highlightSearch method, you can highlight the matched character in DropDownList filtering. The following example demonstrates about how to ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found