Expose schema context from grid and allow usage in buttons
This commit is contained in:
parent
9f285b32a7
commit
c25b8a1448
|
@ -21,7 +21,8 @@
|
||||||
$: schemaComponents = getContextProviderComponents(
|
$: schemaComponents = getContextProviderComponents(
|
||||||
$currentAsset,
|
$currentAsset,
|
||||||
$store.selectedComponentId,
|
$store.selectedComponentId,
|
||||||
"schema"
|
"schema",
|
||||||
|
{ includeSelf: nested }
|
||||||
)
|
)
|
||||||
$: providerOptions = getProviderOptions(formComponents, schemaComponents)
|
$: providerOptions = getProviderOptions(formComponents, schemaComponents)
|
||||||
$: schemaFields = getSchemaFields(parameters?.tableId)
|
$: schemaFields = getSchemaFields(parameters?.tableId)
|
||||||
|
|
|
@ -11,6 +11,7 @@
|
||||||
export let bindings
|
export let bindings
|
||||||
export let value
|
export let value
|
||||||
export let key
|
export let key
|
||||||
|
export let nested
|
||||||
|
|
||||||
const dispatch = createEventDispatcher()
|
const dispatch = createEventDispatcher()
|
||||||
|
|
||||||
|
@ -28,6 +29,7 @@
|
||||||
bindings: allBindings,
|
bindings: allBindings,
|
||||||
removeButton,
|
removeButton,
|
||||||
canRemove: true,
|
canRemove: true,
|
||||||
|
nested,
|
||||||
}
|
}
|
||||||
|
|
||||||
const sanitizeValue = val => {
|
const sanitizeValue = val => {
|
||||||
|
|
|
@ -10,10 +10,27 @@
|
||||||
export let anchor
|
export let anchor
|
||||||
export let removeButton
|
export let removeButton
|
||||||
export let canRemove
|
export let canRemove
|
||||||
|
export let nested
|
||||||
|
|
||||||
$: readableText = isJSBinding(item.text)
|
$: readableText = isJSBinding(item.text)
|
||||||
? "(JavaScript function)"
|
? "(JavaScript function)"
|
||||||
: runtimeToReadableBinding([...bindings, componentBindings], item.text)
|
: runtimeToReadableBinding([...bindings, componentBindings], item.text)
|
||||||
|
|
||||||
|
// If this is a nested setting (for example inside a grid or form block) then
|
||||||
|
// we need to mark all the settings of the actual buttons as nested too, to
|
||||||
|
// allow up to reference context provided by the block
|
||||||
|
const updatedNestedFlags = settings => {
|
||||||
|
if (!nested) {
|
||||||
|
return settings
|
||||||
|
}
|
||||||
|
// Buttons do not currently have any sections, so this works.
|
||||||
|
// We will need to update this in future if the normal button component
|
||||||
|
// gets broken into multiple settings sections
|
||||||
|
return settings?.map(setting => ({
|
||||||
|
...setting,
|
||||||
|
nested: true,
|
||||||
|
}))
|
||||||
|
}
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<div class="list-item-body">
|
<div class="list-item-body">
|
||||||
|
@ -24,6 +41,7 @@
|
||||||
{componentBindings}
|
{componentBindings}
|
||||||
{bindings}
|
{bindings}
|
||||||
on:change
|
on:change
|
||||||
|
parseSettings={updatedNestedFlags}
|
||||||
/>
|
/>
|
||||||
<div class="field-label">{readableText || "Button"}</div>
|
<div class="field-label">{readableText || "Button"}</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
|
@ -6357,7 +6357,10 @@
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
]
|
],
|
||||||
|
"context": {
|
||||||
|
"type": "schema"
|
||||||
|
}
|
||||||
},
|
},
|
||||||
"bbreferencefield": {
|
"bbreferencefield": {
|
||||||
"devComment": "As bb reference is only used for user subtype for now, we are using user for icon and labels",
|
"devComment": "As bb reference is only used for user subtype for now, we are using user for icon and labels",
|
||||||
|
|
|
@ -53,7 +53,11 @@
|
||||||
text: settings.text,
|
text: settings.text,
|
||||||
type: settings.type,
|
type: settings.type,
|
||||||
onClick: async row => {
|
onClick: async row => {
|
||||||
const fn = enrichButtonActions(settings.onClick, get(context))
|
// We add a fake context binding in here, which allows us to pretend
|
||||||
|
// that the grid provides a "schema" binding - that lets us use the
|
||||||
|
// clicked row in things like save row actions
|
||||||
|
const enrichedContext = { ...get(context), [$component.id]: row }
|
||||||
|
const fn = enrichButtonActions(settings.onClick, enrichedContext)
|
||||||
return await fn?.({ row })
|
return await fn?.({ row })
|
||||||
},
|
},
|
||||||
}))
|
}))
|
||||||
|
|
|
@ -9,6 +9,7 @@
|
||||||
hoveredRowId,
|
hoveredRowId,
|
||||||
props,
|
props,
|
||||||
width,
|
width,
|
||||||
|
rows,
|
||||||
focusedRow,
|
focusedRow,
|
||||||
selectedRows,
|
selectedRows,
|
||||||
visibleColumns,
|
visibleColumns,
|
||||||
|
@ -27,6 +28,12 @@
|
||||||
$: end = columnsWidth - 1 - $scroll.left
|
$: end = columnsWidth - 1 - $scroll.left
|
||||||
$: left = Math.min($width - $buttonColumnWidth, end)
|
$: left = Math.min($width - $buttonColumnWidth, end)
|
||||||
|
|
||||||
|
const handleClick = async (button, row) => {
|
||||||
|
await button.onClick?.(row)
|
||||||
|
// Refresh the row in case it changed
|
||||||
|
await rows.actions.refreshRow(row._id)
|
||||||
|
}
|
||||||
|
|
||||||
onMount(() => {
|
onMount(() => {
|
||||||
const observer = new ResizeObserver(entries => {
|
const observer = new ResizeObserver(entries => {
|
||||||
const width = entries?.[0]?.contentRect?.width ?? 0
|
const width = entries?.[0]?.contentRect?.width ?? 0
|
||||||
|
@ -81,7 +88,7 @@
|
||||||
secondary={button.type === "secondary"}
|
secondary={button.type === "secondary"}
|
||||||
warning={button.type === "warning"}
|
warning={button.type === "warning"}
|
||||||
overBackground={button.type === "overBackground"}
|
overBackground={button.type === "overBackground"}
|
||||||
on:click={() => button.onClick?.(row)}
|
on:click={() => handleClick(button, row)}
|
||||||
>
|
>
|
||||||
{button.text || "Button"}
|
{button.text || "Button"}
|
||||||
</Button>
|
</Button>
|
||||||
|
|
|
@ -314,8 +314,12 @@ export const createActions = context => {
|
||||||
|
|
||||||
// Refreshes a specific row
|
// Refreshes a specific row
|
||||||
const refreshRow = async id => {
|
const refreshRow = async id => {
|
||||||
const row = await datasource.actions.getRow(id)
|
try {
|
||||||
replaceRow(id, row)
|
const row = await datasource.actions.getRow(id)
|
||||||
|
replaceRow(id, row)
|
||||||
|
} catch {
|
||||||
|
// Do nothing - we probably just don't support refreshing individual rows
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Refreshes all data
|
// Refreshes all data
|
||||||
|
|
Loading…
Reference in New Issue