This repository has been archived on 2024-08-06. You can view files and clone it, but cannot push or open issues or pull requests.
comicinfo-editor-v2/src/lib/IntegerInput.svelte
2023-10-11 23:04:07 +02:00

45 lines
No EOL
1.3 KiB
Svelte

<script lang="ts">
export let id: string;
export let value: number;
export let negative = false;
export let defaultValue = 0;
if (typeof value === "undefined") {
value = defaultValue;
}
function inputInteger(event: Event & {currentTarget: (EventTarget & HTMLInputElement)}) {
let newValue = event.currentTarget.value;
if (newValue.startsWith("0") && newValue.length > 1) {
newValue = newValue.slice(1, newValue.length)
}
if (newValue != "" && !isNaN(Number(newValue))) {
value = Number(newValue);
}
else if (negative && newValue.startsWith("-")) {
if (isNaN(Number(newValue.slice(1, newValue.length)))) {
event.currentTarget.value = value.toString();
}
}
else if (newValue === "") {
event.currentTarget.value = defaultValue.toString();
value = defaultValue;
}
else {
event.currentTarget.value = value.toString();
}
}
</script>
<input {id} type="text" class="digitInput" style="--valuelen: {value.toString().length + 3}ch" on:input|preventDefault={inputInteger} {value}>
<style>
.digitInput {
width: var(--valuelen);
padding-left: 1ch;
}
</style>