Skip to content

Commit 158a443

Browse files
TASK-1884601 - code review improvements (#129)
1 parent 2f89f47 commit 158a443

File tree

3 files changed

+107
-77
lines changed

3 files changed

+107
-77
lines changed

scripts/dxcomponents/components/containers/templates/data-reference.component.js

Lines changed: 42 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,12 @@ export class DataReferenceComponent extends ContainerBaseComponent {
4242
this.children = this.pConn.getChildren();
4343
this.#updateSelf();
4444

45-
if ((['Dropdown', 'Checkbox'].includes(this.firstChildMeta?.type)) &&
45+
const shouldPreloadOptions =
46+
(['Dropdown', 'Checkbox'].includes(this.firstChildMeta?.type)) &&
4647
this.rawViewMetadata.config?.parameters &&
47-
!this.firstChildMeta.config.deferDatasource) {
48+
!this.firstChildMeta.config.deferDatasource;
49+
50+
if (shouldPreloadOptions) {
4851
const { value, key, text } = this.firstChildMeta.config.datasource.fields;
4952
PCore.getDataApiUtils()
5053
.getData(this.refList, { dataViewParameters: this.parameters }, "")
@@ -119,7 +122,6 @@ export class DataReferenceComponent extends ContainerBaseComponent {
119122
delete this.firstChildMeta.config.readOnly;
120123
}
121124

122-
// todo pelcm add readonly handling
123125
this.#setChildDatasource();
124126

125127
if (this.firstChildMeta?.type === "Dropdown" && !this.firstChildMeta.config.deferDatasource) {
@@ -191,19 +193,38 @@ export class DataReferenceComponent extends ContainerBaseComponent {
191193

192194

193195
#setChildDatasource() {
194-
const { type, config } = this.firstChildMeta;
196+
const { type } = this.firstChildMeta;
195197

196-
if (['Dropdown', 'Checkbox'].includes(type) && !config.deferDatasource && config.datasource) {
197-
const hasParameters = this.rawViewMetadata.config?.parameters;
198-
config.datasource.source = hasParameters ? this.dropDownDataSource : `@DATASOURCE ${this.refList}.pxResults`;
199-
} else if (type === 'AutoComplete') {
200-
config.datasource = this.refList;
201-
if (this.rawViewMetadata.config?.parameters) {
202-
config.parameters = this.parameters;
203-
}
198+
if (type === 'AutoComplete') {
199+
this.#setAutoCompleteDatasource();
200+
} else if (['Dropdown', 'Checkbox'].includes(type)) {
201+
this.#setDropdownOrCheckboxDatasource();
202+
}
203+
}
204+
205+
#setAutoCompleteDatasource() {
206+
const { config } = this.firstChildMeta;
207+
config.datasource = this.refList;
208+
209+
const hasParameters = this.rawViewMetadata.config?.parameters;
210+
if (hasParameters) {
211+
config.parameters = this.parameters;
204212
}
205213
}
206214

215+
#setDropdownOrCheckboxDatasource() {
216+
const { config } = this.firstChildMeta;
217+
218+
if (!config.datasource || config.deferDatasource) {
219+
return;
220+
}
221+
222+
const hasParameters = this.rawViewMetadata.config?.parameters;
223+
config.datasource.source = hasParameters
224+
? this.dropDownDataSource
225+
: `@DATASOURCE ${this.refList}.pxResults`;
226+
}
227+
207228
// Re-create first child with overridden props
208229
// Memoized child in order to stop unmount and remount of the child component when data reference
209230
// rerenders without any actual change
@@ -217,17 +238,6 @@ export class DataReferenceComponent extends ContainerBaseComponent {
217238
});
218239

219240
this.#setReadOnlyDisplayFlags();
220-
// if (
221-
// !this.canBeChangedInReviewMode &&
222-
// this.isDisplayModeEnabled &&
223-
// this.selectionMode === SELECTION_MODE.SINGLE
224-
// ) {
225-
// this.displaySingleRef = true;
226-
// }
227-
//
228-
// if (this.isDisplayModeEnabled && this.selectionMode === SELECTION_MODE.MULTI) {
229-
// this.displayMultiRef = true;
230-
// }
231241

232242
// In the case of a datasource with parameters you cannot load the dropdown before the parameters
233243
if (type === "Dropdown" && this.rawViewMetadata.config?.parameters && this.dropDownDataSource === null) {
@@ -262,11 +272,18 @@ export class DataReferenceComponent extends ContainerBaseComponent {
262272
const isSingleMode = this.selectionMode === SELECTION_MODE.SINGLE;
263273
const isMultiMode = this.selectionMode === SELECTION_MODE.MULTI;
264274

265-
if (isSingleMode && (this.displayAs === 'readonly' || this.isDisplayModeEnabled) && !this.canBeChangedInReviewMode) {
275+
const shouldDisplayOnlySingle = isSingleMode &&
276+
(this.displayAs === 'readonly' || this.isDisplayModeEnabled) &&
277+
!this.canBeChangedInReviewMode;
278+
279+
if (shouldDisplayOnlySingle) {
266280
this.props.displayOnlySingle = true;
267281
}
268282

269-
if (isMultiMode && (['readonly', 'readonlyMulti', 'map'].includes(this.displayAs) || this.isDisplayModeEnabled)) {
283+
const shouldDisplayOnlyMulti = isMultiMode &&
284+
(['readonly', 'readonlyMulti', 'map'].includes(this.displayAs) || this.isDisplayModeEnabled);
285+
286+
if (shouldDisplayOnlyMulti) {
270287
this.props.displayOnlyMulti = true;
271288
}
272289
}

scripts/dxcomponents/components/fields/check-box.component.js

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -38,31 +38,41 @@ export class CheckBoxComponent extends FieldBaseComponent {
3838

3939
this.selectionMode = configProps.selectionMode;
4040
this.props.selectionMode = this.selectionMode;
41+
4142
if (this.selectionMode === MULTI_MODE) {
42-
this.referenceList = configProps.referenceList;
43-
this.selectionList = configProps.selectionList;
44-
this.selectedValues = configProps.readonlyContextList;
45-
this.primaryField = configProps.primaryField;
46-
this.props.readOnly = configProps.renderMode === 'ReadOnly' || configProps.displayMode === 'DISPLAY_ONLY' || configProps.readOnly;
47-
48-
this.datasource = configProps.datasource;
49-
this.selectionKey = configProps.selectionKey;
50-
this.checkboxGroupItems = this.datasource?.source || [];
51-
const dataField = this.selectionKey?.split?.('.')[1] ?? '';
52-
this.checkboxGroupItems.forEach(element => {
53-
element.selected = this.selectedValues?.some?.(data => data[dataField] === element.key);
54-
});
55-
this.props.items = this.checkboxGroupItems;
43+
this.#updateMultiModeProps(configProps);
5644
} else {
57-
this.props.hideLabel = configProps.hideLabel ?? false;
58-
this.props.caption = configProps.caption ?? "";
59-
this.props.trueLabel = configProps.trueLabel ?? "Yes";
60-
this.props.falseLabel = configProps.falseLabel ?? "No";
61-
this.propName = this.pConn.getStateProps().value;
45+
this.#updateSingleModeProps(configProps);
6246
}
47+
6348
this.componentsManager.onComponentPropsUpdate(this);
6449
}
6550

51+
#updateMultiModeProps(configProps) {
52+
this.referenceList = configProps.referenceList;
53+
this.selectionList = configProps.selectionList;
54+
this.selectedValues = configProps.readonlyContextList;
55+
this.primaryField = configProps.primaryField;
56+
this.props.readOnly = configProps.renderMode === 'ReadOnly' || configProps.displayMode === 'DISPLAY_ONLY' || configProps.readOnly;
57+
58+
this.datasource = configProps.datasource;
59+
this.selectionKey = configProps.selectionKey;
60+
this.checkboxGroupItems = this.datasource?.source || [];
61+
const dataField = this.selectionKey?.split?.('.')[1] ?? '';
62+
this.checkboxGroupItems.forEach(element => {
63+
element.selected = this.selectedValues?.some?.(data => data[dataField] === element.key);
64+
});
65+
this.props.items = this.checkboxGroupItems;
66+
}
67+
68+
#updateSingleModeProps(configProps) {
69+
this.props.hideLabel = configProps.hideLabel ?? false;
70+
this.props.caption = configProps.caption ?? "";
71+
this.props.trueLabel = configProps.trueLabel ?? "Yes";
72+
this.props.falseLabel = configProps.falseLabel ?? "No";
73+
this.propName = this.pConn.getStateProps().value;
74+
}
75+
6676
fieldOnChange(value, event) {
6777
this.props.value = value;
6878
handleEvent(this.pConn.getActionsApi(), "changeNblur", this.propName, this.#isChecked());

ui-renderer-cmp/src/commonMain/kotlin/com/pega/constellation/sdk/kmp/ui/renderer/cmp/fields/CheckboxRenderer.kt

Lines changed: 36 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -18,39 +18,42 @@ class CheckboxRenderer : ComponentRenderer<CheckboxComponent> {
1818
FieldValue(label, displayValue)
1919
},
2020
editable = {
21-
if (selectionMode == CheckboxComponent.SelectionMode.SINGLE) {
22-
Checkbox(
23-
value = value.toBoolean(),
24-
caption = caption,
25-
label = label,
26-
helperText = helperText,
27-
validateMessage = validateMessage,
28-
hideLabel = hideLabel,
29-
required = required,
30-
disabled = disabled,
31-
readOnly = readOnly,
32-
onValueChange = { updateValue(it.toString()) },
33-
testTag = "checkbox_[$caption]"
34-
)
35-
} else {
36-
CheckboxGroup(
37-
options = checkboxGroupOptions.map {
38-
MultiSelectOption(
39-
it.key,
40-
it.text,
41-
it.selected
42-
)
43-
},
44-
label = label,
45-
helperText = helperText,
46-
validateMessage = validateMessage,
47-
hideLabel = hideLabel,
48-
required = required,
49-
disabled = disabled,
50-
readOnly = readOnly,
51-
onOptionClick = { id, selected -> onOptionClick(id, selected) },
52-
testTag = "checkbox_group_[$label]"
53-
)
21+
when (selectionMode) {
22+
CheckboxComponent.SelectionMode.SINGLE -> {
23+
Checkbox(
24+
value = value.toBoolean(),
25+
caption = caption,
26+
label = label,
27+
helperText = helperText,
28+
validateMessage = validateMessage,
29+
hideLabel = hideLabel,
30+
required = required,
31+
disabled = disabled,
32+
readOnly = readOnly,
33+
onValueChange = { updateValue(it.toString()) },
34+
testTag = "checkbox_[$caption]"
35+
)
36+
}
37+
CheckboxComponent.SelectionMode.MULTI -> {
38+
CheckboxGroup(
39+
options = checkboxGroupOptions.map {
40+
MultiSelectOption(
41+
it.key,
42+
it.text,
43+
it.selected
44+
)
45+
},
46+
label = label,
47+
helperText = helperText,
48+
validateMessage = validateMessage,
49+
hideLabel = hideLabel,
50+
required = required,
51+
disabled = disabled,
52+
readOnly = readOnly,
53+
onOptionClick = { id, selected -> onOptionClick(id, selected) },
54+
testTag = "checkbox_group_[$label]"
55+
)
56+
}
5457
}
5558
}
5659
)

0 commit comments

Comments
 (0)