Update SPFx webpart Properties

 Customize SPFx webpart Properties

            In this post we will see how to customize SPFx webpart properties & code to be updated.

Titles covered:
1. Files & methods used for webpart Property
2. Bind Webpart Property values in Code
3. Field types used in Property Pane
4. Adding new properties 'ListTitle' & 'ListUrl' in same group
5. Adding Validation to the Properties
6. index-internal.d.ts file uses
6. Adding Dropdown type Webpart property
7. Adding Slider type Webpart property:


1. Files & methods used for webpart Property(ex. "Description" property)

For example refer the below webpart property "Description"


src/WebPart.ts 
    1. References added from -> import from '@microsoft/sp-property-pane'
    2. Under interface(IWebpartWebPartProps) declare the property "Description"
    3. Implement the property in getPropertyPaneConfiguration() method



After this webpart property heading, group name, label text are declared in below files.
(Note: we can also directly hardcode the names in Webpart.ts file)
src/WebPart.ts (getPropertyPaneConfiguration() method  - define and implement properties)
src/loc/mystrings.d.ts (IWebPartStrings Interface - declare property Heading, group name etc... )
src/loc/en-us.ts (define([] array - adding string values for the property Heading, group name etc...)
(Note: we can also directly hard code the label values without using above 2 files)



2. Bind Webpart Property in Code

    In Render() method we can add this code to get the webpart property & it will bind values dynamically.
                    ${escape(this.properties.description)}



Now we will add more properties to the webpart

3. Field types used in Property Pane:

#Property fieldSPFx Typed Object
1LabelPropertyPaneLabel
2TextboxPropertyPaneTextField
3Multiple lines of textPropertyPaneTextField
4LinkPropertyPaneLink
5DropdownPropertyPaneDropdown
6CheckboxPropertyPaneCheckbox
7ChoicePropertyPaneChoiceGroup
8TogglePropertyPaneToggle
9SliderPropertyPaneSlider
10ButtonPropertyPaneButton
11Horizontal RulePropertyPaneHorizontalRule
12CustomOur own implementation using combination of above

4. Adding new properties 'ListTitle' & 'ListUrl' in same group:

    As we mentioned above we will add this properies to
src/WebPart.ts (getPropertyPaneConfiguration() method  - Define and implement properties)
src/loc/mystrings.d.ts (IWebPartStrings Interface - Declare property Heading, group name etc... )
src/loc/en-us.ts (define([] array - Adding string values for the property Heading, group name etc...)
(Note: we can also directly hard code the label values without using above 2 files)




5. Adding Validation to the Properties:

    For example if list URL is empty then we need to show some some error. We can use this ''onGetErrorMessage" property for text validation.

index-internal.d.ts file uses:
    This file is used to find each Webpart Property options(eg. inputs & methods) using 'index-internal.d.ts' file. To open this file easily --> Add 'IPropertyPaneTextFieldProps' under src/webpart.ts & press F12.


We have added validation for List URL should not be empty.
And the code is "onGetErrorMessage"


6. Adding Dropdown type Webpart property:

Take reference from 'index-internal.d.ts' to find default options for dropdown:
 "IPropertyPaneDropdownProps"
"IPropertyPaneDropdownOption" 

And the Actual code to add Dropdown Property:


1.Add 'PropertyPaneDropdown' under import & add Property name under interface (ex. ListName)

2. Add dropdown property & options code under "getPropertyPaneConfiguration()"


7. Adding Slider type Webpart property:

Take reference from 'index-internal.d.ts' to find default options for Slider:

Code to add Slider Property


1. Add 'PropertyPaneSlider' under import webpart
2. Add porperty name to get slider under interface(Example: PercentageCompleted)
3. Add slider code under "getPropertyPaneConfiguration()"






Refer this video for more details






Comments

Popular posts from this blog

Add content type to SharePoint List/Library using REST API

Upload Single/Multiple file by using the REST API and jQuery SharePoint 2013

A type named 'SP.Data. could not be resolved by the model error