1
0
Fork 0
mirror of synced 2024-07-18 12:46:16 +12:00

Merge pull request #9220 from Budibase/fix/option-picker-sorting

Added sorting to the options field
This commit is contained in:
deanhannigan 2023-01-09 15:24:28 +00:00 committed by GitHub
commit 55211861b2
2 changed files with 20 additions and 2 deletions

View file

@ -11,14 +11,31 @@
export let getOptionLabel = option => option
export let getOptionValue = option => option
export let getOptionTitle = option => option
export let sort = false
const dispatch = createEventDispatcher()
const onChange = e => dispatch("change", e.target.value)
const getSortedOptions = (options, getLabel, sort) => {
if (!options?.length || !Array.isArray(options)) {
return []
}
if (!sort) {
return options
}
return [...options].sort((a, b) => {
const labelA = getLabel(a)
const labelB = getLabel(b)
return labelA > labelB ? 1 : -1
})
}
$: parsedOptions = getSortedOptions(options, getOptionLabel, sort)
</script>
<div class={`spectrum-FieldGroup spectrum-FieldGroup--${direction}`}>
{#if options && Array.isArray(options)}
{#each options as option}
{#if parsedOptions && Array.isArray(parsedOptions)}
{#each parsedOptions as option}
<div
title={getOptionTitle(option)}
class="spectrum-Radio spectrum-FieldGroup-item spectrum-Radio--emphasized"

View file

@ -79,6 +79,7 @@
getOptionLabel={flatOptions ? x => x : x => x.label}
getOptionTitle={flatOptions ? x => x : x => x.label}
getOptionValue={flatOptions ? x => x : x => x.value}
{sort}
/>
{/if}
{/if}