update tests
Before Width: | Height: | Size: 28 KiB After Width: | Height: | Size: 28 KiB |
Before Width: | Height: | Size: 36 KiB After Width: | Height: | Size: 36 KiB |
Before Width: | Height: | Size: 26 KiB After Width: | Height: | Size: 28 KiB |
Before Width: | Height: | Size: 27 KiB After Width: | Height: | Size: 29 KiB |
Before Width: | Height: | Size: 40 KiB After Width: | Height: | Size: 41 KiB |
Before Width: | Height: | Size: 125 KiB After Width: | Height: | Size: 132 KiB |
Before Width: | Height: | Size: 70 KiB After Width: | Height: | Size: 71 KiB |
Before Width: | Height: | Size: 23 KiB After Width: | Height: | Size: 25 KiB |
@ -1,21 +1,28 @@
|
|||||||
<div class="field label small" class:suffix>
|
<div class="field label small" class:suffix>
|
||||||
<slot>
|
<slot>
|
||||||
<input type="text" bind:value on:change />
|
<input id={formId} type="text" bind:value on:change />
|
||||||
</slot>
|
</slot>
|
||||||
{#if label}
|
{#if label}
|
||||||
<label class:active>{label}</label>
|
<label for={formId} class:active>{label}</label>
|
||||||
{/if}
|
{/if}
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<script>
|
<script>
|
||||||
|
import { getContext } from "svelte";
|
||||||
|
|
||||||
export let label = "";
|
export let label = "";
|
||||||
export let value = true;
|
export let value = true;
|
||||||
export let placeholder = label;
|
export let placeholder = label;
|
||||||
|
|
||||||
export let suffix = false;
|
export let suffix = false;
|
||||||
export let activeLabel = undefined;
|
export let activeLabel = undefined;
|
||||||
|
export let idPrefix = "formId";
|
||||||
|
|
||||||
$: active = typeof activeLabel === "boolean" ? activeLabel : value;
|
$: active = typeof activeLabel === "boolean" ? activeLabel : value;
|
||||||
|
|
||||||
|
const genUid = getContext("genUid") ?? (() => "genUid missing");
|
||||||
|
|
||||||
|
const formId = genUid(idPrefix);
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<style>
|
<style>
|
||||||
|
Before Width: | Height: | Size: 23 KiB After Width: | Height: | Size: 25 KiB |
Before Width: | Height: | Size: 28 KiB After Width: | Height: | Size: 28 KiB |
Before Width: | Height: | Size: 36 KiB After Width: | Height: | Size: 36 KiB |
Before Width: | Height: | Size: 26 KiB After Width: | Height: | Size: 28 KiB |
Before Width: | Height: | Size: 27 KiB After Width: | Height: | Size: 29 KiB |
Before Width: | Height: | Size: 40 KiB After Width: | Height: | Size: 41 KiB |
Before Width: | Height: | Size: 125 KiB After Width: | Height: | Size: 132 KiB |
Before Width: | Height: | Size: 70 KiB After Width: | Height: | Size: 71 KiB |
17
src/lib/store/uids.js
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
import { writable, get } from "svelte/store";
|
||||||
|
|
||||||
|
export default () => {
|
||||||
|
const store = writable(0);
|
||||||
|
|
||||||
|
const genUid = (prefix = "") => {
|
||||||
|
store.update((x) => x + 1);
|
||||||
|
return prefix.length
|
||||||
|
? [prefix, "" + get(store)].join("-")
|
||||||
|
: "" + get(store);
|
||||||
|
};
|
||||||
|
|
||||||
|
return {
|
||||||
|
store,
|
||||||
|
genUid,
|
||||||
|
};
|
||||||
|
};
|
8
src/lib/store/uids.test.js
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
import uids from "./uids";
|
||||||
|
|
||||||
|
test("basic", () => {
|
||||||
|
const { genUid } = uids();
|
||||||
|
|
||||||
|
expect(genUid()).toEqual("1");
|
||||||
|
expect(genUid("potato")).toEqual("potato-2");
|
||||||
|
});
|
@ -5,8 +5,10 @@
|
|||||||
<script>
|
<script>
|
||||||
import { setContext } from "svelte";
|
import { setContext } from "svelte";
|
||||||
|
|
||||||
import { createApi } from "$lib/store/api.ts";
|
import { createApi } from "$lib/store/api.js";
|
||||||
|
import uids from "$lib/store/uids.js";
|
||||||
import Layout from "$lib/components/MainLayout.svelte";
|
import Layout from "$lib/components/MainLayout.svelte";
|
||||||
|
|
||||||
setContext("api", createApi());
|
setContext("api", createApi());
|
||||||
|
setContext("genUid", uids());
|
||||||
</script>
|
</script>
|
||||||
|
@ -17,9 +17,9 @@ test("we can pass a store", async () => {
|
|||||||
|
|
||||||
api.dispatch.updateIdentification({ shipClass: "Bonobo" });
|
api.dispatch.updateIdentification({ shipClass: "Bonobo" });
|
||||||
|
|
||||||
const { getByPlaceholderText } = render(Page, { context });
|
const { getByLabelText } = render(Page, { context });
|
||||||
|
|
||||||
const classInput = getByPlaceholderText("ship class");
|
const classInput = getByLabelText("ship class");
|
||||||
await fireEvent.input(classInput, { target: { value: "Tarzan" } });
|
await fireEvent.input(classInput, { target: { value: "Tarzan" } });
|
||||||
expect(classInput.value).toEqual("Tarzan");
|
expect(classInput.value).toEqual("Tarzan");
|
||||||
|
|
||||||
|