From f402146f87f89af39f3df4be138bb809b109d810 Mon Sep 17 00:00:00 2001 From: cupcakearmy Date: Sat, 28 Sep 2019 17:25:19 +0200 Subject: [PATCH] ability to add default getter & setters --- lib/index.ts | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/lib/index.ts b/lib/index.ts index 0014c57..6f2a977 100644 --- a/lib/index.ts +++ b/lib/index.ts @@ -4,14 +4,10 @@ import React, { useState } from 'react' export type useFormExtractor = (from: any) => any -export type useFormAutoOptions = { - getter?: string, - setter?: string, - extractor?: useFormExtractor -} - export type useFormOptions = { extractor?: useFormExtractor, + getter?: string, + setter?: string, } export type useFormValidatorFunction = ((s: any) => boolean | Promise) @@ -75,9 +71,9 @@ export const useForm = ({ - [opts.getter || 'onChange']: update(key, opts.extractor), - [opts.setter || 'value']: form[key] as any, + const auto = (key: keyof T, opts: useFormOptions = {}) => ({ + [opts.getter || options.getter || 'onChange']: update(key, opts.extractor), + [opts.setter || options.setter || 'value']: form[key] as any, }) return { form, update, auto, errors }