Skip to main content

How to set Apply button and make the component full width in SharePoint SPFx

Introduction: SharePoint SPFx framework is filled with very rich UI and flexibility to apply dynamic content and responsive User Interface the only thing is we need to enable it, there is lots of option which we can manually configure one of the examples of Apply button.
It is a good practice and an example of rich UI by adding an Apply button in the SPFx property pane. this button has a predefined mechanism that can post the changes made at the property pane asynchronously, so that, you do not need to refresh the page to see your changes.  It is as simple as you are. you only need to add some lines of code in your component.

The below image shows the real-time example of an Apply button with the property pane screen.

To achieve it, only need to follow some steps, in this blog I am going to tell you, everything from the beginning.

Step-1: 
First, of all need to create and the SPFx solution by running the below commands,
using PowerShell or Visual Studio Code

yo @microsoft/sharepoint


I have created the solution with the name dynamic-property.
WebPart with DynamicControl, you can create whatever you like...


after a couple of minutes, the solution was created, and it looks like below

after the solution is created, need to run the build command
Step-2
gulp build

Step-3
Now, need to open the DynamicControlWebPart.ts file and need to add a few lines of code. to achieve this first need to open your solution in the below manner in Visual Studio Code.



You need to open the DynamicControlWebPart.ts file rest of all remain the same, once you open the file you need to copy and paste the below code, for example, I did it with my solution, I have a file with the name of DynamicControlWebPart.ts in this I pasted the below text.

 protected get disableReactivePropertyChanges(): boolean {
    return true;
  }

  protected onAfterPropertyPaneChangesApplied(): void {
    ReactDom.unmountComponentAtNode(this.domElement);
    this.render();
  }

This is complete code, how it will look like

import * as React from 'react';
import * as ReactDom from 'react-dom';
import { Version } from '@microsoft/sp-core-library';
import {
  IPropertyPaneConfiguration,
  PropertyPaneTextField, IPropertyPaneGroup,
PropertyPaneCheckbox, PropertyPaneDropdown,
IPropertyPaneDropdownOption, PropertyPaneChoiceGroup, PropertyPaneButton
} from '@microsoft/sp-property-pane';
import { BaseClientSideWebPart } from '@microsoft/sp-webpart-base';

import * as strings from 'DyanmicControlWebPartStrings';
import DyanmicControl from './components/DyanmicControl';
import { IDyanmicControlProps } from './components/IDyanmicControlProps';


export interface IDyanmicControlWebPartProps {
  description: string;
  Field1: string;
}

export default class DyanmicControlWebPart extends
BaseClientSideWebPart<IDyanmicControlWebPartProps> {

  public render(): void {
    const element: React.ReactElement<IDyanmicControlProps> = React.createElement(
      DyanmicControl,
      {
        description: this.properties.description,
        Field1:this.properties.Field1,
      }
    );

    ReactDom.render(element, this.domElement);
  }

  protected onDispose(): void {
    ReactDom.unmountComponentAtNode(this.domElement);
  }

  protected get dataVersion(): Version {
    return Version.parse('1.0');
  }
  protected get disableReactivePropertyChanges(): boolean {
    return true;
  }

  protected onAfterPropertyPaneChangesApplied(): void {
    ReactDom.unmountComponentAtNode(this.domElement);
    this.render();
  }

  private Call = (e) => {
      alert("Button 1, Clicked");
  }
  protected getPropertyPaneConfiguration(): IPropertyPaneConfiguration {
    const lists : IPropertyPaneDropdownOption[] = [{ key: 1, text: "BMW"},{key: 2, text:"Fiat"},{key: 3, text:"Hundai"},{key: 4, text:"Ford"}];
    const conditionalGroupFields1: IPropertyPaneGroup["groupFields"] = [
      PropertyPaneCheckbox("Field1", {
        text: "Show Box 1",
      }),
     ]

     if(this.properties.Field1) {
      conditionalGroupFields1.push(      
            PropertyPaneTextField('imageLink1', {
              label: "Set an image URL"
            }),
            PropertyPaneTextField('heading1', {
              label: "Set a heading"
            }),  
            PropertyPaneTextField('description1', {
              label: "Set a description"
            }),
            PropertyPaneTextField('redirectURL1', {
              label: "Set a redirection URL"
            }),
            PropertyPaneDropdown('listName', {
              label: "My Drop Down List",
              options: lists,              
            }),
            PropertyPaneChoiceGroup('listName2', {
              label: "My Choice List",
              options: lists,              
            }),
            PropertyPaneButton('btn',{
              text:"Button 1",
              onClick: this.Call
            })      
       );
    }

    return {
      pages: [
        {
          header: {
            description: strings.PropertyPaneDescription
          },
          groups: [                     
            {
               groupFields: conditionalGroupFields1,
            },            
          ]
        }
      ]
    };
  }
}


Once you paste it, need to build your solution, there are chances of showing some problem warnings in visual studio code after doing that changes, you should ignore them it may be due to some version related warnings. 

you only need to do publish and deploy your package or run it on the localhost or you can deploy it on the Dev environment.

How to avail the SPFx web part in full width


to make the SPFx WebPart in the full width, we need to do very small changes to the configuration settings.

Step -1:
Open the MainWebPart.menifest.json, in my solution name, is DynamicControlWebPart.menifest.json is there.


Step-2:
add a new property inside the manifest file just below the supportedHosts line
"supportsFullBleed":true,

and the full code looks like this
{
  "$schema": "https://developer.microsoft.com/json-schemas/spfx/client-side-web-part-manifest.schema.json",
  "id": "2e98d005-8494-4a34-aa53-bd4aad4a8a7b",
  "alias": "DyanmicControlWebPart",
  "componentType": "WebPart",
  "version": "*",
  "manifestVersion": 2,
  "requiresCustomScript": false,
  "supportedHosts": ["SharePointWebPart"],
  "supportsFullBleed":true,
  "preconfiguredEntries": [{
    "groupId": "5c03119e-3074-46fd-976b-c60198311f70", // Other
    "group": { "default": "Other" },
    "title": { "default": "DyanmicControl" },
    "description": { "default": "DyanmicControl description" },
    "officeFabricIconFontName": "page",
    "properties": {
      "description": "DyanmicControl"
    }
  }]
}



Step-3: all is done, only need to run the gulp build command, after that the solution is ready to deploy or run on the local bench.

Step-4: After the deployment, all the components that have Full-Width property enabled will appear in this section.
Only need to select it, and will appear based on the resolution, there is no limit of higher resolution with the full-width WebPart.

Conclusion: after the understanding and implementation of POC we found that Microsoft uses lots of hidden properties with SharePoint Modern application, which makes the framework optimized and lighter, call based on requirement is the perfect example of great architectural


Hope it will help, kindly write your comments if this blog makes things easy, so that, I will improve my upcoming blogs and make them more meaning full !!!!
Thank you :)



Comments

  1. You are giving such interesting information about Full Stack. It is great and beneficial info for us, I really enjoyed reading it. Thankful to you for sharing an article like this.

    ReplyDelete

Post a Comment

Popular posts from this blog

Interview Questions of SPFx SharePoint

What is SPFx? The SharePoint Framework (SPFx) is a web part model that provides full support for client-side SharePoint development, it is easy to integrate with SharePoint data, and extend Microsoft Teams. With the SharePoint Framework, you can use modern web technologies and tools in your preferred development environment to build productive experiences and apps that are responsive and mobile-ready. Scenario Based asking Scenario 1: Scenario: Your team is developing a SharePoint Framework (SPFx) web part that needs to retrieve data from an external API and display it on a SharePoint site. The API requires authentication using OAuth 2.0. The web part should also allow users to refresh the data manually. Question 1: How would you approach implementing this functionality in an SPFx web

Interview Question of Advanced SharePoint SPFx

1. What is WebPack? A module bundling system built on top of Node. js framework is known as a WebPack. It is capable to handle the combination and minification of JavaScript and CSS files, also other files such as images by using plugins. WebPack is the recommended way of bundling the files in JS framework and .Net frameworks. In the SPFx it is used with React Js. 2. What is PowerShell.? PowerShell is a platform introduced by Microsoft to perform cross-platform task automation and configuration management framework, it is made up of a command-line shell, a scripting language. it can be run on Windows, Linux, and macOS. 3. What is bundling and Minification? The terms bundling and minification are the processes of code compressing, which is used to improve the load and request time, It is used in modern JavaScript frameworks and .Net frameworks. It improves the load time by reducing the number of requests to the s

Top20 - SharePoint Framework (SPFx) Interview Questions

1.  Which tool we can use to generate a SharePoint Framework (SPFx) solution? Developers can use  Yeoman to generate SharePoint Framework (SPFx) solution, it is a client-side scaffolding open-source tool to help in web-based development. 2. How developer can ensure that the solution deployed was immediately available to all site collections in SPFx?  To ensure the availability of the deployed solution on all the site collections developer has to configure  skipFeatureDeployment: true  in package-solution.json {     "solution" : {       "name" : "theultimateresources-deployment-client-side-solution" ,       "id" : "as3feca4-4j89-47f1-a0e2-78de8890e5hy" ,       "version" : "2.0.0.0" ,       "skipFeatureDeployment" : true     },     "paths" : {       "zippedPackage" : "solution/theultimateresources-deploy-true.sppkg"     }  }