简体   繁体   English

如何更改下拉选项的颜色?

[英]how to change color of dropdown option?

Any idea with this strange behaviour?对这种奇怪的行为有什么想法吗? When you open dropdown and select for example 'Option 3' then click 'OPTION' button(it sets value to be 'Option 1'), then open dropdown and you can see that 'Option 3' is red but it shouldnt be red, now Option 1 should be red because its selected.当您打开下拉菜单和 select 时,例如“选项 3”,然后单击“选项”按钮(它将值设置为“选项 1”),然后打开下拉菜单,您可以看到“选项 3”是红色的,但它不应该是红色的,现在选项 1 应该是红色的,因为它被选中了。 Any idea?任何的想法?

在此处输入图像描述

try the code here: https://codesandbox.io/s/material-ui-select-forked-5n1lpb?file=/src/material.js试试这里的代码: https://codesandbox.io/s/material-ui-select-forked-5n1lpb?file=/src/material.js

code:代码:

 import React, { useState } from "react"; import { MenuItem, makeStyles, Select, Button, FormControl, Box } from "@material-ui/core"; const useStyles = makeStyles((theme) => ({ select: { backgroundColor: "black", color: "white" }, menuItem: { "&.Mui-selected": { backgroundColor: "red", color: "white" } } })); export default function SimpleSelect() { const classes = useStyles(); const [value, setValue] = useState(""); const handleChange = (event) => { setValue(event.target.value); }; const handleButtonClick = () => { setValue("Option 1"); }; const data = [ { value: "Option 1" }, { value: "Option 2" }, { value: "Option 3" } ]; return ( <div> <Box> <FormControl style={{ display: "flex", flexDirection: "row" }}> <Select // className={classes.select} MenuProps={{ classes: { paper: classes.menuItem }, anchorOrigin: { vertical: "bottom", horizontal: "left" }, transformOrigin: { vertical: "top", horizontal: "left" }, getContentAnchorEl: null }} renderValue={() => <div>{value? value: "Polygon"}</div>} onChange={handleChange} > <MenuItem value="" style={{ marginTop: "1px" }}> <em>(No Selection)</em> </MenuItem> {data.map((option) => { return ( <MenuItem className={classes.select} value={option.value}> {option.value} </MenuItem> ); })} </Select> <Button onClick={handleButtonClick}>Option</Button> </FormControl> </Box> </div> ); }

value prop is missing for Select Select缺少value道具

<Select
  // ...
  value={value}
  // ...  
>
  {/* ... */}
</Select>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM