2021-01-27 11:59:05 +01:00
|
|
|
<script>
|
|
|
|
import SpectrumField from "./SpectrumField.svelte"
|
2021-02-01 14:15:35 +01:00
|
|
|
import Picker from "./Picker.svelte"
|
2021-01-27 11:59:05 +01:00
|
|
|
|
|
|
|
export let field
|
|
|
|
export let label
|
|
|
|
export let placeholder
|
|
|
|
|
2021-01-28 09:47:44 +01:00
|
|
|
let fieldState
|
|
|
|
let fieldApi
|
|
|
|
let fieldSchema
|
2021-01-27 11:59:05 +01:00
|
|
|
|
|
|
|
// Picker state
|
|
|
|
let open = false
|
|
|
|
$: options = fieldSchema?.constraints?.inclusion ?? []
|
|
|
|
$: placeholderText = placeholder || "Choose an option"
|
2021-01-27 19:25:57 +01:00
|
|
|
$: isNull = $fieldState?.value == null || $fieldState?.value === ""
|
2021-02-01 14:15:35 +01:00
|
|
|
$: fieldText = isNull ? placeholderText : $fieldState?.value
|
2021-01-27 11:59:05 +01:00
|
|
|
|
|
|
|
const selectOption = value => {
|
|
|
|
fieldApi.setValue(value)
|
|
|
|
open = false
|
|
|
|
}
|
|
|
|
</script>
|
|
|
|
|
2021-01-28 09:47:44 +01:00
|
|
|
<SpectrumField {field} {label} bind:fieldState bind:fieldApi bind:fieldSchema>
|
|
|
|
{#if fieldState}
|
2021-02-01 14:15:35 +01:00
|
|
|
<Picker
|
|
|
|
bind:open
|
|
|
|
{fieldState}
|
|
|
|
{fieldText}
|
|
|
|
{options}
|
|
|
|
isPlaceholder={isNull}
|
|
|
|
placeholderOption={placeholderText}
|
|
|
|
isOptionSelected={option => option === $fieldState.value}
|
|
|
|
onSelectOption={selectOption} />
|
2021-01-28 09:47:44 +01:00
|
|
|
{/if}
|
2021-01-27 11:59:05 +01:00
|
|
|
</SpectrumField>
|