Skip to content

Instantly share code, notes, and snippets.

@elbow-jason
Created July 2, 2017 02:39
Show Gist options
  • Save elbow-jason/b2108024e360d6db458c8654890d2128 to your computer and use it in GitHub Desktop.
Save elbow-jason/b2108024e360d6db458c8654890d2128 to your computer and use it in GitHub Desktop.
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