Pull form bindable properties from field definitions rather than schema

This commit is contained in:
Andrew Kingston 2021-02-04 13:01:49 +00:00
parent de4b87a208
commit c36ddceb7e
6 changed files with 68 additions and 8 deletions

View File

@ -105,11 +105,27 @@ export const getContextBindings = (rootComponent, componentId) => {
return return
} }
// Get schema and add _id and _rev fields for certain types // Get schema and table for the datasource
let { schema, table } = getSchemaForDatasource(datasource) const isForm = component._component.endsWith("/form")
let { schema, table } = getSchemaForDatasource(datasource, isForm)
if (!schema || !table) { if (!schema || !table) {
return return
} }
// Forms are an edge case. They can have a schema which will be exposed as
// bindable properties, but they can also have custom fields which we need
// to find and provide.
if (isForm) {
const formSchema = buildFormSchema(component)
console.log(formSchema)
Object.keys(formSchema).forEach(field => {
if (!schema[field]) {
schema[field] = formSchema[field]
}
})
}
// Add _id and _rev fields for certain types
if (datasource.type === "table" || datasource.type === "link") { if (datasource.type === "table" || datasource.type === "link") {
schema["_id"] = { type: "string" } schema["_id"] = { type: "string" }
schema["_rev"] = { type: "string " } schema["_rev"] = { type: "string " }
@ -177,7 +193,7 @@ export const getContextBindings = (rootComponent, componentId) => {
/** /**
* Gets a schema for a datasource object. * Gets a schema for a datasource object.
*/ */
export const getSchemaForDatasource = datasource => { export const getSchemaForDatasource = (datasource, isForm = false) => {
let schema, table let schema, table
if (datasource) { if (datasource) {
const { type } = datasource const { type } = datasource
@ -191,6 +207,12 @@ export const getSchemaForDatasource = datasource => {
if (table) { if (table) {
if (type === "view") { if (type === "view") {
schema = cloneDeep(table.views?.[datasource.name]?.schema) schema = cloneDeep(table.views?.[datasource.name]?.schema)
} else if (type === "query" && isForm) {
schema = {}
const params = table.parameters || []
params.forEach(param => {
schema[param.name] = { ...param, type: "string" }
})
} else { } else {
schema = cloneDeep(table.schema) schema = cloneDeep(table.schema)
} }
@ -199,6 +221,32 @@ export const getSchemaForDatasource = datasource => {
return { schema, table } return { schema, table }
} }
/**
* Builds a form schema given a form component.
* A form schema is a schema of all the fields nested anywhere within a form.
*/
const buildFormSchema = component => {
let schema = {}
if (!component) {
return schema
}
const def = store.actions.components.getDefinition(component._component)
const fieldSetting = def?.settings?.find(
setting => setting.key === "field" && setting.type.startsWith("field/")
)
if (fieldSetting && component.field) {
const type = fieldSetting.type.split("field/")[1]
if (type) {
schema[component.field] = { name: component.field, type }
}
}
component._children?.forEach(child => {
const childSchema = buildFormSchema(child)
schema = { ...schema, ...childSchema }
})
return schema
}
/** /**
* utility function for the readableToRuntimeBinding and runtimeToReadableBinding. * utility function for the readableToRuntimeBinding and runtimeToReadableBinding.
*/ */

View File

@ -169,7 +169,11 @@ export function makeTableFormComponents(tableId) {
export function makeQueryFormComponents(queryId) { export function makeQueryFormComponents(queryId) {
const queries = get(backendUiStore).queries const queries = get(backendUiStore).queries
const schema = queries.find(query => query._id === queryId)?.schema ?? [] const params = queries.find(query => query._id === queryId)?.parameters ?? []
let schema = {}
params.forEach(param => {
schema[param.name] = { ...param, type: "string" }
})
return makeSchemaFormComponents(schema) return makeSchemaFormComponents(schema)
} }

View File

@ -78,7 +78,7 @@
"field/longform": LongFormFieldSelect, "field/longform": LongFormFieldSelect,
"field/datetime": DateTimeFieldSelect, "field/datetime": DateTimeFieldSelect,
"field/attachment": AttachmentFieldSelect, "field/attachment": AttachmentFieldSelect,
"field/relationship": RelationshipFieldSelect, "field/link": RelationshipFieldSelect,
} }
const getControl = type => { const getControl = type => {

View File

@ -34,7 +34,7 @@ const deleteRowHandler = async (action, context) => {
} }
const triggerAutomationHandler = async (action, context) => { const triggerAutomationHandler = async (action, context) => {
const { fields } = action.parameters() const { fields } = action.parameters
if (fields) { if (fields) {
const params = {} const params = {}
for (let field in fields) { for (let field in fields) {

View File

@ -1282,7 +1282,7 @@
"styleable": true, "styleable": true,
"settings": [ "settings": [
{ {
"type": "field/relationship", "type": "field/link",
"label": "Field", "label": "Field",
"key": "field" "key": "field"
}, },

View File

@ -133,7 +133,15 @@
} else { } else {
table = await API.fetchTableDefinition(datasource?.tableId) table = await API.fetchTableDefinition(datasource?.tableId)
if (table) { if (table) {
schema = table.schema || {} if (datasource?.type === "query") {
schema = {}
const params = table.parameters || []
params.forEach(param => {
schema[param.name] = { ...param, type: "string" }
})
} else {
schema = table.schema || {}
}
} }
} }
loaded = true loaded = true