Update StringField and OptionsField to use BBUI components
This commit is contained in:
parent
b741d9e9d9
commit
c94bbf9571
|
@ -98,13 +98,11 @@
|
|||
|
||||
const newValue = value == null ? defaultValue : value
|
||||
const newError = validate ? validate(newValue) : null
|
||||
const newValid = !newError
|
||||
|
||||
// Update field state
|
||||
fieldState.update(state => {
|
||||
state.value = newValue
|
||||
state.error = newError
|
||||
state.valid = newValid
|
||||
return state
|
||||
})
|
||||
|
||||
|
@ -120,7 +118,7 @@
|
|||
return state
|
||||
})
|
||||
|
||||
return newValid
|
||||
return !newError
|
||||
}
|
||||
return {
|
||||
setValue,
|
||||
|
@ -138,7 +136,6 @@
|
|||
fieldId: `id-${generateID()}`,
|
||||
value: initialValues[field] ?? defaultValue,
|
||||
error: null,
|
||||
valid: true,
|
||||
disabled: fieldDisabled,
|
||||
})
|
||||
}
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
<script>
|
||||
import { Select } from "@budibase/bbui/internal"
|
||||
import Field from "./Field.svelte"
|
||||
import Picker from "./Picker.svelte"
|
||||
|
||||
export let field
|
||||
export let label
|
||||
|
@ -10,18 +10,6 @@
|
|||
let fieldState
|
||||
let fieldApi
|
||||
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>
|
||||
|
||||
<Field
|
||||
|
@ -33,14 +21,13 @@
|
|||
bind:fieldApi
|
||||
bind:fieldSchema>
|
||||
{#if fieldState}
|
||||
<Picker
|
||||
bind:open
|
||||
{fieldState}
|
||||
{fieldText}
|
||||
{options}
|
||||
isPlaceholder={isNull}
|
||||
placeholderOption={placeholderText}
|
||||
isOptionSelected={option => option === $fieldState.value}
|
||||
onSelectOption={selectOption} />
|
||||
<Select
|
||||
value={$fieldState.value}
|
||||
fieldId={$fieldState.fieldId}
|
||||
disabled={$fieldState.disabled}
|
||||
error={$fieldState.error}
|
||||
options={fieldSchema?.constraints?.inclusion ?? []}
|
||||
{placeholder}
|
||||
on:change={e => fieldApi.setValue(e.detail)} />
|
||||
{/if}
|
||||
</Field>
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
<script>
|
||||
import "@spectrum-css/textfield/dist/index-vars.css"
|
||||
import { TextField } from "@budibase/bbui/internal"
|
||||
import Field from "./Field.svelte"
|
||||
|
||||
export let field
|
||||
|
@ -10,25 +10,6 @@
|
|||
|
||||
let fieldState
|
||||
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>
|
||||
|
||||
<Field
|
||||
|
@ -39,34 +20,13 @@
|
|||
bind:fieldState
|
||||
bind:fieldApi>
|
||||
{#if fieldState}
|
||||
<div
|
||||
class="spectrum-Textfield"
|
||||
class:is-invalid={!$fieldState.valid}
|
||||
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}
|
||||
id={$fieldState.fieldId}
|
||||
value={$fieldState.value || ''}
|
||||
placeholder={placeholder || ''}
|
||||
on:blur={onBlur}
|
||||
{type}
|
||||
class="spectrum-Textfield-input" />
|
||||
</div>
|
||||
<TextField
|
||||
value={$fieldState.value}
|
||||
on:change={e => fieldApi.setValue(e.detail)}
|
||||
disabled={$fieldState.disabled}
|
||||
error={$fieldState.error}
|
||||
id={$fieldState.fieldId}
|
||||
{placeholder}
|
||||
{type} />
|
||||
{/if}
|
||||
</Field>
|
||||
|
||||
<style>
|
||||
.spectrum-Textfield {
|
||||
width: 100%;
|
||||
}
|
||||
</style>
|
||||
|
|
|
@ -1,4 +1,3 @@
|
|||
import "@budibase/bbui/dist/style.css"
|
||||
import "@spectrum-css/vars/dist/spectrum-global.css"
|
||||
import "@spectrum-css/vars/dist/spectrum-medium.css"
|
||||
import "@spectrum-css/vars/dist/spectrum-large.css"
|
||||
|
|
Loading…
Reference in New Issue