Video: steps to create a Cosmos DX component

In this post I want to share my experience with creating a DX component that I could use in my Cosmos React application. While the Cosmos React architecture comes with an extensive library of out-of-the-box assets, the framework provides tools for expanding the UI by adding Cosmos DX components.

Important note

Creation of DX components is applicable for professional (front-end) developers in advanced use cases where out-of-the-box components do not meet the user’s needs. To create custom components, you need knowledge in Cosmos React development and common development tools, such as NPM and Git.

Cosmos React is designed to address the vast majority of enterprise design workflow needs through out-of-the-box assets. Low-code developers, business architects, and UX designers should not create Cosmos DX components in Cosmos React. Make sure to understand the paradigm of creating the design workflow for Cosmos React before starting to think about solving requirements via the creation of new DX components.

The high-level lifecycle

Before watching the video it might be good to see the lifecycle first of the creation of the component. See below picture.

High level you need to create the component locally on your machine. Once that is done and tested you can publish it into your Pega development environment. The DX Component gets created as a rule instance of Rule-UI-Component and needs to be published into an unlocked ruleset. Once it is in the dev environment, you can use your normal practices of deploying to other environments like to use of Deployment Manager.

The video

The video shows all steps that I had to take to create, publish and use the Cosmos DX component. Below the video is a more detailed description of these steps.

https://players.brightcove.net/1519050010001/default_default/index.html?videoId=6300632424001

Detailed steps

My simple use case was to have a way to change the styling of a Text field based on the value. My app is calculating risk declaratively and when the risk is high is should be marked bold and in red, when its low it should show in green.

Step 1: create a project

You need to have a local project created on your laptop. Before you can do that you have to make sure you have installed Git, NodeJS and NPM installed and that you have access to a Pega 8.7 environment where you have an application that is using Cosmos React.

Run the following command in a terminal window to get the project created.

npx @pega/custom-ui-components init

This will download the required libraries to your local machine that you need for thee creation process and it creates your folders structure. The src folder contains the components folder and there your new components will be created. There are three examples on the initial creation.

Step 2: Create the component

Now that you have the structure you can create a component. This is as easy as running the following command

npm run create

It will prompt you for some data; you can define it is only applicable for one application, multiple or any application you add it to. You also need to name in which library it should be. Make sure that you know what type (and subtype) you want to create. There are three main options:

  • Widget: this can be used on a landing page or on some views in the case
  • Layout: to create a custom layout for the fields and/or widgets
  • Field: to create an interactive UI elements. In my example I have used Text

The command creates a folder in the components folder with the name of the organisation, library and component name. In that structure you can find the following important files:

  • The index.jsx file contains the actual component rendering logic.
  • The demo.stories.jsx file contains the demo logic that can be viewed in the storybook.
  • The config.json file contains the mapping of properties for configuring the auto generated property panel. This property panel appears in AppStudio when an application is being authored to use the custom component.

I have used the following code to create my component:

import { Text } from '@pega/cosmos-react-core';
..
..
switch (value) {
    case 'High':
      return 

```
<StyledPePstlRiskTextWrapper><Text variant='h1' status={'error'}>Risk is {value}</Text></StyledPePstlRiskTextWrapper>; 
      break;
    case 'Medium':
      return <StyledPePstlRiskTextWrapper><Text variant='h1' status={'warning'}>Risk is {value}</Text></StyledPePstlRiskTextWrapper>; 
      break;
      case 'Low':
        return <StyledPePstlRiskTextWrapper><Text variant='h1' status={'success'}>Risk is {value}</Text></StyledPePstlRiskTextWrapper>; 
      break;
      case 'Very High':
        return <StyledPePstlRiskTextWrapper><Text variant='h1' status={'error'}>Risk is {value}</Text></StyledPePstlRiskTextWrapper>; 
      break;
    default:
      return <StyledPePstlRiskTextWrapper><Text>Risk is undefined</Text></StyledPePstlRiskTextWrapper>
```

; ;   
  }
} 

Step 3: Test in storybook

Storybook for React is a UI development environment for your React components that is easy to use for testing your component. File demo.stories.jsx has been created for you to use in storybook. You do need to make appropriate changes to that file to assure the right data is available to test for your component. Run the following command in your terminal from within your project folder:

npm start

Then storybook opens a browser window or tab and shows all the components that are part of your project. When you are done testing simply hit CTRL C to shut down storybook in the terminal window.

Step 4: Authenticate the user

Before you can publish, list or delete anything to your Pega instance, you first need to authenticate a user to access the resources in the Pega Server.

For this you need to have the clientid and secret for the DX-CustomComponent instance of an OAuth 2.0 Client Registration. If you have dont have it, you need to regenerate this and save that information (and don’t forget to save the instance!). Then run the following command:

npm run authenticate

You will be prompted for the server url (https://yourdomain/prweb), the username and its password, and the clientid and the secret.

Step 5: Publish the component

Now that you are authenticated you can start publishing your component to the Pega development server. Use:

npm run publish

Select the component you want to publish, name the ruleset and the version and then the component is created and published to your development server as an instance of Rule-UI-Component. The ruleset must be unlocked and the operator that you used for the authentication must have access to that ruleset. It can be a branch ruleset.

Step 6: Use your component

This is the fun part where you actually get the use your component. In your Pega dev environment go to app studio of your Cosmos React application and depending on the type of component you created, open a landing page or a view and then you are able to select your component. If it isn’t showing directly you might want to log out and open a new window.

This is my result:

Where to find more information?

As this is early adoption work there is not yet a full documentation available on docs.pega.com but that will be available soon. The resource I have used is here: https://www.npmjs.com/package/@pega/custom-dx-components

https://design.pega.com contains a lot of information about what is currently available on the Cosmos Design system. The develop tab shows code samples of how to use the items in the library and what options are available. A very useful resource!

@DionLammers

Awesome video!!

A combination of <component_name> form the key to a component. The name spacing scheme is designed to support sharing of component libraries within and outside an organization. Example generated keys tuples are:

MyCo_widgets_weather or YourCo_astronomy_galaxyPicker

Have anyone experienced issue when you publish new version of the widget, but it isn’t available on Pega and still renders an old one?

Couldn’t understand what caches are involved and how to force reload it.

@DionLammers

Bedankt Dion! Good content

@DionLammers nice content and very inspiring!

I wonder if there is any documentation or examples for the Widget component type. I noticed once you chose that one, you can select subtypes: Page, Case, Page & Case. However, files like “config.json” looks exactly like with the Field component type… I wonder a use case where the custom component input can check on a data type to validate discount codes for example.

Would it be possible to publish sources of already existing React Components and Widgets? So it would be easier for devs to see how OOTB Pega implemented those and copy the approach.

Because now it is more like a guessing what Pega expects inside of config.json and other files.

@DionLammers Would you mind giving us an example of how to make a button using a custom component?

@DionLammers

Hello - As per our use case we are trying to add a new (Rule-UI-Component) URL in the application. The button is displaying properly in story book and successfully published in Pega also.

But when we are trying to configure that in APP Studio, The New Control is not available in Display As option. The OOTB URL is the only option to configure.

On further investigation we have found - The Display As option is populates from D_pzAvailableControlsForField. Which populates from pzGetAvailableControlsForField data transform. In this DT all the controls are hardcoded.

Is there any aspects we are missing? could you please highlight.

PFA the details for the same.

Note - We followed this article - Pegasystems Documentation

The implementation is done in Pega V8.8.1

Details_0.docx (267 KB)

@ANKITMITRA - Did you manage to resolve this issue?

@DmitryK7052

Hi Dmitry, my feeling is that this is in a client side cache but I don’t know exactly where. I did experience after adding a few new components that they weren’t visible in app studio and one situation that I updated an existing component and -as you say- it showed the old one. After I opened a new incognito browser window and logged in again, it did show me the components and the right version.

@MARTT anything you can comment on?

@DmitryK7052 @MARTT

Hi, few ideas and questions:

  1. Have any JSON header values changed in the config.json header since since initial publish?
  2. Check if rule instance of class Rule-UI-Component associated with the component is updated on publish
  3. Try a work around by removing the published component first before publishing a new version of the component.

Please open a service request if the issue remains with steps to reproduce the issue.

@DionLammers We are using DX SDK and also created DX Component. It perfectly works editing in APP Studio and rendering on Cosmos react portal. It’s not available on the custom React UI. Is there anything else we need to do to make that available similar to other OOTB Components

@DionLammers Cool stuff Dion. Very helpful in getting your head around these awesome capabilities.

@DionLammers - The npm package mentioned above for creating custom DX components is deprecated in favour of the custom-dx-components package.

Since this was first published there is now a detailed guide on docs.pega.com with the link provided here: Constellation DX Components

Here you will find comprehensive docs, tutorials and an expanding video library that will help to accelerate the learning process.

@Niall Riddell : The link provided is not available, is there any other URL which can be added here ?

@MARTT hi Martt, I kinda gave up on the component creation after fightning with that cache problem for a day. Will try again after new version of it will be released.

If Dion has the same problems - hope that Pegasystems can track and fix the problem internally.

Thanks for all the hard work with React.

@DmitryK7052 Understand need to move on. A service request with steps to reproduce would help facilitate a fix. @MaxDeAngelis

@SUMITKUMAR-1 Pegasystems Documentation