Skip to content

Commit

Permalink
fix(KtField*SelectRemote): Add Value <-> Label Cache
Browse files Browse the repository at this point in the history
  • Loading branch information
FlorianWendelborn committed May 25, 2022
1 parent ae41068 commit 57b540a
Showing 1 changed file with 28 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,26 @@ export default defineComponent<
props.isRemote ? props.query : localQuery.value,
)
/**
* keeps reference of already seen option labels so that
* the tags can be rendered properly when filtering remotely
*/
const seenValueLabelMap = ref<Map<Shared.Value, string>>(new Map())
watch(
() => props.options,
() => {
seenValueLabelMap.value = new Map([
...seenValueLabelMap.value,
...props.options.map((option): [Shared.Value, string] => [
option.value,
option.label,
]),
])
},
{ immediate: true },
)
return {
collapsedTagCount: computed(() =>
props.isMultiple
Expand All @@ -200,7 +220,6 @@ export default defineComponent<
forceUpdateKey: forceUpdateKey.value,
placeholder: props.placeholder ?? undefined,
size: 1,
style: 'flex: 1',
type: 'text',
value: (() => {
if (isDropdownOpen.value) return queryValue.value ?? undefined
Expand Down Expand Up @@ -262,25 +281,13 @@ export default defineComponent<
props.isMultiple
? (field.currentValue as MultiValue)
.filter((_, index) => index < props.collapseTagsAfter)
.map((value): Shared.Option => {
const option = props.options.find(
(option) => option.value === value,
)
// if (!option)
// TODO: incompatible with multi remote
// throw new Error(
// `Couldn’t find option with value “${String(value)}”`,
// )
if (!option)
return {
label: String(value),
value: value as SingleValue,
}
return option
})
.map(
(value): Shared.Option =>
props.options.find((option) => option.value === value) ?? {
label: seenValueLabelMap.value.get(value) ?? String(value),
value,
},
)
: [],
),
Yoco,
Expand Down Expand Up @@ -361,6 +368,7 @@ export default defineComponent<
&__wrapper {
display: flex;
flex: 1;
min-width: 30%; /* TODO: necessary? */
padding: 0;
margin: 0;
Expand Down

0 comments on commit 57b540a

Please sign in to comment.