@pega/custom-dx-components 2.0.9 → 2.0.11

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pega/custom-dx-components",
3
- "version": "2.0.9",
3
+ "version": "2.0.11",
4
4
  "description": "Utility for building custom UI components",
5
5
  "bin": "./bin/index.js",
6
6
  "scripts": {
@@ -64,4 +64,4 @@
64
64
  "eslint": "^8.23.1",
65
65
  "prettier": "2.7.1"
66
66
  }
67
- }
67
+ }
@@ -312,7 +312,7 @@ export default async options => {
312
312
  }
313
313
  ];
314
314
 
315
- console.log(chalk.green("DX Component Builder v2.0.9"));
315
+ console.log(chalk.green("DX Component Builder v2.0.11"));
316
316
  await inquirer.prompt(questions).then(async answers => {
317
317
  const {
318
318
  componentName,
@@ -4,6 +4,8 @@ import PropTypes from 'prop-types';
4
4
 
5
5
  import Styled{{COMPONENT_CLASS_NAME}}Wrapper from './styles';
6
6
 
7
+ // includes in bundle
8
+ import { getAllFields } from './utils';
7
9
 
8
10
  // Duplicated runtime code from Constellation Design System Component
9
11
 
@@ -0,0 +1,226 @@
1
+ import { createUID } from '@pega/cosmos-react-core';
2
+
3
+ //import { LazyMap as LazyComponentMap } from '../../components_map';
4
+
5
+ const LazyComponentMap = {};
6
+
7
+ /**
8
+ * Given the PConnect object of a Template component, retrieve the children
9
+ * metadata of all regions.
10
+ * @param {Function} pConnect PConnect of a Template component.
11
+ */
12
+ export function getAllFields(pConnect) {
13
+ const metadata = pConnect().getRawMetadata();
14
+ if (!metadata.children) {
15
+ return [];
16
+ }
17
+
18
+ let allFields = [];
19
+
20
+ const makeField = (f) => ({
21
+ ...pConnect().resolveConfigProps(f.config),
22
+ type: f.type
23
+ });
24
+
25
+ const hasRegions = !!metadata.children[0]?.children;
26
+ if (hasRegions) {
27
+ allFields = metadata.children.map((region) =>
28
+ region.children.map((field) => {
29
+ // Do not resolve the config props if is status work, instead create component here as status badge and mark as status display
30
+ if (field.config?.value === '@P .pyStatusWork') {
31
+ field.type = 'TextInput';
32
+ field.config.displayAsStatus = true;
33
+ return pConnect().createComponent(field);
34
+ }
35
+
36
+ return makeField(field);
37
+ })
38
+ );
39
+ } else {
40
+ allFields = metadata.children.map(makeField);
41
+ }
42
+
43
+ return allFields;
44
+ }
45
+
46
+ /**
47
+ * A helper function to create an object consisting react component as per the type.
48
+ * This is used by CaseSummary template.
49
+ * @param {object} configObject Object containing meta information for the particular field authored
50
+ * @param {Function} getPConnect PConnect function passed along to other components.
51
+ * @param {string} displayMode displayMode string contains information about the layout of component in review mode.
52
+ */
53
+ export function prepareComponentInCaseSummary(configObject, getPConnect, displayMode) {
54
+ const { config, children } = configObject;
55
+ let { type } = configObject;
56
+ const caseSummaryComponentObject = {};
57
+ let showAddressLabel = true;
58
+ if (config && config.value === '@P .pyStatusWork') {
59
+ config.displayAsStatus = true;
60
+ type = 'TextInput'; // force the type to be TextInput for status field.
61
+ // As TextInput is loaded forcefully sometimes, TextInput component might not be available in lazy map.
62
+ // Load TextInput if it is not available.
63
+ if (!LazyComponentMap[type]) {
64
+ PCore.getAssetLoader().getLoader('component-loader')([type]);
65
+ }
66
+ }
67
+
68
+ caseSummaryComponentObject.name = getPConnect().resolveConfigProps({
69
+ label: config.label
70
+ }).label;
71
+ if (type === 'CaseOperator') {
72
+ switch (caseSummaryComponentObject.name) {
73
+ case 'Create operator':
74
+ caseSummaryComponentObject.name = getPConnect().resolveConfigProps({
75
+ createLabel: config.createLabel
76
+ }).createLabel;
77
+ break;
78
+ case 'Update operator':
79
+ caseSummaryComponentObject.name = getPConnect().resolveConfigProps({
80
+ updateLabel: config.updateLabel
81
+ }).updateLabel;
82
+ break;
83
+ default:
84
+ caseSummaryComponentObject.name = getPConnect().resolveConfigProps({
85
+ resolveLabel: config.resolveLabel
86
+ }).resolveLabel;
87
+ break;
88
+ }
89
+ } else if (type === 'Checkbox') {
90
+ caseSummaryComponentObject.name = getPConnect().resolveConfigProps({
91
+ label: config.caption
92
+ }).label;
93
+ } else if (type === 'Address') {
94
+ showAddressLabel = false;
95
+ }
96
+ const createdComponent = getPConnect().createComponent({
97
+ type,
98
+ children: children ? [...children] : [],
99
+ showAddressLabel,
100
+ config: {
101
+ ...config,
102
+ hideLabel: true,
103
+ key: createUID() // Need a unique key on render of the summary so that the component is recreated each time
104
+ }
105
+ });
106
+ createdComponent.props.getPConnect().setInheritedProp('displayMode', displayMode);
107
+ if (type === 'Address') {
108
+ createdComponent.props.getPConnect().setInheritedProp('showAddressLabel', showAddressLabel);
109
+ }
110
+ // createdComponent.props.getPConnect().setInheritedProp("hideLabel", true);
111
+ caseSummaryComponentObject.value = createdComponent;
112
+ if (type === 'reference') {
113
+ caseSummaryComponentObject.name = createdComponent.props.getPConnect().getInheritedProps().label;
114
+ }
115
+
116
+ return caseSummaryComponentObject;
117
+ }
118
+
119
+ export function getFilteredFields(getPConnect) {
120
+ let primaryFieldsRaw;
121
+ let secondaryFieldsRaw;
122
+ const metadata = getPConnect().getRawMetadata();
123
+ const hasRegions = !!metadata.children[0]?.children;
124
+ if (hasRegions) {
125
+ primaryFieldsRaw = metadata.children[0].children;
126
+ secondaryFieldsRaw = metadata.children[1].children;
127
+ } else {
128
+ [primaryFieldsRaw, secondaryFieldsRaw] = metadata.children;
129
+ }
130
+
131
+ // Filter out fields that are not visible and unsupported types for primary fields (for CaseSummary)
132
+ primaryFieldsRaw = primaryFieldsRaw.filter((item) => {
133
+ const resolvedItem = getPConnect().resolveConfigProps(item.config);
134
+ return resolvedItem.visibility !== false && item.type !== 'TextContent';
135
+ });
136
+
137
+ secondaryFieldsRaw = secondaryFieldsRaw.filter((item) => {
138
+ const resolvedItem = getPConnect().resolveConfigProps(item.config);
139
+ return resolvedItem.visibility !== false && item.type !== 'TextContent';
140
+ });
141
+
142
+ return [primaryFieldsRaw, secondaryFieldsRaw];
143
+ }
144
+
145
+ /**
146
+ * Returns ConfigurableLayout mapped content. With pre-populated default layout configs.
147
+ * @param {object[]} regionData template children item.
148
+ * @returns {object[]} ConfigurableLayout content.
149
+ */
150
+ export function getLayoutDataFromRegion(regionData) {
151
+ const defaultLayoutConfig = {
152
+ width: 'full',
153
+ fillAvailable: true,
154
+ minWidth: [300, 'px']
155
+ };
156
+
157
+ return regionData.props
158
+ ?.getPConnect()
159
+ ?.getChildren()
160
+ ?.map((item, index) => {
161
+ const itemPConnect = item?.getPConnect();
162
+
163
+ return {
164
+ id: itemPConnect?.getComponentName() ? `${itemPConnect.getComponentName()}--${index}` : `item--${index}`,
165
+ content: itemPConnect?.getComponent(),
166
+ layoutConfig: {
167
+ ...defaultLayoutConfig,
168
+ ...itemPConnect?.getConfigProps().layoutConfig
169
+ }
170
+ };
171
+ });
172
+ }
173
+
174
+ /**
175
+ * Determine if the current view is the view of the case step/assignment.
176
+ * @param {Function} pConnect PConnect object for the component
177
+ */
178
+ export function getIsAssignmentView(pConnect) {
179
+ // Get caseInfo content from the store which contains the view info about the current assignment/step
180
+ // TODO To be replaced with pConnect.getCaseInfo().getCurrentAssignmentView when it's available
181
+ const assignmentViewClass = pConnect.getValue(PCore.getConstants().CASE_INFO.CASE_INFO_CLASSID);
182
+ const assignmentViewName = pConnect.getValue(PCore.getConstants().CASE_INFO.ASSIGNMENTACTION_ID);
183
+
184
+ const assignmentViewId = `${assignmentViewName}!${assignmentViewClass}`;
185
+
186
+ // Get the info about the current view from pConnect
187
+ const currentViewId = `${pConnect.getCurrentView()}!${pConnect.getCurrentClassID()}`;
188
+
189
+ return assignmentViewId === currentViewId;
190
+ }
191
+
192
+ /**
193
+ * A hook that gets the instructions content for a view.
194
+ * @param {Function} pConnect PConnect object for the component
195
+ * @param {string} [instructions="casestep"] 'casestep', 'none', or the html content of a Rule-UI-Paragraph rule (processed via core's paragraph annotation handler)
196
+ */
197
+ export function getInstructions(pConnect, instructions = 'casestep') {
198
+ const caseStepInstructions = pConnect.getValue(PCore.getConstants().CASE_INFO.INSTRUCTIONS);
199
+
200
+ // Determine if this view is the current assignment/step view
201
+ const isCurrentAssignmentView = getIsAssignmentView(pConnect);
202
+
203
+ // Case step instructions
204
+ if (instructions === 'casestep' && isCurrentAssignmentView && caseStepInstructions?.length) {
205
+ return caseStepInstructions;
206
+ }
207
+
208
+ // No instructions
209
+ if (instructions === 'none') {
210
+ return undefined;
211
+ }
212
+
213
+ // If the annotation wasn't processed correctly, don't return any instruction text
214
+ if (instructions?.startsWith('@PARAGRAPH')) {
215
+ console.error('@PARAGRAPH annotation was not processed. Hiding custom instructions.');
216
+ return undefined;
217
+ }
218
+
219
+ // Custom instructions from the view
220
+ // The raw metadata for `instructions` will be something like '@PARAGRAPH .SomeParagraphRule' but
221
+ // it is evaluated by core logic to the content
222
+ if (instructions !== 'casestep' && instructions !== 'none') {
223
+ return instructions;
224
+ }
225
+ return undefined;
226
+ }
@@ -4,6 +4,8 @@ import PropTypes from 'prop-types';
4
4
 
5
5
  import Styled{{COMPONENT_CLASS_NAME}}Wrapper from './styles';
6
6
 
7
+ // includes in bundle
8
+ import { getAllFields } from './utils';
7
9
 
8
10
  // Duplicated runtime code from Constellation Design System Component
9
11
 
@@ -0,0 +1,226 @@
1
+ import { createUID } from '@pega/cosmos-react-core';
2
+
3
+ //import { LazyMap as LazyComponentMap } from '../../components_map';
4
+
5
+ const LazyComponentMap = {};
6
+
7
+ /**
8
+ * Given the PConnect object of a Template component, retrieve the children
9
+ * metadata of all regions.
10
+ * @param {Function} pConnect PConnect of a Template component.
11
+ */
12
+ export function getAllFields(pConnect) {
13
+ const metadata = pConnect().getRawMetadata();
14
+ if (!metadata.children) {
15
+ return [];
16
+ }
17
+
18
+ let allFields = [];
19
+
20
+ const makeField = (f) => ({
21
+ ...pConnect().resolveConfigProps(f.config),
22
+ type: f.type
23
+ });
24
+
25
+ const hasRegions = !!metadata.children[0]?.children;
26
+ if (hasRegions) {
27
+ allFields = metadata.children.map((region) =>
28
+ region.children.map((field) => {
29
+ // Do not resolve the config props if is status work, instead create component here as status badge and mark as status display
30
+ if (field.config?.value === '@P .pyStatusWork') {
31
+ field.type = 'TextInput';
32
+ field.config.displayAsStatus = true;
33
+ return pConnect().createComponent(field);
34
+ }
35
+
36
+ return makeField(field);
37
+ })
38
+ );
39
+ } else {
40
+ allFields = metadata.children.map(makeField);
41
+ }
42
+
43
+ return allFields;
44
+ }
45
+
46
+ /**
47
+ * A helper function to create an object consisting react component as per the type.
48
+ * This is used by CaseSummary template.
49
+ * @param {object} configObject Object containing meta information for the particular field authored
50
+ * @param {Function} getPConnect PConnect function passed along to other components.
51
+ * @param {string} displayMode displayMode string contains information about the layout of component in review mode.
52
+ */
53
+ export function prepareComponentInCaseSummary(configObject, getPConnect, displayMode) {
54
+ const { config, children } = configObject;
55
+ let { type } = configObject;
56
+ const caseSummaryComponentObject = {};
57
+ let showAddressLabel = true;
58
+ if (config && config.value === '@P .pyStatusWork') {
59
+ config.displayAsStatus = true;
60
+ type = 'TextInput'; // force the type to be TextInput for status field.
61
+ // As TextInput is loaded forcefully sometimes, TextInput component might not be available in lazy map.
62
+ // Load TextInput if it is not available.
63
+ if (!LazyComponentMap[type]) {
64
+ PCore.getAssetLoader().getLoader('component-loader')([type]);
65
+ }
66
+ }
67
+
68
+ caseSummaryComponentObject.name = getPConnect().resolveConfigProps({
69
+ label: config.label
70
+ }).label;
71
+ if (type === 'CaseOperator') {
72
+ switch (caseSummaryComponentObject.name) {
73
+ case 'Create operator':
74
+ caseSummaryComponentObject.name = getPConnect().resolveConfigProps({
75
+ createLabel: config.createLabel
76
+ }).createLabel;
77
+ break;
78
+ case 'Update operator':
79
+ caseSummaryComponentObject.name = getPConnect().resolveConfigProps({
80
+ updateLabel: config.updateLabel
81
+ }).updateLabel;
82
+ break;
83
+ default:
84
+ caseSummaryComponentObject.name = getPConnect().resolveConfigProps({
85
+ resolveLabel: config.resolveLabel
86
+ }).resolveLabel;
87
+ break;
88
+ }
89
+ } else if (type === 'Checkbox') {
90
+ caseSummaryComponentObject.name = getPConnect().resolveConfigProps({
91
+ label: config.caption
92
+ }).label;
93
+ } else if (type === 'Address') {
94
+ showAddressLabel = false;
95
+ }
96
+ const createdComponent = getPConnect().createComponent({
97
+ type,
98
+ children: children ? [...children] : [],
99
+ showAddressLabel,
100
+ config: {
101
+ ...config,
102
+ hideLabel: true,
103
+ key: createUID() // Need a unique key on render of the summary so that the component is recreated each time
104
+ }
105
+ });
106
+ createdComponent.props.getPConnect().setInheritedProp('displayMode', displayMode);
107
+ if (type === 'Address') {
108
+ createdComponent.props.getPConnect().setInheritedProp('showAddressLabel', showAddressLabel);
109
+ }
110
+ // createdComponent.props.getPConnect().setInheritedProp("hideLabel", true);
111
+ caseSummaryComponentObject.value = createdComponent;
112
+ if (type === 'reference') {
113
+ caseSummaryComponentObject.name = createdComponent.props.getPConnect().getInheritedProps().label;
114
+ }
115
+
116
+ return caseSummaryComponentObject;
117
+ }
118
+
119
+ export function getFilteredFields(getPConnect) {
120
+ let primaryFieldsRaw;
121
+ let secondaryFieldsRaw;
122
+ const metadata = getPConnect().getRawMetadata();
123
+ const hasRegions = !!metadata.children[0]?.children;
124
+ if (hasRegions) {
125
+ primaryFieldsRaw = metadata.children[0].children;
126
+ secondaryFieldsRaw = metadata.children[1].children;
127
+ } else {
128
+ [primaryFieldsRaw, secondaryFieldsRaw] = metadata.children;
129
+ }
130
+
131
+ // Filter out fields that are not visible and unsupported types for primary fields (for CaseSummary)
132
+ primaryFieldsRaw = primaryFieldsRaw.filter((item) => {
133
+ const resolvedItem = getPConnect().resolveConfigProps(item.config);
134
+ return resolvedItem.visibility !== false && item.type !== 'TextContent';
135
+ });
136
+
137
+ secondaryFieldsRaw = secondaryFieldsRaw.filter((item) => {
138
+ const resolvedItem = getPConnect().resolveConfigProps(item.config);
139
+ return resolvedItem.visibility !== false && item.type !== 'TextContent';
140
+ });
141
+
142
+ return [primaryFieldsRaw, secondaryFieldsRaw];
143
+ }
144
+
145
+ /**
146
+ * Returns ConfigurableLayout mapped content. With pre-populated default layout configs.
147
+ * @param {object[]} regionData template children item.
148
+ * @returns {object[]} ConfigurableLayout content.
149
+ */
150
+ export function getLayoutDataFromRegion(regionData) {
151
+ const defaultLayoutConfig = {
152
+ width: 'full',
153
+ fillAvailable: true,
154
+ minWidth: [300, 'px']
155
+ };
156
+
157
+ return regionData.props
158
+ ?.getPConnect()
159
+ ?.getChildren()
160
+ ?.map((item, index) => {
161
+ const itemPConnect = item?.getPConnect();
162
+
163
+ return {
164
+ id: itemPConnect?.getComponentName() ? `${itemPConnect.getComponentName()}--${index}` : `item--${index}`,
165
+ content: itemPConnect?.getComponent(),
166
+ layoutConfig: {
167
+ ...defaultLayoutConfig,
168
+ ...itemPConnect?.getConfigProps().layoutConfig
169
+ }
170
+ };
171
+ });
172
+ }
173
+
174
+ /**
175
+ * Determine if the current view is the view of the case step/assignment.
176
+ * @param {Function} pConnect PConnect object for the component
177
+ */
178
+ export function getIsAssignmentView(pConnect) {
179
+ // Get caseInfo content from the store which contains the view info about the current assignment/step
180
+ // TODO To be replaced with pConnect.getCaseInfo().getCurrentAssignmentView when it's available
181
+ const assignmentViewClass = pConnect.getValue(PCore.getConstants().CASE_INFO.CASE_INFO_CLASSID);
182
+ const assignmentViewName = pConnect.getValue(PCore.getConstants().CASE_INFO.ASSIGNMENTACTION_ID);
183
+
184
+ const assignmentViewId = `${assignmentViewName}!${assignmentViewClass}`;
185
+
186
+ // Get the info about the current view from pConnect
187
+ const currentViewId = `${pConnect.getCurrentView()}!${pConnect.getCurrentClassID()}`;
188
+
189
+ return assignmentViewId === currentViewId;
190
+ }
191
+
192
+ /**
193
+ * A hook that gets the instructions content for a view.
194
+ * @param {Function} pConnect PConnect object for the component
195
+ * @param {string} [instructions="casestep"] 'casestep', 'none', or the html content of a Rule-UI-Paragraph rule (processed via core's paragraph annotation handler)
196
+ */
197
+ export function getInstructions(pConnect, instructions = 'casestep') {
198
+ const caseStepInstructions = pConnect.getValue(PCore.getConstants().CASE_INFO.INSTRUCTIONS);
199
+
200
+ // Determine if this view is the current assignment/step view
201
+ const isCurrentAssignmentView = getIsAssignmentView(pConnect);
202
+
203
+ // Case step instructions
204
+ if (instructions === 'casestep' && isCurrentAssignmentView && caseStepInstructions?.length) {
205
+ return caseStepInstructions;
206
+ }
207
+
208
+ // No instructions
209
+ if (instructions === 'none') {
210
+ return undefined;
211
+ }
212
+
213
+ // If the annotation wasn't processed correctly, don't return any instruction text
214
+ if (instructions?.startsWith('@PARAGRAPH')) {
215
+ console.error('@PARAGRAPH annotation was not processed. Hiding custom instructions.');
216
+ return undefined;
217
+ }
218
+
219
+ // Custom instructions from the view
220
+ // The raw metadata for `instructions` will be something like '@PARAGRAPH .SomeParagraphRule' but
221
+ // it is evaluated by core logic to the content
222
+ if (instructions !== 'casestep' && instructions !== 'none') {
223
+ return instructions;
224
+ }
225
+ return undefined;
226
+ }
@@ -2,6 +2,6 @@ import styled, { css } from 'styled-components';
2
2
 
3
3
  export default styled.div(() => {
4
4
  return css`
5
- margin: 8px 0;
5
+ margin: 0px 0;
6
6
  `;
7
7
  });
@@ -2,6 +2,6 @@ import styled, { css } from 'styled-components';
2
2
 
3
3
  export default styled.div(() => {
4
4
  return css`
5
- margin: 8px 0;
5
+ margin: 0px 0;
6
6
  `;
7
7
  });
@@ -2,6 +2,6 @@ import styled, { css } from 'styled-components';
2
2
 
3
3
  export default styled.div(() => {
4
4
  return css`
5
- margin: 8px 0;
5
+ margin: 0px 0;
6
6
  `;
7
7
  });
@@ -2,6 +2,6 @@ import styled, { css } from 'styled-components';
2
2
 
3
3
  export default styled.div(() => {
4
4
  return css`
5
- margin: 8px 0;
5
+ margin: 0px 0;
6
6
  `;
7
7
  });
@@ -52,7 +52,7 @@
52
52
  },
53
53
  "devDependencies": {
54
54
  "@pega/configs": "^0.3.4",
55
- "@pega/custom-dx-components": "^2.0.9",
55
+ "@pega/custom-dx-components": "^2.0.11",
56
56
  "@storybook/addon-actions": "^6.5.12",
57
57
  "@storybook/addon-essentials": "^6.5.12",
58
58
  "@storybook/addon-links": "^6.5.12",
@@ -306,19 +306,19 @@ export default (
306
306
  contextRegExp: /moment$/
307
307
  }),
308
308
  /* Ignore cosmos icons */
309
- /*
309
+
310
310
  new webpack.IgnorePlugin({
311
311
  resourceRegExp: /^\.\/icons$/,
312
312
  contextRegExp: /@pega\/cosmos-react-core\/lib\/components\/Icon$/
313
313
  }),
314
- */
314
+
315
315
  /* Ignore cosmos icons - windows */
316
- /*
316
+
317
317
  new webpack.IgnorePlugin({
318
318
  resourceRegExp: /^\.\/icons$/,
319
319
  contextRegExp: /@pega\\cosmos-react-core\\lib\\components\\Icon$/
320
320
  }),
321
- */
321
+
322
322
  new webpack.optimize.LimitChunkCountPlugin({
323
323
  maxChunks: 5
324
324
  }),