1
0
Fork 0
mirror of synced 2024-09-08 21:51:58 +12:00

Clean code

This commit is contained in:
Adria Navarro 2023-09-25 12:33:21 +02:00
parent 86feec66b1
commit a175fb3293

View file

@ -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}