[英]How to implement dropdown menu in React JS using Material UI?
我有一個下拉菜單,它有一個嵌套菜單,如下所示。
展開時的下拉列表顯示兩個選項:
問題是在單擊租戶名稱或經銷商 ID 下的子選項中的任何項目時會顯示一秒鍾並消失。 如何解決這個問題,接下來是如何存儲用戶選擇的值?
我的代碼如下:
export default class DropDownMenuSimpleExample extends React.Component {
constructor(props) {
super(props);
this.state = {
dropDownData: [
{
value: '',
tenantName: '',
dealerId: '',
},
],
};
}
handleChange = (event, index, value) => {
this.setState({ value });
console.log(event, index, value);
}
render() {
return (
<DropDownMenu
style={styles.customWidth}
anchorOrigin={{ horizontal: 'left', vertical: 'top' }}
targetOrigin={{ horizontal: 'left', vertical: 'top' }}
className={{ backgroundcolor: '#CFD8DC' }}
>
<MenuItem
value={this.state.value}
onChange={this.handleChange}
primaryText="TENANT NAME"
rightIcon={<ArrowDropRight />}
menuItems={[
<MenuItem value={100} primaryText="CA-CAR" />,
<Divider />,
<MenuItem value={101} primaryText="TEKION" />,
]}
/>
<MenuItem
value={this.state.value}
onChange={this.handleChange}
primaryText="DEALER ID"
rightIcon={<ArrowDropRight />}
menuItems={[
<MenuItem value={1} primaryText="1" />,
<MenuItem value={2} primaryText="2" />,
<MenuItem value={3} primaryText="2" />,
<MenuItem value={4} primaryText="4" />,
]}
/>
</DropDownMenu>
);
}
}
要獲取用戶選擇的值,您需要綁定一個 onChange 事件,例如
<SelectField onChange={(evt,index,name) =>this.getOccasion(name) />
getOccasion()
{
this.setState({
//update to state
})
}
不知道為什么您的列表會出現一秒鍾。檢查控制台,看看它是否顯示任何錯誤。您仍然可以嘗試將您的材料 ui 組件包裝在
<MuiThemeProvider>
但是認為您錯過了 webpack 中的一些加載程序,這可能就是這種行為不尋常的原因
這是我的 webpack 配置,看看它是否有幫助
const postcssPlugins = [
require('postcss-cssnext')(),
require('postcss-modules-values')
];
const scssLoader = [
{ loader: 'style-loader' },
{ loader: 'css-loader' },
{ loader: 'sass-loader' }
];
const postcssLoader = [
{ loader: 'style-loader' },
{ loader: 'css-loader', options: { modules: true } },
{ loader: 'postcss-loader', options: { plugins: () => [...postcssPlugins] } }
];
var path = require('path');
module.exports = {
entry: './src/main/resources/static/js/app.js',
output: {
path: __dirname + '/src/main/resources/static/js',
filename: 'bundle.js'
},
module: {
loaders: [
{
test: /\.jsx?$/,
exclude: /node_modules/,
loader: 'babel-loader',
query: {
presets: ['es2015', 'react']
}
},
{
test: /\.(scss|sass)$/,
loader: scssLoader,
include: [__dirname]
},
{ test: /\.css$/,
loader: postcssLoader,
include: [__dirname]
}
]
}
};
Dropdown.js這是一個 Dropdown 組件類,它幫助我們渲染下拉菜單內容。 當用戶單擊下拉菜單時,該組件類在瀏覽器中呈現更新的下拉菜單列表。
showDropdownMenu :此方法有助於顯示下拉菜單內容。 hideDropdownMenu :此方法有助於隱藏下拉菜單內容。
import React from 'react';
import './style.css';
class Dropdown extends React.Component {
constructor(){
super();
this.state = {
displayMenu: false,
};
this.showDropdownMenu = this.showDropdownMenu.bind(this);
this.hideDropdownMenu = this.hideDropdownMenu.bind(this);
};
showDropdownMenu(event) {
event.preventDefault();
this.setState({ displayMenu: true }, () => {
document.addEventListener('click', this.hideDropdownMenu);
});
}
hideDropdownMenu() {
this.setState({ displayMenu: false }, () => {
document.removeEventListener('click', this.hideDropdownMenu);
});
}
render() {
return (
<div className="dropdown" style = {{background:"red",width:"200px"}} >
<div className="button" onClick={this.showDropdownMenu}> My Setting </div>
{ this.state.displayMenu ? (
<ul>
<li><a className="active" href="#Create Page">Create Page</a></li>
<li><a href="#Manage Pages">Manage Pages</a></li>
<li><a href="#Create Ads">Create Ads</a></li>
<li><a href="#Manage Ads">Manage Ads</a></li>
<li><a href="#Activity Logs">Activity Logs</a></li>
<li><a href="#Setting">Setting</a></li>
<li><a href="#Log Out">Log Out</a></li>
</ul>
):
(
null
)
}
</div>
);
}
}
export default Dropdown;
style.css 這是下拉菜單的樣式表設計。
.dropdown {
position: relative;
display: inline-block;
}
ul {
list-style-type: none;
margin: 0;
padding: 0;
top:45px;
right:0px;
width: 200px;
background-color: white;
font-weight:bold;
position: absolute;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
z-index: 1;
}
li a {
color: #000;
text-decoration: none;
}
li {
padding: 8px 16px;
border-bottom: 1px solid #e5e5e5;
}
li:last-child {
border-bottom: none;
}
li:hover {
background-color: #e5e5e5;
color: white;
}
.button{
width:178px;
height:18px;
background-color:#ff3232 ;
padding:12px;
border-radius:5px;
font-weight:bold;
color:white;
}
.button:before{
content:"";
position:absolute;
width:0px;
height:0px;
border: 10px solid;
border-color: white transparent transparent transparent;
right:6px;
top:18px;
}
index.js 這是一個幫助顯示下拉菜單的主要組件。
import React from 'react';
import ReactDOM from 'react-dom';
import registerServiceWorker from './registerServiceWorker';
import Dropdown from './dropdownmenu/Dropdown';
var displayDropdown = (
<div style={{display: 'flex', justifyContent: 'center'}} >
<Dropdown />
</div>
);
ReactDOM.render(displayDropdown, document.getElementById('root'));
registerServiceWorker();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.