| 1 | <script lang="ts"> |
| 2 | import { createEventDispatcher, onMount } from "svelte"; |
| 3 | const dispatch = createEventDispatcher(); |
| 4 | |
| 5 | export let readonly = false; |
| 6 | export let label = null; |
| 7 | export let placeholder = ""; |
| 8 | export let hintText = ""; |
| 9 | export let invalidText = ""; |
| 10 | |
| 11 | export let value = ""; |
| 12 | export let _class = ""; |
| 13 | export let extra_small = false; |
| 14 | export let medium = false; |
| 15 | export let invalid = false; |
| 16 | |
| 17 | let ref: HTMLElement = null; |
| 18 | |
| 19 | onMount(() => { |
| 20 | ref.focus(); |
| 21 | }) |
| 22 | |
| 23 | let _ = null; |
| 24 | async function handleInput(e) { |
| 25 | clearTimeout(_); |
| 26 | invalid = false; |
| 27 | _ = setTimeout(() => { |
| 28 | dispatch("d_input", {value: value}) |
| 29 | }, 500); |
| 30 | } |
| 31 | </script> |
| 32 | |
| 33 | <div class="input-container {_class}"> |
| 34 | {#if label} |
| 35 | <label for="textInput">{label}:</label> |
| 36 | {/if} |
| 37 | |
| 38 | <input on:input={handleInput} |
| 39 | bind:this={ref} |
| 40 | autocomplete="off" |
| 41 | class="mousetrap" |
| 42 | class:invalid |
| 43 | class:medium |
| 44 | class:extra_small |
| 45 | class:readonly |
| 46 | bind:value type="text" name="textInput" id="textInput" readonly={readonly} placeholder={placeholder}> |
| 47 | {#if hintText && !invalid} |
| 48 | <div class="hint-text" >{hintText}</div> |
| 49 | {/if} |
| 50 | {#if invalidText && invalid} |
| 51 | <div class="hint-text" class:invalid>{invalidText}</div> |
| 52 | {/if} |
| 53 | </div> |
| 54 | |
| 55 | <style lang="scss"> |
| 56 | .input-container { |
| 57 | display: flex; |
| 58 | flex-direction: column; |
| 59 | width: 100%; |
| 60 | } |
| 61 | label { |
| 62 | font-size: 0.9rem; |
| 63 | } |
| 64 | input { |
| 65 | &.extra_small { |
| 66 | width: 12% !important; |
| 67 | padding: 0.4rem !important; |
| 68 | } |
| 69 | &.medium { |
| 70 | width: 30% !important; |
| 71 | padding: 0.4rem !important; |
| 72 | } |
| 73 | border: none; |
| 74 | padding: 0.6em 0.4em 0.6em 0.8em; |
| 75 | width: 100%; |
| 76 | } |
| 77 | .hint-text { |
| 78 | padding: 0 2px; |
| 79 | font-size: 0.9rem; |
| 80 | } |
| 81 | .readonly { |
| 82 | cursor: not-allowed; |
| 83 | } |
| 84 | </style> |