Show/Hide Property Pane Component Based On Another Component's Value In SPFX

Introduction

 
With SPFx webpart property pane configuration, many times we need to show/hide property pane control based on another property pane control's value.
 

Scenario 

 
For example, we have a radio button for Text and Image so on the selection of text we will show text input control and on the selection of the image, we will show image URL control.
 
Output Image
 
Let's see the step-by-step implementation.
 

Implementation

  • Open a command prompt
  • Move to the path where you want to create a project
  • Create a project directory using:
  1. md SPFx-show-hide-component  
Move to the above-created directory using:
  1. cd SPFx-show-hide-component  
Now execute the below command to create an SPFx solution:
  1. yo @microsoft/sharepoint    
It will ask some questions, as shown below:
 
SPFx Setup
 
After a successful installation, we can open a project in any source code tool. Here, I am using the VS code, so I will execute the command:
  1. code .  
Move to the src> webparts > webpart > components > I{webpartname}props.ts file and we will declare the properties as shown below:
  1. export interface ISpfxShowHideComponentProps {  
  2.   description: string;  
  3.   textOrImageType: string;  
  4.   simpleText: string;  
  5.   imageUrl: string;  
  6. }  
Now move to the src > webparts > webpart > {webaprtname}webpart.ts
 
Here, we need TextField and ChoiceGroup to add property pane controls. The import statement will be like this:
  1. import {  
  2.   IPropertyPaneConfiguration,  
  3.   PropertyPaneChoiceGroup,  
  4.   PropertyPaneTextField  
  5. } from '@microsoft/sp-property-pane';  
Now add webpart properties in I{webpartname}WebpartProps as shown below:      
  1. export interface ISpfxShowHideComponentWebPartProps {  
  2.   description: string;  
  3.   textOrImageType: string;  
  4.   simpleText: string;  
  5.   imageUrl: string;  
  6. }  
Move to the {webpartname}Webpart.manifest.json to set the default value in properties as shown below:
  1. "properties": {  
  2.        "description""spfx-show-hide-component",  
  3.        "simpleText""SPFx show hide component based on another conponent's value",  
  4.        "imageUrl""https://png.pngtree.com/png-clipart/20200401/original/pngtree-modern-flat-design-concept-of-programmers-at-work-concept-software-development-png-image_5332892.jpg"  
  5.      }  
Now go to the render() method and set other properties as the description declaration:
  1. public render(): void {  
  2.    const element: React.ReactElement<ISpfxShowHideComponentProps> = React.createElement(  
  3.      SpfxShowHideComponent,  
  4.      {  
  5.        description: this.properties.description,  
  6.        textOrImageType: this.properties.textOrImageType,  
  7.        simpleText: this.properties.simpleText,  
  8.        imageUrl: this.properties.imageUrl,  
  9.      }  
  10.    );  
  11.   
  12.    ReactDom.render(element, this.domElement);  
  13.  }  
Now go to the getPropertyPaneConfiguration() method and create a property pane choice control to select image/text as shown below:
  1. PropertyPaneChoiceGroup('textOrImageType', {  
  2.     label: 'Image/Text',  
  3.     options: [  
  4.         {  
  5.             key: 'Text',  
  6.             text: 'Text',  
  7.             checked: true  
  8.         },  
  9.         {  
  10.             key: 'Image',  
  11.             text: 'Image',  
  12.         }  
  13.     ]  
  14. })  
Here we will take two variables to declare propertypane configuration.
 
We have a property called textOrImageType as a choice group so we will check the conditions. If the selected value of textOrImageType is equal to text then we will create a control for text, otherwise, it will be an image so in the else condition, we will show image URL control.
  1. let textControl: any = [];  
  2. let imageSourceControl: any = [];  
  3.   
  4. if (this.properties.textOrImageType === "Text") {  
  5.   textControl = PropertyPaneTextField('simpleText', {  
  6.     label: "Text",  
  7.     placeholder: "Enter Text"  
  8.   });  
  9. }  
  10. else {  
  11.   imageSourceControl = PropertyPaneTextField('imageUrl', {  
  12.     label: "Image URL",  
  13.     placeholder: "Enter Image URL"  
  14.   });  
  15. }  
Now move to the {webpartname}.tsx file to render data on the page. Here, we will show text if the text is selected otherwise we will show the image if the image will be selected.
  1. import * as React from 'react';  
  2. import styles from './SpfxShowHideComponent.module.scss';  
  3. import { ISpfxShowHideComponentProps } from './ISpfxShowHideComponentProps';  
  4. import { escape } from '@microsoft/sp-lodash-subset';  
  5.   
  6. export default class SpfxShowHideComponent extends React.Component<ISpfxShowHideComponentProps, {}> {  
  7.   public render(): React.ReactElement<ISpfxShowHideComponentProps> {  
  8.     return (  
  9.       <div className={styles.spfxShowHideComponent}>  
  10.         {this.props.textOrImageType == "Text" ? this.props.simpleText :  
  11.           (<img src={this.props.imageUrl} height="250px" width="250px" alt="No Image" />)  
  12.         }  
  13.       </div>  
  14.     );  
  15.   }  
  16. }  
Output
 
Output
 
Find the complete source code here.
 

Summary

 
In this article, we have seen how to show/hide property pane control based on another property pane control's value.
 
I hope this helps.
 
Sharing is caring!!