MapComplete/src/UI/InputElement/ValidatedInput.svelte

164 lines
4.6 KiB
Svelte
Raw Normal View History

<script lang="ts">
import { UIEventSource } from "../../Logic/UIEventSource";
import type { ValidatorType } from "./Validators";
import Validators from "./Validators";
import { ExclamationIcon } from "@rgossiaux/svelte-heroicons/solid";
import { Translation } from "../i18n/Translation";
import { createEventDispatcher, onDestroy } from "svelte";
import { Validator } from "./Validator";
import { Unit } from "../../Models/Unit";
import UnitInput from "../Popup/UnitInput.svelte";
import { Utils } from "../../Utils";
import { twMerge } from "tailwind-merge";
export let type: ValidatorType;
export let feedback: UIEventSource<Translation> | undefined = undefined;
export let cls: string = undefined;
export let getCountry: () => string | undefined;
export let placeholder: string | Translation | undefined;
export let unit: Unit = undefined;
/**
* Valid state, exported to the calling component
*/
export let value: UIEventSource<string | undefined>;
2023-11-09 16:30:26 +01:00
/**
* Internal state bound to the input element.
*
* This is only copied to 'value' when appropriate so that no invalid values leak outside;
* Additionally, the unit is added when copying
*/
let _value = new UIEventSource(value.data ?? "");
2023-11-09 16:30:26 +01:00
let validator: Validator = Validators.get(type ?? "string");
2023-11-09 16:30:26 +01:00
if (validator === undefined) {
console.warn("Didn't find a validator for type", type);
2023-11-09 16:30:26 +01:00
}
let selectedUnit: UIEventSource<string> = new UIEventSource<string>(undefined);
let _placeholder = placeholder ?? validator?.getPlaceholder() ?? type;
2023-11-09 16:30:26 +01:00
function initValueAndDenom() {
if (unit && value.data) {
const [v, denom] = unit?.findDenomination(value.data, getCountry);
2023-11-09 16:30:26 +01:00
if (denom) {
_value.setData(v);
selectedUnit.setData(denom.canonical);
2023-11-09 16:30:26 +01:00
} else {
_value.setData(value.data ?? "");
2023-11-09 16:30:26 +01:00
}
} else {
_value.setData(value.data ?? "");
}
2023-11-09 16:30:26 +01:00
}
initValueAndDenom();
2023-06-11 01:32:30 +02:00
2023-11-09 16:30:26 +01:00
$: {
// The type changed -> reset some values
validator = Validators.get(type ?? "string");
2023-06-11 01:32:30 +02:00
_placeholder = placeholder ?? validator?.getPlaceholder() ?? type;
feedback?.setData(validator?.getFeedback(_value.data, getCountry));
initValueAndDenom();
2023-11-09 16:30:26 +01:00
}
function setValues() {
// Update the value stores
const v = _value.data;
2023-11-09 16:30:26 +01:00
if (!validator?.isValid(v, getCountry) || v === "") {
feedback?.setData(validator?.getFeedback(v, getCountry));
value.setData("");
return;
2023-06-16 02:36:11 +02:00
}
2023-06-11 01:32:30 +02:00
2023-11-09 16:30:26 +01:00
if (unit !== undefined && isNaN(Number(v))) {
value.setData(undefined);
return;
2023-03-31 03:28:11 +02:00
}
2023-10-20 19:04:55 +02:00
feedback?.setData(undefined);
2023-11-09 16:30:26 +01:00
if (selectedUnit.data) {
value.setData(unit.toOsm(v, selectedUnit.data))
2023-11-09 16:30:26 +01:00
} else {
value.setData(v);
2023-11-09 16:30:26 +01:00
}
}
onDestroy(_value.addCallbackAndRun((_) => setValues()));
if (unit === undefined) {
onDestroy(
value.addCallbackAndRunD((fromUpstream) => {
if (_value.data !== fromUpstream && fromUpstream !== "") {
_value.setData(fromUpstream);
}
})
);
}else{
// Handled by the UnitInput
}
onDestroy(selectedUnit.addCallback((_) => setValues()));
2023-11-09 16:30:26 +01:00
if (validator === undefined) {
throw (
"Not a valid type (no validator found) for type '" +
type +
"'; did you perhaps mean one of: " +
Utils.sortedByLevenshteinDistance(
type,
Validators.AllValidators.map((v) => v.name),
(v) => v
)
.slice(0, 5)
.join(", ")
);
2023-11-09 16:30:26 +01:00
}
const isValid = _value.map((v) => validator?.isValid(v, getCountry) ?? true);
2023-11-09 16:30:26 +01:00
let htmlElem: HTMLInputElement;
2023-11-09 16:30:26 +01:00
let dispatch = createEventDispatcher<{ selected; submit }>();
2023-11-09 16:30:26 +01:00
$: {
if (htmlElem !== undefined) {
htmlElem.onfocus = () => dispatch("selected");
2023-11-09 16:30:26 +01:00
}
}
/**
* Dispatches the submit, but only if the value is valid
*/
function sendSubmit() {
if (feedback?.data) {
console.log("Not sending a submit as there is feedback");
2023-10-20 19:04:55 +02:00
}
dispatch("submit");
2023-11-09 16:30:26 +01:00
}
</script>
2023-06-20 01:32:24 +02:00
{#if validator?.textArea}
2023-11-09 16:30:26 +01:00
<form on:submit|preventDefault={() => sendSubmit()}>
<textarea
class="w-full"
bind:value={$_value}
inputmode={validator?.inputmode ?? "text"}
placeholder={_placeholder}
/>
</form>
{:else}
2023-11-09 16:30:26 +01:00
<form class={twMerge("inline-flex", cls)} on:submit|preventDefault={() => sendSubmit()}>
<input
bind:this={htmlElem}
bind:value={$_value}
class="w-full"
inputmode={validator?.inputmode ?? "text"}
placeholder={_placeholder}
/>
{#if !$isValid}
<ExclamationIcon class="-ml-6 h-6 w-6" />
{/if}
{#if unit !== undefined}
<UnitInput {unit} {selectedUnit} textValue={_value} upstreamValue={value} {getCountry} />
2023-11-09 16:30:26 +01:00
{/if}
</form>
{/if}