Handle sorting
This commit is contained in:
parent
26362c44e8
commit
ab4f399033
|
@ -22,6 +22,7 @@
|
||||||
let fieldSchema
|
let fieldSchema
|
||||||
let tableDefinition
|
let tableDefinition
|
||||||
let searchTerm
|
let searchTerm
|
||||||
|
let open
|
||||||
|
|
||||||
$: multiselect = fieldSchema?.relationshipType !== "one-to-many"
|
$: multiselect = fieldSchema?.relationshipType !== "one-to-many"
|
||||||
$: linkedTableId = fieldSchema?.tableId
|
$: linkedTableId = fieldSchema?.tableId
|
||||||
|
@ -70,6 +71,26 @@
|
||||||
}, {}),
|
}, {}),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$: options = Object.values(optionsObj)
|
||||||
|
$: {
|
||||||
|
// We don't want to reorder while the dropdown is open, to avoid UX jumps
|
||||||
|
if (!open) {
|
||||||
|
options = options.sort((a, b) => {
|
||||||
|
const selectedValues = flatten(fieldState?.value) || []
|
||||||
|
|
||||||
|
const aIsSelected = selectedValues.find(v => v === a._id)
|
||||||
|
const bIsSelected = selectedValues.find(v => v === b._id)
|
||||||
|
if (aIsSelected && !bIsSelected) {
|
||||||
|
return -1
|
||||||
|
} else if (!aIsSelected && bIsSelected) {
|
||||||
|
return 1
|
||||||
|
}
|
||||||
|
|
||||||
|
return a[primaryDisplay] > b[primaryDisplay]
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
$: fetchRows(searchTerm, primaryDisplay)
|
$: fetchRows(searchTerm, primaryDisplay)
|
||||||
|
|
||||||
const fetchRows = (searchTerm, primaryDisplay) => {
|
const fetchRows = (searchTerm, primaryDisplay) => {
|
||||||
|
@ -145,7 +166,7 @@
|
||||||
{#if fieldState}
|
{#if fieldState}
|
||||||
<svelte:component
|
<svelte:component
|
||||||
this={component}
|
this={component}
|
||||||
options={Object.values(optionsObj)}
|
{options}
|
||||||
{autocomplete}
|
{autocomplete}
|
||||||
value={selectedValue}
|
value={selectedValue}
|
||||||
on:change={multiselect ? multiHandler : singleHandler}
|
on:change={multiselect ? multiHandler : singleHandler}
|
||||||
|
@ -156,9 +177,9 @@
|
||||||
getOptionLabel={getDisplayName}
|
getOptionLabel={getDisplayName}
|
||||||
getOptionValue={option => option._id}
|
getOptionValue={option => option._id}
|
||||||
{placeholder}
|
{placeholder}
|
||||||
sort
|
|
||||||
bind:searchTerm
|
bind:searchTerm
|
||||||
loading={$fetch.loading}
|
loading={$fetch.loading}
|
||||||
|
bind:open
|
||||||
/>
|
/>
|
||||||
{/if}
|
{/if}
|
||||||
</Field>
|
</Field>
|
||||||
|
|
Loading…
Reference in New Issue