PnP SPFx Property Controls

repository·master·Indexed 42 days ago

https://github.com/pnp/sp-dev-fx-property-controls

A collection of reusable property pane controls for SharePoint Framework (SPFx) solutions, version 3.24.0. It provides specialized input components to enhance the property pane experience, including controls such as PropertyFieldBrandFontPicker, PropertyFieldButton, PropertyFieldButtonWithCallout, and PropertyFieldCheckboxWithCallout.

Tokens
79.9K
Snippets
182
Records
374
Agent score
53%

What's inside PnP SPFx Property Controls

  1. Get started with sp-dev-fx-property-controls

    master

    The sp-dev-fx-property-controls package provides property controls specifically designed for SharePoint Framework (SPFx) solutions.

    Note: If you are looking for content controls (used within the web part UI) rather than property controls (used in the property pane), you should use the sp-dev-fx-controls-react repository instead.

    For detailed installation guides and API usage, refer to the official documentation: https://pnp.github.io/sp-dev-fx-property-controls/

  2. Get detailed color data with valueAsObject

    master

    By default, PropertyFieldColorPicker returns a CSS-compatible string (e.g., Hex, RGBA, or named colors).

    To receive a structured IColor object instead, set the valueAsObject property to true. This is useful for advanced scenarios requiring specific color components.

    The IColor object contains the following properties:

    PropertyTypeDescription
    strstringCSS-compatible string (same as default behavior)
    hexstringHex value (does not reflect alpha)
    rnumberRed
    gnumberGreen
    bnumberBlue
    hnumberHue
    snumberSaturation
    vnumberValue
  3. When to use PropertyFieldEnterpriseTermPicker vs PropertyFieldTermPicker

    master

    Choosing between the two term picker controls depends on your term store size and specific requirements:

    PropertyFieldEnterpriseTermPicker

    • Use Case: "Enterprise" scenarios with large term stores containing many groups, term sets, and terms.
    • Mechanism: Loads term sets and terms on demand (only when expanded in the tree view).
    • Dependencies: Uses @pnp/sp-taxonomy (from @pnp/pnpjs), which increases the bundle/package size.
    • Key Feature: Supports requesting term labels.

    PropertyFieldTermPicker

    • Use Case: Small to medium-sized term stores.
    • Mechanism: Loads all groups and term sets from the term service during the initial request.
    • Dependencies: Lightweight; does not require additional taxonomy libraries.
    • Limitation: Does not support on-demand loading or term labels.

    Recommendation: Use PropertyFieldTermPicker unless you specifically require on-demand loading for large datasets or need term labels.

  4. Get IColor object values from PropertyFieldSwatchColorPicker

    master

    By default, the control returns a CSS-compatible string. If you need more detailed color information (like RGB or HSL components), set the valueAsObject property to true. This will cause the control to return an IColor object instead of a string.

    The IColor object contains the following properties:

    PropertyTypeDescription
    strstringCSS-compatible string (same as default behavior).
    hexstringHex value (does not reflect alpha).
    rnumberRed component.
    gnumberGreen component.
    bnumberBlue component.
    hnumberHue.
    snumberSaturation.
    vnumberValue.
  5. Define the Palette data structure

    master

    Palettes must be provided as a Record<string, string[]>.

    • Key: The palette name or identifier (e.g., "Ocean", "Brand Colors").
    • Value: An array of CSS-compatible color strings (hex values).

    Example structure:

    const palettes: Record<string, string[]> = {
      "Ocean": ["#006994", "#40E0D0", "#0077BE", "#00CED1"],
      "Sunset": ["#FF6B6B", "#FFA07A", "#FFD700", "#FF4500"]
    };
    const palettes: Record<string, string[]> = {
      "Ocean": ["#006994", "#40E0D0", "#0077BE", "#00CED1"],
      "Sunset": ["#FF6B6B", "#FFA07A", "#FFD700", "#FF4500"],
      "Forest": ["#228B22", "#32CD32", "#90EE90", "#006400"]
    };
  6. Use PropertyFieldCollectionData to manage list/collection data

    master

    The PropertyFieldCollectionData control allows you to insert and manage a collection of data (an array of objects) within your SPFx web part property pane. It supports various data types such as strings, numbers, booleans, dropdowns, and custom renders. The resulting data is returned as a JSON array of objects where each object represents a row in the collection.

    [
      {"Title":"Person","Lastname":"1","Age":"42","City":"helsinki","Sign":true},
      {"Title":"Person","Lastname":"2","Age":"42","City":"helsinki","Sign":true}
    ]
  7. Use PropertyFieldContentTypePicker to select Content Types

    master

    The PropertyFieldContentTypePicker control provides a dropdown in the SharePoint Framework property pane that allows users to select a Content Type. It can automatically retrieve Content Types from either the current SharePoint site or a specific SharePoint list provided via a listId.

    import {
      PropertyFieldContentTypePicker,
      PropertyFieldContentTypeOrderBy
    } from '@pnp/spfx-property-controls/lib/PropertyFieldContentTypePicker';
    
    // In your web part's groupFields configuration:
    PropertyFieldContentTypePicker('contentType', {
      label: 'Select a Content Type',
      context: this.context,
      selectedContentType: this.properties.contentType,
      disabled: false,
      orderBy: PropertyFieldContentTypeOrderBy.Name,
      onPropertyChange: this.onPropertyPaneFieldChanged.bind(this),
      properties: this.properties,
      onGetErrorMessage: null,
      deferredValidationTime: 0,
      key: 'contentTypePickerFieldId'
    })
  8. Use the PropertyPaneMarkdownContent control

    master

    The PropertyPaneMarkdownContent control allows you to embed Markdown content directly into a SharePoint Framework (SPFx) property pane. This is useful for providing user guidance, documentation, or formatted text that is difficult to create with standard controls. It supports arbitrary HTML and allows you to override the rendering of specific HTML tags with custom React components or CSS classes.

    Setup Steps

    1. Ensure @pnp/spfx-property-controls is installed.
    2. Import the control:
    import { PropertyPaneMarkdownContent } from '@pnp/spfx-property-controls/lib/PropertyPaneMarkdownContent';
    1. Define your Markdown string.
    2. Add the control to your groupFields in the web part property pane configuration.

    Important: Indentation Warning

    Markdown parsers interpret four spaces of indentation as a code block. To avoid accidentally rendering your entire content as a code block, ensure your Markdown string is not indented within the template literal.

    // GOOD: No leading indentation on the content lines
    const md: string = `To get started select an items from the library.
        
    You can pick any items from the list.`;
    
    // BAD: Indented content will be treated as a code block
    const md: string = `
        To get started select an items from the library.
        
        You can pick any items from the list.`;
    import { PropertyPaneMarkdownContent } from '@pnp/spfx-property-controls/lib/PropertyPaneMarkdownContent';
    
    const md: string = `
    ### This is Markdown
    
    [Markdown](http://daringfireball.net/projects/markdown/) lets you write content in a really natural way.
    
      * You can have lists, like this one
      * Make things **bold** or *italic*
      * Embed snippets of \`code\`
      * Create [links](/)
      * ...
    
    Also supports GitHub-flavored Markdown checklists
    
    - [x] Checklist item 1
    - [x] Checklist item 2
    - [ ] Checklist item 3
    
    <small>Sample content borrowed with thanks to [markdown-to-jsx](https://probablyup.com/markdown-to-jsx/) ❤️</small>`;
    
    // In your property pane configuration:
    PropertyPaneMarkdownContent({
      markdown: md,
      key: 'markdownSample'
    })
  9. Use the PropertyFieldIconPicker control

    master

    The PropertyFieldIconPicker is a property pane control that allows users to search for and select an icon from the office-ui-fabric-react library.

    Key Behaviors:

    • The picker opens in a separate panel (or dialog) to maintain readability of the icon list.
    • It currently supports single icon selection only.
    • It does not display the selected icon outside of the picker panel itself.

    To use it, ensure you have installed @pnp/spfx-property-controls and import the module directly from the library path.

    import { PropertyFieldIconPicker } from '@pnp/spfx-property-controls/lib/PropertyFieldIconPicker';
    
    // Inside your property pane implementation:
    PropertyFieldIconPicker('iconPicker', {
      currentIcon: this.properties.iconPicker,
      key: "iconPickerId",
      onSave: (icon: string) => { 
        console.log(icon); 
        this.properties.iconPicker = icon; 
      },
      onChanged: (icon: string) => { 
        console.log(icon); 
      },
      buttonLabel: "Icon",
      renderOption: "panel",
      properties: this.properties,
      onPropertyChange: this.onPropertyPaneFieldChanged.bind(this),
      label: "Icon Picker"              
    })
  10. Use the PropertyFieldLabelWithCallout control

    master

    The PropertyFieldLabelWithCallout control displays a label in the SPFx property pane that, when triggered, opens a callout containing additional information or HTML content.

    To use it:

    1. Ensure @pnp/spfx-property-controls is installed.
    2. Import PropertyFieldLabelWithCallout and CalloutTriggers.
    3. Add the control to your groupFields configuration in the web part property pane.
    import { CalloutTriggers } from '@pnp/spfx-property-controls/lib/Callout';
    import { PropertyFieldLabelWithCallout } from '@pnp/spfx-property-controls/lib/PropertyFieldLabelWithCallout';
    
    // Inside your property pane configuration:
    PropertyFieldLabelWithCallout('fakeProp', {
      calloutTrigger: CalloutTriggers.Click,
      key: 'LabelWithCalloutFieldId',
      calloutContent: 'Use dropdowns below to select list and list\'s field to work with',
      calloutWidth: 200,
      text: 'Select List and Field'
    })
  11. Use the PropertyFieldSearch control

    master

    The PropertyFieldSearch control provides an input field for search functionality within a SharePoint Framework (SPFx) property pane.

    To use it:

    1. Ensure @pnp/spfx-property-controls is installed.
    2. Import PropertyFieldSearch from @pnp/spfx-property-controls/lib/PropertyFieldSearch.
    3. Define a property in your Web Part interface to store the search string.
    4. Add the control to your groupFields configuration in the Web Part class.
    import { PropertyFieldSearch } from '@pnp/spfx-property-controls/lib/PropertyFieldSearch';
    
    export interface IPropertyControlsTestWebPartProps {
      searchValue: string;
    }
    
    // Inside your Web Part class configuration:
    PropertyFieldSearch("searchValue", {
      key: "search",
      placeholder: 'Search libraries',
      value: this.properties.searchValue,
      onSearch: this._onSearch,
      styles: { root: { margin: 10 } }
    })
  12. Use the PropertyFieldViewPicker control

    master

    The PropertyFieldViewPicker control allows users to select a SharePoint list view from a dropdown in the SPFx property pane. It automatically retrieves available views for a specified list ID.

    To implement it, follow these steps:

    1. Install the dependency: Ensure @pnp/spfx-property-controls is installed in your project.
    2. Import the control: Import PropertyFieldViewPicker and the PropertyFieldViewPickerOrderBy enum.
    3. Prepare properties: Define a property in your web part's interface to store the selected view ID (e.g., view: string).
    4. Configure the property pane: Add the control to your groupFields configuration, providing the listId (usually retrieved from a PropertyFieldListPicker) and the context of the web part.
    import { PropertyFieldViewPicker, PropertyFieldViewPickerOrderBy } from '@pnp/spfx-property-controls/lib/PropertyFieldViewPicker';
    
    // Inside your web part's property pane configuration:
    PropertyFieldViewPicker('view', {
      label: 'Select a view',
      listId: this.properties.list, // The ID of the list to fetch views from
      selectedView: this.properties.view, // The current stored view ID
      orderBy: PropertyFieldViewPickerOrderBy.Title,
      onPropertyChange: this.onPaneFieldChanged.bind(this),
      properties: this.properties,
      context: this.context,
      key: 'viewPickerFieldId'
    })