This repository has been archived on 2024-03-28. You can view files and clone it, but cannot push or open issues or pull requests.
atsebay.t/ui/src/components/ListInput.svelte

44 lines
1.1 KiB
Svelte

<script>
import { createEventDispatcher, tick } from 'svelte';
const dispatch = createEventDispatcher();
let className = '';
export { className as class };
export let qid = "";
export let kind = "list";
export let value = "";
export let placeholder = "";
let nb;
let mval = [];
$: mval = value?value.split('\n'):[];
$: {
nb = Number(kind.substring(4));
while (mval.length < nb) {
mval.push("");
}
}
</script>
{#each mval as v, i}
<input
id={qid + "l" + i}
class={className}
type="text"
bind:value={mval[i]}
{placeholder}
on:change={() => { value = mval.join('\n').trim(); dispatch("change"); }}
on:blur={() => { if (kind == "list" && i == mval.length - 1 && mval[i] == "") { mval.pop(); mval = mval; } }}
>
{/each}
{#if kind == "list" && (mval.length == 0 || mval[mval.length-1] != "")}
<input
class={className}
type="text"
{placeholder}
on:focus={() => { mval.push(""); mval = mval; tick().then(() => document.getElementById(qid + "l" + (mval.length-1)).focus()); }}
>
{/if}