Prettier and fix for bug - if selected is not found, fetch it.

This commit is contained in:
mike12345567 2025-02-14 14:25:37 +00:00
parent 3b34b7ad5a
commit f04451a2ed
2 changed files with 72 additions and 23 deletions

View File

@ -4,7 +4,12 @@
import { fetchData, Utils } from "@budibase/frontend-core" import { fetchData, Utils } from "@budibase/frontend-core"
import { getContext } from "svelte" import { getContext } from "svelte"
import Field from "./Field.svelte" import Field from "./Field.svelte"
import type { SearchFilter, RelationshipFieldMetadata, Table, Row } from "@budibase/types" import type {
SearchFilter,
RelationshipFieldMetadata,
Table,
Row,
} from "@budibase/types"
const { API } = getContext("sdk") const { API } = getContext("sdk")
@ -24,7 +29,10 @@
export let primaryDisplay: string | undefined = undefined export let primaryDisplay: string | undefined = undefined
export let span: number | undefined = undefined export let span: number | undefined = undefined
export let helpText: string | undefined = undefined export let helpText: string | undefined = undefined
export let type: FieldType.LINK | FieldType.BB_REFERENCE | FieldType.BB_REFERENCE_SINGLE = FieldType.LINK export let type:
| FieldType.LINK
| FieldType.BB_REFERENCE
| FieldType.BB_REFERENCE_SINGLE = FieldType.LINK
type RelationshipValue = { _id: string; [key: string]: any } type RelationshipValue = { _id: string; [key: string]: any }
type OptionObj = Record<string, RelationshipValue> type OptionObj = Record<string, RelationshipValue>
@ -36,6 +44,7 @@
let tableDefinition: Table | null | undefined let tableDefinition: Table | null | undefined
let searchTerm: any let searchTerm: any
let open: boolean let open: boolean
let selectedValue: string[] | string
$: multiselect = $: multiselect =
[FieldType.LINK, FieldType.BB_REFERENCE].includes(type) && [FieldType.LINK, FieldType.BB_REFERENCE].includes(type) &&
@ -71,7 +80,11 @@
if (!Array.isArray(valueAsSafeArray)) { if (!Array.isArray(valueAsSafeArray)) {
valueAsSafeArray = [fieldState.value] valueAsSafeArray = [fieldState.value]
} }
optionsObj = valueAsSafeArray.reduce((accumulator: OptionObj, value: { _id: string, primaryDisplay: any }) => { optionsObj = valueAsSafeArray.reduce(
(
accumulator: OptionObj,
value: { _id: string; primaryDisplay: any }
) => {
// fieldState has to be an array of strings to be valid for an update // fieldState has to be an array of strings to be valid for an update
// therefore we cannot guarantee value will be an object // therefore we cannot guarantee value will be an object
// https://linear.app/budibase/issue/BUDI-7577/refactor-the-relationshipfield-component-to-have-better-support-for // https://linear.app/budibase/issue/BUDI-7577/refactor-the-relationshipfield-component-to-have-better-support-for
@ -83,7 +96,9 @@
[primaryDisplay]: value.primaryDisplay, [primaryDisplay]: value.primaryDisplay,
} }
return accumulator return accumulator
}, {}) },
{}
)
} }
} }
@ -104,8 +119,12 @@
enrichedOptions = enrichedOptions.sort((a: OptionObj, b: OptionObj) => { enrichedOptions = enrichedOptions.sort((a: OptionObj, b: OptionObj) => {
const selectedValues = flatten(fieldState?.value) || [] const selectedValues = flatten(fieldState?.value) || []
const aIsSelected = selectedValues.find((v: RelationshipValue) => v === a._id) const aIsSelected = selectedValues.find(
const bIsSelected = selectedValues.find((v: RelationshipValue) => v === b._id) (v: RelationshipValue) => v === a._id
)
const bIsSelected = selectedValues.find(
(v: RelationshipValue) => v === b._id
)
if (aIsSelected && !bIsSelected) { if (aIsSelected && !bIsSelected) {
return -1 return -1
} else if (!aIsSelected && bIsSelected) { } else if (!aIsSelected && bIsSelected) {
@ -117,7 +136,11 @@
} }
} }
$: { if (filter || defaultValue) { forceFetchRows() } } $: {
if (filter || defaultValue) {
forceFetchRows()
}
}
$: debouncedFetchRows(searchTerm, primaryDisplay, defaultValue) $: debouncedFetchRows(searchTerm, primaryDisplay, defaultValue)
const forceFetchRows = async () => { const forceFetchRows = async () => {
@ -127,7 +150,11 @@
selectedValue = [] selectedValue = []
debouncedFetchRows(searchTerm, primaryDisplay, defaultValue) debouncedFetchRows(searchTerm, primaryDisplay, defaultValue)
} }
async function fetchRows(searchTerm: any, primaryDisplay: string, defaultVal: string | string[]) { async function fetchRows(
searchTerm: any,
primaryDisplay: string,
defaultVal: string | string[]
) {
const allRowsFetched = const allRowsFetched =
$fetch.loaded && $fetch.loaded &&
!Object.keys($fetch.query?.string || {}).length && !Object.keys($fetch.query?.string || {}).length &&
@ -137,14 +164,32 @@
return return
} }
// must be an array // must be an array
const defaultValArray: string[] = !defaultVal ? [] : !Array.isArray(defaultVal) ? defaultVal.split(",") : defaultVal const defaultValArray: string[] = !defaultVal
? []
: !Array.isArray(defaultVal)
? defaultVal.split(",")
: defaultVal
if (defaultVal && optionsObj && defaultValArray.some(val => !optionsObj[val])) { if (
defaultVal &&
optionsObj &&
defaultValArray.some(val => !optionsObj[val])
) {
await fetch.update({ await fetch.update({
query: { oneOf: { _id: defaultValArray } }, query: { oneOf: { _id: defaultValArray } },
}) })
} }
if (
(Array.isArray(selectedValue) &&
selectedValue.some(val => !optionsObj[val])) ||
(selectedValue && !optionsObj[selectedValue as string])
) {
await fetch.update({
query: { oneOf: { _id: Array.isArray(selectedValue) ? selectedValue : [selectedValue] }}
})
}
// Ensure we match all filters, rather than any // Ensure we match all filters, rather than any
// @ts-expect-error this doesn't fit types, but don't want to change it yet // @ts-expect-error this doesn't fit types, but don't want to change it yet
const baseFilter: any = (filter || []).filter(x => x.operator !== "allOr") const baseFilter: any = (filter || []).filter(x => x.operator !== "allOr")
@ -206,6 +251,10 @@
fetch.nextPage() fetch.nextPage()
} }
} }
const componentValue = () => {
return selectedValue as any
}
</script> </script>
<Field <Field
@ -227,7 +276,7 @@
this={component} this={component}
options={enrichedOptions} options={enrichedOptions}
{autocomplete} {autocomplete}
value={selectedValue} value={componentValue()}
on:change={handleChange} on:change={handleChange}
on:loadMore={loadMore} on:loadMore={loadMore}
id={fieldState.fieldId} id={fieldState.fieldId}