|
<script lang="ts"> |
|
import { createEventDispatcher } from "svelte"; |
|
|
|
import FieldInput from "@graphite/components/widgets/inputs/FieldInput.svelte"; |
|
|
|
const dispatch = createEventDispatcher<{ commitText: string }>(); |
|
|
|
|
|
export let label: string | undefined = undefined; |
|
export let tooltip: string | undefined = undefined; |
|
export let placeholder: string | undefined = undefined; |
|
|
|
export let disabled = false; |
|
|
|
export let value: string; |
|
|
|
export let centered = false; |
|
export let minWidth = 0; |
|
|
|
let className = ""; |
|
export { className as class }; |
|
export let classes: Record<string, boolean> = {}; |
|
|
|
let self: FieldInput | undefined; |
|
let editing = false; |
|
|
|
function onTextFocused() { |
|
editing = true; |
|
|
|
self?.selectAllText(value); |
|
} |
|
|
|
|
|
|
|
function onTextChanged() { |
|
|
|
if (!editing) return; |
|
|
|
onTextChangeCanceled(); |
|
|
|
|
|
if (self) dispatch("commitText", self.getValue()); |
|
|
|
|
|
self?.setInputElementValue(self.getValue()); |
|
} |
|
|
|
function onTextChangeCanceled() { |
|
editing = false; |
|
|
|
self?.unFocus(); |
|
} |
|
|
|
export function focus() { |
|
self?.focus(); |
|
} |
|
|
|
export function element(): HTMLInputElement | HTMLTextAreaElement | undefined { |
|
return self?.element(); |
|
} |
|
</script> |
|
|
|
<FieldInput |
|
class={`text-input ${className}`.trim()} |
|
classes={{ centered, ...classes }} |
|
styles={{ ...(minWidth > 0 ? { "min-width": `${minWidth}px` } : {}) }} |
|
{value} |
|
on:value |
|
on:textFocused={onTextFocused} |
|
on:textChanged={onTextChanged} |
|
on:textChangeCanceled={onTextChangeCanceled} |
|
spellcheck={true} |
|
{label} |
|
{disabled} |
|
{tooltip} |
|
{placeholder} |
|
bind:this={self} |
|
/> |
|
|
|
<style lang="scss" global> |
|
.text-input { |
|
flex-shrink: 0; |
|
|
|
input { |
|
text-align: left; |
|
} |
|
|
|
&.centered { |
|
input:not(:focus) { |
|
text-align: center; |
|
} |
|
} |
|
} |
|
</style> |
|
|