Applying styles to multiple Selects #5384
-
Hi, this is probably a very easy fix but I've been searching for hours but I'm afraid I lack the knowledge to search for the right thing. I'm pretty new to React. I reduced the code to include only relevant info. The part that isn't working is the "state.menuIsOpen ? 0 : 1" however the isSelected inside of option does correctly change the background color of both select's option. My main question is how do I pass the current Select "object" to selectStyles so it knows which Select object I'm currently referring to? I think props are usually how this is handled in React, but I'm not sure. I tried passing in useState variables that changed when each Select menu was open/closed, but that caused both of them to open at the same time. const ProfileCharts = () => {
const selectStyles = {
menu: (provided, state) => ({
...provided,
opacity: state.menuIsOpen ? 0 : 1,
transition: "all 120ms ease-in-out",
}),
option: (provided, state) => ({
...provided,
backgroundColor: state.isSelected ? "hsl(193, 81%, 58%)" : "white",
"&:hover": {
backgroundColor: "hsl(193, 81%, 58%)",
transition: "all 120ms ease-out",
},
}),
};
return(
<>
<Select
className="game-mode-select"
options={gameModeOptions}
onChange={(newValue) => handleGameModeSelect(newValue.value)}
placeholder="Filter by game mode"
styles={selectStyles}
/>
<Select
className="song-select"
options={songOptions}
onChange={(newValue) => handleSongSelect(newValue.value)}
placeholder="Filter by song"
styles={selectStyles}
/>
</>
)
}
export default ProfileCharts; |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
Hi @markoleptic, Code Sandbox. See last 2 selects Code:
|
Beta Was this translation helpful? Give feedback.
Hi @markoleptic,
If I am understanding correctly, you want to color hover background for both select differently but want to keep common style object.
You can append identifiable string and make conditional styling accordingly.
Code Sandbox. See last 2 selects
Code: