Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 16 additions & 10 deletions frontend/src/components/widgets/inputs/NumberInput.vue
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,8 @@
<script lang="ts">
import { defineComponent, PropType } from "vue";

import { clamp } from "@/utilities/math";

export type IncrementBehavior = "Add" | "Multiply" | "Callback" | "None";
export type IncrementDirection = "Decrease" | "Increase";

Expand Down Expand Up @@ -240,13 +242,21 @@ export default defineComponent({
if (invalid) sanitized = this.value;

if (this.isInteger) sanitized = Math.round(sanitized);
if (typeof this.min === "number" && !Number.isNaN(this.min)) sanitized = Math.max(sanitized, this.min);
if (typeof this.max === "number" && !Number.isNaN(this.max)) sanitized = Math.min(sanitized, this.max);
sanitized = clamp(newValue, this.min, this.max);

if (!invalid) this.$emit("update:value", sanitized);

const roundingPower = 10 ** this.displayDecimalPlaces;
const displayValue = Math.round(sanitized * roundingPower) / roundingPower;
this.setText(sanitized);
},
setText(value: number) {
// Find the amount of digits on the left side of the Decimal
// 10.25 == 2
// 1.23 == 1
// 0.23 == 0 - Reason for the slightly more complicated code
const leftSideDigits = Math.max(Math.floor(value).toString().length, 0) * Math.sign(value);

const roundingPower = 10 ** Math.max(this.displayDecimalPlaces - leftSideDigits, 0);
const displayValue = Math.round(value * roundingPower) / roundingPower;
this.text = `${displayValue}${this.unit}`;
},
},
Expand All @@ -258,13 +268,9 @@ export default defineComponent({
return;
}

let sanitized = newValue;
if (typeof this.min === "number") sanitized = Math.max(sanitized, this.min);
if (typeof this.max === "number") sanitized = Math.min(sanitized, this.max);
const sanitized = clamp(newValue, this.min, this.max);

const roundingPower = 10 ** this.displayDecimalPlaces;
const displayValue = Math.round(sanitized * roundingPower) / roundingPower;
this.text = `${displayValue}${this.unit}`;
this.setText(sanitized);
},
},
mounted() {
Expand Down