Fix automatic form generation from relationship and calculated view schemas

This commit is contained in:
Andrew Kingston 2021-02-11 09:17:36 +00:00
parent 2e9122ca81
commit 24c3f3019f
4 changed files with 16 additions and 35 deletions

View File

@ -211,7 +211,9 @@ export const getSchemaForDatasource = (datasource, isForm = false) => {
schema = {} schema = {}
const params = table.parameters || [] const params = table.parameters || []
params.forEach(param => { params.forEach(param => {
schema[param.name] = { ...param, type: "string" } if (param?.name) {
schema[param.name] = { ...param, type: "string" }
}
}) })
} else { } else {
schema = cloneDeep(table.schema) schema = cloneDeep(table.schema)

View File

@ -6,7 +6,7 @@ import {
makeMainForm, makeMainForm,
makeTitleContainer, makeTitleContainer,
makeSaveButton, makeSaveButton,
makeTableFormComponents, makeDatasourceFormComponents,
} from "./utils/commonComponents" } from "./utils/commonComponents"
export default function(tables) { export default function(tables) {
@ -51,7 +51,8 @@ const createScreen = table => {
}) })
// Add all form fields from this schema to the field group // Add all form fields from this schema to the field group
makeTableFormComponents(table._id).forEach(component => { const datasource = { type: "table", tableId: table._id }
makeDatasourceFormComponents(datasource).forEach(component => {
fieldGroup.addChild(component) fieldGroup.addChild(component)
}) })

View File

@ -7,9 +7,9 @@ import {
makeBreadcrumbContainer, makeBreadcrumbContainer,
makeTitleContainer, makeTitleContainer,
makeSaveButton, makeSaveButton,
makeTableFormComponents,
makeMainForm, makeMainForm,
spectrumColor, spectrumColor,
makeDatasourceFormComponents,
} from "./utils/commonComponents" } from "./utils/commonComponents"
export default function(tables) { export default function(tables) {
@ -109,7 +109,8 @@ const createScreen = table => {
}) })
// Add all form fields from this schema to the field group // Add all form fields from this schema to the field group
makeTableFormComponents(table._id).forEach(component => { const datasource = { type: "table", tableId: table._id }
makeDatasourceFormComponents(datasource).forEach(component => {
fieldGroup.addChild(component) fieldGroup.addChild(component)
}) })

View File

@ -1,7 +1,6 @@
import { get } from "svelte/store"
import { Component } from "./Component" import { Component } from "./Component"
import { rowListUrl } from "../rowListScreen" import { rowListUrl } from "../rowListScreen"
import { backendUiStore } from "builderStore" import { getSchemaForDatasource } from "../../../dataBinding"
export function spectrumColor(number) { export function spectrumColor(number) {
// Acorn throws a parsing error in this file if the word g-l-o-b-a-l is found // Acorn throws a parsing error in this file if the word g-l-o-b-a-l is found
@ -174,37 +173,15 @@ const fieldTypeToComponentMap = {
link: "relationshipfield", link: "relationshipfield",
} }
export function makeTableFormComponents(tableId) {
const tables = get(backendUiStore).tables
const schema = tables.find(table => table._id === tableId)?.schema ?? {}
return makeSchemaFormComponents(schema)
}
export function makeQueryFormComponents(queryId) {
const queries = get(backendUiStore).queries
const params = queries.find(query => query._id === queryId)?.parameters ?? []
let schema = {}
params.forEach(param => {
schema[param.name] = { ...param, type: "string" }
})
return makeSchemaFormComponents(schema)
}
export function makeDatasourceFormComponents(datasource) { export function makeDatasourceFormComponents(datasource) {
if (!datasource) { const { schema } = getSchemaForDatasource(datasource, true)
return []
}
return datasource.type === "table"
? makeTableFormComponents(datasource.tableId)
: makeQueryFormComponents(datasource._id)
}
function makeSchemaFormComponents(schema) {
let components = [] let components = []
let fields = Object.keys(schema || {}) let fields = Object.keys(schema || {})
fields.forEach(field => { fields.forEach(field => {
const fieldSchema = schema[field] const fieldSchema = schema[field]
const componentType = fieldTypeToComponentMap[fieldSchema.type] const fieldType =
typeof fieldSchema === "object" ? fieldSchema.type : fieldSchema
const componentType = fieldTypeToComponentMap[fieldType]
const fullComponentType = `@budibase/standard-components/${componentType}` const fullComponentType = `@budibase/standard-components/${componentType}`
if (componentType) { if (componentType) {
const component = new Component(fullComponentType) const component = new Component(fullComponentType)
@ -214,10 +191,10 @@ function makeSchemaFormComponents(schema) {
label: field, label: field,
placeholder: field, placeholder: field,
}) })
if (fieldSchema.type === "options") { if (fieldType === "options") {
component.customProps({ placeholder: "Choose an option " }) component.customProps({ placeholder: "Choose an option " })
} }
if (fieldSchema.type === "boolean") { if (fieldType === "boolean") {
component.customProps({ text: field, label: "" }) component.customProps({ text: field, label: "" })
} }
components.push(component) components.push(component)