Learn the fundamentals of React, including simple and class components, state, props, and submitting form data.
Last active
December 28, 2018 16:42
-
-
Save h4091/0d2c03b0be32574967cd4900290e141c to your computer and use it in GitHub Desktop.
Test31
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
<div id="root"></div> |
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
class Chosen extends React.Component { | |
componentDidMount() { | |
this.$el = $(this.el); | |
this.$el.chosen(); | |
this.handleChange = this.handleChange.bind(this); | |
this.$el.on('change', this.handleChange); | |
} | |
componentWillUnmount() { | |
this.$el.off('change', this.handleChange); | |
this.$el.chosen('destroy'); | |
} | |
handleChange(e) { | |
this.props.onChange(e.target.value); | |
} | |
render() { | |
return ( | |
<div> | |
<select className="Chosen-select" ref={el => this.el = el}> | |
{this.props.children} | |
</select> | |
</div> | |
); | |
} | |
} | |
function Example() { | |
return ( | |
<Chosen onChange={value => console.log(value)}> | |
<option>vanilla</option> | |
<option>chocolate</option> | |
<option>strawberry</option> | |
</Chosen> | |
); | |
} | |
ReactDOM.render( | |
<Example />, | |
document.getElementById('root') | |
); |
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
<script src="https://unpkg.com/react/umd/react.development.js"></script> | |
<script src="https://unpkg.com/react-dom/umd/react-dom.development.js"></script> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/chosen/1.6.2/chosen.jquery.min.js"></script> |
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
.Chosen-select { | |
width: 150px; | |
} |
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
<link href="https://cdnjs.cloudflare.com/ajax/libs/chosen/1.6.2/chosen.min.css" rel="stylesheet" /> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment