2021-01-27 11:59:05 +01:00
|
|
|
<script>
|
2021-02-05 11:53:25 +01:00
|
|
|
import Field from "./Field.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-02-17 16:16:44 +01:00
|
|
|
export let disabled = false
|
2021-01-27 11:59:05 +01:00
|
|
|
|
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-02-05 11:53:25 +01:00
|
|
|
<Field
|
|
|
|
{field}
|
|
|
|
{label}
|
2021-02-17 16:16:44 +01:00
|
|
|
{disabled}
|
2021-02-05 11:53:25 +01:00
|
|
|
type="options"
|
|
|
|
bind:fieldState
|
|
|
|
bind:fieldApi
|
|
|
|
bind:fieldSchema>
|
2021-01-28 09:47:44 +01:00
|
|
|
{#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-02-05 11:53:25 +01:00
|
|
|
</Field>
|