Add ability for BBUI text fields to update on change and default to true

This commit is contained in:
Andrew Kingston 2021-05-18 08:37:07 +01:00
parent bda46f3cf2
commit ea1583478a
4 changed files with 37 additions and 9 deletions

View File

@ -2,10 +2,11 @@
import "@spectrum-css/search/dist/index-vars.css" import "@spectrum-css/search/dist/index-vars.css"
import { createEventDispatcher } from "svelte" import { createEventDispatcher } from "svelte"
export let value = "" export let value = null
export let placeholder = null export let placeholder = null
export let disabled = false export let disabled = false
export let id = null export let id = null
export let updateOnChange = true
const dispatch = createEventDispatcher() const dispatch = createEventDispatcher()
let focus = false let focus = false
@ -23,6 +24,13 @@
updateValue(event.target.value) updateValue(event.target.value)
} }
const onInput = event => {
if (!updateOnChange) {
return
}
updateValue(event.target.value)
}
const updateValueOnEnter = event => { const updateValueOnEnter = event => {
if (event.key === "Enter") { if (event.key === "Enter") {
updateValue(event.target.value) updateValue(event.target.value)
@ -44,15 +52,18 @@
<use xlink:href="#spectrum-icon-18-Magnify" /> <use xlink:href="#spectrum-icon-18-Magnify" />
</svg> </svg>
<input <input
on:click
on:keyup={updateValueOnEnter}
{disabled} {disabled}
{id} {id}
value={value || ""} value={value || ""}
placeholder={placeholder || ""} placeholder={placeholder || ""}
on:click
on:blur
on:focus
on:input
on:blur={onBlur} on:blur={onBlur}
on:focus={onFocus} on:focus={onFocus}
on:input on:input={onInput}
on:keyup={updateValueOnEnter}
type="search" type="search"
class="spectrum-Textfield-input spectrum-Search-input" class="spectrum-Textfield-input spectrum-Search-input"
autocomplete="off" autocomplete="off"

View File

@ -2,13 +2,14 @@
import "@spectrum-css/textfield/dist/index-vars.css" import "@spectrum-css/textfield/dist/index-vars.css"
import { createEventDispatcher } from "svelte" import { createEventDispatcher } from "svelte"
export let value = "" export let value = null
export let placeholder = null export let placeholder = null
export let type = "text" export let type = "text"
export let disabled = false export let disabled = false
export let error = null export let error = null
export let id = null export let id = null
export let readonly = false export let readonly = false
export let updateOnChange = true
const dispatch = createEventDispatcher() const dispatch = createEventDispatcher()
let focus = false let focus = false
@ -37,7 +38,13 @@
} }
focus = false focus = false
updateValue(event.target.value) updateValue(event.target.value)
dispatch("blur") }
const onInput = event => {
if (readonly || !updateOnChange) {
return
}
updateValue(event.target.value)
} }
const updateValueOnEnter = event => { const updateValueOnEnter = event => {
@ -66,16 +73,19 @@
</svg> </svg>
{/if} {/if}
<input <input
on:click
on:keyup={updateValueOnEnter}
{disabled} {disabled}
{readonly} {readonly}
{id} {id}
value={value || ""} value={value || ""}
placeholder={placeholder || ""} placeholder={placeholder || ""}
on:click
on:blur
on:focus
on:input
on:blur={onBlur} on:blur={onBlur}
on:focus={onFocus} on:focus={onFocus}
on:input on:input={onInput}
on:keyup={updateValueOnEnter}
{type} {type}
class="spectrum-Textfield-input" class="spectrum-Textfield-input"
/> />

View File

@ -11,6 +11,7 @@
export let disabled = false export let disabled = false
export let readonly = false export let readonly = false
export let error = null export let error = null
export let updateOnChange = true
const dispatch = createEventDispatcher() const dispatch = createEventDispatcher()
const onChange = e => { const onChange = e => {
@ -21,6 +22,7 @@
<Field {label} {labelPosition} {error}> <Field {label} {labelPosition} {error}>
<TextField <TextField
{updateOnChange}
{error} {error}
{disabled} {disabled}
{readonly} {readonly}
@ -31,5 +33,6 @@
on:click on:click
on:input on:input
on:blur on:blur
on:focus
/> />
</Field> </Field>

View File

@ -8,6 +8,7 @@
export let labelPosition = "above" export let labelPosition = "above"
export let placeholder = null export let placeholder = null
export let disabled = false export let disabled = false
export let updateOnChange = true
const dispatch = createEventDispatcher() const dispatch = createEventDispatcher()
const onChange = e => { const onChange = e => {
@ -18,11 +19,14 @@
<Field {label} {labelPosition}> <Field {label} {labelPosition}>
<Search <Search
{updateOnChange}
{disabled} {disabled}
{value} {value}
{placeholder} {placeholder}
on:change={onChange} on:change={onChange}
on:click on:click
on:input on:input
on:focus
on:blur
/> />
</Field> </Field>