Update StringField and OptionsField to use BBUI components

This commit is contained in:
Andrew Kingston 2021-04-15 11:51:18 +01:00
parent 10fe99346f
commit 13e5c5ad61
4 changed files with 19 additions and 76 deletions

View File

@ -98,13 +98,11 @@
const newValue = value == null ? defaultValue : value const newValue = value == null ? defaultValue : value
const newError = validate ? validate(newValue) : null const newError = validate ? validate(newValue) : null
const newValid = !newError
// Update field state // Update field state
fieldState.update(state => { fieldState.update(state => {
state.value = newValue state.value = newValue
state.error = newError state.error = newError
state.valid = newValid
return state return state
}) })
@ -120,7 +118,7 @@
return state return state
}) })
return newValid return !newError
} }
return { return {
setValue, setValue,
@ -138,7 +136,6 @@
fieldId: `id-${generateID()}`, fieldId: `id-${generateID()}`,
value: initialValues[field] ?? defaultValue, value: initialValues[field] ?? defaultValue,
error: null, error: null,
valid: true,
disabled: fieldDisabled, disabled: fieldDisabled,
}) })
} }

View File

@ -1,6 +1,6 @@
<script> <script>
import { Select } from "@budibase/bbui/internal"
import Field from "./Field.svelte" import Field from "./Field.svelte"
import Picker from "./Picker.svelte"
export let field export let field
export let label export let label
@ -10,18 +10,6 @@
let fieldState let fieldState
let fieldApi let fieldApi
let fieldSchema let fieldSchema
// Picker state
let open = false
$: options = fieldSchema?.constraints?.inclusion ?? []
$: placeholderText = placeholder || "Choose an option"
$: isNull = $fieldState?.value == null || $fieldState?.value === ""
$: fieldText = isNull ? placeholderText : $fieldState?.value
const selectOption = value => {
fieldApi.setValue(value)
open = false
}
</script> </script>
<Field <Field
@ -33,14 +21,13 @@
bind:fieldApi bind:fieldApi
bind:fieldSchema> bind:fieldSchema>
{#if fieldState} {#if fieldState}
<Picker <Select
bind:open value={$fieldState.value}
{fieldState} fieldId={$fieldState.fieldId}
{fieldText} disabled={$fieldState.disabled}
{options} error={$fieldState.error}
isPlaceholder={isNull} options={fieldSchema?.constraints?.inclusion ?? []}
placeholderOption={placeholderText} {placeholder}
isOptionSelected={option => option === $fieldState.value} on:change={e => fieldApi.setValue(e.detail)} />
onSelectOption={selectOption} />
{/if} {/if}
</Field> </Field>

View File

@ -1,5 +1,5 @@
<script> <script>
import "@spectrum-css/textfield/dist/index-vars.css" import { TextField } from "@budibase/bbui/internal"
import Field from "./Field.svelte" import Field from "./Field.svelte"
export let field export let field
@ -10,25 +10,6 @@
let fieldState let fieldState
let fieldApi let fieldApi
let input
const updateValue = value => {
if (type === "number") {
const float = parseFloat(value)
value = isNaN(float) ? null : float
}
fieldApi.setValue(value)
}
const onBlur = event => {
updateValue(event.target.value)
}
const updateValueOnEnter = event => {
if (event.key === "Enter") {
updateValue(event.target.value)
}
}
</script> </script>
<Field <Field
@ -39,34 +20,13 @@
bind:fieldState bind:fieldState
bind:fieldApi> bind:fieldApi>
{#if fieldState} {#if fieldState}
<div <TextField
class="spectrum-Textfield" value={$fieldState.value}
class:is-invalid={!$fieldState.valid} on:change={e => fieldApi.setValue(e.detail)}
class:is-disabled={$fieldState.disabled}>
{#if !$fieldState.valid}
<svg
class="spectrum-Icon spectrum-Icon--sizeM spectrum-Textfield-validationIcon"
focusable="false"
aria-hidden="true">
<use xlink:href="#spectrum-icon-18-Alert" />
</svg>
{/if}
<input
on:keyup={updateValueOnEnter}
bind:this={input}
disabled={$fieldState.disabled} disabled={$fieldState.disabled}
error={$fieldState.error}
id={$fieldState.fieldId} id={$fieldState.fieldId}
value={$fieldState.value || ''} {placeholder}
placeholder={placeholder || ''} {type} />
on:blur={onBlur}
{type}
class="spectrum-Textfield-input" />
</div>
{/if} {/if}
</Field> </Field>
<style>
.spectrum-Textfield {
width: 100%;
}
</style>

View File

@ -1,4 +1,3 @@
import "@budibase/bbui/dist/style.css"
import "@spectrum-css/vars/dist/spectrum-global.css" import "@spectrum-css/vars/dist/spectrum-global.css"
import "@spectrum-css/vars/dist/spectrum-medium.css" import "@spectrum-css/vars/dist/spectrum-medium.css"
import "@spectrum-css/vars/dist/spectrum-large.css" import "@spectrum-css/vars/dist/spectrum-large.css"