Skip to content

Instantly share code, notes, and snippets.

@szagi3891
Last active September 27, 2018 18:42
Show Gist options
  • Save szagi3891/ce52a6fac1d55f5809819c2733b68e48 to your computer and use it in GitHub Desktop.
Save szagi3891/ce52a6fac1d55f5809819c2733b68e48 to your computer and use it in GitHub Desktop.
Mobx forms
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