Skip to content

Commit

Permalink
Config UI: better duration fields (#17222)
Browse files Browse the repository at this point in the history
  • Loading branch information
naltatis authored Nov 13, 2024
1 parent 31373f9 commit 3685c21
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
15 changes: 15 additions & 0 deletions assets/js/components/Config/PropertyField.vue
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,14 @@
import "@h2d2/shopicons/es/regular/minus";
import VehicleIcon from "../VehicleIcon";
import SelectGroup from "../SelectGroup.vue";
import formatter from "../../mixins/formatter";
const NS_PER_SECOND = 1000000000;
export default {
name: "PropertyField",
components: { VehicleIcon, SelectGroup },
mixins: [formatter],
props: {
id: String,
property: String,
Expand Down Expand Up @@ -149,6 +153,9 @@ export default {
if (this.property === "capacity") {
return "kWh";
}
if (this.type === "Duration") {
return this.fmtSecondUnit(this.value);
}
return null;
},
icons() {
Expand Down Expand Up @@ -203,6 +210,10 @@ export default {
return Array.isArray(this.modelValue) ? this.modelValue.join("\n") : "";
}
if (this.type === "Duration" && typeof this.modelValue === "number") {
return this.modelValue / NS_PER_SECOND;
}
return this.modelValue;
},
set(value) {
Expand All @@ -216,6 +227,10 @@ export default {
newValue = value ? value.split("\n") : [];
}
if (this.type === "Duration" && typeof newValue === "number") {
newValue = newValue * NS_PER_SECOND;
}
this.$emit("update:modelValue", newValue);
},
},
Expand Down
9 changes: 9 additions & 0 deletions assets/js/mixins/formatter.js
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,15 @@ export default {
day: "numeric",
}).format(date);
},
fmtSecondUnit: function (seconds) {
return new Intl.NumberFormat(this.$i18n?.locale, {
style: "unit",
unit: "second",
unitDisplay: "long",
})
.formatToParts(seconds)
.find((part) => part.type === "unit").value;
},
fmtMoney: function (amout = 0, currency = "EUR", decimals = true, withSymbol = false) {
const currencyDisplay = withSymbol ? "narrowSymbol" : "code";
const result = new Intl.NumberFormat(this.$i18n?.locale, {
Expand Down

0 comments on commit 3685c21

Please sign in to comment.