mirror of
https://github.com/dzeiocom/FormManager.git
synced 2025-04-23 03:12:14 +00:00
Added Typing for form content
Signed-off-by: Florian Bouillon <florian.bouillon@delta-wings.net>
This commit is contained in:
parent
72a62539cf
commit
3366dda9aa
@ -17,7 +17,7 @@ import AttributeListeners from './attributes/AttributeListeners';
|
|||||||
* @export
|
* @export
|
||||||
* @class FormManager
|
* @class FormManager
|
||||||
*/
|
*/
|
||||||
export default class FormManager {
|
export default class FormManager<T extends Record<string, any> = Record<string, any>> {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* List of inputs
|
* List of inputs
|
||||||
@ -26,7 +26,7 @@ export default class FormManager {
|
|||||||
* @type {InputArray}
|
* @type {InputArray}
|
||||||
* @memberof FormManager
|
* @memberof FormManager
|
||||||
*/
|
*/
|
||||||
public inputs: InputArray = {}
|
public inputs: InputArray<T> = {} as any
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* List of interfaces
|
* List of interfaces
|
||||||
@ -110,7 +110,7 @@ export default class FormManager {
|
|||||||
* @memberof FormManager
|
* @memberof FormManager
|
||||||
*/
|
*/
|
||||||
public setupInputs() {
|
public setupInputs() {
|
||||||
this.inputs = {};
|
this.inputs = {} as any;
|
||||||
const formID = this.form.getAttribute("id")
|
const formID = this.form.getAttribute("id")
|
||||||
|
|
||||||
// Find every inputs
|
// Find every inputs
|
||||||
@ -119,7 +119,7 @@ export default class FormManager {
|
|||||||
// Find each input their class
|
// Find each input their class
|
||||||
request.forEach((element: HTMLElement) => {
|
request.forEach((element: HTMLElement) => {
|
||||||
let el = this.getInit(element)
|
let el = this.getInit(element)
|
||||||
if (el) this.inputs[el.getName()] = el
|
if (el) this.inputs[el.getName() as keyof T] = el
|
||||||
});
|
});
|
||||||
this.attributeManager.trigger(AttributeListeners.FORM_INIT)
|
this.attributeManager.trigger(AttributeListeners.FORM_INIT)
|
||||||
return this
|
return this
|
||||||
@ -236,7 +236,7 @@ export default class FormManager {
|
|||||||
* @param {*} value
|
* @param {*} value
|
||||||
* @memberof FormManager
|
* @memberof FormManager
|
||||||
*/
|
*/
|
||||||
public setValue(name: string, value: any) {
|
public setValue(name: keyof T, value: any) {
|
||||||
if (!this.inputs.hasOwnProperty(name)) {
|
if (!this.inputs.hasOwnProperty(name)) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -248,7 +248,7 @@ export default class FormManager {
|
|||||||
return this
|
return this
|
||||||
}
|
}
|
||||||
|
|
||||||
public setJSON(data: {[key:string]: any}) {
|
public setJSON(data: Partial<T>) {
|
||||||
for (const key in data) {
|
for (const key in data) {
|
||||||
if (!data.hasOwnProperty(key)) {
|
if (!data.hasOwnProperty(key)) {
|
||||||
continue
|
continue
|
||||||
@ -259,7 +259,7 @@ export default class FormManager {
|
|||||||
return this
|
return this
|
||||||
}
|
}
|
||||||
|
|
||||||
public getValue(name: string): any {
|
public getValue(name: keyof T): any {
|
||||||
if (!this.inputs.hasOwnProperty(name)) {
|
if (!this.inputs.hasOwnProperty(name)) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -272,7 +272,7 @@ export default class FormManager {
|
|||||||
*
|
*
|
||||||
* @memberof FormManager
|
* @memberof FormManager
|
||||||
*/
|
*/
|
||||||
public getJSON(): {[key: string]: any} {
|
public getJSON(): T {
|
||||||
const jsonObject: any = {}
|
const jsonObject: any = {}
|
||||||
for (const name in this.inputs) {
|
for (const name in this.inputs) {
|
||||||
if (this.inputs.hasOwnProperty(name)) {
|
if (this.inputs.hasOwnProperty(name)) {
|
||||||
@ -292,7 +292,7 @@ export default class FormManager {
|
|||||||
* @param {*} json the JSON
|
* @param {*} json the JSON
|
||||||
* @memberof FormManager
|
* @memberof FormManager
|
||||||
*/
|
*/
|
||||||
public fillFromJSON(json: any) {
|
public fillFromJSON(json: Partial<T>) {
|
||||||
for (const key in json) {
|
for (const key in json) {
|
||||||
if (!json.hasOwnProperty(key)) {
|
if (!json.hasOwnProperty(key)) {
|
||||||
continue
|
continue
|
||||||
@ -365,7 +365,7 @@ export default class FormManager {
|
|||||||
* @param {string} inputName
|
* @param {string} inputName
|
||||||
* @memberof FormManager
|
* @memberof FormManager
|
||||||
*/
|
*/
|
||||||
public setModeForInput(mode: FMMode, inputName: string) {
|
public setModeForInput(mode: FMMode, inputName: keyof T) {
|
||||||
if (mode == FMMode.ViewMode) {
|
if (mode == FMMode.ViewMode) {
|
||||||
if (this.inputs[inputName]) {
|
if (this.inputs[inputName]) {
|
||||||
this.inputs[inputName].element.setAttribute("disabled", "")
|
this.inputs[inputName].element.setAttribute("disabled", "")
|
||||||
@ -395,7 +395,7 @@ export default class FormManager {
|
|||||||
this.attributeManager.trigger(AttributeListeners.POST_CLEAR)
|
this.attributeManager.trigger(AttributeListeners.POST_CLEAR)
|
||||||
}
|
}
|
||||||
|
|
||||||
public clearInput(input: string) {
|
public clearInput(input: keyof T) {
|
||||||
if (this.inputs.hasOwnProperty(input)) {
|
if (this.inputs.hasOwnProperty(input)) {
|
||||||
const inp = this.inputs[input];
|
const inp = this.inputs[input];
|
||||||
inp.setValue(undefined)
|
inp.setValue(undefined)
|
||||||
|
@ -5,6 +5,6 @@ import AbstractInput from "../AbstractInput";
|
|||||||
*
|
*
|
||||||
* @interface InputArray
|
* @interface InputArray
|
||||||
*/
|
*/
|
||||||
export default interface InputArray {
|
type InputArray<T = Record<string, any>> = Record<keyof T, AbstractInput>
|
||||||
[key: string]: AbstractInput
|
|
||||||
}
|
export default InputArray
|
||||||
|
Loading…
x
Reference in New Issue
Block a user