@avinlab/form
Advanced tools
Comparing version
@@ -1,14 +0,15 @@ | ||
type UpdateHandler = (values: Record<string, any>, prevValues: Record<string, any>) => void; | ||
type UpdateFieldHandler = (newValue: any, oldValue: any) => void; | ||
interface Form { | ||
values: Record<string, any>; | ||
prevValues: Record<string, any>; | ||
setValue: (fieldName: string, value: any) => void; | ||
onUpdateField: (fieldName: string, cb: UpdateFieldHandler) => void; | ||
offUpdateField: (fieldName: string, cb: UpdateFieldHandler) => void; | ||
onUpdate: (cb: UpdateHandler) => void; | ||
offUpdate: (cb: UpdateHandler) => void; | ||
type FormValues = Record<string, any>; | ||
type UpdateHandler<TFormValues extends FormValues> = (values: TFormValues, prevValues: TFormValues) => void; | ||
type UpdateFieldHandler<T> = (newValue: T, oldValue: T) => void; | ||
interface Form<TFormValues extends FormValues> { | ||
values: TFormValues; | ||
prevValues: TFormValues; | ||
setValue: <TFieldName extends keyof TFormValues>(fieldName: TFieldName, value: TFormValues[TFieldName]) => void; | ||
onUpdateField: <TFieldName extends keyof TFormValues>(fieldName: TFieldName, cb: UpdateFieldHandler<TFormValues[TFieldName]>) => void; | ||
offUpdateField: <TFieldName extends keyof TFormValues>(fieldName: TFieldName, cb: UpdateFieldHandler<TFormValues[TFieldName]>) => void; | ||
onUpdate: (cb: UpdateHandler<TFormValues>) => void; | ||
offUpdate: (cb: UpdateHandler<TFormValues>) => void; | ||
} | ||
declare const createForm: (initialValues: Record<string, any>) => Form; | ||
declare const createForm: <TFormValues extends FormValues>(initialValues: TFormValues) => Form<TFormValues>; | ||
export { Form, UpdateHandler, createForm }; | ||
export { Form, FormValues, UpdateHandler, createForm }; |
// src/createForm.ts | ||
var createForm = (initialValues) => { | ||
let values = { ...initialValues }; | ||
let prevValues = {}; | ||
let prevValues = { ...initialValues }; | ||
let _onUpdateHandlers = []; | ||
@@ -24,7 +24,11 @@ let _onUpdateFieldHandlers = {}; | ||
const onUpdateField = (fieldName, cb) => { | ||
_onUpdateFieldHandlers[fieldName] = _onUpdateFieldHandlers[fieldName] || []; | ||
if (!_onUpdateFieldHandlers[fieldName]) { | ||
_onUpdateFieldHandlers[fieldName] = []; | ||
} | ||
_onUpdateFieldHandlers[fieldName].push(cb); | ||
}; | ||
const offUpdateField = (fieldName, cb) => { | ||
_onUpdateFieldHandlers[fieldName] = _onUpdateFieldHandlers[fieldName] || []; | ||
if (!_onUpdateFieldHandlers[fieldName]) { | ||
_onUpdateFieldHandlers[fieldName] = []; | ||
} | ||
_onUpdateFieldHandlers[fieldName] = _onUpdateFieldHandlers[fieldName].filter( | ||
@@ -31,0 +35,0 @@ (handler) => handler !== cb |
@@ -1,15 +0,16 @@ | ||
import { Form } from './createForm.js'; | ||
import { FormValues, Form } from './createForm.js'; | ||
type ValidationFunction = (values: Record<string, any>, prevValues: Record<string, any>) => Record<string, any | undefined>; | ||
type ValidateHandler = (errors: Record<string, any>) => void; | ||
interface FormValidation { | ||
errors: Record<string, any>; | ||
type FormErrors = Record<string, any>; | ||
type ValidationFunction<TFormErrors extends FormErrors, TFormValues extends FormValues> = (values: TFormValues, prevValues: TFormValues) => TFormErrors; | ||
type ValidateHandler = (errors: FormErrors) => void; | ||
interface FormValidation<TFormErrors extends FormErrors, TFormValues extends FormValues = FormValues> { | ||
errors: TFormErrors; | ||
isValid: boolean; | ||
validate: () => void; | ||
setValidation: (validationFunc: ValidationFunction) => void; | ||
setValidation: (validationFunc: ValidationFunction<TFormErrors, TFormValues>) => void; | ||
onValidate: (cb: ValidateHandler) => void; | ||
offValidate: (cb: ValidateHandler) => void; | ||
} | ||
declare const createFormValidation: (form: Form, validationFunc?: ValidationFunction) => FormValidation; | ||
declare const createFormValidation: <TFormErrors extends FormErrors, TFormValues extends FormValues>(form: Form<TFormValues>, validationFunc?: ValidationFunction<TFormErrors, TFormValues> | undefined) => FormValidation<TFormErrors, TFormValues>; | ||
export { FormValidation, ValidationFunction, createFormValidation }; | ||
export { FormErrors, FormValidation, ValidationFunction, createFormValidation }; |
@@ -5,6 +5,5 @@ // src/createFormValidation.ts | ||
let _onValidateHandlers = []; | ||
let errors = {}; | ||
let errors; | ||
let isValid = true; | ||
let _validationFunc = null; | ||
let _isValidatedOnce = false; | ||
const validate = () => { | ||
@@ -21,7 +20,6 @@ if (_validationFunc) { | ||
}); | ||
const shouldUpdateErrors = !objectsAreEqual(newErrors, errors) || !_isValidatedOnce; | ||
const shouldUpdateErrors = !objectsAreEqual(newErrors, errors); | ||
if (shouldUpdateErrors) { | ||
errors = newErrors; | ||
isValid = !Object.keys(errors).length; | ||
_isValidatedOnce = true; | ||
_onValidateHandlers.forEach((cb) => { | ||
@@ -28,0 +26,0 @@ cb(newErrors); |
@@ -1,2 +0,2 @@ | ||
export { Form, UpdateHandler, createForm } from './createForm.js'; | ||
export { FormValidation, ValidationFunction, createFormValidation } from './createFormValidation.js'; | ||
export { Form, FormValues, UpdateHandler, createForm } from './createForm.js'; | ||
export { FormErrors, FormValidation, ValidationFunction, createFormValidation } from './createFormValidation.js'; |
{ | ||
"name": "@avinlab/form", | ||
"type": "module", | ||
"version": "0.1.3", | ||
"version": "0.2.0", | ||
"description": "", | ||
@@ -38,3 +38,3 @@ "author": "Avin Lambrero <avin.github@gmail.com> (https://github.com/avin/)", | ||
}, | ||
"gitHead": "fad8b8a0c06f1ba19083e189b994727a06f7ef75" | ||
"gitHead": "b6877927a9e6c670089746b87667c3e0e14f984d" | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
38590
4.78%439
0.92%