React OTP Input In Spfx


The react-otp-input is a plugin written in React.js to initialize the React otp input component in React forms.
Open a command prompt. Create a directory for the SPFx solution.
md spfx-Otpinput
Navigate to the above-created directory.
cd spfx-Otpinput
Run the Yeoman SharePoint Generator to create the solution.
yo @microsoft/sharepoint
Solution Name
Hit Enter to have the default name (spfx-Otpinput in 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 - ReactOtpInput
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 install --save react-otp-input  
 Necessary imports:
  1. import OtpInput from 'react-otp-input';  
 To initialize otp input component, insert the below tag in render method:
  1. <OtpInput  
  2.           onChange={this.handleChange}  
  3.           numInputs={6}  
  4.           separator={<span>-</span>}  
  5.         />  
Styling the component
  1. inputStyle={{  
  2.       width: '3rem',  
  3.       height: '3rem',  
  4.       margin: '20px 1rem',  
  5.       fontSize: '2rem',  
  6.       borderRadius: 4,  
  7.       border: '2px solid rgba(0,0,0,0.3)',  
  8.     }}  
As per your own css, you can change the look of the component.
Full code
In ReactOtpInput.tsx
  1. import * as React from 'react';  
  2. import { IReactOtpInputProps } from './IReactOtpInputProps';  
  3. import OtpInput from 'react-otp-input';  
  4. export interface IOtopState {        
  5.   otp?:string;      
  6. }   
  7. /*     
  8.     this.state = { 
  9.       otp: '', 
  10.       numInputs: 4, 
  11.       separator: '-', 
  12.       isDisabled: false, 
  13.       hasErrored: false, 
  14.       isInputNum: false, 
  15.     }; */  
  16. export default class ReactOtpInput extends React.Component<IReactOtpInputProps,IOtopState> {  
  17.   constructor(props) {    
  18.     super(props);    
  19.     this.state = {  
  20.       otp: '',  
  21.     };  this.handleChange = this.handleChange.bind(this);   
  22.     this.clearotp = this.clearotp.bind(this);   
  24.   }   
  25.   public handleChange = otp => {  
  26.     this.setState({ otp });  
  27.     console.log(otp);  
  28.   }  
  29.   public clearotp = () => {  
  30.     this.setState({ otp:'' });  
  32.   }  
  33.   public render(): React.ReactElement<IReactOtpInputProps> {  
  34.     return (  
  35.       <div >  
  36.       <OtpInput  
  37.       inputStyle={{  
  38.         width: '3rem',  
  39.         height: '3rem',  
  40.         margin: '20px 1rem',  
  41.         fontSize: '2rem',  
  42.         borderRadius: 4,  
  43.         border: '2px solid rgba(0,0,0,0.3)',  
  44.       }}  
  45.           onChange={this.handleChange}  
  46.           numInputs={6}  
  47.           separator={<span>-</span>}  
  48.           shouldAutoFocus  
  49.           value={this.state.otp}  
  50.           isInputNum={true}  
  51.         />  
  52.           <button  
  53.                 className="btn margin-top--large"  
  54.                 disabled={this.state.otp.toString().length < 6}  
  55.               >  
  56.                 Verify  OTP  
  57.               </button>  
  58.               <button  
  59.                 className="btn margin-top--large"  
  60.                 onClick={this.clearotp}  
  61.               >  
  62.                 clear otp  
  63.               </button>  
  64.       </div>  
  65.     );  
  66.   }  
  67. }  
  • numInputs - It specifies the number of input boxes.
  • onChange -This event caputres the state value when input will change.
  • isDisabled- it disabled all the inputs.
  • shouldAutoFocus -autofocus the input when the page load.
  • isInputNum - it restricts input to only numbers.
  • separator- it specifies the textboxes break.
  • value-its the final output value of the value entered.
Sample output
React OTP Input In Spfx


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

Recommended Ebook

Getting Started with SharePoint Framework Development using TypeScript, PnP JS, and React JS

Download Now!
Similar Articles