![](/img/trans.png)
[英]Not able to get name property of select dropdown in Material UI React
[英]Select is not working onClick IconComponent(dropdown-arrow) in react material ui
下面是我的代碼片段。 我遇到了一個問題,當我點擊 IconComponent( dropdown-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
當前嘗試指定的唯一屬性, SelectInput
我認為您最好通過所有道具:
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 的整體布局不再有意義。
如果您使用的是材質 UI 圖標,則只需在圖標組件中添加以下 3 行 css:
position: absolute !important;
right: 0 !important;
pointer-events: none !important;
您需要做的就是將 className 指針事件設置為 none 和 position - absolute 用於圖標,position - relative 用於選擇。
這是一個例子:
icon: {
pointerEvents: 'none',
position: 'absolute',
right: '10px'
},
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.