2021-01-27 11:59:05 +01:00
|
|
|
<script>
|
2021-08-03 21:30:06 +02:00
|
|
|
import { CoreSelect, RadioGroup } from "@budibase/bbui"
|
2021-02-05 11:53:25 +01:00
|
|
|
import Field from "./Field.svelte"
|
2021-01-27 11:59:05 +01:00
|
|
|
|
|
|
|
export let field
|
|
|
|
export let label
|
|
|
|
export let placeholder
|
2021-02-17 16:16:44 +01:00
|
|
|
export let disabled = false
|
2021-08-03 21:30:06 +02:00
|
|
|
export let optionsType = "select"
|
2021-08-04 15:33:51 +02:00
|
|
|
export let defaultValue
|
2021-08-16 12:24:25 +02:00
|
|
|
export let optionsSource = "schema"
|
|
|
|
export let dataProvider
|
|
|
|
export let labelColumn
|
|
|
|
export let valueColumn
|
2021-08-17 11:32:01 +02:00
|
|
|
export let customOptions
|
2021-01-27 11:59:05 +01:00
|
|
|
|
2021-01-28 09:47:44 +01:00
|
|
|
let fieldState
|
|
|
|
let fieldApi
|
|
|
|
let fieldSchema
|
2021-08-16 12:24:25 +02:00
|
|
|
|
|
|
|
$: flatOptions = optionsSource == null || optionsSource === "schema"
|
|
|
|
$: options = getOptions(
|
|
|
|
optionsSource,
|
|
|
|
fieldSchema,
|
|
|
|
dataProvider,
|
|
|
|
labelColumn,
|
|
|
|
valueColumn
|
|
|
|
)
|
|
|
|
|
|
|
|
const getOptions = (
|
|
|
|
optionsSource,
|
|
|
|
fieldSchema,
|
|
|
|
dataProvider,
|
|
|
|
labelColumn,
|
|
|
|
valueColumn
|
|
|
|
) => {
|
|
|
|
// Take options from schema
|
|
|
|
if (optionsSource == null || optionsSource === "schema") {
|
|
|
|
return fieldSchema?.constraints?.inclusion ?? []
|
|
|
|
}
|
|
|
|
|
|
|
|
// Extract options from data provider
|
|
|
|
if (optionsSource === "provider" && valueColumn) {
|
|
|
|
let optionsSet = {}
|
|
|
|
dataProvider?.rows?.forEach(row => {
|
|
|
|
const value = row?.[valueColumn]
|
|
|
|
if (value) {
|
|
|
|
const label = row[labelColumn] || value
|
|
|
|
optionsSet[value] = { value, label }
|
|
|
|
}
|
|
|
|
})
|
|
|
|
return Object.values(optionsSet)
|
|
|
|
}
|
|
|
|
|
2021-08-17 11:32:01 +02:00
|
|
|
// Extract custom options
|
|
|
|
if (optionsSource === "custom" && customOptions) {
|
|
|
|
return customOptions
|
|
|
|
}
|
|
|
|
|
2021-08-16 12:24:25 +02:00
|
|
|
return []
|
|
|
|
}
|
2021-01-27 11:59:05 +01:00
|
|
|
</script>
|
|
|
|
|
2021-02-05 11:53:25 +01:00
|
|
|
<Field
|
|
|
|
{field}
|
|
|
|
{label}
|
2021-02-17 16:16:44 +01:00
|
|
|
{disabled}
|
2021-08-04 15:33:51 +02:00
|
|
|
{defaultValue}
|
2021-02-05 11:53:25 +01:00
|
|
|
type="options"
|
|
|
|
bind:fieldState
|
|
|
|
bind:fieldApi
|
2021-05-04 12:04:42 +02:00
|
|
|
bind:fieldSchema
|
|
|
|
>
|
2021-01-28 09:47:44 +01:00
|
|
|
{#if fieldState}
|
2021-08-03 21:34:26 +02:00
|
|
|
{#if optionsType === "select"}
|
2021-08-03 21:30:06 +02:00
|
|
|
<CoreSelect
|
|
|
|
value={$fieldState.value}
|
|
|
|
id={$fieldState.fieldId}
|
|
|
|
disabled={$fieldState.disabled}
|
|
|
|
error={$fieldState.error}
|
2021-08-16 12:24:25 +02:00
|
|
|
{options}
|
2021-08-03 21:30:06 +02:00
|
|
|
{placeholder}
|
|
|
|
on:change={e => fieldApi.setValue(e.detail)}
|
2021-08-16 12:24:25 +02:00
|
|
|
getOptionLabel={flatOptions ? x => x : x => x.label}
|
|
|
|
getOptionValue={flatOptions ? x => x : x => x.value}
|
2021-08-03 21:30:06 +02:00
|
|
|
/>
|
2021-08-03 21:34:26 +02:00
|
|
|
{:else if optionsType === "radio"}
|
2021-08-03 21:30:06 +02:00
|
|
|
<RadioGroup
|
|
|
|
value={$fieldState.value}
|
|
|
|
id={$fieldState.fieldId}
|
|
|
|
disabled={$fieldState.disabled}
|
|
|
|
error={$fieldState.error}
|
|
|
|
options={fieldSchema?.constraints?.inclusion ?? []}
|
|
|
|
on:change={e => fieldApi.setValue(e.detail)}
|
|
|
|
/>
|
|
|
|
{/if}
|
2021-01-28 09:47:44 +01:00
|
|
|
{/if}
|
2021-02-05 11:53:25 +01:00
|
|
|
</Field>
|