summaryrefslogtreecommitdiff
path: root/hi-ui/src/lib/MessageInput.svelte
blob: e526fe873cef8c31d2214f3ab4c33ea552fb6818 (plain)
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
<script>
    import { tick } from 'svelte';
    import { postToChannel } from '../apiServer';
    import { activeChannel } from '../store';

    let input;
    let value;
    let disabled;
    activeChannel.subscribe((value) => {
        disabled = !value.isSet();
        if (input && !disabled) {
            input.focus();
        }
    });

    async function handleSubmit(event) {
        disabled = true;
        // TODO try/catch:
        await postToChannel($activeChannel.get(), value);
        value = '';
        disabled = false;
        await tick();
        input.focus();
    }
</script>

<form on:submit|preventDefault={handleSubmit} class="flex flex-row flex-nowrap">
    <input bind:this={input} bind:value={value} disabled={disabled} type="search" class="flex-auto h-6 input" />
    <button color="primary variant-filled-secondary" type="submit" class="flex-none w-4 h-4">&raquo;</button>
</form>