formhero/lib/index.ts

96 lines
3.5 KiB
TypeScript
Raw Normal View History

2023-01-31 00:26:27 +00:00
import React, { useEffect, useState } from 'react'
2019-09-26 19:13:27 +00:00
2023-01-31 00:26:27 +00:00
export type FieldOptions<G extends string = 'onChange', S extends string = 'value'> = {
2019-10-19 06:36:50 +00:00
extractor?: useFormExtractor
2023-01-31 00:26:27 +00:00
getter: G
setter: S
2019-09-26 19:13:27 +00:00
}
2023-01-31 00:26:27 +00:00
type RuleFunctionReturn = boolean | string
type RuleFunction<I> = (value: I) => RuleFunctionReturn | Promise<RuleFunctionReturn>
type Rule<I> = RuleFunction<I> | RegExp
type RuleObject<I> = Rule<I> | { rule: Rule<I>; message: string }
type RuleSet<I> = RuleObject<I> | RuleObject<I>[]
2019-09-26 19:13:27 +00:00
2023-01-31 00:26:27 +00:00
function isSimpleRule<I>(obj: RuleObject<I>): obj is Rule<I> {
return obj instanceof RegExp || typeof obj === 'function'
2019-09-26 19:13:27 +00:00
}
2023-01-31 00:26:27 +00:00
export type useFormExtractor = (from: any) => any
2019-09-26 19:13:27 +00:00
export const HTMLInputExtractor: useFormExtractor = (e: React.FormEvent<HTMLInputElement>) => e.currentTarget.value
export const HTMLCheckboxExtractor: useFormExtractor = (e: React.FormEvent<HTMLInputElement>) => e.currentTarget.checked
2023-01-31 00:26:27 +00:00
export type FormOptions<R> = {
rules: R
// fields: FieldOptions
2019-09-28 17:00:49 +00:00
}
2023-01-31 00:26:27 +00:00
// Form = Type of form
// R = Rules, derived from F
// E = Errors, derived from F
export const useForm = <Form extends object, R extends { [K in keyof Form]?: RuleSet<Form[K]> }, E extends { [key in keyof R]?: RuleFunctionReturn }>(init: Form, options?: FormOptions<R>) => {
const validators: R = options?.rules ?? ({} as R)
2019-10-19 06:36:50 +00:00
2023-01-31 00:26:27 +00:00
const [form, setForm] = useState<Form>(init)
const [errors, setErrors] = useState<E>({} as E)
const [isValid, setIsValid] = useState<boolean>(true)
2019-10-19 06:36:50 +00:00
useEffect(() => {
setIsValid(!Object.values(errors).reduce((acc, cur) => acc || cur !== undefined, false))
}, [errors])
2023-01-31 00:26:27 +00:00
const setField = <A extends keyof Form>(key: A, value: Form[A]) => {
2019-10-19 06:36:50 +00:00
setForm({
...form,
[key]: value,
})
}
2023-01-31 00:26:27 +00:00
async function applyRule<I>(value: any, rule: Rule<I>): Promise<RuleFunctionReturn> {
if (typeof rule === 'function') return await rule(value)
if (rule instanceof RegExp) return rule.test(value)
throw new Error(`Unsupported validator: ${rule}`)
2019-10-19 06:36:50 +00:00
}
2023-01-31 00:26:27 +00:00
async function validate<K extends keyof Form>(key: K, value: Form[K]) {
const set: RuleSet<Form[K]> | undefined = validators[key] as any
if (!set) return
const rules = Array.isArray(set) ? set : [set]
let newValue = undefined
for (const rule of rules) {
const simple = isSimpleRule(rule)
const fn = simple ? rule : rule.rule
const result = await applyRule(value, fn)
if (result !== true) {
newValue = simple ? (typeof result === 'string' ? result : true) : rule.message
break
}
2019-10-19 06:36:50 +00:00
}
2023-01-31 00:26:27 +00:00
setErrors({
...errors,
[key]: newValue,
})
2019-10-19 06:36:50 +00:00
}
2023-01-31 00:26:27 +00:00
function update<A extends keyof Form, RAW = any>(key: A, extractor?: (e: RAW) => Form[A]) {
return (value: RAW) => {
const extracted = extractor ? extractor(value) : HTMLInputExtractor(value)
setField(key, extracted)
validate(key, extracted)
}
2019-10-19 06:36:50 +00:00
}
2023-01-31 00:26:27 +00:00
type FieldReturn<K extends keyof Form, G extends string, S extends string> = { [getter in G]: ReturnType<typeof update<K>> } & { [setter in S]: Form[K] }
function field<K extends keyof Form>(key: K): FieldReturn<K, 'onChange', 'value'>
function field<K extends keyof Form, G extends string, S extends string>(key: K, opts: FieldOptions<G, S>): FieldReturn<K, G, S>
function field<K extends keyof Form, G extends string, S extends string>(key: K, opts?: FieldOptions<G, S>): FieldReturn<K, G, S> {
return {
[opts?.getter || 'onChange']: update<K>(key, opts?.extractor),
[opts?.setter || 'value']: form[key],
} as FieldReturn<K, G, S>
}
2019-10-19 06:36:50 +00:00
2023-01-31 00:26:27 +00:00
return { form, field, errors, isValid, setForm, setErrors, setField }
2019-10-19 06:36:50 +00:00
}