aboutsummaryrefslogtreecommitdiff
path: root/src/lib/Tools/InputTemplate.svelte
blob: 52b9fd46589568f2aa39e37d8beed18485e09963 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
<script lang="ts">
  import { clearAllParameters } from '$lib/Utility/parameters';
  import { onMount } from 'svelte';
  import SettingHint from '$lib/Settings/SettingHint.svelte';

  interface Props {
    field: string;
    submission: string;
    event?: string | undefined;
    submitText: string;
    saveParameters?: string[];
    onSubmit?: any;
    preserveCase?: boolean;
    prompt?: any;
    hint?: string | undefined;
    children?: import('svelte').Snippet;
  }

  let {
    field,
    submission = $bindable(),
    event = undefined,
    submitText,
    saveParameters = [],
    onSubmit = () => {
    return;
  },
    preserveCase = false,
    prompt = `Enter a ${
    preserveCase ? field : field.toLowerCase()
  } to search for to continue.`,
    hint = undefined,
    children
  }: Props = $props();

  let input = $state('');

  onMount(() => clearAllParameters(saveParameters));
</script>

<div class="card">
  <p>
    <!-- svelte-ignore missing_declaration -->
    <input
      type="text"
      placeholder={field}
      bind:value={input}
      onkeypress={(e) => {
        if (e.key === 'Enter') {
          submission = input;

          onSubmit();

          // eslint-disable-next-line no-undef
          if (event) umami.track(event);
        }
      }}
    />
    <button
      class="button-lined"
      onclick={() => {
        submission = input;

        onSubmit();
      }}
      title="Or click your Enter key"
      data-umami-event={event}
    >
      {submitText}
    </button>

    {#if hint !== undefined}
      <br />
      <div style="margin-top: .25rem;">
        <SettingHint>{hint}</SettingHint>
      </div>
    {/if}
  </p>

  {#if submission !== ''}
    {@render children?.()}
  {:else}
    <p></p>

    {prompt}
  {/if}
</div>