2021-01-26 09:55:44 +01:00
|
|
|
<script>
|
2021-01-27 11:59:05 +01:00
|
|
|
import "@spectrum-css/fieldlabel/dist/index-vars.css"
|
2021-01-26 09:55:44 +01:00
|
|
|
import { setContext, getContext, onMount } from "svelte"
|
|
|
|
import { writable, get } from "svelte/store"
|
2021-01-26 15:40:44 +01:00
|
|
|
import { createValidatorFromConstraints } from "./validation"
|
2021-01-26 09:55:44 +01:00
|
|
|
|
|
|
|
export let datasource
|
|
|
|
export let theme
|
|
|
|
export let size
|
|
|
|
|
2021-01-28 09:47:44 +01:00
|
|
|
const { styleable, API, setBindableValue } = getContext("sdk")
|
2021-01-26 09:55:44 +01:00
|
|
|
const component = getContext("component")
|
|
|
|
|
|
|
|
let loaded = false
|
2021-01-26 15:40:44 +01:00
|
|
|
let schema
|
|
|
|
let table
|
2021-01-26 09:55:44 +01:00
|
|
|
let fieldMap = {}
|
|
|
|
|
|
|
|
// Form state contains observable data about the form
|
|
|
|
const formState = writable({ values: {}, errors: {}, valid: true })
|
2021-01-28 17:31:55 +01:00
|
|
|
$: updateFormState(fieldMap)
|
|
|
|
$: setBindableValue($component.id, $formState.values)
|
2021-01-26 09:55:44 +01:00
|
|
|
|
|
|
|
// Form API contains functions to control the form
|
|
|
|
const formApi = {
|
2021-01-28 17:31:55 +01:00
|
|
|
registerField: field => {
|
2021-01-26 09:55:44 +01:00
|
|
|
if (!field) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if (fieldMap[field] != null) {
|
|
|
|
return fieldMap[field]
|
|
|
|
}
|
2021-01-26 15:40:44 +01:00
|
|
|
|
|
|
|
// Create validation function based on field schema
|
|
|
|
const constraints = schema?.[field]?.constraints
|
|
|
|
const validate = createValidatorFromConstraints(constraints, field, table)
|
|
|
|
|
2021-01-26 09:55:44 +01:00
|
|
|
fieldMap[field] = {
|
|
|
|
fieldState: makeFieldState(field),
|
2021-01-28 17:31:55 +01:00
|
|
|
fieldApi: makeFieldApi(field, validate),
|
2021-01-27 11:59:05 +01:00
|
|
|
fieldSchema: schema?.[field] ?? {},
|
2021-01-26 09:55:44 +01:00
|
|
|
}
|
|
|
|
fieldMap = fieldMap
|
|
|
|
return fieldMap[field]
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// Provide both form API and state to children
|
2021-01-27 19:25:57 +01:00
|
|
|
setContext("form", { formApi, formState })
|
2021-01-26 09:55:44 +01:00
|
|
|
|
|
|
|
// Creates an API for a specific field
|
2021-01-28 19:03:44 +01:00
|
|
|
const makeFieldApi = (field, validate) => {
|
2021-01-26 09:55:44 +01:00
|
|
|
return {
|
|
|
|
setValue: value => {
|
|
|
|
const { fieldState } = fieldMap[field]
|
|
|
|
fieldState.update(state => {
|
|
|
|
state.value = value
|
|
|
|
state.error = validate ? validate(value) : null
|
|
|
|
state.valid = !state.error
|
|
|
|
return state
|
|
|
|
})
|
|
|
|
fieldMap = fieldMap
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Creates observable state data about a specific field
|
|
|
|
const makeFieldState = field => {
|
|
|
|
return writable({
|
|
|
|
field,
|
2021-01-28 09:47:44 +01:00
|
|
|
fieldId: `${Math.random()
|
|
|
|
.toString(32)
|
|
|
|
.substr(2)}/${field}`,
|
2021-01-26 09:55:44 +01:00
|
|
|
value: null,
|
|
|
|
error: null,
|
|
|
|
valid: true,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Updates the form states from the field data
|
|
|
|
const updateFormState = fieldMap => {
|
|
|
|
let values = {}
|
|
|
|
let errors = {}
|
|
|
|
Object.entries(fieldMap).forEach(([field, formField]) => {
|
|
|
|
const fieldState = get(formField.fieldState)
|
|
|
|
values[field] = fieldState.value
|
|
|
|
if (fieldState.error) {
|
|
|
|
errors[field] = fieldState.error
|
|
|
|
}
|
|
|
|
})
|
|
|
|
const valid = Object.keys(errors).length === 0
|
|
|
|
formState.set({ values, errors, valid })
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fetches the form schema from this form's datasource, if one exists
|
|
|
|
const fetchSchema = async () => {
|
|
|
|
if (!datasource?.tableId) {
|
|
|
|
schema = {}
|
2021-01-26 15:40:44 +01:00
|
|
|
table = null
|
2021-01-26 09:55:44 +01:00
|
|
|
} else {
|
2021-01-26 15:40:44 +01:00
|
|
|
table = await API.fetchTableDefinition(datasource?.tableId)
|
2021-01-26 09:55:44 +01:00
|
|
|
if (table) {
|
|
|
|
if (datasource.type === "query") {
|
2021-01-26 15:40:44 +01:00
|
|
|
console.log("No implementation for queries yet")
|
|
|
|
schema = {}
|
2021-01-26 09:55:44 +01:00
|
|
|
} else {
|
|
|
|
schema = table.schema || {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
loaded = true
|
|
|
|
}
|
|
|
|
|
|
|
|
// Load the form schema on mount
|
|
|
|
onMount(fetchSchema)
|
|
|
|
</script>
|
|
|
|
|
|
|
|
<div
|
|
|
|
lang="en"
|
|
|
|
dir="ltr"
|
|
|
|
use:styleable={$component.styles}
|
|
|
|
class={`spectrum ${size || 'spectrum--medium'} ${theme || 'spectrum--light'}`}>
|
2021-01-27 19:25:57 +01:00
|
|
|
{#if loaded}
|
|
|
|
<slot />
|
|
|
|
{/if}
|
2021-01-26 09:55:44 +01:00
|
|
|
</div>
|