在SelectInput.js
(Select
利用)中,图标呈现如下:
<IconComponent className={classes.icon} />
通过自定义图标的方式,您忽略了SelectInput
传递的 className 属性,这会阻止它正常运行。
以下是自定义图标的几个示例。第一种情况直接使用图标 ( IconComponent={ExpandMoreIcon}
) 而不将其包装在另一个函数中。第二种情况显示了更接近您尝试做的事情(支持在其上指定您自己的类)。虽然className
是SelectInput
当前尝试指定的唯一属性,但我认为您最好通过所有props:
const iconStyles = {
selectIcon: {
color: "green"
}
};
const CustomExpandMore = withStyles(iconStyles)(
({ className, classes, ...rest }) => {
return (
<ExpandMoreIcon
{...rest}
className={classNames(className, classes.selectIcon)}
/>
);
}
);
这是完整的示例:
import React from "react";
import PropTypes from "prop-types";
import { withStyles } from "@material-ui/core/styles";
import InputLabel from "@material-ui/core/InputLabel";
import MenuItem from "@material-ui/core/MenuItem";
import FormControl from "@material-ui/core/FormControl";
import Select from "@material-ui/core/Select";
import ExpandMoreIcon from "@material-ui/icons/ExpandMore";
import clsx from "clsx";
const styles = (theme) => ({
root: {
display: "flex",
flexWrap: "wrap"
},
formControl: {
margin: theme.spacing.unit,
minWidth: 120
},
selectEmpty: {
marginTop: theme.spacing.unit * 2
}
});
const iconStyles = {
selectIcon: {
color: "green"
}
};
const CustomExpandMore = withStyles(iconStyles)(
({ className, classes, ...rest }) => {
return (
<ExpandMoreIcon
{...rest}
className={clsx(className, classes.selectIcon)}
/>
);
}
);
class SimpleSelect extends React.Component {
state = {
age: "",
name: "hai"
};
handleChange = (event) => {
this.setState({ [event.target.name]: event.target.value });
};
render() {
const { classes } = this.props;
return (
<form className={classes.root} autoComplete="off">
<FormControl className={classes.formControl}>
<InputLabel htmlFor="age-simple">Age</InputLabel>
<Select
value={this.state.age}
onChange={this.handleChange}
inputProps={{
name: "age",
id: "age-simple"
}}
IconComponent={ExpandMoreIcon}
>
<MenuItem value="">
<em>None</em>
</MenuItem>
<MenuItem value={10}>Ten</MenuItem>
<MenuItem value={20}>Twenty</MenuItem>
<MenuItem value={30}>Thirty</MenuItem>
</Select>
</FormControl>
<FormControl className={classes.formControl}>
<InputLabel htmlFor="age-simple">Age</InputLabel>
<Select
value={this.state.age}
onChange={this.handleChange}
inputProps={{
name: "age",
id: "age-simple"
}}
IconComponent={CustomExpandMore}
>
<MenuItem value="">
<em>None</em>
</MenuItem>
<MenuItem value={10}>Ten</MenuItem>
<MenuItem value={20}>Twenty</MenuItem>
<MenuItem value={30}>Thirty</MenuItem>
</Select>
</FormControl>
</form>
);
}
}
SimpleSelect.propTypes = {
classes: PropTypes.object.isRequired
};
export default withStyles(styles)(SimpleSelect);
这是 Material-UI 添加到图标NativeSelect.js
的样式(可以在其中导出样式以便Select.js
可以重用它们):
icon: {
// We use a position absolute over a flexbox in order to forward the pointer events
// to the input and to support wrapping tags..
position: 'absolute',
right: 0,
top: 'calc(50% - 12px)', // Center vertically
pointerEvents: 'none', // Don't block pointer events on the select under the icon.
color: theme.palette.action.active,
'&$disabled': {
color: theme.palette.action.disabled,
},
},
/* Styles applied to the icon component if the popup is open. */
iconOpen: {
transform: 'rotate(180deg)',
},
/* Styles applied to the icon component if `variant="filled"`. */
iconFilled: {
right: 7,
},
/* Styles applied to the icon component if `variant="outlined"`. */
iconOutlined: {
right: 7,
},
绝对定位将图标保持在 的可点击区域内Select
。如果没有绝对定位,图标将导致组成 Select 的不同元素发生移动。单击图标应该继续工作的位置,但元素移动的方式导致 Select 的整体布局不再有意义。