React Rating Component In Spfx

Introduction

 
The react-rating is a plugin written in React.js to initialize the React rating component in React forms.
 
Open a command prompt. Create a directory for the SPFx solution. 
 
md spfx-ReactRating
 
Navigate to the above-created directory.
 
cd spfx-ReactRating
 
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 - ReactRating
 
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-rating  
 Necessary imports,
  1. import Rating from 'react-rating';  
 To initialize Raing component, insert the below tag in render method,
  1. <Rating />  
 Since we are using font awesome icons for rating we need to import font awesome css from the cdn path of fontawesome n/w.
  1. SPComponentLoader.loadCss('https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css');  
Full Code
 
in ReactRating.tsx
  1. import * as React from 'react';  
  2. import { IReactRatingProps } from './IReactRatingProps';  
  3. import './mystyle.css';  
  4. import { SPComponentLoader } from '@microsoft/sp-loader';    
  5. SPComponentLoader.loadCss('https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css');  
  6. import Rating from 'react-rating';  
  7. export default class ReactRating extends React.Component<IReactRatingProps, {}> {  
  8.   public render(): React.ReactElement<IReactRatingProps> {  
  9.     return (  
  10.       <div >  
  11.         <Rating />  
  12.           
  13. <br></br>  
  14. <Rating  
  15.   emptySymbol={<span className="icon-text">-</span>}  
  16.   fullSymbol={[1,2,3,4,5].map(n => <span className="icon-text">{n}</span>)}  
  17. />  
  18. <br></br>  
  19. <Rating  
  20.   emptySymbol="fa fa-star-o fa-2x"  
  21.   fullSymbol="fa fa-star fa-2x"  
  22. />  
  23. <br></br>  
  24. <Rating  
  25.   emptySymbol="fa fa-thumbs-down fa-2x"  
  26.   fullSymbol="fa fa-thumbs-up fa-2x"  
  27. />  
  28. <Rating  
  29.   emptySymbol="glyphicon glyphicon-heart-empty"  
  30.   fullSymbol="glyphicon glyphicon-heart"  
  31. />  
  32. <br></br>  
  33. <Rating  
  34.   emptySymbol="fa fa-star-o fa-2x"  
  35.   fullSymbol="fa fa-star fa-2x"  
  36.   fractions={2}  
  37. />  
  38. <br></br>  
  39. <Rating  
  40.   stop={6}  
  41.   emptySymbol={['fa fa-star-o fa-2x low''fa fa-star-o fa-2x low',  
  42.     'fa fa-star-o fa-2x medium''fa fa-star-o fa-2x medium',  
  43.     'fa fa-star-o fa-2x high''fa fa-star-o fa-2x high']}  
  44.   fullSymbol={['fa fa-star fa-2x low''fa fa-star fa-2x low',  
  45.     'fa fa-star fa-2x medium''fa fa-star fa-2x medium',  
  46.     'fa fa-star fa-2x high''fa fa-star fa-2x high']}  
  47. />  
  48. <br></br>  
  49. <Rating  
  50.   stop={4}  
  51.   emptySymbol="fa fa-battery-empty fa-2x"  
  52.   fullSymbol={['fa fa-battery-1 fa-2x''fa fa-battery-2 fa-2x',  
  53.     'fa fa-battery-3 fa-2x''fa fa-battery-4 fa-2x']}  
  54. />  
  55.       </div>  
  56.     );  
  57.   }  
  58. }  
 in mystyle.css
  1. .icon-text {  
  2.     display: inline-block;  
  3.     width: 26px;  
  4.     font-size: 2em;  
  5.     background-color: white;  
  6.   }  
  7.   .low {  
  8.     color: red;  
  9.   }  
  10.   .medium {  
  11.     color: orange;  
  12.   }  
  13.   .high {  
  14.     color: green;  
  15.   }  
  16.   .icon {  
  17.     width: 26px;  
  18.     height: 26px;  
  19.   }  
mystyle.css is a custom based styling css. Here I am using it for coloring the icons. 
 

Properties

 
start - Range start value
stop - Range stop value 
fractions -split the single rating point into more pieces
initialrating - the default rating value
placeholderrating- just a placeholder 
 readonly- not able to edit
direction- whether rtl or ltr
quiet- whether to animate or not
emptySymbol- React element, inline style object, or classes applied to the rating symbols when empty 
fullSymbol- React element, inline style object, or classes applied to the rating symbols when full
 

Event Types

 
onChange - sets the value to the state when the rating is changed
  1. onChange={(rate) => alert(rate)}  
onClick- sets the value to the state when the rating is clicked
  1. onClick={(rate) => alert(rate)}  
onHover - sets the value to the state when the rating is clicked 
  1. onHover={(rate) => alert(rate)}  
 Some sample rating outputs:
 
 React Rating Component In Spfx
 
For more details visit Here. 
 

Conclusion

 
Hence we learned about initializing React Raing component in Spfx forms using react-Rating plugin. I hope this helps someone. Happy coding :)