Respect ordering of options when using a data provider options source

This commit is contained in:
Andrew Kingston 2024-03-21 16:24:35 +00:00
parent c6fca37b43
commit d3535a255d
1 changed files with 6 additions and 18 deletions

View File

@ -6,36 +6,24 @@ export const getOptions = (
valueColumn, valueColumn,
customOptions customOptions
) => { ) => {
const isArray = fieldSchema?.type === "array"
// Take options from schema // Take options from schema
if (optionsSource == null || optionsSource === "schema") { if (optionsSource == null || optionsSource === "schema") {
return fieldSchema?.constraints?.inclusion ?? [] return fieldSchema?.constraints?.inclusion ?? []
} }
if (optionsSource === "provider" && isArray) {
let optionsSet = {}
dataProvider?.rows?.forEach(row => {
const value = row?.[valueColumn]
if (value != null) {
const label = row[labelColumn] || value
optionsSet[value] = { value, label }
}
})
return Object.values(optionsSet)
}
// Extract options from data provider // Extract options from data provider
if (optionsSource === "provider" && valueColumn) { if (optionsSource === "provider" && valueColumn) {
let optionsSet = {} let valueCache = {}
let options = []
dataProvider?.rows?.forEach(row => { dataProvider?.rows?.forEach(row => {
const value = row?.[valueColumn] const value = row?.[valueColumn]
if (value != null) { if (value != null && !valueCache[value]) {
valueCache[value] = true
const label = row[labelColumn] || value const label = row[labelColumn] || value
optionsSet[value] = { value, label } options.push({ value, label })
} }
}) })
return Object.values(optionsSet) return options
} }
// Extract custom options // Extract custom options