system-praktyk-front/src/forms/helpers.ts

21 lines
561 B
TypeScript

import { DOMEvent } from "@/helpers";
type UpdatingEvent = "onBlur" | "onChange" | "onInput";
type FormFieldHelperOptions<T> = {
event: UpdatingEvent
}
export function formFieldProps<T>(subject: T, update: (value: T) => void, options: Partial<FormFieldHelperOptions<T>> = {}) {
const {
event = "onChange"
} = options;
return (field: keyof T) => ({
value: subject[field],
[event]: (event: DOMEvent<HTMLInputElement>) => update({
...subject,
[field]: event.target.value,
} as T)
})
}