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,
Necessary imports,
- import Rating from 'react-rating';
To initialize Raing component, insert the below tag in render method,
Since we are using font awesome icons for rating we need to import font awesome css from the cdn path of fontawesome n/w.
- SPComponentLoader.loadCss('https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css');
Full Code
in ReactRating.tsx
- import * as React from 'react';
- import { IReactRatingProps } from './IReactRatingProps';
- import './mystyle.css';
- import { SPComponentLoader } from '@microsoft/sp-loader';
- SPComponentLoader.loadCss('https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css');
- import Rating from 'react-rating';
- export default class ReactRating extends React.Component<IReactRatingProps, {}> {
- public render(): React.ReactElement<IReactRatingProps> {
- return (
- <div >
- <Rating />
-
- <br></br>
- <Rating
- emptySymbol={<span className="icon-text">-</span>}
- fullSymbol={[1,2,3,4,5].map(n => <span className="icon-text">{n}</span>)}
- />
- <br></br>
- <Rating
- emptySymbol="fa fa-star-o fa-2x"
- fullSymbol="fa fa-star fa-2x"
- />
- <br></br>
- <Rating
- emptySymbol="fa fa-thumbs-down fa-2x"
- fullSymbol="fa fa-thumbs-up fa-2x"
- />
- <Rating
- emptySymbol="glyphicon glyphicon-heart-empty"
- fullSymbol="glyphicon glyphicon-heart"
- />
- <br></br>
- <Rating
- emptySymbol="fa fa-star-o fa-2x"
- fullSymbol="fa fa-star fa-2x"
- fractions={2}
- />
- <br></br>
- <Rating
- stop={6}
- emptySymbol={['fa fa-star-o fa-2x low', 'fa fa-star-o fa-2x low',
- 'fa fa-star-o fa-2x medium', 'fa fa-star-o fa-2x medium',
- 'fa fa-star-o fa-2x high', 'fa fa-star-o fa-2x high']}
- fullSymbol={['fa fa-star fa-2x low', 'fa fa-star fa-2x low',
- 'fa fa-star fa-2x medium', 'fa fa-star fa-2x medium',
- 'fa fa-star fa-2x high', 'fa fa-star fa-2x high']}
- />
- <br></br>
- <Rating
- stop={4}
- emptySymbol="fa fa-battery-empty fa-2x"
- fullSymbol={['fa fa-battery-1 fa-2x', 'fa fa-battery-2 fa-2x',
- 'fa fa-battery-3 fa-2x', 'fa fa-battery-4 fa-2x']}
- />
- </div>
- );
- }
- }
in mystyle.css
- .icon-text {
- display: inline-block;
- width: 26px;
- font-size: 2em;
- background-color: white;
- }
- .low {
- color: red;
- }
- .medium {
- color: orange;
- }
- .high {
- color: green;
- }
- .icon {
- width: 26px;
- height: 26px;
- }
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
- onChange={(rate) => alert(rate)}
onClick- sets the value to the state when the rating is clicked
- onClick={(rate) => alert(rate)}
onHover - sets the value to the state when the rating is clicked
- onHover={(rate) => alert(rate)}
Some sample rating outputs:
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 :)