Clean code
This commit is contained in:
parent
86feec66b1
commit
a175fb3293
packages/client/src/components/app/forms
|
@ -48,8 +48,11 @@
|
||||||
|
|
||||||
let optionsObj = {}
|
let optionsObj = {}
|
||||||
let initialValuesProcessed
|
let initialValuesProcessed
|
||||||
|
|
||||||
$: {
|
$: {
|
||||||
if (!initialValuesProcessed && primaryDisplay) {
|
if (!initialValuesProcessed && primaryDisplay) {
|
||||||
|
// Persist the initial values as options, allowing them to be present in the dropdown,
|
||||||
|
// even if they are not in the inital fetch results
|
||||||
initialValuesProcessed = true
|
initialValuesProcessed = true
|
||||||
optionsObj = {
|
optionsObj = {
|
||||||
...optionsObj,
|
...optionsObj,
|
||||||
|
@ -63,19 +66,22 @@
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
$: optionsObj = {
|
|
||||||
|
$: enrichedOptions = enrichOptions(optionsObj, $fetch.rows)
|
||||||
|
const enrichOptions = (optionsObj, fetchResults) => {
|
||||||
|
const result = {
|
||||||
...optionsObj,
|
...optionsObj,
|
||||||
...($fetch.rows || [])?.reduce((accumulator, row) => {
|
...(fetchResults || [])?.reduce((accumulator, row) => {
|
||||||
accumulator[row._id] = row
|
accumulator[row._id] = row
|
||||||
return accumulator
|
return accumulator
|
||||||
}, {}),
|
}, {}),
|
||||||
}
|
}
|
||||||
|
return Object.values(result)
|
||||||
$: options = Object.values(optionsObj)
|
}
|
||||||
$: {
|
$: {
|
||||||
// We don't want to reorder while the dropdown is open, to avoid UX jumps
|
// We don't want to reorder while the dropdown is open, to avoid UX jumps
|
||||||
if (!open) {
|
if (!open) {
|
||||||
options = options.sort((a, b) => {
|
enrichedOptions = enrichedOptions.sort((a, b) => {
|
||||||
const selectedValues = flatten(fieldState?.value) || []
|
const selectedValues = flatten(fieldState?.value) || []
|
||||||
|
|
||||||
const aIsSelected = selectedValues.find(v => v === a._id)
|
const aIsSelected = selectedValues.find(v => v === a._id)
|
||||||
|
@ -166,7 +172,7 @@
|
||||||
{#if fieldState}
|
{#if fieldState}
|
||||||
<svelte:component
|
<svelte:component
|
||||||
this={component}
|
this={component}
|
||||||
{options}
|
options={enrichedOptions}
|
||||||
{autocomplete}
|
{autocomplete}
|
||||||
value={selectedValue}
|
value={selectedValue}
|
||||||
on:change={multiselect ? multiHandler : singleHandler}
|
on:change={multiselect ? multiHandler : singleHandler}
|
||||||
|
|
Loading…
Reference in New Issue