Create Rich Text control w/ image and video in SPFx.
Introduction
Rich Text Control is one of the most-used controls in classic SharePoint Lists but if we need to create a customization for Modern SharePoint List form using SharePoint Framework there is no easy way to do so. But in this article, we will understand how we can use react-quill control to save the rich text field and also display it for the existing items.
react-quill is based on Quill which is trusted by Microsoft. To know more about the react-quill control please check the below link.
So let us understand how we can use react-quill in our Custom SharePoint List Form.
Step 1 - Create a List
Before creating a Custom form we would create a SharePoint list for saving the data.
List Name:- Custom RichText
Columns:- Title(Single Line of Text)
Description(Multi-Line of Text in Plain Text format)
Note
We are using Plain Text but we will save the HTML in it because the images in React quill will be converted in base64 and will get truncated in Rich Text format.
Step 2 - Create SPFx Solution
Considering we have the environment setup completed we can use the below command to create new SPFx solution. We will use React framework for creating the Webpart
|
|
Use the below values when asked while creating the solution.
Step 3 - Add the reference for react-quill
To add a reference to React quill in our project we need to use the below command which would install the required dependencies in our solution.
|
|
Saving the data we would use PnPJs so let us install the required dependencies for PnPJs.
|
|
Step 4 - Update the WebPart Code
Now that we have all the dependencies added in our solution we can update the code.
Let us set up the sp object so that we can use it to save the list item and retrieve it.
Open the file name named RichTextFieldWebPart.ts.
Import sp using the below code.
|
|
And add the below function to setup sp object.
|
|
Open the file named RichTextField.tsx to update the code for the rich text field.
Let us add the required reference for React quill using the below code.
import ReactQuill from 'react-quill'; import 'react-quill/dist/quill.snow.css';
Let us now create and add required interfaces.
|
|
Now for using react quill we can use custom toolbar so we would add all the required toolbar using the below constant:
|
|
Now in the render method of the component, we will have to add the below code which contains one text field and one React quill text editor:
|
|
For saving the data and all the on change functions we need to add the below code:
|
|
We are also fetching the item with the query string value of itemid to display the value of the existing saved item.
So once all of the code is updated we can test our Webpart which would save the data and also display the saved item. We can save images videos and all text formatting which are available.
Outcome
Conclusion
For rich text field in SPFx react quill can be one of the best controls which can be used for saving the rich text field.
This will even help us save images and videos in the rich text field in SharePoint List.