Keep options

This commit is contained in:
Adria Navarro 2023-09-22 12:39:49 +02:00
parent b56081405e
commit 85854aa8be
1 changed files with 15 additions and 23 deletions

View File

@ -53,12 +53,24 @@
[primaryDisplay]: x.primaryDisplay, [primaryDisplay]: x.primaryDisplay,
}))) })))
$: options = processOptions(selectedValue, $fetch.rows, initialValues) $: allOptions = {
...(allOptions || {}),
...[...($fetch.rows || []), ...(initialValues || [])]?.reduce((p, c) => {
p[c._id] = c
return p
}, {}),
}
$: options = fetchTerm
? Object.values(allOptions).filter(row =>
row[primaryDisplay].includes(fetchTerm)
)
: Object.values(allOptions)
let lastFetchedTerm let lastFetchedTerm
$: search(fetchTerm) $: fetchRows(fetchTerm)
const search = fetchTerm => { const fetchRows = fetchTerm => {
const termChangedSinceFetch = (lastFetchedTerm || "") !== (fetchTerm || "") const termChangedSinceFetch = (lastFetchedTerm || "") !== (fetchTerm || "")
const allRowsFetched = !lastFetchedTerm && !$fetch.hasNextPage const allRowsFetched = !lastFetchedTerm && !$fetch.hasNextPage
@ -70,10 +82,6 @@
query: { string: { [primaryDisplay]: fetchTerm } }, query: { string: { [primaryDisplay]: fetchTerm } },
}) })
} }
} else {
options = fetchTerm
? $fetch.rows.filter(row => row[primaryDisplay].includes(fetchTerm))
: $fetch.rows
} }
} }
@ -87,22 +95,6 @@
return values.map(value => (typeof value === "object" ? value._id : value)) return values.map(value => (typeof value === "object" ? value._id : value))
} }
const processOptions = (selectedValue, fetchedRows, initialValues = []) => {
selectedValue ??= []
if (!Array.isArray(selectedValue)) {
selectedValue = [selectedValue]
}
const missingItems = selectedValue
.filter(v => !fetchedRows.find(r => r._id === v))
.map(v => initialValues.find(o => o._id === v))
.filter(x => x)
const result = [...missingItems, ...fetchedRows]
return result
}
const getDisplayName = row => { const getDisplayName = row => {
return row?.[primaryDisplay] || "-" return row?.[primaryDisplay] || "-"
} }