React Dropdown In SPFX✔️

Introduction

 
The React-Select-Plus is a plugin written in React.js to initialize the React Dropdown component in React forms.
 
Open a command prompt. Create a directory for the SPFx solution.
 
md spfx-ReactDropDown
 
Navigate to the above-created directory.
 
cd spfx-ReactDropDown
 
Run the Yeoman SharePoint Generator to create the solution.
 
yo @microsoft/sharepoint
 
Solution Name
 
Hit Enter to have the default name (spfx-ReactDropDown this case) or type in any other name for your solution.
Selected choice - Hit Enter
 
Target for the component
 
Here, we can select the target environment where we are planning to deploy the client web part; i.e., SharePoint Online or SharePoint OnPremise (SharePoint 2016 onwards).
Selected choice - SharePoint Online only (latest).
 
Place of files
 
We may choose to use the same folder or create a subfolder for our solution.
Selected choice - same folder.
 
Deployment option
 
Selecting Y will allow the app to be deployed instantly to all sites and be accessible everywhere.
Selected choice - N (install on each site explicitly).
 
Permissions to access web APIs
 
Choose if the components in the solution require permission to access web APIs that are unique and not shared with other components in the tenant.
Selected choice - N (solution contains unique permissions).
 
Type of client-side component to create
 
We can choose to create a client-side web part or an extension. Choose the web part option.
Selected choice - WebPart
 
Web part name
 
Hit Enter to select the default name or type in any other name.
Selected choice - ReactSelectPlus
 
Web part description
 
Hit Enter to select the default description or type in any other value.
 
Framework to use
 
Select any JavaScript framework to develop the component. Available choices are - No JavaScript Framework, React, and Knockout.
 
Selected choice - React
 
The Yeoman generator will perform a scaffolding process to generate the solution. The scaffolding process will take a significant amount of time.
 
Once the scaffolding process is completed, lock down the version of project dependencies by running the below command,
 
npm shrinkwrap
 
In the command prompt, type the below command to open the solution in the code editor of your choice.
 
NPM Packages used, 
  1. npm i react-select-plus  
Necessary imports,
  1. import Select from 'react-select-plus';  
  2. import 'react-select-plus/dist/react-select-plus.css';  
Full Code
  1. import * as React from 'react';  
  2. import { IReactSelectPlusProps } from './IReactSelectPlusProps';  
  3. import Select from 'react-select-plus';  
  4. import 'react-select-plus/dist/react-select-plus.css';  
  5. export interface ISelectState {        
  6.   selectedOption?:string;      
  7. }   
  8. export default class ReactSelectPlus extends React.Component<IReactSelectPlusProps, ISelectState> {  
  9.   constructor(props) {    
  10.     super(props);    
  11.     this.state = {  
  12.       selectedOption: ''  
  13.     };    
  14.   this.handleChange=this.handleChange.bind(this);  
  15.   }   
  16.  private  handleChange = (selectedOption) => {  
  17.     this.setState({ selectedOption });  
  18.     console.log(`Selected: ${selectedOption.label}`);  
  19.   }  
  20.   public render(): React.ReactElement<IReactSelectPlusProps> {  
  21.     const { selectedOption } = this.state;  
  22.     const value = selectedOption ;  
  23.     return (  
  24.       <div >  
  25.        <Select  
  26.         name="form-field-name"  
  27.         multi  //for Multi select
  28.         autofocus  
  29.         value={value}  
  30.         onChange={this.handleChange}  
  31.         options={ops}  
  32.       />  
  33.       </div>  
  34.     );  
  35.   }  
  36. }  
Important Props
 
onChange - change function event
 
onClose - close button clicking event
 
multi - Select more than one options
 
value - it refers the deafult value
 
autofocus - intial focus
 
searchable - whether search option is needed
 
clearable - whether clear icon is needed 
 
For more properties visit here.
 
Sample  object array format for  dropdown
  1. var ops =[  
  2.   { value: 'one', label: 'One' },  
  3.   { value: 'two', label: 'Two' },  
  4.   { value: 'three', label: 'three' },  
  5.   { value: 'four', label: 'four' },  
  6.   { value: 'five', label: 'five' },  
  7.   { value: 'six', label: 'six' },  
  8.   { value: 'seven', label: 'seven' },  
  9.   { value: 'eight', label: 'eight' , clearableValue: false},  
  10. ];  
Expected result (Here I neglect the attribute multi):
 
React Dropdown In SPFX
 
Sample object array format for grouping dropdown option value:
  1. var ops = [{  
  2.     label: 'Black',  
  3.     value: 'black',  
  4. }, {  
  5.     label: 'Primary Colors',  
  6.     options: [{  
  7.         label: 'Yellow',  
  8.         value: 'yellow'  
  9.     }, {  
  10.         label: 'Red',  
  11.         value: 'red'  
  12.     }, {  
  13.         label: 'Blue',  
  14.         value: 'blue'  
  15.     }]  
  16. }, {  
  17.     label: 'Secondary Colors',  
  18.     options: [{  
  19.         label: 'Orange',  
  20.         value: 'orange'  
  21.     }, {  
  22.         label: 'Purple',  
  23.         options: [{  
  24.             label: 'Light Purple',  
  25.             value: 'light_purple'  
  26.         }, {  
  27.             label: 'Medium Purple',  
  28.             value: 'medium_purple'  
  29.         }, {  
  30.             label: 'Dark Purple',  
  31.             value: 'dark_purple'  
  32.         }]  
  33.     }, {  
  34.         label: 'Green',  
  35.         value: 'green'  
  36.     }]  
  37. }, {  
  38.     label: 'White',  
  39.     value: 'white',  
  40. }];  
Expected result (here I am using multi attribute):
 
React Dropdown In SPFX
 
Output for using multitag without grouping:
 
React Dropdown In SPFX
 

Conclusion

 
We learned about initializing React Dropdown component in Spfx forms using react-select-plus plugin. I hope this helps someone. Happy coding :)