How to Detect Text Change Event in SPFx Property Pane Text Field


SharePoint Framework applications make the development of the web part much easier, we can do everything in the client-side. For every web part, properties are very important to do the configuration. It can be anything like user-specific configuration or data specific configuration or UI specific configuration.
Here we are going to see how to detect the onTextChange event in PropertyPaneTextField control and perform fill in other fields based on the value in the textfield.
Take some scenario to understand this better. Let’s have a text box in the Property field to fill in the “Site URL” and a cascading dropdown field which will list the Library names in the site. Cascading dropdowns are the one whose value depends on another field.
Let’s take a look at the step by step implementation for this.
Step 1
Create a new SharePoint Framework application, please take a look at my previous article to get started with SPFx web parts
Step 2
Go to the Webpart.ts file which you can find in the below location:
Step 3
Add the property variables in the webpart Property interface like below to hold the selected property value:
  1. export interface IWebPartNameWebPartProps {  
  2.     description: string;  
  3.     listname: string;  
  4.     SiteUrl: string;  
  5. }  
Step 4
We are going to add two property here, a text field and dropdown field. So, we need to import the required packages at the top of your file like below:
  1. import {  
  2.     IPropertyPaneConfiguration,  
  3.     PropertyPaneTextField,  
  4.     PropertyPaneDropdown,  
  5.     IPropertyPaneDropdownOption  
  6. } from '@microsoft/sp-property-pane';  
Step 5
Declare a public variable in the webpart class to store the dropdown options like below:
  1. private dropdownOptions: IPropertyPaneDropdownOption[] = [];    
Step 6
Look for the method called getPropertyPaneConfiguration where you can see default property called Description, if you want you can remove it.
Step 7
Add the text property pane field next to the description like below:
  1. PropertyPaneTextField('SiteUrl', {  
  2.     label: ‘Enter the Site URL’,  
  3.     validateOnFocusOut: true,  
  4.     onGetErrorMessage: this.bindLibraryNames.bind(this)  
  5. }),  
We will define the bindLibraryNames method later in this article, let’s see remain attributes:
label – Label to display for this text box
validateOnFocusOut - This will tell the control to call validation on the textbox when the cursor leaves the control, which in turn triggering the OnBlur event.
onGerErrorMessage – this will be triggered when the cursor leaves the control it also helps to set the validation message and to perform any action based on the entered value. The latest value in the textbox will be passed as input to this method.
Step 8
Add the dropdown property pane field next to the text box field like below:
  1. PropertyPaneDropdown('libraryname', {  
  2.     label: 'Select Library',  
  3.     options: this.dropdownOptions  
  4. }),  
options – source for the list of dropdown values
Step 9 
Now time to define the bindLibraryNames method. This method takes one input parameter, site URL value, which will be passed by onGetErrorMessage method of the textbox.
This method will return a string, if the returned string is not empty then field validation fails. If the returned string is empty then field validation passes.
In this method we will set or reset the value for the dropdownOptions variable we declare in Step 5.
After filling the values in dropdownOptions variable refresh the spfx property pane using below piece of code:
  1. this.context.propertyPane.refresh();   
Here is the full implementation of the method:
  1. public bindLibraryNames1(value: string): string {  
  2.     if (value && value.toLowerCase().includes("")) {  
  3.         this.context.spHttpClient.get(value + "/_api/web/lists?$filter=BaseTemplate eq 101 and Title ne 'Site Assets' and Title ne 'Style Library'", SPHttpClient.configurations.v1).then((response) => {  
  4.             response.json().then((result) => {  
  5.                 this.dropdownOptions = [];  
  6.                 result.forEach((element, key) => {  
  7.                     this.dropdownOptions.push({  
  8.                         key: key,  
  9.                         text: element.Title  
  10.                     });  
  11.                 });  
  12.                 this.context.propertyPane.refresh();  
  13.             }).catch(() => {  
  14.                 this.dropdownOptions = [];  
  15.                 this.context.propertyPane.refresh();  
  16.             });  
  17.             return "";  
  18.         });  
  19.     } else {  
  20.         this.dropdownOptions = [];  
  21.         this.context.propertyPane.refresh();  
  22.         return "Please enter valid site url"  
  23.     }  
  24. }   
We are done with the implementation now run the webpart using workbench. Enter the site URL, and the library dropdown will be filled with values. If the URL entered is invalid, the text box will show a validation message.


I hope this article helps you to understand how to trigger the text change event in the SPFx property pane text field. If you have any questions/issues about this article, please let me know in the comments.