|
|
|
@ -1,7 +1,7 @@ |
|
|
|
<script> |
|
|
|
import { CoreSelect, CoreMultiselect } from "@budibase/bbui" |
|
|
|
import { getContext } from "svelte" |
|
|
|
import Field from "./Field.svelte" |
|
|
|
import Picker from "./Picker.svelte" |
|
|
|
|
|
|
|
const { API } = getContext("sdk") |
|
|
|
|
|
|
|
@ -14,31 +14,10 @@ |
|
|
|
let fieldApi |
|
|
|
let fieldSchema |
|
|
|
|
|
|
|
// Picker state |
|
|
|
let options = [] |
|
|
|
let tableDefinition |
|
|
|
let fieldText = "" |
|
|
|
|
|
|
|
const setFieldText = value => { |
|
|
|
if (fieldSchema?.relationshipType === "one-to-many") { |
|
|
|
if (value?.length && options?.length) { |
|
|
|
const row = options.find(row => row._id === value[0]?._id) |
|
|
|
return getDisplayName(row) |
|
|
|
} else { |
|
|
|
return placeholder || "Choose an option" |
|
|
|
} |
|
|
|
} else { |
|
|
|
if (value?.length) { |
|
|
|
return `${value?.length ?? 0} selected rows` |
|
|
|
} else { |
|
|
|
return placeholder || "Choose some options" |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
$: options, (fieldText = setFieldText($fieldState?.value)) |
|
|
|
$: valueLookupMap = getValueLookupMap($fieldState?.value) |
|
|
|
$: isOptionSelected = option => valueLookupMap[option] === true |
|
|
|
$: multiselect = fieldSchema?.relationshipType !== "one-to-many" |
|
|
|
$: linkedTableId = fieldSchema?.tableId |
|
|
|
$: fetchRows(linkedTableId) |
|
|
|
$: fetchTable(linkedTableId) |
|
|
|
@ -63,29 +42,12 @@ |
|
|
|
return row?.[tableDefinition?.primaryDisplay || "_id"] || "-" |
|
|
|
} |
|
|
|
|
|
|
|
const getValueLookupMap = value => { |
|
|
|
let map = {} |
|
|
|
if (value?.length) { |
|
|
|
value.forEach(option => { |
|
|
|
if (option?._id) { |
|
|
|
map[option._id] = true |
|
|
|
} |
|
|
|
}) |
|
|
|
} |
|
|
|
return map |
|
|
|
const singleHandler = e => { |
|
|
|
fieldApi.setValue(e.detail == null ? [] : [e.detail]) |
|
|
|
} |
|
|
|
|
|
|
|
const toggleOption = id => { |
|
|
|
if (fieldSchema.relationshipType === "one-to-many") { |
|
|
|
fieldApi.setValue(id ? [{ _id: id }] : []) |
|
|
|
} else { |
|
|
|
if ($fieldState.value.find(option => option?._id === id)) { |
|
|
|
const filtered = $fieldState.value.filter(option => option?._id !== id) |
|
|
|
fieldApi.setValue(filtered) |
|
|
|
} else { |
|
|
|
fieldApi.setValue([...$fieldState.value, { _id: id }]) |
|
|
|
} |
|
|
|
} |
|
|
|
const multiHandler = e => { |
|
|
|
fieldApi.setValue(e.detail) |
|
|
|
} |
|
|
|
</script> |
|
|
|
|
|
|
|
@ -98,12 +60,16 @@ |
|
|
|
bind:fieldApi |
|
|
|
bind:fieldSchema |
|
|
|
defaultValue={[]}> |
|
|
|
<Picker |
|
|
|
{fieldState} |
|
|
|
{fieldText} |
|
|
|
{options} |
|
|
|
{isOptionSelected} |
|
|
|
onSelectOption={toggleOption} |
|
|
|
getOptionLabel={getDisplayName} |
|
|
|
getOptionValue={option => option._id} /> |
|
|
|
{#if fieldState} |
|
|
|
<svelte:component |
|
|
|
this={multiselect ? CoreMultiselect : CoreSelect} |
|
|
|
{options} |
|
|
|
value={multiselect ? $fieldState.value : $fieldState.value?.[0]} |
|
|
|
on:change={multiselect ? multiHandler : singleHandler} |
|
|
|
fieldId={$fieldState.fieldId} |
|
|
|
disabled={$fieldState.disabled} |
|
|
|
error={$fieldState.error} |
|
|
|
getOptionLabel={getDisplayName} |
|
|
|
getOptionValue={option => option._id} /> |
|
|
|
{/if} |
|
|
|
</Field> |
|
|
|
|