|
|
|
@ -8,6 +8,7 @@ |
|
|
|
export let theme |
|
|
|
export let size |
|
|
|
export let disabled = false |
|
|
|
export let type = "Create" |
|
|
|
|
|
|
|
const component = getContext("component") |
|
|
|
const context = getContext("context") |
|
|
|
@ -19,15 +20,29 @@ |
|
|
|
let fieldMap = {} |
|
|
|
|
|
|
|
// Returns the closes data context which isn't a built in context |
|
|
|
const getInitialValues = context => { |
|
|
|
const getInitialValues = (type, dataSource, context) => { |
|
|
|
// Only inherit values for update forms |
|
|
|
if (type !== "Update") { |
|
|
|
return {} |
|
|
|
} |
|
|
|
// Only inherit values for forms targetting internal tables |
|
|
|
if (!dataSource?.tableId) { |
|
|
|
return {} |
|
|
|
} |
|
|
|
// Don't inherit values representing built in contexts |
|
|
|
if (["user", "url"].includes(context.closestComponentId)) { |
|
|
|
return {} |
|
|
|
} |
|
|
|
return context[`${context.closestComponentId}`] || {} |
|
|
|
// Only inherit values if the table ID matches |
|
|
|
const closestContext = context[`${context.closestComponentId}`] || {} |
|
|
|
if (dataSource.tableId !== closestContext?.tableId) { |
|
|
|
return {} |
|
|
|
} |
|
|
|
return closestContext |
|
|
|
} |
|
|
|
|
|
|
|
// Use the closest data context as the initial form values |
|
|
|
const initialValues = getInitialValues($context) |
|
|
|
const initialValues = getInitialValues(type, dataSource, $context) |
|
|
|
|
|
|
|
// Form state contains observable data about the form |
|
|
|
const formState = writable({ values: initialValues, errors: {}, valid: true }) |
|
|
|
@ -46,6 +61,7 @@ |
|
|
|
const constraints = schema?.[field]?.constraints |
|
|
|
const validate = createValidatorFromConstraints(constraints, field, table) |
|
|
|
|
|
|
|
// Construct field object |
|
|
|
fieldMap[field] = { |
|
|
|
fieldState: makeFieldState( |
|
|
|
field, |
|
|
|
@ -57,7 +73,14 @@ |
|
|
|
} |
|
|
|
|
|
|
|
// Set initial value |
|
|
|
fieldMap[field].fieldApi.setValue(defaultValue, true) |
|
|
|
const initialValue = get(fieldMap[field].fieldState).value |
|
|
|
formState.update(state => ({ |
|
|
|
...state, |
|
|
|
values: { |
|
|
|
...state.values, |
|
|
|
[field]: initialValue, |
|
|
|
}, |
|
|
|
})) |
|
|
|
|
|
|
|
return fieldMap[field] |
|
|
|
}, |
|
|
|
|