2021-01-26 09:55:44 +01:00
|
|
|
<script>
|
2021-06-10 12:17:14 +02:00
|
|
|
import { getContext } from "svelte"
|
|
|
|
import InnerForm from "./InnerForm.svelte"
|
2021-01-26 09:55:44 +01:00
|
|
|
|
2021-03-19 14:09:22 +01:00
|
|
|
export let dataSource
|
2021-01-26 09:55:44 +01:00
|
|
|
export let theme
|
|
|
|
export let size
|
2021-02-17 16:16:44 +01:00
|
|
|
export let disabled = false
|
2021-06-09 13:55:17 +02:00
|
|
|
export let actionType = "Create"
|
2021-01-26 09:55:44 +01:00
|
|
|
|
2021-02-01 19:51:22 +01:00
|
|
|
const context = getContext("context")
|
2021-01-26 09:55:44 +01:00
|
|
|
|
2021-02-16 16:30:20 +01:00
|
|
|
// Returns the closes data context which isn't a built in context
|
2021-06-09 13:53:12 +02:00
|
|
|
const getInitialValues = (type, dataSource, context) => {
|
|
|
|
// Only inherit values for update forms
|
|
|
|
if (type !== "Update") {
|
|
|
|
return {}
|
|
|
|
}
|
2021-06-25 13:15:45 +02:00
|
|
|
// Only inherit values for forms targeting internal tables
|
2021-06-09 13:53:12 +02:00
|
|
|
if (!dataSource?.tableId) {
|
|
|
|
return {}
|
|
|
|
}
|
|
|
|
// Don't inherit values representing built in contexts
|
2021-02-16 16:30:20 +01:00
|
|
|
if (["user", "url"].includes(context.closestComponentId)) {
|
|
|
|
return {}
|
|
|
|
}
|
2021-06-09 13:53:12 +02:00
|
|
|
// Only inherit values if the table ID matches
|
|
|
|
const closestContext = context[`${context.closestComponentId}`] || {}
|
|
|
|
if (dataSource.tableId !== closestContext?.tableId) {
|
|
|
|
return {}
|
|
|
|
}
|
|
|
|
return closestContext
|
2021-02-16 16:30:20 +01:00
|
|
|
}
|
|
|
|
|
2021-06-10 12:17:14 +02:00
|
|
|
$: initialValues = getInitialValues(actionType, dataSource, $context)
|
|
|
|
$: resetKey = JSON.stringify(initialValues)
|
2021-01-26 09:55:44 +01:00
|
|
|
</script>
|
|
|
|
|
2021-06-10 12:17:14 +02:00
|
|
|
{#key resetKey}
|
|
|
|
<InnerForm
|
|
|
|
{dataSource}
|
|
|
|
{theme}
|
|
|
|
{size}
|
|
|
|
{disabled}
|
|
|
|
{actionType}
|
|
|
|
{initialValues}
|
2021-05-04 12:04:42 +02:00
|
|
|
>
|
2021-06-10 12:17:14 +02:00
|
|
|
<slot />
|
|
|
|
</InnerForm>
|
|
|
|
{/key}
|