budibase/packages/builder/src/templates/commonComponents.js

78 lines
2.4 KiB
JavaScript
Raw Normal View History

import { Component } from "./Component"
import { getSchemaForDatasource } from "../dataBinding"
const fieldTypeToComponentMap = {
string: "stringfield",
number: "numberfield",
bigint: "bigintfield",
options: "optionsfield",
array: "multifieldselect",
boolean: "booleanfield",
longform: "longformfield",
datetime: "datetimefield",
attachment: "attachmentfield",
link: "relationshipfield",
json: "jsonfield",
barcodeqr: "codescanner",
}
export function makeDatasourceFormComponents(datasource) {
const { schema } = getSchemaForDatasource(null, datasource, {
formSchema: true,
})
let components = []
let fields = Object.keys(schema || {})
2021-05-04 12:32:22 +02:00
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") {
2021-02-19 13:56:24 +01:00
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
}