Last active
August 29, 2015 14:19
-
-
Save gustavoguichard/7e2bac7340c9494fc735 to your computer and use it in GitHub Desktop.
BS Dropdown in React
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React from 'react'; | |
import $ from 'jquery'; | |
require('bootstrap-webpack'); | |
var Dropdown = React.createClass({ | |
componentDidMount() { | |
$(this.getDOMNode).on('shown.bs.dropdown', function(event){ | |
this.props.onChange(true); | |
}.bind(this)); | |
$(this.getDOMNode).on('hidden.bs.dropdown', function(event){ | |
this.props.onChange(false); | |
}.bind(this)); | |
}, | |
getClasses() { | |
var classes = ['dropdown']; | |
if(this.props.isShowing) classes.push('open'); | |
return classes.join(' '); | |
}, | |
render () { | |
return ( | |
<div className={this.getClasses()}> | |
<button ref="dropbutton" className="btn btn-default" data-toggle="dropdown" aria-haspopup="true" aria-expanded={this.props.isShowing}> | |
{this.props.label} | |
<span className="caret"></span> | |
</button> | |
<ul ref="dropmenu" className="dropdown-menu" role="menu" aria-labelledby="dLabel"> | |
{this.props.children} | |
</ul> | |
</div> | |
); | |
} | |
}); | |
var DropdownItem = React.createClass({ | |
render () { | |
return ( | |
<li> | |
<a onClick={this.props.onClick}>{this.props.children}</a> | |
</li> | |
); | |
} | |
}); | |
var App = React.createClass({ | |
getInitialState() { | |
return { | |
dropOpen: false | |
}; | |
}, | |
handleChange(isOpen) { | |
this.setState({ dropOpen: isOpen }); | |
}, | |
render () { | |
return ( | |
<div className="container"> | |
<pre>{JSON.stringify(this.state, null, 2)}</pre> | |
<h1>Dropdown</h1> | |
<ul className="nav nav-pills"> | |
<li> | |
<Dropdown onChange={this.handleChange} isShowing={this.state.dropOpen} label="Dropdown Trigger"> | |
<DropdownItem onClick={() => alert('clicked')}>one</DropdownItem> | |
<DropdownItem>two</DropdownItem> | |
<DropdownItem>three</DropdownItem> | |
</Dropdown> | |
</li> | |
</ul> | |
</div> | |
); | |
} | |
}); | |
React.render(<App/>, document.getElementById('app')); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment