From 39e5385925e95c5b1eab33732a81afe940989f44 Mon Sep 17 00:00:00 2001 From: johndoe6345789 Date: Sun, 18 Jan 2026 13:36:49 +0000 Subject: [PATCH] Extend delete action selector resolution --- JSON_EXPRESSION_SYSTEM.md | 12 ++++++++++++ src/hooks/ui/use-action-executor.ts | 21 ++++++++++++++++++--- 2 files changed, 30 insertions(+), 3 deletions(-) diff --git a/JSON_EXPRESSION_SYSTEM.md b/JSON_EXPRESSION_SYSTEM.md index 8fd9d9a..1024cf6 100644 --- a/JSON_EXPRESSION_SYSTEM.md +++ b/JSON_EXPRESSION_SYSTEM.md @@ -140,6 +140,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 528a5c8..047db63 100644 --- a/src/hooks/ui/use-action-executor.ts +++ b/src/hooks/ui/use-action-executor.ts @@ -2,6 +2,7 @@ import { useCallback } from 'react' import { toast } from 'sonner' import { Action, JSONUIContext } from '@/types/json-ui' import { evaluateExpression, evaluateTemplate } from '@/lib/json-ui/expression-evaluator' +import { getNestedValue } from '@/lib/json-ui/utils' export function useActionExecutor(context: JSONUIContext) { const { data, updateData, executeAction: contextExecute } = context @@ -53,13 +54,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