Implementing increment component

10/01/2019

2 minutes to read

In this article

This sample component shows how to bind data with Power Apps component framework and error handling. This component renders as a textbox with an Increment button in the runtime. The text box shows the current value and the Increment button is clickable. Whenever you click on the button, the value within the textbox is increased by 1. The increment value can be changed to any number you wish.

To implement this component, the first thing you need to define the Manifest file, and then implement the custom logic in TypeScript. You can download the sample component from here.

Code

import { IInputs, IOutputs } from "./generated/ManifestTypes";
export class TSIncrementControl
implements ComponentFramework.StandardControl<IInputs, IOutputs> {
// Value of the field is stored and used inside the control
private _value: number;
// Power Apps component framework framework delegate which will be assigned to this object which would be called whenever an update happens.
private _notifyOutputChanged: () => void;
// label element created as part of this control
private label: HTMLInputElement;
// button element created as part of this control
private button: HTMLButtonElement;
// reference to the control container HTMLDivElement
// This element contains all elements of our custom control example
private _container: HTMLDivElement;
/**
* Empty constructor.
*/
constructor() {}
/**
* Used to initialize the control instance. Controls can kick off remote server calls and other initialization actions here.
* Data-set values are not initialized here, use updateView.
* @param context The entire property bag available to control via Context Object; It contains values as set up by the customizer mapped to property names defined in the manifest, as well as utility functions.
* @param notifyOutputChanged A callback method to alert the framework that the control has new outputs ready to be retrieved asynchronously.
* @param state A piece of data that persists in one session for a single user. Can be set at any point in a controls life cycle by calling 'setControlState' in the Mode interface.
* @param container If control is marked control-type='standard', it receives an empty div element within which it can render its content.
*/
public init(
context: ComponentFramework.Context<IInputs>,
notifyOutputChanged: () => void,
state: ComponentFramework.Dictionary,
container: HTMLDivElement
) {
// Creating the label for the control and setting the relevant values.
this.label = document.createElement("input");
this.label.setAttribute("type", "label");
this.label.addEventListener("blur", this.onInputBlur.bind(this));
//Create a button to increment the value by 1.
this.button = document.createElement("button");
// Get the localized string from localized string
this.button.innerHTML = context.resources.getString(
"TS_IncrementControl_ButtonLabel"
);
this.button.classList.add("SimpleIncrement_Button_Style");
this._notifyOutputChanged = notifyOutputChanged;
//this.button.addEventListener("click", (event) => { this._value = this._value + 1; this._notifyOutputChanged();});
this.button.addEventListener("click", this.onButtonClick.bind(this));
// Adding the label and button created to the container DIV.
this._container = document.createElement("div");
this._container.appendChild(this.label);
this._container.appendChild(this.button);
container.appendChild(this._container);
}
/**
* Button Event handler for the button created as part of this control
* @param event
*/
private onButtonClick(event: Event): void {
this._value = this._value + 1;
this._notifyOutputChanged();
}
/**
* Input Blur Event handler for the input created as part of this control
* @param event
*/
private onInputBlur(event: Event): void {
let inputNumber = Number(this.label.value);
this._value = isNaN(inputNumber)
? ((this.label.value as any) as number)
: inputNumber;
this._notifyOutputChanged();
}
/**
* Called when any value in the property bag has changed. This includes field values, data-sets, global values such as container height and width, offline status, control metadata values such as label, visible, etc.
* @param context The entire property bag available to control via Context Object; It contains values as set up by the customizer mapped to names defined in the manifest, as well as utility functions
*/
public updateView(context: ComponentFramework.Context<IInputs>): void {
// This method would render the control with the updated values after we call NotifyOutputChanged
//set the value of the field control to the raw value from the configured field
this._value = context.parameters.value.raw;
this.label.value = this._value != null ? this._value.toString() : "";
if (context.parameters.value.error) {
this.label.classList.add("SimpleIncrement_Input_Error_Style");
} else {
this.label.classList.remove("SimpleIncrement_Input_Error_Style");
}
}
/**
* It is called by the framework prior to a control receiving new data.
* @returns an object based on nomenclature defined in manifest, expecting object[s] for property marked as “bound” or “output”
*/
public getOutputs(): IOutputs {
// custom code goes here - remove the line below and return the correct output
let result: IOutputs = {
value: this._value
};
return result;
}
/**
* Called when the control is to be removed from the DOM tree. Controls should use this call for cleanup.
* i.e. canceling any pending remote calls, removing listeners, etc.
*/
public destroy(): void {}
}

When you click on the button, the value in the text box is increased by 1. The updated value will flow to Power Apps component framework through notifyOutputChanged method.

Note

You can change the increment value when you are configuring the component to the field on the form.

Edit the value in the text box, and if it is a valid integer, then it updates the value to Power Apps component framework. You can continuously click the Increment button and update it. If it’s an invalid integer, an error message pops out.