UNPKG

@pnp/spfx-property-controls

Version:

Reusable property pane controls for SharePoint Framework solutions

80 lines 3.67 kB
import { PropertyPaneFieldType } from '@microsoft/sp-property-pane'; import { getColorFromString } from '@fluentui/react'; import * as React from 'react'; import * as ReactDom from 'react-dom'; import { setPropertyValue } from '../../helpers/GeneralHelper'; import { PropertyFieldSwatchColorPickerStyle, } from './IPropertyFieldSwatchColorPicker'; import PropertyFieldSwatchColorPickerHost from './PropertyFieldSwatchColorPickerHost'; class PropertyFieldSwatchColorPickerBuilder { constructor(_targetProperty, _properties) { //Properties defined by IPropertyPaneField this.type = PropertyPaneFieldType.Custom; this.targetProperty = _targetProperty; this.properties = { key: _properties.key, label: _properties.label, onPropertyChange: _properties.onPropertyChange, selectedColor: _properties.selectedColor, colors: _properties.colors, showAsCircles: _properties.showAsCircles, columnCount: _properties.columnCount, disabled: _properties.disabled, properties: _properties.properties, style: _properties.style, iconName: _properties.iconName, onRender: this.onRender.bind(this) }; if (typeof _properties.selectedColor === 'undefined') { this.color = null; } else { if (typeof _properties.selectedColor === 'string') { this.color = _properties.selectedColor; } else { this.color = _properties.selectedColor.str; } } this.valueAsObject = _properties.valueAsObject; } render() { if (!this.elem) { return; } this.onRender(this.elem); } onRender(elem, ctx, changeCallback) { if (!this.elem) { this.elem = elem; } this.changeCB = changeCallback; const element = React.createElement(PropertyFieldSwatchColorPickerHost, { label: this.properties.label, disabled: this.properties.disabled, colors: (typeof this.properties.colors === 'undefined' || this.properties.colors.length === 0) ? [{ color: '#FFFFFF' }] : this.properties.colors, showAsCircles: this.properties.showAsCircles, columnCount: typeof this.properties.columnCount === 'undefined' ? 6 : Math.min(Math.max(1, this.properties.columnCount), 8), selectedColor: this.color, style: this.properties.style || PropertyFieldSwatchColorPickerStyle.Inline, iconName: this.properties.iconName || 'Color', onColorChanged: this.onColorChanged.bind(this) }); ReactDom.render(element, elem); } onColorChanged(id, newColor) { if (this.properties.onPropertyChange && newColor !== null) { const newValue = (this.valueAsObject ? getColorFromString(newColor) : newColor); const oldValue = (this.valueAsObject ? getColorFromString(this.color) : this.color); this.color = newColor; this.properties.onPropertyChange(this.targetProperty, oldValue, newValue); setPropertyValue(this.properties.properties, this.targetProperty, newValue); if (typeof this.changeCB !== 'undefined' && this.changeCB !== null) { this.changeCB(this.targetProperty, newValue); } } } } export function PropertyFieldSwatchColorPicker(targetProperty, properties) { return new PropertyFieldSwatchColorPickerBuilder(targetProperty, properties); } //# sourceMappingURL=PropertyFieldSwatchColorPicker.js.map