Browse Source

Added functions to help set/get default value

tags/0.2.0
Florian Bouillon 2 months ago
parent
commit
b2c238ea13
Signed by: Avior <florian.bouillon@delta-wings.net> GPG Key ID: B143FF27EF555D16
2 changed files with 18 additions and 8 deletions
  1. +12
    -3
      FMInput.ts
  2. +6
    -5
      FormManager.ts

+ 12
- 3
FMInput.ts 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")
}


+ 6
- 5
FormManager.ts 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()
}
}
})
}
}


Loading…
Cancel
Save