/src/PropertyFieldAlignPicker.ts
https://github.com/OlivierCC/sp-client-custom-fields · TypeScript · 225 lines · 108 code · 14 blank · 103 comment · 11 complexity · f67f5dbc9043d1a2fbf252804b215f78 MD5 · raw file
- /**
- * @file PropertyFieldAlignPicker.ts
- * Define a custom field of type PropertyFieldAlignPicker for
- * the SharePoint Framework (SPfx)
- *
- * @copyright 2016 Olivier Carpentier
- * Released under MIT licence
- */
- import * as React from 'react';
- import * as ReactDom from 'react-dom';
- import {
- IPropertyPaneField,
- PropertyPaneFieldType,
- IPropertyPaneCustomFieldProps
- } from '@microsoft/sp-webpart-base';
- import PropertyFieldAlignPickerHost, { IPropertyFieldAlignPickerHostProps } from './PropertyFieldAlignPickerHost';
- /**
- * @interface
- * Public properties of the PropertyFieldAlignPicker custom field
- *
- */
- export interface IPropertyFieldAlignPickerProps {
- /**
- * @var
- * Property field label displayed on top
- */
- label: string;
- /**
- * @var
- * Initial value
- */
- initialValue?: string;
- /**
- * @function
- * Defines a onPropertyChange function to raise when the selected Color changed.
- * Normally this function must be always defined with the 'this.onPropertyChange.bind(this)'
- * method of the web part object.
- */
- onPropertyChanged(propertyPath: string, oldValue: any, newValue: any): void;
- /**
- * @function
- * This API is called to render the web part.
- * Normally this function must be always defined with the 'this.render.bind(this)'
- * method of the web part object.
- */
- render(): void;
- /**
- * This property is used to indicate the web part's PropertyPane interaction mode: Reactive or NonReactive.
- * The default behaviour is Reactive.
- */
- disableReactivePropertyChanges?: boolean;
- /**
- * @var
- * Parent Web Part properties
- */
- properties: any;
- /**
- * @var
- * An UNIQUE key indicates the identity of this control
- */
- key?: string;
- /**
- * Whether the property pane field is enabled or not.
- */
- disabled?: boolean;
- /**
- * The method is used to get the validation error message and determine whether the input value is valid or not.
- *
- * When it returns string:
- * - If valid, it returns empty string.
- * - If invalid, it returns the error message string and the text field will
- * show a red border and show an error message below the text field.
- *
- * When it returns Promise<string>:
- * - The resolved value is display as error message.
- * - The rejected, the value is thrown away.
- *
- */
- onGetErrorMessage?: (value: string) => string | Promise<string>;
- /**
- * Custom Field will start to validate after users stop typing for `deferredValidationTime` milliseconds.
- * Default value is 200.
- */
- deferredValidationTime?: number;
- }
- /**
- * @interface
- * Private properties of the PropertyFieldAlignPicker custom field.
- * We separate public & private properties to include onRender & onDispose method waited
- * by the PropertyFieldCustom, witout asking to the developer to add it when he's using
- * the PropertyFieldAlignPicker.
- *
- */
- export interface IPropertyFieldAlignPickerPropsInternal extends IPropertyPaneCustomFieldProps {
- label: string;
- initialValue?: string;
- targetProperty: string;
- onRender(elem: HTMLElement): void;
- onDispose(elem: HTMLElement): void;
- onPropertyChanged(propertyPath: string, oldValue: any, newValue: any): void;
- render(): void;
- disableReactivePropertyChanges?: boolean;
- properties: any;
- key: string;
- disabled?: boolean;
- onGetErrorMessage?: (value: string) => string | Promise<string>;
- deferredValidationTime?: number;
- }
- /**
- * @interface
- * Represents a PropertyFieldAlignPicker object
- *
- */
- class PropertyFieldAlignPickerBuilder implements IPropertyPaneField<IPropertyFieldAlignPickerPropsInternal> {
- //Properties defined by IPropertyPaneField
- public type: PropertyPaneFieldType = PropertyPaneFieldType.Custom;
- public targetProperty: string;
- public properties: IPropertyFieldAlignPickerPropsInternal;
- //Custom properties
- private label: string;
- private initialValue: string;
- private onPropertyChanged: (propertyPath: string, oldValue: any, newValue: any) => void;
- private customProperties: any;
- private key: string;
- private disabled: boolean = false;
- private onGetErrorMessage: (value: string) => string | Promise<string>;
- private deferredValidationTime: number = 200;
- private renderWebPart: () => void;
- private disableReactivePropertyChanges: boolean = false;
- /**
- * @function
- * Ctor
- */
- public constructor(_targetProperty: string, _properties: IPropertyFieldAlignPickerPropsInternal) {
- this.render = this.render.bind(this);
- this.targetProperty = _properties.targetProperty;
- this.properties = _properties;
- this.label = _properties.label;
- this.initialValue = _properties.initialValue;
- this.properties.onDispose = this.dispose;
- this.properties.onRender = this.render;
- this.onPropertyChanged = _properties.onPropertyChanged;
- this.customProperties = _properties.properties;
- this.key = _properties.key;
- if (_properties.disabled === true)
- this.disabled = _properties.disabled;
- this.onGetErrorMessage = _properties.onGetErrorMessage;
- if (_properties.deferredValidationTime !== undefined)
- this.deferredValidationTime = _properties.deferredValidationTime;
- this.renderWebPart = _properties.render;
- if (_properties.disableReactivePropertyChanges !== undefined && _properties.disableReactivePropertyChanges != null)
- this.disableReactivePropertyChanges = _properties.disableReactivePropertyChanges;
- }
- /**
- * @function
- * Renders the field content
- */
- private render(elem: HTMLElement): void {
- //Construct the JSX properties
- const element: React.ReactElement<IPropertyFieldAlignPickerHostProps> = React.createElement(PropertyFieldAlignPickerHost, {
- label: this.label,
- initialValue: this.initialValue,
- targetProperty: this.targetProperty,
- onDispose: this.dispose,
- onRender: this.render,
- onPropertyChanged: this.onPropertyChanged,
- properties: this.customProperties,
- key: this.key,
- disabled: this.disabled,
- onGetErrorMessage: this.onGetErrorMessage,
- deferredValidationTime: this.deferredValidationTime,
- render: this.renderWebPart,
- disableReactivePropertyChanges: this.disableReactivePropertyChanges
- });
- //Calls the REACT content generator
- ReactDom.render(element, elem);
- }
- /**
- * @function
- * Disposes the current object
- */
- private dispose(elem: HTMLElement): void {
- }
- }
- /**
- * @function
- * Helper method to create the customer field on the PropertyPane.
- * @param targetProperty - Target property the custom field is associated to.
- * @param properties - Strongly typed custom field properties.
- */
- export function PropertyFieldAlignPicker(targetProperty: string, properties: IPropertyFieldAlignPickerProps): IPropertyPaneField<IPropertyFieldAlignPickerPropsInternal> {
- //Create an internal properties object from the given properties
- var newProperties: IPropertyFieldAlignPickerPropsInternal = {
- label: properties.label,
- targetProperty: targetProperty,
- initialValue: properties.initialValue,
- onPropertyChanged: properties.onPropertyChanged,
- properties: properties.properties,
- onDispose: null,
- onRender: null,
- key: properties.key,
- disabled: properties.disabled,
- onGetErrorMessage: properties.onGetErrorMessage,
- deferredValidationTime: properties.deferredValidationTime,
- render: properties.render,
- disableReactivePropertyChanges: properties.disableReactivePropertyChanges
- };
- //Calls the PropertyFieldAlignPicker builder object
- //This object will simulate a PropertyFieldCustom to manage his rendering process
- return new PropertyFieldAlignPickerBuilder(targetProperty, newProperties);
- }