|
|
|
@ -8,10 +8,51 @@ |
|
|
|
export let disabled = false |
|
|
|
export let optionsType = "select" |
|
|
|
export let defaultValue |
|
|
|
export let optionsSource = "schema" |
|
|
|
export let dataProvider |
|
|
|
export let labelColumn |
|
|
|
export let valueColumn |
|
|
|
|
|
|
|
let fieldState |
|
|
|
let fieldApi |
|
|
|
let fieldSchema |
|
|
|
|
|
|
|
$: 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) |
|
|
|
} |
|
|
|
|
|
|
|
return [] |
|
|
|
} |
|
|
|
</script> |
|
|
|
|
|
|
|
<Field |
|
|
|
@ -31,9 +72,11 @@ |
|
|
|
id={$fieldState.fieldId} |
|
|
|
disabled={$fieldState.disabled} |
|
|
|
error={$fieldState.error} |
|
|
|
options={fieldSchema?.constraints?.inclusion ?? []} |
|
|
|
{options} |
|
|
|
{placeholder} |
|
|
|
on:change={e => fieldApi.setValue(e.detail)} |
|
|
|
getOptionLabel={flatOptions ? x => x : x => x.label} |
|
|
|
getOptionValue={flatOptions ? x => x : x => x.value} |
|
|
|
/> |
|
|
|
{:else if optionsType === "radio"} |
|
|
|
<RadioGroup |
|
|
|
|