2023-03-29 17:56:42 +02:00
|
|
|
import BaseUIElement from "../BaseUIElement"
|
|
|
|
import { Translation } from "../i18n/Translation"
|
|
|
|
import Translations from "../i18n/Translations"
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A 'TextFieldValidator' contains various methods to check and cleanup an entered value or to give feedback.
|
|
|
|
* They also double as an index of supported types for textfields in MapComplete
|
|
|
|
*/
|
|
|
|
export abstract class Validator {
|
|
|
|
public readonly name: string
|
|
|
|
/*
|
|
|
|
* An explanation for the theme builder.
|
|
|
|
* This can indicate which special input element is used, ...
|
|
|
|
* */
|
|
|
|
public readonly explanation: string
|
|
|
|
/**
|
|
|
|
* What HTML-inputmode to use
|
|
|
|
*/
|
|
|
|
public readonly inputmode?: string
|
2023-03-30 04:51:56 +02:00
|
|
|
public readonly textArea: boolean
|
2023-03-29 17:56:42 +02:00
|
|
|
|
2023-03-30 04:51:56 +02:00
|
|
|
constructor(
|
|
|
|
name: string,
|
|
|
|
explanation: string | BaseUIElement,
|
|
|
|
inputmode?: string,
|
|
|
|
textArea?: false | boolean
|
|
|
|
) {
|
2023-03-29 17:56:42 +02:00
|
|
|
this.name = name
|
|
|
|
this.inputmode = inputmode
|
2023-03-30 04:51:56 +02:00
|
|
|
this.textArea = textArea ?? false
|
2023-03-29 17:56:42 +02:00
|
|
|
if (this.name.endsWith("textfield")) {
|
|
|
|
this.name = this.name.substr(0, this.name.length - "TextField".length)
|
|
|
|
}
|
|
|
|
if (this.name.endsWith("textfielddef")) {
|
|
|
|
this.name = this.name.substr(0, this.name.length - "TextFieldDef".length)
|
|
|
|
}
|
|
|
|
if (typeof explanation === "string") {
|
|
|
|
this.explanation = explanation
|
|
|
|
} else {
|
|
|
|
this.explanation = explanation.AsMarkdown()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets a piece of feedback. By default, validation.<type> will be used, resulting in a generic 'not a valid <type>'.
|
|
|
|
* However, inheritors might overwrite this to give more specific feedback
|
2023-05-11 02:17:41 +02:00
|
|
|
*
|
|
|
|
* Returns 'undefined' if the element is valid
|
2023-03-29 17:56:42 +02:00
|
|
|
*/
|
2023-06-02 08:42:08 +02:00
|
|
|
public getFeedback(s: string, _?: () => string): Translation | undefined {
|
2023-05-11 02:17:41 +02:00
|
|
|
if(this.isValid(s)){
|
|
|
|
return undefined
|
|
|
|
}
|
2023-03-29 17:56:42 +02:00
|
|
|
const tr = Translations.t.validation[this.name]
|
|
|
|
if (tr !== undefined) {
|
|
|
|
return tr["feedback"]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-06 01:23:55 +02:00
|
|
|
public getPlaceholder(){
|
|
|
|
return Translations.t.validation[this.name].description
|
|
|
|
}
|
|
|
|
|
2023-06-02 08:42:08 +02:00
|
|
|
public isValid(_: string, __?: () => string): boolean {
|
2023-03-29 17:56:42 +02:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2023-04-21 20:50:38 +02:00
|
|
|
/**
|
|
|
|
* Reformats for the human
|
|
|
|
*/
|
2023-06-02 08:42:08 +02:00
|
|
|
public reformat(s: string, _?: () => string): string {
|
2023-03-29 17:56:42 +02:00
|
|
|
return s
|
|
|
|
}
|
|
|
|
}
|