Add FieldGroup component to allow easy mixing of fields and other content in forms
This commit is contained in:
parent
bfebf0226a
commit
8df4de0ddb
|
@ -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
|
{#if loaded}
|
||||||
class="spectrum-Form"
|
<slot />
|
||||||
class:spectrum-Form--labelsAbove={labelPosition === 'above'}>
|
{/if}
|
||||||
{#if loaded}
|
|
||||||
<slot />
|
|
||||||
{/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,21 +24,23 @@
|
||||||
{: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}
|
||||||
<div class="spectrum-Form-item" use:styleable={$component.styles}>
|
<FieldGroupFallback>
|
||||||
{#if label}
|
<div class="spectrum-Form-item" use:styleable={$component.styles}>
|
||||||
<label
|
{#if label}
|
||||||
for={fieldId}
|
<label
|
||||||
class={`spectrum-FieldLabel spectrum-FieldLabel--sizeM spectrum-Form-itemLabel ${labelPositionClass}`}>
|
for={fieldId}
|
||||||
{label}
|
class={`spectrum-FieldLabel spectrum-FieldLabel--sizeM spectrum-Form-itemLabel ${labelPositionClass}`}>
|
||||||
</label>
|
{label}
|
||||||
{/if}
|
</label>
|
||||||
<div class="spectrum-Form-itemField">
|
|
||||||
<slot />
|
|
||||||
{#if $fieldState.error}
|
|
||||||
<div class="error">{$fieldState.error}</div>
|
|
||||||
{/if}
|
{/if}
|
||||||
|
<div class="spectrum-Form-itemField">
|
||||||
|
<slot />
|
||||||
|
{#if $fieldState.error}
|
||||||
|
<div class="error">{$fieldState.error}</div>
|
||||||
|
{/if}
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</FieldGroupFallback>
|
||||||
{/if}
|
{/if}
|
||||||
|
|
||||||
<style>
|
<style>
|
||||||
|
|
|
@ -20,23 +20,21 @@
|
||||||
</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
|
class="spectrum-Icon spectrum-Icon--sizeM spectrum-Textfield-validationIcon"
|
||||||
class="spectrum-Icon spectrum-Icon--sizeM spectrum-Textfield-validationIcon"
|
focusable="false"
|
||||||
focusable="false"
|
aria-hidden="true">
|
||||||
aria-hidden="true">
|
<use xlink:href="#spectrum-icon-18-Alert" />
|
||||||
<use xlink:href="#spectrum-icon-18-Alert" />
|
</svg>
|
||||||
</svg>
|
{/if}
|
||||||
{/if}
|
<input
|
||||||
<input
|
id={$fieldState.fieldId}
|
||||||
id={$fieldState.fieldId}
|
value={$fieldState.value || ''}
|
||||||
value={$fieldState.value || ''}
|
placeholder={placeholder || ''}
|
||||||
placeholder={placeholder || ''}
|
on:blur={onBlur}
|
||||||
on:blur={onBlur}
|
{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