forked from MapComplete/MapComplete
UX: allow ',' in input of floats, kerb height now accepts pfloat instead of pnat
This commit is contained in:
parent
5319412dac
commit
cd0400fc51
3 changed files with 28 additions and 7 deletions
|
@ -629,7 +629,7 @@
|
||||||
"es": "Altura del bordillo de la puerta",
|
"es": "Altura del bordillo de la puerta",
|
||||||
"it": "Altezza del gradino della porta"
|
"it": "Altezza del gradino della porta"
|
||||||
},
|
},
|
||||||
"type": "pnat"
|
"type": "pfloat"
|
||||||
},
|
},
|
||||||
"mappings": [
|
"mappings": [
|
||||||
{
|
{
|
||||||
|
|
|
@ -6,15 +6,28 @@ import { ValidatorType } from "../Validators"
|
||||||
export default class FloatValidator extends Validator {
|
export default class FloatValidator extends Validator {
|
||||||
inputmode: "decimal" = "decimal" as const
|
inputmode: "decimal" = "decimal" as const
|
||||||
|
|
||||||
|
protected static readonly formattingHasComma = ("" + 1.42).indexOf(",") > 0
|
||||||
|
|
||||||
constructor(name?: ValidatorType, explanation?: string) {
|
constructor(name?: ValidatorType, explanation?: string) {
|
||||||
super(name ?? "float", explanation ?? "A decimal number", "decimal")
|
super(name ?? "float", explanation ?? "A decimal number", "decimal")
|
||||||
}
|
}
|
||||||
|
|
||||||
isValid(str) {
|
/**
|
||||||
|
*
|
||||||
|
* new FloatValidator().isValid("0,2") // => true
|
||||||
|
*/
|
||||||
|
isValid(str: string) {
|
||||||
|
console.log("Is valid?", str, FloatValidator.formattingHasComma)
|
||||||
|
if (!FloatValidator.formattingHasComma) {
|
||||||
|
str = str.replace(",", ".")
|
||||||
|
}
|
||||||
return !isNaN(Number(str)) && !str.endsWith(".") && !str.endsWith(",")
|
return !isNaN(Number(str)) && !str.endsWith(".") && !str.endsWith(",")
|
||||||
}
|
}
|
||||||
|
|
||||||
reformat(str): string {
|
reformat(str: string): string {
|
||||||
|
if (!FloatValidator.formattingHasComma) {
|
||||||
|
str = str.replace(",", ".")
|
||||||
|
}
|
||||||
return "" + Number(str)
|
return "" + Number(str)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,14 +1,22 @@
|
||||||
import { Translation } from "../../i18n/Translation"
|
import { Translation } from "../../i18n/Translation"
|
||||||
import Translations from "../../i18n/Translations"
|
import Translations from "../../i18n/Translations"
|
||||||
import { Validator } from "../Validator"
|
import FloatValidator from "./FloatValidator"
|
||||||
|
|
||||||
export default class PFloatValidator extends Validator {
|
export default class PFloatValidator extends FloatValidator {
|
||||||
constructor() {
|
constructor() {
|
||||||
super("pfloat", "A positive decimal number or zero")
|
super("pfloat", "A positive decimal number or zero")
|
||||||
}
|
}
|
||||||
|
|
||||||
isValid = (str) =>
|
isValid(str: string) {
|
||||||
!isNaN(Number(str)) && Number(str) >= 0 && !str.endsWith(".") && !str.endsWith(",")
|
if (!super.isValid(str)) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
if (!FloatValidator.formattingHasComma) {
|
||||||
|
str = str.replace(",", ".")
|
||||||
|
}
|
||||||
|
const n = Number(str)
|
||||||
|
return n >= 0
|
||||||
|
}
|
||||||
|
|
||||||
getFeedback(s: string): Translation {
|
getFeedback(s: string): Translation {
|
||||||
const spr = super.getFeedback(s)
|
const spr = super.getFeedback(s)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue