This commit is contained in:
Adria Navarro 2025-03-03 19:05:30 +01:00
parent d833a0e099
commit c44b1f5e11
1 changed files with 24 additions and 18 deletions

View File

@ -11,8 +11,8 @@
export let placeholder: string | null = null export let placeholder: string | null = null
export let disabled: boolean = false export let disabled: boolean = false
export let options: Option[] = [] export let options: Option[] = []
export let getOptionLabel = (option: Option) => option export let getOptionLabel = (option: Option, _index?: number) => option
export let getOptionValue = (option: Option) => option export let getOptionValue = (option: Option, _index?: number) => option
export let readonly: boolean = false export let readonly: boolean = false
export let autocomplete: boolean = false export let autocomplete: boolean = false
export let sort: boolean = false export let sort: boolean = false
@ -31,10 +31,15 @@
$: optionLookupMap = getOptionLookupMap(options) $: optionLookupMap = getOptionLookupMap(options)
$: fieldText = getFieldText(arrayValue, optionLookupMap, placeholder) $: fieldText = getFieldText(arrayValue, optionLookupMap, placeholder)
$: isOptionSelected = optionValue => selectedLookupMap[optionValue] === true $: isOptionSelected = (optionValue: string) =>
selectedLookupMap[optionValue] === true
$: toggleOption = makeToggleOption(selectedLookupMap, arrayValue) $: toggleOption = makeToggleOption(selectedLookupMap, arrayValue)
const getFieldText = (value, map, placeholder) => { const getFieldText = (
value: string[],
map: Record<string, any> | null,
placeholder: string | null
) => {
if (Array.isArray(value) && value.length > 0) { if (Array.isArray(value) && value.length > 0) {
if (!map) { if (!map) {
return "" return ""
@ -46,8 +51,8 @@
} }
} }
const getSelectedLookupMap = value => { const getSelectedLookupMap = (value: string[]) => {
let map = {} const map: Record<string, boolean> = {}
if (Array.isArray(value) && value.length > 0) { if (Array.isArray(value) && value.length > 0) {
value.forEach(option => { value.forEach(option => {
if (option) { if (option) {
@ -58,22 +63,23 @@
return map return map
} }
const getOptionLookupMap = options => { const getOptionLookupMap = (options: Option[]) => {
let map = null if (!options?.length) {
if (options?.length) { return null
map = {}
options.forEach((option, idx) => {
const optionValue = getOptionValue(option, idx)
if (optionValue != null) {
map[optionValue] = getOptionLabel(option, idx) || ""
}
})
} }
const map: Record<string, any> = {}
options.forEach((option, idx) => {
const optionValue = getOptionValue(option, idx)
if (optionValue != null) {
map[optionValue] = getOptionLabel(option, idx) || ""
}
})
return map return map
} }
const makeToggleOption = (map, value) => { const makeToggleOption = (map: Record<string, boolean>, value: string[]) => {
return optionValue => { return (optionValue: string) => {
if (map[optionValue]) { if (map[optionValue]) {
const filtered = value.filter(option => option !== optionValue) const filtered = value.filter(option => option !== optionValue)
dispatch("change", filtered) dispatch("change", filtered)