forked from tsai/budibase
10 changed files with 136 additions and 80 deletions
@ -0,0 +1,52 @@ |
|||||
|
export const getOptions = ( |
||||
|
optionsSource, |
||||
|
fieldSchema, |
||||
|
dataProvider, |
||||
|
labelColumn, |
||||
|
valueColumn, |
||||
|
customOptions |
||||
|
) => { |
||||
|
const isArray = fieldSchema?.type === "array" |
||||
|
// Take options from schema
|
||||
|
if (optionsSource == null || optionsSource === "schema") { |
||||
|
if (isArray) { |
||||
|
return fieldSchema?.constraints?.inclusion[0] ?? [] |
||||
|
} |
||||
|
return fieldSchema?.constraints?.inclusion ?? [] |
||||
|
} |
||||
|
|
||||
|
if (optionsSource === "provider" && isArray) { |
||||
|
let optionsSet = {} |
||||
|
|
||||
|
dataProvider?.rows?.forEach(row => { |
||||
|
const value = row?.[valueColumn] |
||||
|
if (value) { |
||||
|
const label = row[labelColumn] || value |
||||
|
optionsSet[value] = { value, label } |
||||
|
} |
||||
|
}) |
||||
|
return Object.values(optionsSet) |
||||
|
} |
||||
|
|
||||
|
|
||||
|
|
||||
|
// Extract options from data provider
|
||||
|
if (optionsSource === "provider" && valueColumn) { |
||||
|
let optionsSet = {} |
||||
|
dataProvider?.rows?.forEach(row => { |
||||
|
const value = row?.[valueColumn] |
||||
|
if (value) { |
||||
|
const label = row[labelColumn] || value |
||||
|
optionsSet[value] = { value, label } |
||||
|
} |
||||
|
}) |
||||
|
return Object.values(optionsSet) |
||||
|
} |
||||
|
|
||||
|
// Extract custom options
|
||||
|
if (optionsSource === "custom" && customOptions) { |
||||
|
return customOptions |
||||
|
} |
||||
|
|
||||
|
return [] |
||||
|
} |
||||
Loading…
Reference in new issue