以下是我的代码段。我遇到一个问题,当我单击IconComponent(droparrow-arrow)时,“选择”组件未打开。
<Select
IconComponent={() => (
<ExpandMore className="dropdown-arrow" />
)}
onChange={this.handleSelectUpdate.bind(this)}
value={this.state.somestate}
inputProps={{
name: "someprops1",
id: "someprops1"
}}
disabled={this.props.someprops1.length === 1}
className="dropdown"
>
>
{_.map(this.props.someprops2, (item, id) => {
return (
<MenuItem value={item.somekey} key={id}>
{item.somekey}
</MenuItem>
);
})}
</Select>
最佳答案
在 SelectInput.js
(Select
利用)中,图标呈现如下:
<IconComponent className={classes.icon} />
使用自定义图标的方式,您忽略了SelectInput
传递的className属性,该属性阻止了它的正确运行。以下是几个自定义图标的示例。第一种情况是直接使用图标(
IconComponent={ExpandMoreIcon}
),而没有将其包装在另一个函数中。第二种情况显示的内容与您要执行的操作更接近(支持在其上指定您自己的类)。尽管className
是SelectInput
当前尝试指定的唯一属性,但我认为最好通过所有 Prop :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的总体布局不再有意义。