Last active
September 27, 2018 18:42
-
-
Save szagi3891/ce52a6fac1d55f5809819c2733b68e48 to your computer and use it in GitHub Desktop.
Mobx forms
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type InType<T> = { | |
readonly [P in keyof T]: T[P] | FormValue<any, T[P]>; | |
}; | |
export class FieldsGroupState<T> { | |
private fields: InType<T>; | |
constructor(fields: InType<T>) { | |
this.fields = fields; | |
} | |
@computed get model(): T | Error { | |
const modelOut = {}; | |
for (const [key, item] of Object.entries(this.fields)) { | |
if (item instanceof FormValue) { | |
const value = item.valueModel; | |
if (value instanceof Error) { | |
return value; | |
} | |
//@ts-ignore | |
modelOut[key] = value; | |
} else { | |
//@ts-ignore | |
modelOut[key] = item; | |
} | |
} | |
//@ts-ignore | |
return modelOut; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment