64 lines
2.1 KiB
JavaScript
64 lines
2.1 KiB
JavaScript
import { FieldTypeToComponentMap } from "components/design/settings/controls/FieldConfiguration/utils"
|
|
import { Component } from "./Component"
|
|
import { getSchemaForDatasource } from "dataBinding"
|
|
|
|
export function makeDatasourceFormComponents(datasource) {
|
|
const { schema } = getSchemaForDatasource(null, datasource, {
|
|
formSchema: true,
|
|
})
|
|
let components = []
|
|
let fields = Object.keys(schema || {})
|
|
fields.forEach(field => {
|
|
const fieldSchema = schema[field]
|
|
// skip autocolumns
|
|
if (fieldSchema.autocolumn || fieldSchema.nestedJSON) {
|
|
return
|
|
}
|
|
const fieldType =
|
|
typeof fieldSchema === "object" ? fieldSchema.type : fieldSchema
|
|
const componentType = FieldTypeToComponentMap[fieldType]
|
|
const fullComponentType = `@budibase/standard-components/${componentType}`
|
|
if (componentType) {
|
|
const component = new Component(fullComponentType)
|
|
.instanceName(field)
|
|
.customProps({
|
|
field,
|
|
label: field,
|
|
placeholder: field,
|
|
})
|
|
if (fieldType === "options") {
|
|
component.customProps({
|
|
placeholder: "Choose an option",
|
|
optionsType: "select",
|
|
optionsSource: "schema",
|
|
})
|
|
} else if (fieldType === "longform") {
|
|
component.customProps({
|
|
format: "auto",
|
|
})
|
|
} else if (fieldType === "array") {
|
|
component.customProps({
|
|
placeholder: "Choose an option",
|
|
optionsSource: "schema",
|
|
})
|
|
} else if (fieldType === "link") {
|
|
let placeholder =
|
|
fieldSchema.relationshipType === "one-to-many"
|
|
? "Choose an option"
|
|
: "Choose some options"
|
|
component.customProps({ placeholder })
|
|
} else if (fieldType === "boolean") {
|
|
component.customProps({ text: field, label: "" })
|
|
} else if (fieldType === "datetime") {
|
|
component.customProps({
|
|
enableTime: !fieldSchema?.dateOnly,
|
|
timeOnly: fieldSchema?.timeOnly,
|
|
ignoreTimezones: fieldSchema.ignoreTimezones,
|
|
})
|
|
}
|
|
components.push(component)
|
|
}
|
|
})
|
|
return components
|
|
}
|