Skip to content

Commit ee0d047

Browse files
TASK-1786159: add support for add/remove/reorder rows
1 parent d07d97b commit ee0d047

File tree

8 files changed

+364
-145
lines changed

8 files changed

+364
-145
lines changed

core/src/commonMain/kotlin/com/pega/constellation/sdk/kmp/core/components/containers/SimpleTableManual.kt

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ import androidx.compose.runtime.setValue
66
import com.pega.constellation.sdk.kmp.core.api.BaseComponent
77
import com.pega.constellation.sdk.kmp.core.api.Component
88
import com.pega.constellation.sdk.kmp.core.api.ComponentContext
9+
import com.pega.constellation.sdk.kmp.core.api.ComponentEvent
910
import com.pega.constellation.sdk.kmp.core.api.ComponentId
1011
import com.pega.constellation.sdk.kmp.core.components.getBoolean
11-
import com.pega.constellation.sdk.kmp.core.components.getInt
1212
import com.pega.constellation.sdk.kmp.core.components.getJSONArray
1313
import com.pega.constellation.sdk.kmp.core.components.getString
1414
import kotlinx.serialization.json.JsonObject
@@ -21,6 +21,12 @@ class SimpleTableManualComponent(context: ComponentContext) : BaseComponent(cont
2121
private set
2222
var displayMode by mutableStateOf(DisplayMode.DISPLAY_ONLY)
2323
private set
24+
var allowAddRows by mutableStateOf(true)
25+
private set
26+
var allowReorderRows by mutableStateOf(true)
27+
private set
28+
var addButtonLabel by mutableStateOf("")
29+
private set
2430
var columnNames by mutableStateOf(emptyList<String>())
2531
private set
2632
var rows by mutableStateOf(emptyList<Row>())
@@ -30,6 +36,9 @@ class SimpleTableManualComponent(context: ComponentContext) : BaseComponent(cont
3036
visible = props.getBoolean("visible")
3137
label = props.getString("label")
3238
displayMode = DisplayMode.valueOf(props.getString("displayMode"))
39+
allowAddRows = props.getBoolean("allowAddRows")
40+
allowReorderRows = props.getBoolean("allowReorderRows")
41+
addButtonLabel = props.getString("addButtonLabel")
3342
columnNames = getColumnNames(props)
3443
rows = getRows(props)
3544
}
@@ -46,10 +55,43 @@ class SimpleTableManualComponent(context: ComponentContext) : BaseComponent(cont
4655
val componentIds = rowJsonObject.getJSONArray("cellComponentIds")
4756
val ids = componentIds.mapWithIndex { getString(it).toInt() }
4857
val cellComponents = context.componentManager.getComponents(ids.map { ComponentId(it) })
49-
Row(rowJsonObject.getInt("id"), cellComponents.map { Cell(it) })
58+
Row(
59+
cells = cellComponents.map { Cell(it) },
60+
showEditButton = rowJsonObject.getBoolean("showEditButton"),
61+
showDeleteButton = rowJsonObject.getBoolean("showDeleteButton")
62+
)
5063
}
5164

52-
data class Row(val id: Int, val cells: List<Cell>)
65+
fun addRow() = context.sendComponentEvent(ComponentEvent.simpleTableManualAddRow())
66+
67+
fun deleteRow(rowId: Int) =
68+
context.sendComponentEvent(ComponentEvent.simpleTableManualDeleteRow(rowId))
69+
70+
fun reorderRow(fromIndex: Int, toIndex: Int) =
71+
context.sendComponentEvent(ComponentEvent.simpleTableManualReorderRow(fromIndex, toIndex))
72+
73+
private fun ComponentEvent.Companion.simpleTableManualAddRow() =
74+
ComponentEvent("SimpleTableManualEvent", eventData = mapOf("type" to "addRow"))
75+
76+
private fun ComponentEvent.Companion.simpleTableManualDeleteRow(itemId: Int) =
77+
ComponentEvent(
78+
"SimpleTableManualEvent",
79+
eventData = mapOf("type" to "deleteRow", "rowId" to itemId.toString())
80+
)
81+
82+
private fun ComponentEvent.Companion.simpleTableManualReorderRow(fromIndex: Int, toIndex: Int) =
83+
ComponentEvent(
84+
"SimpleTableManualEvent", eventData = mapOf(
85+
"type" to "reorderRow",
86+
"fromIndex" to fromIndex.toString(), "toIndex" to toIndex.toString()
87+
)
88+
)
89+
90+
data class Row(
91+
val cells: List<Cell>,
92+
val showEditButton: Boolean,
93+
val showDeleteButton: Boolean
94+
)
5395

5496
data class Cell(val component: Component)
5597

scripts/dxcomponents/components/containers/templates/field-group-template.component.js

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Utils } from "../../../helpers/utils.js";
22
import { BaseComponent } from "../../base.component.js";
3-
import { getReferenceList } from "./template-utils.js";
3+
import {evaluateAllowRowAction, getReferenceList} from "./template-utils.js";
44

55
export class FieldGroupTemplateComponent extends BaseComponent {
66
jsComponentPConnectData = {};
@@ -97,7 +97,7 @@ export class FieldGroupTemplateComponent extends BaseComponent {
9797
this.pConn,
9898
index,
9999
lookForChildInConfig,
100-
this.evaluateAllowRowAction(allowRowEdit, item)
100+
evaluateAllowRowAction(allowRowEdit, item)
101101
).getPConnect();
102102
const newComponent = this.componentsManager.upsert(oldComponent, newPConn.meta.type, [newPConn]);
103103
updatedItems.push({
@@ -107,7 +107,7 @@ export class FieldGroupTemplateComponent extends BaseComponent {
107107
? this.getDynamicHeader(item, index)
108108
: this.getStaticHeader(this.heading, index),
109109
component: newComponent,
110-
allowDelete: this.allowedActions.delete && this.evaluateAllowRowAction(allowRowDelete, item),
110+
allowDelete: this.allowedActions.delete && evaluateAllowRowAction(allowRowDelete, item),
111111
});
112112
});
113113
this.items = updatedItems;
@@ -144,7 +144,7 @@ export class FieldGroupTemplateComponent extends BaseComponent {
144144
return; // no-op
145145
}
146146

147-
if (event.type == "FieldGroupTemplateEvent") {
147+
if (event.type === "FieldGroupTemplateEvent") {
148148
switch (event.eventData?.type) {
149149
case "addItem":
150150
this.addFieldGroupItem();
@@ -236,15 +236,6 @@ export class FieldGroupTemplateComponent extends BaseComponent {
236236
return pConnect;
237237
}
238238

239-
evaluateAllowRowAction(rawExpression, rowData) {
240-
if (rawExpression === undefined || rawExpression === true) return true;
241-
if (rawExpression.startsWith?.("@E ")) {
242-
const expression = rawExpression.replace("@E ", "");
243-
return PCore.getExpressionEngine().evaluate(expression, rowData);
244-
}
245-
return false;
246-
}
247-
248239
destroyItems() {
249240
this.items.forEach((item) => {
250241
item.component.destroy?.();

0 commit comments

Comments
 (0)