/src/propertyFields/listPicker/PropertyFieldListPicker.ts

https://github.com/SharePoint/sp-dev-fx-property-controls · TypeScript · 182 lines · 143 code · 12 blank · 27 comment · 16 complexity · f280d3a233556eaeb66c30231aa273b5 MD5 · raw file

  1. import * as React from 'react';
  2. import * as ReactDom from 'react-dom';
  3. import {
  4. IPropertyPaneField,
  5. PropertyPaneFieldType
  6. } from '@microsoft/sp-property-pane';
  7. import { BaseComponentContext } from '@microsoft/sp-component-base';
  8. import PropertyFieldListPickerHost from './PropertyFieldListPickerHost';
  9. import PropertyFieldListMultiPickerHost from './PropertyFieldListMultiPickerHost';
  10. import { IPropertyFieldListPickerHostProps, ISPList } from './IPropertyFieldListPickerHost';
  11. import { IPropertyFieldListMultiPickerHostProps } from './IPropertyFieldListMultiPickerHost';
  12. import { PropertyFieldListPickerOrderBy, IPropertyFieldListPickerProps, IPropertyFieldListPickerPropsInternal, IPropertyFieldList } from './IPropertyFieldListPicker';
  13. /**
  14. * Represents a PropertyFieldListPicker object
  15. */
  16. class PropertyFieldListPickerBuilder implements IPropertyPaneField<IPropertyFieldListPickerPropsInternal> {
  17. //Properties defined by IPropertyPaneField
  18. public type: PropertyPaneFieldType = PropertyPaneFieldType.Custom;
  19. public targetProperty: string;
  20. public properties: IPropertyFieldListPickerPropsInternal;
  21. //Custom properties label: string;
  22. private label: string;
  23. private context: BaseComponentContext;
  24. private webAbsoluteUrl?: string;
  25. private selectedList: string | IPropertyFieldList;
  26. private selectedLists: string[] | IPropertyFieldList[];
  27. private baseTemplate: number;
  28. private orderBy: PropertyFieldListPickerOrderBy;
  29. private multiSelect: boolean;
  30. private showSelectAll: boolean;
  31. private selectAllInList: boolean;
  32. private selectAllInListLabel: string;
  33. private includeHidden: boolean;
  34. private listsToExclude: string[];
  35. private includeListTitleAndUrl: boolean;
  36. public onPropertyChange(propertyPath: string, oldValue: any, newValue: any): void { }
  37. private customProperties: any;
  38. private key: string;
  39. private disabled: boolean = false;
  40. private onGetErrorMessage: (value: string) => string | Promise<string>;
  41. private deferredValidationTime: number = 200;
  42. private renderWebPart: () => void;
  43. private disableReactivePropertyChanges: boolean = false;
  44. private filter: string;
  45. private onListsRetrieved?: (lists: ISPList[]) => PromiseLike<ISPList[]> | ISPList[];
  46. /**
  47. * Constructor method
  48. */
  49. public constructor(_targetProperty: string, _properties: IPropertyFieldListPickerPropsInternal) {
  50. this.render = this.render.bind(this);
  51. this.targetProperty = _targetProperty;
  52. this.properties = _properties;
  53. this.properties.onDispose = this.dispose;
  54. this.properties.onRender = this.render;
  55. this.label = _properties.label;
  56. this.context = _properties.context;
  57. this.webAbsoluteUrl = _properties.webAbsoluteUrl;
  58. this.selectedList = _properties.selectedList;
  59. this.selectedLists = _properties.selectedLists;
  60. this.baseTemplate = _properties.baseTemplate;
  61. this.orderBy = _properties.orderBy;
  62. this.multiSelect = _properties.multiSelect;
  63. this.showSelectAll = _properties.showSelectAll;
  64. this.selectAllInList = _properties.selectAllInList;
  65. this.selectAllInListLabel = _properties.selectAllInListLabel;
  66. this.includeHidden = _properties.includeHidden;
  67. this.onPropertyChange = _properties.onPropertyChange;
  68. this.customProperties = _properties.properties;
  69. this.key = _properties.key;
  70. this.onGetErrorMessage = _properties.onGetErrorMessage;
  71. this.listsToExclude = _properties.listsToExclude;
  72. this.filter = _properties.filter;
  73. this.onListsRetrieved = _properties.onListsRetrieved;
  74. this.includeListTitleAndUrl = _properties.includeListTitleAndUrl;
  75. if (_properties.disabled === true) {
  76. this.disabled = _properties.disabled;
  77. }
  78. if (_properties.deferredValidationTime) {
  79. this.deferredValidationTime = _properties.deferredValidationTime;
  80. }
  81. }
  82. /**
  83. * Renders the SPListPicker field content
  84. */
  85. private render(elem: HTMLElement, ctx?: any, changeCallback?: (targetProperty?: string, newValue?: any) => void): void {
  86. const componentProps = {
  87. label: this.label,
  88. targetProperty: this.targetProperty,
  89. context: this.context,
  90. webAbsoluteUrl: this.webAbsoluteUrl,
  91. baseTemplate: this.baseTemplate,
  92. orderBy: this.orderBy,
  93. multiSelect: this.multiSelect,
  94. includeHidden: this.includeHidden,
  95. onDispose: this.dispose,
  96. onRender: this.render,
  97. onChange: changeCallback,
  98. onPropertyChange: this.onPropertyChange,
  99. properties: this.customProperties,
  100. key: this.key,
  101. disabled: this.disabled,
  102. onGetErrorMessage: this.onGetErrorMessage,
  103. deferredValidationTime: this.deferredValidationTime,
  104. listsToExclude: this.listsToExclude,
  105. filter: this.filter,
  106. onListsRetrieved: this.onListsRetrieved,
  107. includeListTitleAndUrl: this.includeListTitleAndUrl
  108. };
  109. // Check if the multi or single select component has to get loaded
  110. if (this.multiSelect) {
  111. // Multi selector
  112. componentProps['selectedLists'] = this.selectedLists;
  113. componentProps['showSelectAll'] = this.showSelectAll;
  114. componentProps['selectAllInList'] = this.selectAllInList;
  115. componentProps['selectAllInListLabel'] = this.selectAllInListLabel;
  116. const element: React.ReactElement<IPropertyFieldListMultiPickerHostProps> = React.createElement(PropertyFieldListMultiPickerHost, componentProps);
  117. // Calls the REACT content generator
  118. ReactDom.render(element, elem);
  119. } else {
  120. // Single selector
  121. componentProps['selectedList'] = this.selectedList;
  122. const element: React.ReactElement<IPropertyFieldListPickerHostProps> = React.createElement(PropertyFieldListPickerHost, componentProps);
  123. // Calls the REACT content generator
  124. ReactDom.render(element, elem);
  125. }
  126. }
  127. /**
  128. * Disposes the current object
  129. */
  130. private dispose(elem: HTMLElement): void {
  131. }
  132. }
  133. /**
  134. * Helper method to create a SPList Picker on the PropertyPane.
  135. * @param targetProperty - Target property the SharePoint list picker is associated to.
  136. * @param properties - Strongly typed SPList Picker properties.
  137. */
  138. export function PropertyFieldListPicker(targetProperty: string, properties: IPropertyFieldListPickerProps): IPropertyPaneField<IPropertyFieldListPickerPropsInternal> {
  139. //Create an internal properties object from the given properties
  140. const newProperties: IPropertyFieldListPickerPropsInternal = {
  141. label: properties.label,
  142. targetProperty: targetProperty,
  143. context: properties.context,
  144. webAbsoluteUrl: properties.webAbsoluteUrl,
  145. selectedList: !Array.isArray(properties.selectedList) ? properties.selectedList : null,
  146. selectedLists: Array.isArray(properties.selectedList) ? properties.selectedList : null,
  147. baseTemplate: properties.baseTemplate,
  148. orderBy: properties.orderBy,
  149. multiSelect: properties.multiSelect || false,
  150. showSelectAll: properties.showSelectAll || false,
  151. selectAllInList: properties.selectAllInList || false,
  152. selectAllInListLabel: properties.selectAllInListLabel,
  153. includeHidden: properties.includeHidden,
  154. onPropertyChange: properties.onPropertyChange,
  155. properties: properties.properties,
  156. onDispose: null,
  157. onRender: null,
  158. key: properties.key,
  159. disabled: properties.disabled,
  160. onGetErrorMessage: properties.onGetErrorMessage,
  161. deferredValidationTime: properties.deferredValidationTime,
  162. listsToExclude: properties.listsToExclude,
  163. filter: properties.filter,
  164. onListsRetrieved: properties.onListsRetrieved,
  165. includeListTitleAndUrl: properties.includeListTitleAndUrl
  166. };
  167. //Calls the PropertyFieldListPicker builder object
  168. //This object will simulate a PropertyFieldCustom to manage his rendering process
  169. return new PropertyFieldListPickerBuilder(targetProperty, newProperties);
  170. }