|
|
|
@ -11,6 +11,7 @@ |
|
|
|
export let getOptionLabel = option => option |
|
|
|
export let getOptionValue = option => option |
|
|
|
export let getOptionIcon = () => null |
|
|
|
export let getOptionColour = () => null |
|
|
|
export let readonly = false |
|
|
|
export let quiet = false |
|
|
|
export let autoWidth = false |
|
|
|
@ -20,35 +21,27 @@ |
|
|
|
const dispatch = createEventDispatcher() |
|
|
|
let open = false |
|
|
|
$: fieldText = getFieldText(value, options, placeholder) |
|
|
|
$: fieldIcon = getFieldIcon(value, options, placeholder) |
|
|
|
|
|
|
|
const getFieldText = (value, options, placeholder) => { |
|
|
|
// Always use placeholder if no value |
|
|
|
if (value == null || value === "") { |
|
|
|
return placeholder || "Choose an option" |
|
|
|
} |
|
|
|
$: fieldIcon = getFieldAttribute(getOptionIcon, value, options) |
|
|
|
$: fieldColour = getFieldAttribute(getOptionColour, value, options) |
|
|
|
|
|
|
|
const getFieldAttribute = (getAttribute, value, options) => { |
|
|
|
// Wait for options to load if there is a value but no options |
|
|
|
if (!options?.length) { |
|
|
|
return "" |
|
|
|
} |
|
|
|
|
|
|
|
// Render the label if the selected option is found, otherwise raw value |
|
|
|
const index = options.findIndex( |
|
|
|
(option, idx) => getOptionValue(option, idx) === value |
|
|
|
) |
|
|
|
return index !== -1 ? getOptionLabel(options[index], index) : value |
|
|
|
return index !== -1 ? getAttribute(options[index], index) : null |
|
|
|
} |
|
|
|
|
|
|
|
const getFieldIcon = (value, options) => { |
|
|
|
// Wait for options to load if there is a value but no options |
|
|
|
if (!options?.length) { |
|
|
|
return "" |
|
|
|
const getFieldText = (value, options, placeholder) => { |
|
|
|
// Always use placeholder if no value |
|
|
|
if (value == null || value === "") { |
|
|
|
return placeholder || "Choose an option" |
|
|
|
} |
|
|
|
const index = options.findIndex( |
|
|
|
(option, idx) => getOptionValue(option, idx) === value |
|
|
|
) |
|
|
|
return index !== -1 ? getOptionIcon(options[index], index) : null |
|
|
|
|
|
|
|
return getFieldAttribute(getOptionLabel, value, options) |
|
|
|
} |
|
|
|
|
|
|
|
const selectOption = value => { |
|
|
|
@ -66,12 +59,14 @@ |
|
|
|
{disabled} |
|
|
|
{readonly} |
|
|
|
{fieldText} |
|
|
|
{fieldIcon} |
|
|
|
{fieldColour} |
|
|
|
{options} |
|
|
|
{autoWidth} |
|
|
|
{getOptionLabel} |
|
|
|
{getOptionValue} |
|
|
|
{getOptionIcon} |
|
|
|
{fieldIcon} |
|
|
|
{getOptionColour} |
|
|
|
{autocomplete} |
|
|
|
{sort} |
|
|
|
isPlaceholder={value == null || value === ""} |
|
|
|
|