Select2 is very popular for create very user friendly dropdown with support for searching, tagging, remote data sets, infinite scrolling, and many other highly used options.
;
Using the same in your react page-
Install Select2
From CDN
<link href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.10/css/select2.min.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.10/js/select2.min.js"></script>
From NPM
npm install select2
After installation we just need to put the selector with the html element and beautiful dropdown will created.
Simple Js Example:
<select class="js-example-basic-single" name="state">
<option value="AL">Alabama</option>
...
<option value="WY">Wyoming</option>
</select>
$(document).ready(function() {
$('.js-example-basic-single').select2();
});
With React and Typescript
But when we are using js framework like reactjs it does work in every time. So I have created a gist to create Select2 in reactJs when we are using react with tyescript.
We need to install all type definition of Select2 for typescript.
npm install --save @types/select2
Here is the gist to show Select2 in react page using hooks-
;
Using the same in your react page-
<Select2Wrapper value={this.state.country_code} onChange={this.handleSelectChange}
className={'form-control'} id={'country_code'} name={'country_code'}
data={{
placeholder: "Select Options",
data: this.state.countrycodes
}}
/>
You can put the all Select2 Options according to docs inside data props. It can
handle all the functionality and options.
Comments
Post a Comment