1
0
Fork 0
mirror of synced 2024-10-05 20:44:47 +13:00

Update text field values on Enter press

This commit is contained in:
Andrew Kingston 2021-02-03 15:02:50 +00:00
parent b701416f7c
commit 7efdff487e

View file

@ -1,4 +1,5 @@
<script> <script>
import { onMount } from "svelte"
import "@spectrum-css/textfield/dist/index-vars.css" import "@spectrum-css/textfield/dist/index-vars.css"
import SpectrumField from "./SpectrumField.svelte" import SpectrumField from "./SpectrumField.svelte"
@ -9,15 +10,25 @@
let fieldState let fieldState
let fieldApi let fieldApi
let input
const onBlur = event => { const updateValue = value => {
let value = event.target.value
if (type === "number") { if (type === "number") {
const float = parseFloat(value) const float = parseFloat(value)
value = isNaN(float) ? null : float value = isNaN(float) ? null : float
} }
fieldApi.setValue(value) fieldApi.setValue(value)
} }
const onBlur = event => {
updateValue(event.target.value)
}
const updateValueOnEnter = event => {
if (event.key === "Enter") {
updateValue(event.target.value)
}
}
</script> </script>
<SpectrumField {label} {field} bind:fieldState bind:fieldApi> <SpectrumField {label} {field} bind:fieldState bind:fieldApi>
@ -32,6 +43,8 @@
</svg> </svg>
{/if} {/if}
<input <input
on:keyup={updateValueOnEnter}
bind:this={input}
id={$fieldState.fieldId} id={$fieldState.fieldId}
value={$fieldState.value || ''} value={$fieldState.value || ''}
placeholder={placeholder || ''} placeholder={placeholder || ''}