, , . , , (, , ), ( , ). , , . , , .
- , Dropdown, " ", . ( fiddle).
var EVENTNAME = "dropdown-close";
document.addEventListener('click', (e)=>
{
window.dispatchEvent(new CustomEvent(EVENTNAME, {
detail: e.srcElement
}));
});
var DropDown = React.createClass({
getInitialState: function()
{
return {open: false};
},
render()
{
let menu = null;
if (this.state.open) {
menu = this.props.children;
}
return <div className="dropdown">
<a className="dropdown-toggle" ref="toggle" onClick={this.toggleMenu}>Dropdown</a>
{menu}
</div>
},
toggleMenu(e)
{
this.setState({open: !this.state.open});
},
closeMenu(e)
{
if (e.detail !== this.refs.toggle)
{
this.setState({open: false});
}
},
componentWillMount()
{
var that = this;
window.addEventListener(EVENTNAME, this.closeMenu);
},
componentWillUnmount()
{
var that = this;
window.removeEventListener(EVENTNAME, this.closeMenu);
}
});
ReactDOM.render(
<div>
<h1>First</h1>
<DropDown>
<li>Item 1 (in my case these are also React comps)</li>
<li>Item 2</li>
<li>Item 3</li>
</DropDown>
<hr/>
<h1>Second</h1>
<DropDown>
<li>Item 1 (in my case these are also React comps)</li>
<li>Item 2</li>
<li>Item 3</li>
</DropDown>
</div>,
document.getElementById('container')
);
, . . , , , , .