Add FieldGroup component to allow easy mixing of fields and other content in forms
This commit is contained in:
parent
6631fe2af8
commit
e1f40ea066
|
@ -8,6 +8,7 @@
|
||||||
"icon": "ri-file-edit-line",
|
"icon": "ri-file-edit-line",
|
||||||
"children": [
|
"children": [
|
||||||
"form",
|
"form",
|
||||||
|
"fieldgroup",
|
||||||
"stringfield",
|
"stringfield",
|
||||||
"numberfield",
|
"numberfield",
|
||||||
"optionsfield",
|
"optionsfield",
|
||||||
|
|
|
@ -1118,12 +1118,21 @@
|
||||||
"value": "spectrum--large"
|
"value": "spectrum--large"
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
|
}
|
||||||
|
]
|
||||||
},
|
},
|
||||||
|
"fieldgroup": {
|
||||||
|
"name": "Field Group",
|
||||||
|
"description": "A group of fields in a form.",
|
||||||
|
"icon": "ri-edit-box-line",
|
||||||
|
"styleable": true,
|
||||||
|
"hasChildren": true,
|
||||||
|
"settings": [
|
||||||
{
|
{
|
||||||
"type": "select",
|
"type": "select",
|
||||||
"label": "Labels",
|
"label": "Labels",
|
||||||
"key": "labelPosition",
|
"key": "labelPosition",
|
||||||
"defaultValue": "left",
|
"defaultValue": "above",
|
||||||
"options": [
|
"options": [
|
||||||
{
|
{
|
||||||
"label": "Left",
|
"label": "Left",
|
||||||
|
|
|
@ -0,0 +1,17 @@
|
||||||
|
<script>
|
||||||
|
import { getContext, setContext } from "svelte"
|
||||||
|
|
||||||
|
export let labelPosition = "above"
|
||||||
|
|
||||||
|
const { styleable } = getContext("sdk")
|
||||||
|
const component = getContext("component")
|
||||||
|
setContext("fieldGroup", { labelPosition })
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<div use:styleable={$component.styles}>
|
||||||
|
<form
|
||||||
|
class="spectrum-Form"
|
||||||
|
class:spectrum-Form--labelsAbove={labelPosition === 'above'}>
|
||||||
|
<slot />
|
||||||
|
</form>
|
||||||
|
</div>
|
|
@ -0,0 +1,14 @@
|
||||||
|
<script>
|
||||||
|
import { getContext } from "svelte"
|
||||||
|
|
||||||
|
const fieldGroupContext = getContext("fieldGroup")
|
||||||
|
const labelPosition = fieldGroupContext?.labelPosition || "above"
|
||||||
|
</script>
|
||||||
|
|
||||||
|
{#if fieldGroupContext}
|
||||||
|
<slot />
|
||||||
|
{:else}
|
||||||
|
<div class="spectrum-Form--labelsAbove">
|
||||||
|
<slot />
|
||||||
|
</div>
|
||||||
|
{/if}
|
|
@ -7,7 +7,6 @@
|
||||||
export let datasource
|
export let datasource
|
||||||
export let theme
|
export let theme
|
||||||
export let size
|
export let size
|
||||||
export let labelPosition = "left"
|
|
||||||
|
|
||||||
const { styleable, API } = getContext("sdk")
|
const { styleable, API } = getContext("sdk")
|
||||||
const component = getContext("component")
|
const component = getContext("component")
|
||||||
|
@ -51,7 +50,7 @@
|
||||||
}
|
}
|
||||||
|
|
||||||
// Provide both form API and state to children
|
// Provide both form API and state to children
|
||||||
setContext("form", { formApi, formState, labelPosition })
|
setContext("form", { formApi, formState })
|
||||||
|
|
||||||
// Creates an API for a specific field
|
// Creates an API for a specific field
|
||||||
const makeFieldApi = (field, validate) => {
|
const makeFieldApi = (field, validate) => {
|
||||||
|
@ -122,11 +121,7 @@
|
||||||
dir="ltr"
|
dir="ltr"
|
||||||
use:styleable={$component.styles}
|
use:styleable={$component.styles}
|
||||||
class={`spectrum ${size || 'spectrum--medium'} ${theme || 'spectrum--light'}`}>
|
class={`spectrum ${size || 'spectrum--medium'} ${theme || 'spectrum--light'}`}>
|
||||||
<form
|
|
||||||
class="spectrum-Form"
|
|
||||||
class:spectrum-Form--labelsAbove={labelPosition === 'above'}>
|
|
||||||
{#if loaded}
|
{#if loaded}
|
||||||
<slot />
|
<slot />
|
||||||
{/if}
|
{/if}
|
||||||
</form>
|
|
||||||
</div>
|
</div>
|
||||||
|
|
|
@ -18,7 +18,7 @@
|
||||||
let open = false
|
let open = false
|
||||||
$: options = fieldSchema?.constraints?.inclusion ?? []
|
$: options = fieldSchema?.constraints?.inclusion ?? []
|
||||||
$: placeholderText = placeholder || "Choose an option"
|
$: placeholderText = placeholder || "Choose an option"
|
||||||
$: isNull = $fieldState.value == null || $fieldState.value === ""
|
$: isNull = $fieldState?.value == null || $fieldState?.value === ""
|
||||||
|
|
||||||
// Update value on blur only
|
// Update value on blur only
|
||||||
const selectOption = value => {
|
const selectOption = value => {
|
||||||
|
|
|
@ -1,19 +1,22 @@
|
||||||
<script>
|
<script>
|
||||||
import Placeholder from "./Placeholder.svelte"
|
import Placeholder from "./Placeholder.svelte"
|
||||||
|
import FieldGroupFallback from "./FieldGroupFallback.svelte"
|
||||||
import { getContext } from "svelte"
|
import { getContext } from "svelte"
|
||||||
|
|
||||||
export let label
|
export let label
|
||||||
export let field
|
export let field
|
||||||
|
|
||||||
const formContext = getContext("form")
|
const formContext = getContext("form")
|
||||||
|
const fieldGroupContext = getContext("fieldGroup")
|
||||||
const { styleable } = getContext("sdk")
|
const { styleable } = getContext("sdk")
|
||||||
const component = getContext("component")
|
const component = getContext("component")
|
||||||
const { labelPosition, formApi } = formContext || {}
|
const { formApi } = formContext || {}
|
||||||
|
const labelPosition = fieldGroupContext?.labelPosition || "above"
|
||||||
const formField = formApi?.registerField(field) ?? {}
|
const formField = formApi?.registerField(field) ?? {}
|
||||||
const { fieldId, fieldState } = formField
|
const { fieldId, fieldState } = formField
|
||||||
|
|
||||||
$: labelPositionClass =
|
$: labelPositionClass =
|
||||||
labelPosition === "top" ? "" : `spectrum-FieldLabel--${labelPosition}`
|
labelPosition === "above" ? "" : `spectrum-FieldLabel--${labelPosition}`
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
{#if !fieldId}
|
{#if !fieldId}
|
||||||
|
@ -21,6 +24,7 @@
|
||||||
{:else if !formContext}
|
{:else if !formContext}
|
||||||
<Placeholder>Form components need to be wrapped in a Form</Placeholder>
|
<Placeholder>Form components need to be wrapped in a Form</Placeholder>
|
||||||
{:else}
|
{:else}
|
||||||
|
<FieldGroupFallback>
|
||||||
<div class="spectrum-Form-item" use:styleable={$component.styles}>
|
<div class="spectrum-Form-item" use:styleable={$component.styles}>
|
||||||
{#if label}
|
{#if label}
|
||||||
<label
|
<label
|
||||||
|
@ -36,6 +40,7 @@
|
||||||
{/if}
|
{/if}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
</FieldGroupFallback>
|
||||||
{/if}
|
{/if}
|
||||||
|
|
||||||
<style>
|
<style>
|
||||||
|
|
|
@ -20,7 +20,6 @@
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<SpectrumField {label} {field}>
|
<SpectrumField {label} {field}>
|
||||||
<div>
|
|
||||||
<div class="spectrum-Textfield" class:is-invalid={!$fieldState.valid}>
|
<div class="spectrum-Textfield" class:is-invalid={!$fieldState.valid}>
|
||||||
{#if !$fieldState.valid}
|
{#if !$fieldState.valid}
|
||||||
<svg
|
<svg
|
||||||
|
@ -38,5 +37,4 @@
|
||||||
{type}
|
{type}
|
||||||
class="spectrum-Textfield-input" />
|
class="spectrum-Textfield-input" />
|
||||||
</div>
|
</div>
|
||||||
</div>
|
|
||||||
</SpectrumField>
|
</SpectrumField>
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
export { default as form } from "./Form.svelte"
|
export { default as form } from "./Form.svelte"
|
||||||
|
export { default as fieldgroup } from "./FieldGroup.svelte"
|
||||||
export { default as stringfield } from "./StringField.svelte"
|
export { default as stringfield } from "./StringField.svelte"
|
||||||
export { default as numberfield } from "./NumberField.svelte"
|
export { default as numberfield } from "./NumberField.svelte"
|
||||||
export { default as optionsfield } from "./OptionsField.svelte"
|
export { default as optionsfield } from "./OptionsField.svelte"
|
||||||
|
|
Loading…
Reference in New Issue