Added functions to help set/get default value

This commit is contained in:
Florian Bouillon 2019-10-08 17:34:11 +02:00
parent dfa6058cf0
commit b2c238ea13
No known key found for this signature in database
GPG Key ID: B143FF27EF555D16
2 changed files with 18 additions and 8 deletions

View File

@ -8,9 +8,7 @@ export default class FMInput {
constructor(element: HTMLElement, form: FormManager) {
this.element = element as HTMLInputElement
this.form = form
if (this.element.hasAttribute("data-default")) {
this.setValue(this.getDefault(this.element.dataset.default))
}
this.setToDefault()
}
/**
@ -41,9 +39,20 @@ export default class FMInput {
}
getDefault(args: string): any {
if (args.startsWith("run:")) {
args = args.split("run:")[1]
return eval(args)
}
return args
}
setToDefault() {
if (this.element.hasAttribute("data-default")) {
return this.setValue(this.getDefault(this.element.dataset.default))
}
return this.setValue("")
}
getName(): string {
return this.element.getAttribute("name") == undefined ? this.element.dataset.name : this.element.getAttribute("name")
}

View File

@ -101,9 +101,6 @@ import FMInput from "./FMInput"
*/
/**
* Manager for Forms
*
@ -319,8 +316,12 @@ export default class FormManager {
*/
public clear() {
this.form.querySelectorAll("[name]").forEach((el: HTMLInputElement) => {
el.value = ""
el.removeAttribute("value")
for (const name in this.inputs) {
if (this.inputs.hasOwnProperty(name)) {
const input = this.inputs[name];
input.setToDefault()
}
}
})
}
}