【发布时间】:2018-09-28 23:53:52
【问题描述】:
就在最近,我参与了一个新项目,该项目要求我将手放在 JS、React 和 Material-UI 上,如果我的问题不够精确,请提前道歉。
我想替换一个用 jquery 完成的模块,该模块正在处理一个从页脚打开的“抽屉”呈现到抽屉中的特定反应组件。
我想用一个反应组件替换这个元素,并考虑为此使用material-ui,http://www.material-ui.com/#/components/drawer,所以我想将抽屉修改为从底部而不是从左侧出来。 更专业的人可以建议您如何解决这个问题吗?
谢谢!
P.S.:我已经知道我可以在网上冲浪并找到具有类似行为的组件,这个任务更多是一种熟悉 JS 和反应的方式。
P.P.S.:目前该项目依赖于material-ui v0.20.0,我已经看到eskers建议的v1.+ beta版本中有一个示例
编辑
在花了一些时间之后,我在我的代码下方创建了自己的组件,以防有人需要。
define(function (require) {
var React = require('react');
var DrawerButton = require('./DrawerButton');
require('./TabbedDrawer.less');
class TabbedDrawer extends React.Component {
constructor(props) {
super(props);
this.state = {
drawerOpened: false,
buttonSelected: null,
drawerHeight: 0
}
this.openDrawer = this.openDrawer.bind(this);
this.renderMyLabels = this.renderMyLabels.bind(this);
}
renderMyLabels() {
var renderedLabels = this.props.labels.map(function(label, _key) {
return (
<DrawerButton
functionDrawer={this.openDrawer}
labelKey={_key}
buttonSelected={this.state.buttonSelected}
drawerOpened={this.state.drawerOpened}>
{label}
</DrawerButton>
);
}, this);
return renderedLabels;
}
openDrawer(buttonClicked) {
if(this.state.drawerOpened && (buttonClicked == this.state.buttonSelected)) {
this.setState({buttonSelected: null,
drawerOpened: !this.state.drawerOpened});
} else if(this.state.drawerOpened && (buttonClicked != this.state.buttonSelected)) {
this.setState({buttonSelected: buttonClicked});
} else {
this.setState({buttonSelected: buttonClicked,
drawerOpened: !this.state.drawerOpened});
}
}
render() {
const ElementToRender = (this.state.buttonSelected !== null) ? this.props.children[this.state.buttonSelected] : null;
const myElement = (ElementToRender !== null) ? <ElementToRender /> : "";
const drawerStyle = this.state.drawerOpened ? {display: 'block'} : {display: 'none'};
const tabStyle = this.state.drawerOpened ? {bottom: '250px'} : {bottom: '0px'};
return (
<div id="geppettoDrawer">
<span id="tabber" style={tabStyle}>
{this.renderMyLabels()}
</span>
<div id="drawer" style={drawerStyle}>
<div className="topLine" >
</div>
{myElement}
</div>
</div>
);
}
}
return TabbedDrawer;
});
@import "./../../../../style/less/components/colors";
@-webkit-keyframes fadeIn {
from {
opacity: 0;
}
to {
opacity: 1;
}
}
@keyframes fadeIn {
from {
opacity: 0;
}
to {
opacity: 1;
}
}
#geppettoDrawer {
display: inline-block;
}
#tabber {
-webkit-animation: fadeIn 1s;
animation: fadeIn 1s;
border-bottom: 0.5px solid rgb(252, 99, 32);
width: 100%;
display: block;
margin-top: 0px;
text-decoration: none;
left: 0;
bottom: 0;
position: absolute;
background: transparent;
}
#tabButton {
color: @primary_color;
margin: 0 auto;
margin-bottom: 26px;
position: relative;
border-color: rgb(252, 99, 32);
border: 0.5px;
border-bottom: 0px;
border-style: solid;
box-shadow: none;
text-shadow: none;
width: 140px;
height: 35px;
padding: 7px 20px;
text-align: center;
display: inline-block;
cursor: pointer!important;
background: rgba(50, 50, 53, 0.8);
}
#drawer {
-webkit-animation: fadeIn 1s;
animation: fadeIn 1s;
height: 250px;
width: 100%;
background-color: black;
color: white;
position: fixed;
bottom: 0px;
left: 0px;
display: none;
transition: opacity 1s ease-out;
-webkit-animation: fadeIn 1s;
animation: fadeIn 1s;
background: rgba(50, 50, 53, 0.8);
}
#drawer.topLine {
border-top: solid rgb(243, 250, 247); /* 1px for the "border" size */
cursor: n-resize;
resize: horizontal;
}
define(function (require) {
var React = require('react');
require('./TabbedDrawer.less');
class DrawerButton extends React.Component {
constructor(props) {
super(props);
this.state = {
mouseOver: false,
buttonActived: false}
this.activeButton = this.activeButton.bind(this);
this.overButton = this.overButton.bind(this);
this.outButton = this.outButton.bind(this);
}
activeButton() {
if((this.props.labelKey === this.props.buttonSelected) && this.props.drawerOpened)
this.setState({buttonActived: true,
mouseOver: false});
else
this.setState({buttonActived: false,
mouseOver: false});
this.props.functionDrawer(this.props.labelKey);
}
overButton() {
if((this.state.buttonActived === false))
this.setState({mouseOver: true});
}
outButton() {
if(!this.props.drawerOpened)
this.setState({buttonActived: false});
if((this.state.buttonActived === false))
this.setState({mouseOver: false});
}
render() {
if(this.props.labelKey === this.props.buttonSelected) {
var buttonStyle = {background: 'rgba(252, 99, 32, 0.8)', color: 'rgba(50, 50, 53)'};
} else {
var buttonStyle = (this.state.mouseOver) ? {background: 'rgba(252, 99, 32, 0.8)',
color: 'rgba(50, 50, 53)'} :
{background: 'rgba(50, 50, 53, 0.8)',
color: 'rgba(252, 99, 32)'};
}
return (
<span
id="tabButton"
onClick={this.activeButton}
onMouseOver={this.overButton}
onMouseOut={this.outButton}
style={buttonStyle}>
{this.props.children}
</span>
);
}
}
return DrawerButton;
})
【问题讨论】:
标签: javascript reactjs material-ui