Skip to main content

OptionSet PCF Control


Overview

The OptionsetControl is a PowerApps component framework (PCF) control designed to display and interact with an Option Set attribute in a model-driven app. The control renders option set values as buttons and allows users to select a value.

Features

  • Dynamically retrieves option set values from the OptionSetAttribute.
  • Displays options as buttons with customizable colors.
  • Allows toggling selection.
  • Provides soft default colors when custom colors are not provided.
  • Updates and maintains state based on user interaction and data context changes.

Implementation Details

1. Initialization (init Method)

The init method initializes the control by:

  • Assigning context and necessary parameters.
  • Parsing color settings.
  • Extracting the default or currently selected option set value.
  • Creating and rendering buttons for each option set value.
  • Attaching the main container to the provided container.

2. Parsing Colors (parseColors Method)

Parses the color mapping for each option set value from a JSON string. If no valid JSON is provided, it defaults to an empty mapping.

3. Creating Buttons (createButtons Method)

  • Clears existing buttons before rendering.
  • Checks if option set values are available; if not, displays a disabled placeholder button.
  • Iterates through option set values, creating a button for each with a dynamically assigned color.
  • Attaches event listeners for selection handling.

4. Soft Color Generation (getSoftColor Method)

Provides default soft colors if specific colors are not provided for an option set value.

5. Updating View (updateView Method)

  • Refreshes colors and selected values based on context updates.
  • Updates the button selection state.
  • Disables buttons if the control is in a disabled state.

6. Handling Selection (onButtonClick Method)

  • Updates the selected value upon button click.
  • Allows toggling between selection and deselection.
  • Triggers an output change notification.

7. Output Handling (getOutputs Method)

Returns the selected option set value. If no value is selected, it returns -1.

8. Cleanup (destroy Method)

Clears the container content to ensure proper resource cleanup when the control is removed.


Dependencies

  • ComponentFramework.StandardControl<IInputs, IOutputs>
  • OptionSetAttribute (a required parameter providing option values and metadata)
  • CSS styling from OptionsetControl.css

Customization

  • Developers can modify colors by passing a JSON mapping of option set values to colors.
  • Additional styles and UI elements can be customized via OptionsetControl.css.

Edge Cases Considered

  • Handles missing or invalid color mappings.
  • Ensures buttons are disabled when the control state is disabled.
  • Defaults to a visually distinct but neutral color if no custom color is set.

Potential Enhancements

  • Add animations for a smoother user experience.
  • Provide accessibility enhancements (e.g., ARIA attributes).
  • Allow multi-selection if needed.
OUTPUT



Comments

Popular posts from this blog

Comparison: Using Workflows vs. JavaScript vs. Plugins in Dynamics CRM?

  There are three ways to automate actions in Microsoft Dynamics CRM: workflows, JavaScript, or plugins. In this blog we will discuss the difference between them and how to choose which option is appropriate for your task. Workflows  can perform tasks such as updating data or sending email. They are triggered by saving records, creating records, or changing specific fields on a form, and once triggered, they run on their own in the background. As you can see in the example of  How to Assign a Territory to a Lead in Dynamics CRM , you can even look up data in another entity. JavaScript  runs on the screen while the user is using a form. JavaScript is triggered by events within the page, updating a field or saving, and it is commonly used to hide or show different fields on the forms. You can also, for instance,  Populate a CRM 2011 Lookup or PartyList Field Using JavaScript  by having a lookup automatically linked to the record based on what is entered in an...

Task Activity Timeline

  1. Overview The PCF Calendar Control is a custom PowerApps component that displays activities in a calendar-like view. It supports multiple views (monthly, weekly, yearly, daily), allows users to expand/collapse records for each date, and provides a scrollable interface for better usability. The control is built using TypeScript and CSS, adhering to best practices for type safety and maintainability. 2. Features View Modes: Monthly View : Groups activities by month. Weekly View : Groups activities by week. Yearly View : Groups activities by year. Daily View : Displays activities for individual days. Expand/Collapse Functionality: Users can click on a date to expand or collapse its associated records. Smooth animations enhance the user experience. Scrollable Container: A scrollable container ensures that large datasets are manageable. Responsive Design: The control adjusts its layout for smaller screens. Type Safety: The code uses TypeScript interfaces to avoid the use of any and...

Trigger JavaScript on Click of Button PCF Control

  Overview The TriggerJavascript control is a custom PCF (PowerApps Component Framework) control that renders a button with customizable label, icon, and on-click script execution. The control allows users to dynamically trigger JavaScript code upon clicking the button. Dependencies IInputs, IOutputs from ./generated/ManifestTypes (Auto-generated types from PowerApps) CSS styling from ./CSS/TriggerJavascript.css Class: TriggerJavascript This class implements ComponentFramework.StandardControl<IInputs, IOutputs> and manages rendering a button inside a container, dynamically executing JavaScript code on button click. Properties Private Properties _container: HTMLDivElement - A reference to the container element where the button will be rendered. Methods constructor() Initializes a new instance of the TriggerJavascript control. init(context: ComponentFramework.Context<IInputs>, notifyOutputChanged: () => void, state: ComponentFramework.Dictionary, container: HTMLD...