2020-11-16 19:05:17 +01:00
|
|
|
import { Component } from "./Component"
|
2024-02-05 13:08:45 +01:00
|
|
|
import { getSchemaForDatasource } from "dataBinding"
|
2020-11-16 19:05:17 +01:00
|
|
|
|
2021-02-02 15:32:58 +01:00
|
|
|
const fieldTypeToComponentMap = {
|
|
|
|
string: "stringfield",
|
|
|
|
number: "numberfield",
|
2023-07-11 15:01:55 +02:00
|
|
|
bigint: "bigintfield",
|
2021-02-02 15:32:58 +01:00
|
|
|
options: "optionsfield",
|
2021-08-20 16:56:11 +02:00
|
|
|
array: "multifieldselect",
|
2021-02-02 15:32:58 +01:00
|
|
|
boolean: "booleanfield",
|
|
|
|
longform: "longformfield",
|
|
|
|
datetime: "datetimefield",
|
|
|
|
attachment: "attachmentfield",
|
|
|
|
link: "relationshipfield",
|
2021-12-03 10:20:45 +01:00
|
|
|
json: "jsonfield",
|
2022-10-07 12:00:25 +02:00
|
|
|
barcodeqr: "codescanner",
|
2021-02-02 15:32:58 +01:00
|
|
|
}
|
|
|
|
|
2021-02-03 15:53:13 +01:00
|
|
|
export function makeDatasourceFormComponents(datasource) {
|
2022-02-15 15:59:11 +01:00
|
|
|
const { schema } = getSchemaForDatasource(null, datasource, {
|
|
|
|
formSchema: true,
|
|
|
|
})
|
2021-02-02 15:32:58 +01:00
|
|
|
let components = []
|
2021-02-03 15:53:13 +01:00
|
|
|
let fields = Object.keys(schema || {})
|
2021-05-04 12:32:22 +02:00
|
|
|
fields.forEach(field => {
|
2021-02-02 15:32:58 +01:00
|
|
|
const fieldSchema = schema[field]
|
2021-02-15 20:59:30 +01:00
|
|
|
// skip autocolumns
|
2021-12-10 15:18:01 +01:00
|
|
|
if (fieldSchema.autocolumn || fieldSchema.nestedJSON) {
|
2021-02-15 20:59:30 +01:00
|
|
|
return
|
|
|
|
}
|
2021-02-11 10:17:36 +01:00
|
|
|
const fieldType =
|
|
|
|
typeof fieldSchema === "object" ? fieldSchema.type : fieldSchema
|
|
|
|
const componentType = fieldTypeToComponentMap[fieldType]
|
2021-02-02 15:32:58 +01:00
|
|
|
const fullComponentType = `@budibase/standard-components/${componentType}`
|
|
|
|
if (componentType) {
|
|
|
|
const component = new Component(fullComponentType)
|
|
|
|
.instanceName(field)
|
|
|
|
.customProps({
|
|
|
|
field,
|
|
|
|
label: field,
|
|
|
|
placeholder: field,
|
|
|
|
})
|
2021-02-11 10:17:36 +01:00
|
|
|
if (fieldType === "options") {
|
2021-08-17 14:58:51 +02:00
|
|
|
component.customProps({
|
|
|
|
placeholder: "Choose an option",
|
|
|
|
optionsType: "select",
|
|
|
|
optionsSource: "schema",
|
|
|
|
})
|
2022-06-07 09:31:00 +02:00
|
|
|
} else if (fieldType === "longform") {
|
2022-02-07 13:11:20 +01:00
|
|
|
component.customProps({
|
|
|
|
format: "auto",
|
|
|
|
})
|
2022-06-07 09:31:00 +02:00
|
|
|
} else if (fieldType === "array") {
|
2021-08-24 17:14:38 +02:00
|
|
|
component.customProps({
|
|
|
|
placeholder: "Choose an option",
|
|
|
|
optionsSource: "schema",
|
|
|
|
})
|
2022-06-07 09:31:00 +02:00
|
|
|
} 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"
|
2021-02-12 16:47:20 +01:00
|
|
|
component.customProps({ placeholder })
|
2022-06-07 09:31:00 +02:00
|
|
|
} else if (fieldType === "boolean") {
|
2021-02-02 15:32:58 +01:00
|
|
|
component.customProps({ text: field, label: "" })
|
2022-06-07 09:31:00 +02:00
|
|
|
} else if (fieldType === "datetime") {
|
|
|
|
component.customProps({
|
|
|
|
enableTime: !fieldSchema?.dateOnly,
|
|
|
|
timeOnly: fieldSchema?.timeOnly,
|
|
|
|
ignoreTimezones: fieldSchema.ignoreTimezones,
|
|
|
|
})
|
2021-02-02 15:32:58 +01:00
|
|
|
}
|
|
|
|
components.push(component)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
return components
|
|
|
|
}
|