Add type setting to forms and fix inheriting initial values
This commit is contained in:
parent
7f468c0701
commit
d47b871e9e
|
@ -1023,6 +1023,13 @@
|
||||||
"ValidateForm"
|
"ValidateForm"
|
||||||
],
|
],
|
||||||
"settings": [
|
"settings": [
|
||||||
|
{
|
||||||
|
"type": "select",
|
||||||
|
"label": "Type",
|
||||||
|
"key": "type",
|
||||||
|
"options": ["Create", "Update"],
|
||||||
|
"defaultValue": "Create"
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"type": "schema",
|
"type": "schema",
|
||||||
"label": "Schema",
|
"label": "Schema",
|
||||||
|
|
|
@ -8,6 +8,7 @@
|
||||||
export let theme
|
export let theme
|
||||||
export let size
|
export let size
|
||||||
export let disabled = false
|
export let disabled = false
|
||||||
|
export let type = "Create"
|
||||||
|
|
||||||
const component = getContext("component")
|
const component = getContext("component")
|
||||||
const context = getContext("context")
|
const context = getContext("context")
|
||||||
|
@ -19,15 +20,29 @@
|
||||||
let fieldMap = {}
|
let fieldMap = {}
|
||||||
|
|
||||||
// Returns the closes data context which isn't a built in context
|
// 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)) {
|
if (["user", "url"].includes(context.closestComponentId)) {
|
||||||
return {}
|
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
|
// 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
|
// Form state contains observable data about the form
|
||||||
const formState = writable({ values: initialValues, errors: {}, valid: true })
|
const formState = writable({ values: initialValues, errors: {}, valid: true })
|
||||||
|
@ -46,6 +61,7 @@
|
||||||
const constraints = schema?.[field]?.constraints
|
const constraints = schema?.[field]?.constraints
|
||||||
const validate = createValidatorFromConstraints(constraints, field, table)
|
const validate = createValidatorFromConstraints(constraints, field, table)
|
||||||
|
|
||||||
|
// Construct field object
|
||||||
fieldMap[field] = {
|
fieldMap[field] = {
|
||||||
fieldState: makeFieldState(
|
fieldState: makeFieldState(
|
||||||
field,
|
field,
|
||||||
|
@ -57,7 +73,14 @@
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set initial value
|
// 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]
|
return fieldMap[field]
|
||||||
},
|
},
|
||||||
|
|
Loading…
Reference in New Issue