diff --git a/JSON_EXPRESSION_SYSTEM.md b/JSON_EXPRESSION_SYSTEM.md index 40dabfe..a32aad4 100644 --- a/JSON_EXPRESSION_SYSTEM.md +++ b/JSON_EXPRESSION_SYSTEM.md @@ -154,6 +154,18 @@ Remove an item from an array. } ``` +Example with the selector derived from event data: + +```json +{ + "id": "remove-clicked", + "type": "delete", + "target": "todos", + "path": "meta.id", + "expression": "event.todoId ?? data.selectedId" +} +``` + ## Common Patterns ### 1. Input Field Updates diff --git a/src/hooks/ui/use-action-executor.ts b/src/hooks/ui/use-action-executor.ts index fd8364f..589233d 100644 --- a/src/hooks/ui/use-action-executor.ts +++ b/src/hooks/ui/use-action-executor.ts @@ -98,13 +98,27 @@ export function useActionExecutor(context: JSONUIContext) { } case 'delete': { - if (!action.target || !action.value) return + if (!action.target) return const currentData = data[action.target] || [] + + let selectorValue + if (action.compute) { + selectorValue = action.compute(data, event) + } else if (action.expression) { + selectorValue = evaluateExpression(action.expression, evaluationContext) + } else if (action.valueTemplate) { + selectorValue = evaluateTemplate(action.valueTemplate, evaluationContext) + } else { + selectorValue = action.value + } + + if (selectorValue === undefined) return + const filtered = currentData.filter((item: any) => { if (action.path) { - return item[action.path] !== action.value + return getNestedValue(item, action.path) !== selectorValue } - return item !== action.value + return item !== selectorValue }) updateData(action.target, filtered) break