Automatically make forms use latest data context if it matches

This commit is contained in:
Andrew Kingston 2021-01-29 16:40:19 +00:00
parent 8be1d1e530
commit 4e9fe01ef6
2 changed files with 21 additions and 8 deletions

View File

@ -1132,7 +1132,6 @@
"name": "Text Field", "name": "Text Field",
"icon": "ri-edit-box-line", "icon": "ri-edit-box-line",
"styleable": true, "styleable": true,
"bindable": true,
"settings": [ "settings": [
{ {
"type": "field/string", "type": "field/string",
@ -1155,7 +1154,6 @@
"name": "Number Field", "name": "Number Field",
"icon": "ri-edit-box-line", "icon": "ri-edit-box-line",
"styleable": true, "styleable": true,
"bindable": true,
"settings": [ "settings": [
{ {
"type": "field/number", "type": "field/number",
@ -1178,7 +1176,6 @@
"name": "Options Picker", "name": "Options Picker",
"icon": "ri-edit-box-line", "icon": "ri-edit-box-line",
"styleable": true, "styleable": true,
"bindable": true,
"settings": [ "settings": [
{ {
"type": "field/options", "type": "field/options",
@ -1202,7 +1199,6 @@
"name": "Checkbox", "name": "Checkbox",
"icon": "ri-edit-box-line", "icon": "ri-edit-box-line",
"styleable": true, "styleable": true,
"bindable": true,
"settings": [ "settings": [
{ {
"type": "field/boolean", "type": "field/boolean",

View File

@ -10,16 +10,27 @@
const { styleable, API, setBindableValue, DataProvider } = getContext("sdk") const { styleable, API, setBindableValue, DataProvider } = getContext("sdk")
const component = getContext("component") const component = getContext("component")
const dataContext = getContext("data")
let loaded = false let loaded = false
let schema let schema
let table let table
let fieldMap = {} let fieldMap = {}
// Checks if the closest data context matches the model for this forms
// datasource, and use it as the initial form values if so
const getInitialValues = context => {
return context && context.tableId === datasource?.tableId ? context : {}
}
// Use the closest data context as the initial form values if it matches
const initialValues = getInitialValues(
$dataContext[$dataContext.closestComponentId]
)
// Form state contains observable data about the form // Form state contains observable data about the form
const formState = writable({ values: {}, errors: {}, valid: true }) const formState = writable({ values: initialValues, errors: {}, valid: true })
$: updateFormState(fieldMap) $: updateFormState(fieldMap)
$: setBindableValue($component.id, $formState.values)
// Form API contains functions to control the form // Form API contains functions to control the form
const formApi = { const formApi = {
@ -71,7 +82,7 @@
fieldId: `${Math.random() fieldId: `${Math.random()
.toString(32) .toString(32)
.substr(2)}/${field}`, .substr(2)}/${field}`,
value: null, value: initialValues[field] ?? null,
error: null, error: null,
valid: true, valid: true,
}) })
@ -79,7 +90,7 @@
// Updates the form states from the field data // Updates the form states from the field data
const updateFormState = fieldMap => { const updateFormState = fieldMap => {
let values = {} let values = { ...initialValues }
let errors = {} let errors = {}
Object.entries(fieldMap).forEach(([field, formField]) => { Object.entries(fieldMap).forEach(([field, formField]) => {
const fieldState = get(formField.fieldState) const fieldState = get(formField.fieldState)
@ -126,3 +137,9 @@
{/if} {/if}
</div> </div>
</DataProvider> </DataProvider>
<style>
div {
padding: 20px;
}
</style>