Created
July 2, 2017 02:39
-
-
Save elbow-jason/b2108024e360d6db458c8654890d2128 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
export interface FormFieldParams { | |
form: any | |
field: string | |
} | |
const applySetterAndGetter = <T extends Function>(component: T, params: FormFieldParams): T => { | |
let options = { | |
get: () => { | |
return params.form.payload()[params.field] | |
}, | |
set: (value: any) => { | |
params.form.updateValue({name: params.field, value}) | |
} | |
} | |
Object.defineProperty(component.prototype, params.field, options); | |
return component | |
} | |
export const FormField = <T extends Function>(form: FormAccess, field: string): Function => { | |
return function(target: T) { | |
return applySetterAndGetter(target, {form, field}) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment