initial standard-components relationship field component update

This commit is contained in:
Keviin Åberg Kultalahti 2021-02-12 16:47:20 +01:00
parent 5afeb600a6
commit 8539a6dcbe
3 changed files with 37 additions and 2 deletions

View File

@ -194,6 +194,10 @@ export function makeDatasourceFormComponents(datasource) {
if (fieldType === "options") {
component.customProps({ placeholder: "Choose an option " })
}
if (fieldType === "link") {
let placeholder = fieldSchema.oneToMany ? 'Choose an option' : 'Choose some options'
component.customProps({ placeholder })
}
if (fieldType === "boolean") {
component.customProps({ text: field, label: "" })
}

View File

@ -1333,6 +1333,11 @@
"type": "text",
"label": "Label",
"key": "label"
},
{
"type": "text",
"label": "Placeholder",
"key": "placeholder"
}
]
}

View File

@ -7,6 +7,7 @@
export let field
export let label
export let placeholder
let fieldState
let fieldApi
@ -15,8 +16,29 @@
// Picker state
let options = []
let tableDefinition
let fieldText = ""
$: fieldText = `${$fieldState?.value?.length ?? 0} selected rows`
const setFieldText = (value) => {
if (fieldSchema?.oneToMany) {
if (value?.length) {
const row = options.find(row => row._id === value)
if (!row) return placeholder || 'Choose an option'
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'
}
}
}
$: console.log(placeholder)
$: fieldText = setFieldText($fieldState?.value)
$: valueLookupMap = getValueLookupMap($fieldState?.value)
$: isOptionSelected = option => valueLookupMap[option] === true
$: linkedTableId = fieldSchema?.tableId
@ -54,11 +76,15 @@
}
const toggleOption = option => {
if ($fieldState.value.includes(option)) {
if (fieldSchema.oneToMany) {
fieldApi.setValue([option])
} else {
if ($fieldState.value.includes(option)) {
fieldApi.setValue($fieldState.value.filter(x => x !== option))
} else {
fieldApi.setValue([...$fieldState.value, option])
}
}
}
</script>