Skip to content

A plugin marketplace for GitHub Copilot and other AI agents that provides Power Platform development plugins, including reusable skills, agents, and commands for building and deploying solutions.

3.0 KB
Raw Blame
import { IInputs, IOutputs } from "./generated/ManifestTypes";
import "./css/{{name}}.css";

export class {{name}} implements ComponentFramework.StandardControl<IInputs, IOutputs> {
    private container: HTMLDivElement | null = null;
    private input: HTMLInputElement | null = null;
    private message: HTMLDivElement | null = null;
    private context: ComponentFramework.Context<IInputs> | null = null;
    private notifyOutputChanged: (() => void) | null = null;
    private value = "";
    private readonly onInput = (): void => {
        if (!this.input) return;
        const next = this.input.value;
        if (next === this.value) return;
        this.value = next;
        this.notifyOutputChanged?.();
    };

    public init(
        context: ComponentFramework.Context<IInputs>,
        notifyOutputChanged: () => void,
        _state: ComponentFramework.Dictionary,
        container: HTMLDivElement,
    ): void {
        this.container = container;
        this.context = context;
        this.notifyOutputChanged = notifyOutputChanged;

        const root = document.createElement("div");
        root.className = "{{cssClass}}";

        this.input = document.createElement("input");
        this.input.className = "{{cssInputClass}}";
        this.input.type = "text";
        this.input.setAttribute("aria-label", {{displayNameJson}});
        this.input.addEventListener("input", this.onInput);

        this.message = document.createElement("div");
        this.message.className = "{{cssMessageClass}}";
        this.message.hidden = true;

        root.append(this.input, this.message);
        container.append(root);
        this.updateView(context);
    }

    public updateView(context: ComponentFramework.Context<IInputs>): void {
        this.context = context;
        const parameter = context.parameters.sampleProperty;
        const readable = parameter?.security?.readable ?? true;
        const editable = parameter?.security?.editable ?? true;
        const next = parameter?.raw == null ? "" : String(parameter.raw);
        this.value = next;

        if (!this.input || !this.message) return;
        this.input.value = next;
        this.input.disabled = context.mode.isControlDisabled || !readable;
        this.input.readOnly = !editable;
        this.input.hidden = !readable;
        this.message.hidden = readable;
        this.message.textContent = readable ? "" : this.getString("{{name}}_PermissionDenied");
    }

    public getOutputs(): IOutputs {
        return {
            sampleProperty: this.value,
        };
    }

    public destroy(): void {
        if (this.input) {
            this.input.removeEventListener("input", this.onInput);
        }
        this.container?.replaceChildren();
        this.input = null;
        this.message = null;
        this.container = null;
        this.context = null;
        this.notifyOutputChanged = null;
    }

    private getString(key: string): string {
        return this.context?.resources?.getString(key) || key;
    }
}