SPFx - PropertyPane Custom Controls - page 3

Going back to the helloworld project, let's import the numberPicker control.
We will use the relative path to the module and I choose the numberPicker parent's folder to scaffold a fresh helloworld template.
  • In the HelloWorlWebPart.ts import the module
import {NumberPicker} from '../../../../numberPicker/numberPicker';

  • Let's define a property called picker 

export interface IHelloWorldWebPartProps {
  description: string;
  picker:number;
}

  • On the render function let's print the picker property. Add this line to the domElement's innerHtml
<p class="${ styles.description }">Picker:${this.properties.picker||1}</p>

  • Finally, create a new instance of the numberPicker on the propertyPaneConfiguration
protected getPropertyPaneConfiguration(): IPropertyPaneConfiguration {
    return {
      pages: [
        {
          header: {
            description: strings.PropertyPaneDescription
          },
          groups: [
            {
              groupName: strings.BasicGroupName,
              groupFields: [
                PropertyPaneTextField('description', {
                  label: strings.DescriptionFieldLabel
                }),
                new NumberPicker("picker", {
                  applyOnClick: false,
                  deferredValidationTime: 0,
                  label: "Picker",
                  description: "This is the number picker control",
                  max:100,
                  min:1,
                  properties: this.properties
                })
              ]
            }
          ]
        }
      ]
    };
  }

Now, just run gulp serve and test the control on the property pane.

I don't go further with technical details of this control. I made this approach because I think is easy to understand. Even if you are not an expert on interface implementations, you can use this structure to define any kind of controls. Just take a special attention to the NumberPickerFieldProp interface. This is the public interface that you will use on the property pane configuration and where you define the parameters of the control. Then, just implement the onRender flow, you have access to the control's parameters on the this.config object. You should also give special attention to the changeCallback function that is used to perform the property changes. This will trigger a new webpart render with the new value of the property.

I hope this helps you to build your own reusable control. Just make sure you build a structure similar to the one's used in this tutorial, define your control's properties, behaviour and styles. That should be simple!


GitHub Project

Example of two number pickers

Comments

Popular posts from this blog

Property Pane dynamic fields

Handling theme changes on a MS Teams Tab WebPart

Sharing Dynamic Data between WebParts