Extend delete action selector resolution

This commit is contained in:
2026-01-18 13:36:49 +00:00
parent 9ea7c15f5d
commit 39e5385925
2 changed files with 30 additions and 3 deletions

View File

@@ -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

View File

@@ -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