mirror of
https://github.com/cupcakearmy/formhero.git
synced 2024-12-23 00:26:23 +00:00
ability to add default getter & setters
This commit is contained in:
parent
152f0b7c51
commit
f402146f87
14
lib/index.ts
14
lib/index.ts
@ -4,14 +4,10 @@ import React, { useState } from 'react'
|
|||||||
|
|
||||||
export type useFormExtractor = (from: any) => any
|
export type useFormExtractor = (from: any) => any
|
||||||
|
|
||||||
export type useFormAutoOptions = {
|
|
||||||
getter?: string,
|
|
||||||
setter?: string,
|
|
||||||
extractor?: useFormExtractor
|
|
||||||
}
|
|
||||||
|
|
||||||
export type useFormOptions = {
|
export type useFormOptions = {
|
||||||
extractor?: useFormExtractor,
|
extractor?: useFormExtractor,
|
||||||
|
getter?: string,
|
||||||
|
setter?: string,
|
||||||
}
|
}
|
||||||
|
|
||||||
export type useFormValidatorFunction = ((s: any) => boolean | Promise<boolean>)
|
export type useFormValidatorFunction = ((s: any) => boolean | Promise<boolean>)
|
||||||
@ -75,9 +71,9 @@ export const useForm = <T, U extends { [key in keyof T]: useFormValidator }, E e
|
|||||||
_validate(key, extracted)
|
_validate(key, extracted)
|
||||||
}
|
}
|
||||||
|
|
||||||
const auto = (key: keyof T, opts: useFormAutoOptions = {}) => ({
|
const auto = (key: keyof T, opts: useFormOptions = {}) => ({
|
||||||
[opts.getter || 'onChange']: update(key, opts.extractor),
|
[opts.getter || options.getter || 'onChange']: update(key, opts.extractor),
|
||||||
[opts.setter || 'value']: form[key] as any,
|
[opts.setter || options.setter || 'value']: form[key] as any,
|
||||||
})
|
})
|
||||||
|
|
||||||
return { form, update, auto, errors }
|
return { form, update, auto, errors }
|
||||||
|
Loading…
Reference in New Issue
Block a user