@mantine/form 9.2.2 → 9.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/cjs/FormProvider/FormProvider.cjs +0 -1
- package/cjs/FormProvider/FormProvider.cjs.map +1 -1
- package/cjs/actions/actions.cjs +0 -1
- package/cjs/actions/actions.cjs.map +1 -1
- package/cjs/hooks/use-form-errors/use-form-errors.cjs +0 -1
- package/cjs/hooks/use-form-errors/use-form-errors.cjs.map +1 -1
- package/cjs/hooks/use-form-list/use-form-list.cjs +0 -1
- package/cjs/hooks/use-form-list/use-form-list.cjs.map +1 -1
- package/cjs/hooks/use-form-validating/use-form-validating.cjs +0 -1
- package/cjs/hooks/use-form-validating/use-form-validating.cjs.map +1 -1
- package/cjs/hooks/use-form-values/use-form-values.cjs +0 -1
- package/cjs/hooks/use-form-values/use-form-values.cjs.map +1 -1
- package/cjs/hooks/use-form-watch/use-form-watch.cjs +0 -1
- package/cjs/hooks/use-form-watch/use-form-watch.cjs.map +1 -1
- package/cjs/paths/set-path.cjs +0 -1
- package/cjs/paths/set-path.cjs.map +1 -1
- package/cjs/use-field.cjs +0 -1
- package/cjs/use-field.cjs.map +1 -1
- package/cjs/use-form.cjs +0 -1
- package/cjs/use-form.cjs.map +1 -1
- package/package.json +1 -1
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"FormProvider.cjs","names":["useForm"],"sources":["../../src/FormProvider/FormProvider.tsx"],"sourcesContent":["import { createContext, use } from 'react';\nimport { UseForm, UseFormReturnType } from '../types';\nimport { useForm } from '../use-form';\n\nexport interface FormProviderProps<Form> {\n form: Form;\n children: React.ReactNode;\n}\n\nexport function createFormContext<Values, TransformedValues = Values, Rules = any>() {\n type Form = UseFormReturnType<Values, TransformedValues, Rules>;\n\n const FormContext = createContext<Form | null>(null);\n\n function FormProvider({ form, children }: FormProviderProps<Form>) {\n return <FormContext value={form}>{children}</FormContext>;\n }\n\n function useFormContext() {\n const ctx = use(FormContext);\n if (!ctx) {\n throw new Error('useFormContext was called outside of FormProvider context');\n }\n\n return ctx;\n }\n\n return [FormProvider, useFormContext, useForm] as [\n React.FC<FormProviderProps<Form>>,\n () => Form,\n UseForm<Values, TransformedValues, Rules>,\n ];\n}\n"],"mappings":"
|
|
1
|
+
{"version":3,"file":"FormProvider.cjs","names":["useForm"],"sources":["../../src/FormProvider/FormProvider.tsx"],"sourcesContent":["import { createContext, use } from 'react';\nimport { UseForm, UseFormReturnType } from '../types';\nimport { useForm } from '../use-form';\n\nexport interface FormProviderProps<Form> {\n form: Form;\n children: React.ReactNode;\n}\n\nexport function createFormContext<Values, TransformedValues = Values, Rules = any>() {\n type Form = UseFormReturnType<Values, TransformedValues, Rules>;\n\n const FormContext = createContext<Form | null>(null);\n\n function FormProvider({ form, children }: FormProviderProps<Form>) {\n return <FormContext value={form}>{children}</FormContext>;\n }\n\n function useFormContext() {\n const ctx = use(FormContext);\n if (!ctx) {\n throw new Error('useFormContext was called outside of FormProvider context');\n }\n\n return ctx;\n }\n\n return [FormProvider, useFormContext, useForm] as [\n React.FC<FormProviderProps<Form>>,\n () => Form,\n UseForm<Values, TransformedValues, Rules>,\n ];\n}\n"],"mappings":";;;;;AASA,SAAgB,oBAAqE;CAGnF,MAAM,eAAA,GAAA,MAAA,eAAyC,IAAI;CAEnD,SAAS,aAAa,EAAE,MAAM,YAAqC;EACjE,OAAO,iBAAA,GAAA,kBAAA,KAAC,aAAD;GAAa,OAAO;GAAO;EAAsB,CAAA;CAC1D;CAEA,SAAS,iBAAiB;EACxB,MAAM,OAAA,GAAA,MAAA,KAAU,WAAW;EAC3B,IAAI,CAAC,KACH,MAAM,IAAI,MAAM,2DAA2D;EAG7E,OAAO;CACT;CAEA,OAAO;EAAC;EAAc;EAAgBA,iBAAAA;CAAO;AAK/C"}
|
package/cjs/actions/actions.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"actions.cjs","names":["useLayoutEffect","useEffect"],"sources":["../../src/actions/actions.ts"],"sourcesContent":["import { useEffect, useLayoutEffect } from 'react';\nimport type {\n ClearErrors,\n ClearFieldError,\n InsertListItem,\n RemoveListItem,\n ReorderListItem,\n Reset,\n ResetDirty,\n ResetStatus,\n SetErrors,\n SetFieldError,\n SetFieldValue,\n SetFormStatus,\n SetInitialValues,\n SetValues,\n UseFormReturnType,\n} from '../types';\n\nfunction dispatchEvent(type: string, detail?: any): any {\n window.dispatchEvent(new CustomEvent(type, { detail }));\n}\n\nfunction validateFormName(name: string) {\n if (!/^[0-9a-zA-Z-]+$/.test(name)) {\n throw new Error(\n `[@mantine/use-form] Form name \"${name}\" is invalid, it should contain only letters, numbers and dashes`\n );\n }\n}\n\nexport const useIsomorphicEffect = typeof window !== 'undefined' ? useLayoutEffect : useEffect;\n\nexport function createFormActions<FormValues extends Record<string, any> = Record<string, any>>(\n name: string\n) {\n validateFormName(name);\n\n const setFieldValue: SetFieldValue<FormValues> = (path, value) =>\n dispatchEvent(`mantine-form:${name}:set-field-value`, { path, value });\n\n const setValues: SetValues<FormValues> = (values) =>\n dispatchEvent(`mantine-form:${name}:set-values`, values);\n\n const setInitialValues: SetInitialValues<FormValues> = (values) =>\n dispatchEvent(`mantine-form:${name}:set-initial-values`, values);\n\n const setErrors: SetErrors = (errors) => dispatchEvent(`mantine-form:${name}:set-errors`, errors);\n\n const setFieldError: SetFieldError<FormValues> = (path, error) =>\n dispatchEvent(`mantine-form:${name}:set-field-error`, { path, error });\n\n const clearFieldError: ClearFieldError = (path) =>\n dispatchEvent(`mantine-form:${name}:clear-field-error`, path);\n\n const clearErrors: ClearErrors = () => dispatchEvent(`mantine-form:${name}:clear-errors`);\n\n const reset: Reset = () => dispatchEvent(`mantine-form:${name}:reset`);\n\n const validate: () => void = () => dispatchEvent(`mantine-form:${name}:validate`);\n\n const validateField: (path: any) => void = (path) =>\n dispatchEvent(`mantine-form:${name}:validate-field`, path);\n\n const reorderListItem: ReorderListItem<FormValues> = (path, payload) =>\n dispatchEvent(`mantine-form:${name}:reorder-list-item`, { path, payload });\n\n const removeListItem: RemoveListItem<FormValues> = (path, index) =>\n dispatchEvent(`mantine-form:${name}:remove-list-item`, { path, index });\n\n const insertListItem: InsertListItem<FormValues> = (path, item, index) =>\n dispatchEvent(`mantine-form:${name}:insert-list-item`, { path, index, item });\n\n const setDirty: SetFormStatus = (value) => dispatchEvent(`mantine-form:${name}:set-dirty`, value);\n\n const setTouched: SetFormStatus = (value) =>\n dispatchEvent(`mantine-form:${name}:set-touched`, value);\n\n const resetDirty: ResetDirty<FormValues> = (values) =>\n dispatchEvent(`mantine-form:${name}:reset-dirty`, values);\n\n const resetTouched: ResetStatus = () => dispatchEvent(`mantine-form:${name}:reset-touched`);\n\n return {\n setFieldValue,\n setValues,\n setInitialValues,\n setErrors,\n setFieldError,\n clearFieldError,\n clearErrors,\n reset,\n validate,\n validateField,\n reorderListItem,\n removeListItem,\n insertListItem,\n setDirty,\n setTouched,\n resetDirty,\n resetTouched,\n };\n}\n\nfunction useFormEvent(eventKey: string | undefined, handler: (event: any) => void) {\n useIsomorphicEffect(() => {\n if (eventKey) {\n window.addEventListener(eventKey, handler);\n return () => window.removeEventListener(eventKey, handler);\n }\n return undefined;\n }, [eventKey]);\n}\n\nexport function useFormActions<Values = Record<string, unknown>, TransformedValues = Values>(\n name: string | undefined,\n form: UseFormReturnType<Values, TransformedValues, any>\n) {\n if (name) {\n validateFormName(name);\n }\n\n useFormEvent(`mantine-form:${name}:set-field-value`, (event: CustomEvent) =>\n form.setFieldValue(event.detail.path, event.detail.value)\n );\n\n useFormEvent(`mantine-form:${name}:set-values`, (event: CustomEvent) =>\n form.setValues(event.detail)\n );\n\n useFormEvent(`mantine-form:${name}:set-initial-values`, (event: CustomEvent) =>\n form.setInitialValues(event.detail)\n );\n\n useFormEvent(`mantine-form:${name}:set-errors`, (event: CustomEvent) =>\n form.setErrors(event.detail)\n );\n\n useFormEvent(`mantine-form:${name}:set-field-error`, (event: CustomEvent) =>\n form.setFieldError(event.detail.path, event.detail.error)\n );\n\n useFormEvent(`mantine-form:${name}:clear-field-error`, (event: CustomEvent) =>\n form.clearFieldError(event.detail)\n );\n\n useFormEvent(`mantine-form:${name}:clear-errors`, form.clearErrors);\n useFormEvent(`mantine-form:${name}:reset`, form.reset);\n useFormEvent(`mantine-form:${name}:validate`, form.validate);\n\n useFormEvent(`mantine-form:${name}:validate-field`, (event: CustomEvent) =>\n form.validateField(event.detail)\n );\n\n useFormEvent(`mantine-form:${name}:reorder-list-item`, (event: CustomEvent) =>\n form.reorderListItem(event.detail.path, event.detail.payload)\n );\n\n useFormEvent(`mantine-form:${name}:remove-list-item`, (event: CustomEvent) =>\n form.removeListItem(event.detail.path, event.detail.index)\n );\n\n useFormEvent(`mantine-form:${name}:insert-list-item`, (event: CustomEvent) =>\n form.insertListItem(event.detail.path, event.detail.item, event.detail.index)\n );\n\n useFormEvent(`mantine-form:${name}:set-dirty`, (event: CustomEvent) =>\n form.setDirty(event.detail)\n );\n\n useFormEvent(`mantine-form:${name}:set-touched`, (event: CustomEvent) =>\n form.setTouched(event.detail)\n );\n\n useFormEvent(`mantine-form:${name}:reset-dirty`, (event: CustomEvent) =>\n form.resetDirty(event.detail)\n );\n\n useFormEvent(`mantine-form:${name}:reset-touched`, form.resetTouched);\n}\n"],"mappings":"
|
|
1
|
+
{"version":3,"file":"actions.cjs","names":["useLayoutEffect","useEffect"],"sources":["../../src/actions/actions.ts"],"sourcesContent":["import { useEffect, useLayoutEffect } from 'react';\nimport type {\n ClearErrors,\n ClearFieldError,\n InsertListItem,\n RemoveListItem,\n ReorderListItem,\n Reset,\n ResetDirty,\n ResetStatus,\n SetErrors,\n SetFieldError,\n SetFieldValue,\n SetFormStatus,\n SetInitialValues,\n SetValues,\n UseFormReturnType,\n} from '../types';\n\nfunction dispatchEvent(type: string, detail?: any): any {\n window.dispatchEvent(new CustomEvent(type, { detail }));\n}\n\nfunction validateFormName(name: string) {\n if (!/^[0-9a-zA-Z-]+$/.test(name)) {\n throw new Error(\n `[@mantine/use-form] Form name \"${name}\" is invalid, it should contain only letters, numbers and dashes`\n );\n }\n}\n\nexport const useIsomorphicEffect = typeof window !== 'undefined' ? useLayoutEffect : useEffect;\n\nexport function createFormActions<FormValues extends Record<string, any> = Record<string, any>>(\n name: string\n) {\n validateFormName(name);\n\n const setFieldValue: SetFieldValue<FormValues> = (path, value) =>\n dispatchEvent(`mantine-form:${name}:set-field-value`, { path, value });\n\n const setValues: SetValues<FormValues> = (values) =>\n dispatchEvent(`mantine-form:${name}:set-values`, values);\n\n const setInitialValues: SetInitialValues<FormValues> = (values) =>\n dispatchEvent(`mantine-form:${name}:set-initial-values`, values);\n\n const setErrors: SetErrors = (errors) => dispatchEvent(`mantine-form:${name}:set-errors`, errors);\n\n const setFieldError: SetFieldError<FormValues> = (path, error) =>\n dispatchEvent(`mantine-form:${name}:set-field-error`, { path, error });\n\n const clearFieldError: ClearFieldError = (path) =>\n dispatchEvent(`mantine-form:${name}:clear-field-error`, path);\n\n const clearErrors: ClearErrors = () => dispatchEvent(`mantine-form:${name}:clear-errors`);\n\n const reset: Reset = () => dispatchEvent(`mantine-form:${name}:reset`);\n\n const validate: () => void = () => dispatchEvent(`mantine-form:${name}:validate`);\n\n const validateField: (path: any) => void = (path) =>\n dispatchEvent(`mantine-form:${name}:validate-field`, path);\n\n const reorderListItem: ReorderListItem<FormValues> = (path, payload) =>\n dispatchEvent(`mantine-form:${name}:reorder-list-item`, { path, payload });\n\n const removeListItem: RemoveListItem<FormValues> = (path, index) =>\n dispatchEvent(`mantine-form:${name}:remove-list-item`, { path, index });\n\n const insertListItem: InsertListItem<FormValues> = (path, item, index) =>\n dispatchEvent(`mantine-form:${name}:insert-list-item`, { path, index, item });\n\n const setDirty: SetFormStatus = (value) => dispatchEvent(`mantine-form:${name}:set-dirty`, value);\n\n const setTouched: SetFormStatus = (value) =>\n dispatchEvent(`mantine-form:${name}:set-touched`, value);\n\n const resetDirty: ResetDirty<FormValues> = (values) =>\n dispatchEvent(`mantine-form:${name}:reset-dirty`, values);\n\n const resetTouched: ResetStatus = () => dispatchEvent(`mantine-form:${name}:reset-touched`);\n\n return {\n setFieldValue,\n setValues,\n setInitialValues,\n setErrors,\n setFieldError,\n clearFieldError,\n clearErrors,\n reset,\n validate,\n validateField,\n reorderListItem,\n removeListItem,\n insertListItem,\n setDirty,\n setTouched,\n resetDirty,\n resetTouched,\n };\n}\n\nfunction useFormEvent(eventKey: string | undefined, handler: (event: any) => void) {\n useIsomorphicEffect(() => {\n if (eventKey) {\n window.addEventListener(eventKey, handler);\n return () => window.removeEventListener(eventKey, handler);\n }\n return undefined;\n }, [eventKey]);\n}\n\nexport function useFormActions<Values = Record<string, unknown>, TransformedValues = Values>(\n name: string | undefined,\n form: UseFormReturnType<Values, TransformedValues, any>\n) {\n if (name) {\n validateFormName(name);\n }\n\n useFormEvent(`mantine-form:${name}:set-field-value`, (event: CustomEvent) =>\n form.setFieldValue(event.detail.path, event.detail.value)\n );\n\n useFormEvent(`mantine-form:${name}:set-values`, (event: CustomEvent) =>\n form.setValues(event.detail)\n );\n\n useFormEvent(`mantine-form:${name}:set-initial-values`, (event: CustomEvent) =>\n form.setInitialValues(event.detail)\n );\n\n useFormEvent(`mantine-form:${name}:set-errors`, (event: CustomEvent) =>\n form.setErrors(event.detail)\n );\n\n useFormEvent(`mantine-form:${name}:set-field-error`, (event: CustomEvent) =>\n form.setFieldError(event.detail.path, event.detail.error)\n );\n\n useFormEvent(`mantine-form:${name}:clear-field-error`, (event: CustomEvent) =>\n form.clearFieldError(event.detail)\n );\n\n useFormEvent(`mantine-form:${name}:clear-errors`, form.clearErrors);\n useFormEvent(`mantine-form:${name}:reset`, form.reset);\n useFormEvent(`mantine-form:${name}:validate`, form.validate);\n\n useFormEvent(`mantine-form:${name}:validate-field`, (event: CustomEvent) =>\n form.validateField(event.detail)\n );\n\n useFormEvent(`mantine-form:${name}:reorder-list-item`, (event: CustomEvent) =>\n form.reorderListItem(event.detail.path, event.detail.payload)\n );\n\n useFormEvent(`mantine-form:${name}:remove-list-item`, (event: CustomEvent) =>\n form.removeListItem(event.detail.path, event.detail.index)\n );\n\n useFormEvent(`mantine-form:${name}:insert-list-item`, (event: CustomEvent) =>\n form.insertListItem(event.detail.path, event.detail.item, event.detail.index)\n );\n\n useFormEvent(`mantine-form:${name}:set-dirty`, (event: CustomEvent) =>\n form.setDirty(event.detail)\n );\n\n useFormEvent(`mantine-form:${name}:set-touched`, (event: CustomEvent) =>\n form.setTouched(event.detail)\n );\n\n useFormEvent(`mantine-form:${name}:reset-dirty`, (event: CustomEvent) =>\n form.resetDirty(event.detail)\n );\n\n useFormEvent(`mantine-form:${name}:reset-touched`, form.resetTouched);\n}\n"],"mappings":";;;AAmBA,SAAS,cAAc,MAAc,QAAmB;CACtD,OAAO,cAAc,IAAI,YAAY,MAAM,EAAE,OAAO,CAAC,CAAC;AACxD;AAEA,SAAS,iBAAiB,MAAc;CACtC,IAAI,CAAC,kBAAkB,KAAK,IAAI,GAC9B,MAAM,IAAI,MACR,kCAAkC,KAAK,iEACzC;AAEJ;AAEA,MAAa,sBAAsB,OAAO,WAAW,cAAcA,MAAAA,kBAAkBC,MAAAA;AAErF,SAAgB,kBACd,MACA;CACA,iBAAiB,IAAI;CAErB,MAAM,iBAA4C,MAAM,UACtD,cAAc,gBAAgB,KAAK,mBAAmB;EAAE;EAAM;CAAM,CAAC;CAEvE,MAAM,aAAoC,WACxC,cAAc,gBAAgB,KAAK,cAAc,MAAM;CAEzD,MAAM,oBAAkD,WACtD,cAAc,gBAAgB,KAAK,sBAAsB,MAAM;CAEjE,MAAM,aAAwB,WAAW,cAAc,gBAAgB,KAAK,cAAc,MAAM;CAEhG,MAAM,iBAA4C,MAAM,UACtD,cAAc,gBAAgB,KAAK,mBAAmB;EAAE;EAAM;CAAM,CAAC;CAEvE,MAAM,mBAAoC,SACxC,cAAc,gBAAgB,KAAK,qBAAqB,IAAI;CAE9D,MAAM,oBAAiC,cAAc,gBAA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AAiB,MAAM,MAAM,CACpC;CAEA,aAAa,gBAAgB,KAAK,eAAe,UAC/C,KAAK,UAAU,MAAM,MAAM,CAC7B;CAEA,aAAa,gBAAgB,KAAK,oBAAoB,UACpD,KAAK,cAAc,MAAM,OAAO,MAAM,MAAM,OAAO,KAAK,CAC1D;CAEA,aAAa,gBAAgB,KAAK,sBAAsB,UACtD,KAAK,gBAAgB,MAAM,MAAM,CACnC;CAEA,aAAa,gBAAgB,KAAK,gBAAgB,KAAK,WAAW;CAClE,aAAa,gBAAgB,KAAK,SAAS,KAAK,KAAK;CACrD,aAAa,gBAAgB,KAAK,YAAY,KAAK,QAAQ;CAE3D,aAAa,gBAAgB,KAAK,mBAAmB,UACnD,KAAK,cAAc,MAAM,MAAM,CACjC;CAEA,aAAa,gBAAgB,KAAK,sBAAsB,UACtD,KAAK,gBAAgB,MAAM,OAAO,MAAM,MAAM,OAAO,OAAO,CAC9D;CAEA,aAAa,gBAAgB,KAAK,qBAAqB,UACrD,KAAK,eAAe,MAAM,OAAO,MAAM,MAAM,OAAO,KAAK,CAC3D;CAEA,aAAa,gBAAgB,KAAK,qBAAqB,UACrD,KAAK,eAAe,MAAM,OAAO,MAAM,MAAM,OAAO,MAAM,MAAM,OAAO,KAAK,CAC9E;CAEA,aAAa,gBAAgB,KAAK,cAAc,UAC9C,KAAK,SAAS,MAAM,MAAM,CAC5B;CAEA,aAAa,gBAAgB,KAAK,gBAAgB,UAChD,KAAK,WAAW,MAAM,MAAM,CAC9B;CAEA,aAAa,gBAAgB,KAAK,gBAAgB,UAChD,KAAK,WAAW,MAAM,MAAM,CAC9B;CAEA,aAAa,gBAAgB,KAAK,iBAAiB,KAAK,YAAY;AACtE"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"use-form-errors.cjs","names":["filterErrors"],"sources":["../../../src/hooks/use-form-errors/use-form-errors.ts"],"sourcesContent":["import { useCallback, useRef, useState } from 'react';\nimport { ClearErrors, ClearFieldError, FormErrors, SetErrors, SetFieldError } from '../../types';\nimport { filterErrors } from './filter-errors/filter-errors';\n\nexport interface $FormErrors<Values extends Record<string, any>> {\n errorsState: FormErrors;\n setErrors: SetErrors;\n clearErrors: ClearErrors;\n setFieldError: SetFieldError<Values>;\n clearFieldError: ClearFieldError;\n}\n\nexport function useFormErrors<Values extends Record<string, any>>(\n initialErrors: FormErrors\n): $FormErrors<Values> {\n const [errorsState, setErrorsState] = useState(filterErrors(initialErrors));\n const errorsRef = useRef(errorsState);\n\n const setErrors: SetErrors = useCallback((errors) => {\n setErrorsState((current) => {\n const newErrors = filterErrors(typeof errors === 'function' ? errors(current) : errors);\n errorsRef.current = newErrors;\n return newErrors;\n });\n }, []);\n\n const clearErrors: ClearErrors = useCallback(() => setErrors({}), []);\n\n const clearFieldError: ClearFieldError = useCallback(\n (path) => {\n if (errorsRef.current[path as string] === undefined) {\n return;\n }\n\n setErrors((current) => {\n const errors = { ...current };\n delete errors[path as string];\n return errors;\n });\n },\n [errorsState]\n );\n\n const setFieldError: SetFieldError<Values> = useCallback(\n (path, error) => {\n if (error == null || error === false) {\n clearFieldError(path);\n } else if (errorsRef.current[path as string] !== error) {\n setErrors((current) => ({ ...current, [path]: error }));\n }\n },\n [errorsState]\n );\n\n return {\n errorsState,\n setErrors,\n clearErrors,\n setFieldError,\n clearFieldError,\n };\n}\n"],"mappings":"
|
|
1
|
+
{"version":3,"file":"use-form-errors.cjs","names":["filterErrors"],"sources":["../../../src/hooks/use-form-errors/use-form-errors.ts"],"sourcesContent":["import { useCallback, useRef, useState } from 'react';\nimport { ClearErrors, ClearFieldError, FormErrors, SetErrors, SetFieldError } from '../../types';\nimport { filterErrors } from './filter-errors/filter-errors';\n\nexport interface $FormErrors<Values extends Record<string, any>> {\n errorsState: FormErrors;\n setErrors: SetErrors;\n clearErrors: ClearErrors;\n setFieldError: SetFieldError<Values>;\n clearFieldError: ClearFieldError;\n}\n\nexport function useFormErrors<Values extends Record<string, any>>(\n initialErrors: FormErrors\n): $FormErrors<Values> {\n const [errorsState, setErrorsState] = useState(filterErrors(initialErrors));\n const errorsRef = useRef(errorsState);\n\n const setErrors: SetErrors = useCallback((errors) => {\n setErrorsState((current) => {\n const newErrors = filterErrors(typeof errors === 'function' ? errors(current) : errors);\n errorsRef.current = newErrors;\n return newErrors;\n });\n }, []);\n\n const clearErrors: ClearErrors = useCallback(() => setErrors({}), []);\n\n const clearFieldError: ClearFieldError = useCallback(\n (path) => {\n if (errorsRef.current[path as string] === undefined) {\n return;\n }\n\n setErrors((current) => {\n const errors = { ...current };\n delete errors[path as string];\n return errors;\n });\n },\n [errorsState]\n );\n\n const setFieldError: SetFieldError<Values> = useCallback(\n (path, error) => {\n if (error == null || error === false) {\n clearFieldError(path);\n } else if (errorsRef.current[path as string] !== error) {\n setErrors((current) => ({ ...current, [path]: error }));\n }\n },\n [errorsState]\n );\n\n return {\n errorsState,\n setErrors,\n clearErrors,\n setFieldError,\n clearFieldError,\n };\n}\n"],"mappings":";;;;AAYA,SAAgB,cACd,eACqB;CACrB,MAAM,CAAC,aAAa,mBAAA,GAAA,MAAA,UAA2BA,sBAAAA,aAAa,aAAa,CAAC;CAC1E,MAAM,aAAA,GAAA,MAAA,QAAmB,WAAW;CAEpC,MAAM,aAAA,GAAA,MAAA,cAAoC,WAAW;EACnD,gBAAgB,YAAY;GAC1B,MAAM,YAAYA,sBAAAA,aAAa,OAAO,WAAW,aAAa,OAAO,OAAO,IAAI,MAAM;GACtF,UAAU,UAAU;GACpB,OAAO;EACT,CAAC;CACH,GAAG,CAAC,CAAC;CAEL,MAAM,eAAA,GAAA,MAAA,mBAA6C,UAAU,CAAC,CAAC,GAAG,CAAC,CAAC;CAEpE,MAAM,mBAAA,GAAA,MAAA,cACH,SAAS;EACR,IAAI,UAAU,QAAQ,UAAoB,KAAA,GACxC;EAGF,WAAW,YAAY;GACrB,MAAM,SAAS,EAAE,GAAG,QAAQ;GAC5B,OAAO,OAAO;GACd,OAAO;EACT,CAAC;CACH,GACA,CAAC,WAAW,CACd;CAaA,OAAO;EACL;EACA;EACA;EACA,gBAAA,GAAA,MAAA,cAdC,MAAM,UAAU;GACf,IAAI,SAAS,QAAQ,UAAU,OAC7B,gBAAgB,IAAI;QACf,IAAI,UAAU,QAAQ,UAAoB,OAC/C,WAAW,aAAa;IAAE,GAAG;KAAU,OAAO;GAAM,EAAE;EAE1D,GACA,CAAC,WAAW,CAOA;EACZ;CACF;AACF"}
|
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
"use client";
|
|
2
|
-
require("../../_virtual/_rolldown/runtime.cjs");
|
|
3
2
|
const require_change_error_indices = require("../../lists/change-error-indices.cjs");
|
|
4
3
|
const require_reorder_errors = require("../../lists/reorder-errors.cjs");
|
|
5
4
|
const require_reorder_path = require("../../paths/reorder-path.cjs");
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"use-form-list.cjs","names":["reorderErrors","reorderPath","changeErrorIndices","removePath","insertPath","replacePath"],"sources":["../../../src/hooks/use-form-list/use-form-list.ts"],"sourcesContent":["import { useCallback } from 'react';\nimport { changeErrorIndices, reorderErrors } from '../../lists';\nimport { insertPath, removePath, reorderPath, replacePath } from '../../paths';\nimport { InsertListItem, RemoveListItem, ReorderListItem, ReplaceListItem } from '../../types';\nimport type { $FormErrors } from '../use-form-errors/use-form-errors';\nimport type { $FormStatus } from '../use-form-status/use-form-status';\nimport type { $FormValues } from '../use-form-values/use-form-values';\nimport type { $FormWatch } from '../use-form-watch/use-form-watch';\n\ninterface UseFormListInput<Values extends Record<string, any>> {\n $values: $FormValues<Values>;\n $errors: $FormErrors<Values>;\n $status: $FormStatus<Values>;\n $watch: $FormWatch<Values>;\n}\n\nexport function useFormList<Values extends Record<string, any>>({\n $values,\n $errors,\n $status,\n $watch,\n}: UseFormListInput<Values>) {\n const reorderListItem: ReorderListItem<Values> = useCallback((path, payload) => {\n const previousValues = $values.refValues.current;\n $status.clearFieldDirty(path);\n $errors.setErrors((errs) => reorderErrors(path, payload, errs));\n $values.setValues({\n values: reorderPath(path, payload, $values.refValues.current),\n updateState: true,\n });\n $watch.notifyWatchSubscribers(previousValues);\n }, []);\n\n const removeListItem: RemoveListItem<Values> = useCallback((path, index) => {\n const previousValues = $values.refValues.current;\n $status.clearFieldDirty(path);\n $errors.setErrors((errs) => changeErrorIndices(path, index, errs, -1));\n $values.setValues({\n values: removePath(path, index, $values.refValues.current),\n updateState: true,\n });\n $watch.notifyWatchSubscribers(previousValues);\n }, []);\n\n const insertListItem: InsertListItem<Values> = useCallback((path, item, index) => {\n const previousValues = $values.refValues.current;\n $status.clearFieldDirty(path);\n $errors.setErrors((errs) => changeErrorIndices(path, index, errs, 1));\n $values.setValues({\n values: insertPath(path, item, index, $values.refValues.current),\n updateState: true,\n });\n $watch.notifyWatchSubscribers(previousValues);\n }, []);\n\n const replaceListItem: ReplaceListItem<Values> = useCallback((path, index, item) => {\n const previousValues = $values.refValues.current;\n $status.clearFieldDirty(path);\n $values.setValues({\n values: replacePath(path, item, index, $values.refValues.current),\n updateState: true,\n });\n $watch.notifyWatchSubscribers(previousValues);\n }, []);\n\n return { reorderListItem, removeListItem, insertListItem, replaceListItem };\n}\n"],"mappings":"
|
|
1
|
+
{"version":3,"file":"use-form-list.cjs","names":["reorderErrors","reorderPath","changeErrorIndices","removePath","insertPath","replacePath"],"sources":["../../../src/hooks/use-form-list/use-form-list.ts"],"sourcesContent":["import { useCallback } from 'react';\nimport { changeErrorIndices, reorderErrors } from '../../lists';\nimport { insertPath, removePath, reorderPath, replacePath } from '../../paths';\nimport { InsertListItem, RemoveListItem, ReorderListItem, ReplaceListItem } from '../../types';\nimport type { $FormErrors } from '../use-form-errors/use-form-errors';\nimport type { $FormStatus } from '../use-form-status/use-form-status';\nimport type { $FormValues } from '../use-form-values/use-form-values';\nimport type { $FormWatch } from '../use-form-watch/use-form-watch';\n\ninterface UseFormListInput<Values extends Record<string, any>> {\n $values: $FormValues<Values>;\n $errors: $FormErrors<Values>;\n $status: $FormStatus<Values>;\n $watch: $FormWatch<Values>;\n}\n\nexport function useFormList<Values extends Record<string, any>>({\n $values,\n $errors,\n $status,\n $watch,\n}: UseFormListInput<Values>) {\n const reorderListItem: ReorderListItem<Values> = useCallback((path, payload) => {\n const previousValues = $values.refValues.current;\n $status.clearFieldDirty(path);\n $errors.setErrors((errs) => reorderErrors(path, payload, errs));\n $values.setValues({\n values: reorderPath(path, payload, $values.refValues.current),\n updateState: true,\n });\n $watch.notifyWatchSubscribers(previousValues);\n }, []);\n\n const removeListItem: RemoveListItem<Values> = useCallback((path, index) => {\n const previousValues = $values.refValues.current;\n $status.clearFieldDirty(path);\n $errors.setErrors((errs) => changeErrorIndices(path, index, errs, -1));\n $values.setValues({\n values: removePath(path, index, $values.refValues.current),\n updateState: true,\n });\n $watch.notifyWatchSubscribers(previousValues);\n }, []);\n\n const insertListItem: InsertListItem<Values> = useCallback((path, item, index) => {\n const previousValues = $values.refValues.current;\n $status.clearFieldDirty(path);\n $errors.setErrors((errs) => changeErrorIndices(path, index, errs, 1));\n $values.setValues({\n values: insertPath(path, item, index, $values.refValues.current),\n updateState: true,\n });\n $watch.notifyWatchSubscribers(previousValues);\n }, []);\n\n const replaceListItem: ReplaceListItem<Values> = useCallback((path, index, item) => {\n const previousValues = $values.refValues.current;\n $status.clearFieldDirty(path);\n $values.setValues({\n values: replacePath(path, item, index, $values.refValues.current),\n updateState: true,\n });\n $watch.notifyWatchSubscribers(previousValues);\n }, []);\n\n return { reorderListItem, removeListItem, insertListItem, replaceListItem };\n}\n"],"mappings":";;;;;;;;;AAgBA,SAAgB,YAAgD,EAC9D,SACA,SACA,SACA,UAC2B;CA4C3B,OAAO;EAAE,kBAAA,GAAA,MAAA,cA3CqD,MAAM,YAAY;GAC9E,MAAM,iBAAiB,QAAQ,UAAU;GACzC,QAAQ,gBAAgB,IAAI;GAC5B,QAAQ,WAAW,SAASA,uBAAAA,cAAc,MAAM,SAAS,IAAI,CAAC;GAC9D,QAAQ,UAAU;IAChB,QAAQC,qBAAAA,YAAY,MAAM,SAAS,QAAQ,UAAU,OAAO;IAC5D,aAAa;GACf,CAAC;GACD,OAAO,uBAAuB,cAAc;EAC9C,GAAG,CAAC,CAkCmB;EAAG,iBAAA,GAAA,MAAA,cAhCkC,MAAM,UAAU;GAC1E,MAAM,iBAAiB,QAAQ,UAAU;GACzC,QAAQ,gBAAgB,IAAI;GAC5B,QAAQ,WAAW,SAASC,6BAAAA,mBAAmB,MAAM,OAAO,MAAM,EAAE,CAAC;GACrE,QAAQ,UAAU;IAChB,QAAQC,oBAAAA,WAAW,MAAM,OAAO,QAAQ,UAAU,OAAO;IACzD,aAAa;GACf,CAAC;GACD,OAAO,uBAAuB,cAAc;EAC9C,GAAG,CAAC,CAuBmC;EAAG,iBAAA,GAAA,MAAA,cArBkB,MAAM,MAAM,UAAU;GAChF,MAAM,iBAAiB,QAAQ,UAAU;GACzC,QAAQ,gBAAgB,IAAI;GAC5B,QAAQ,WAAW,SAASD,6BAAAA,mBAAmB,MAAM,OAAO,MAAM,CAAC,CAAC;GACpE,QAAQ,UAAU;IAChB,QAAQE,oBAAAA,WAAW,MAAM,MAAM,OAAO,QAAQ,UAAU,OAAO;IAC/D,aAAa;GACf,CAAC;GACD,OAAO,uBAAuB,cAAc;EAC9C,GAAG,CAAC,CAYmD;EAAG,kBAAA,GAAA,MAAA,cAVI,MAAM,OAAO,SAAS;GAClF,MAAM,iBAAiB,QAAQ,UAAU;GACzC,QAAQ,gBAAgB,IAAI;GAC5B,QAAQ,UAAU;IAChB,QAAQC,qBAAAA,YAAY,MAAM,MAAM,OAAO,QAAQ,UAAU,OAAO;IAChE,aAAa;GACf,CAAC;GACD,OAAO,uBAAuB,cAAc;EAC9C,GAAG,CAAC,CAEoE;CAAE;AAC5E"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"use-form-validating.cjs","names":[],"sources":["../../../src/hooks/use-form-validating/use-form-validating.ts"],"sourcesContent":["import { useCallback, useRef, useState } from 'react';\n\nexport interface $FormValidating {\n validating: boolean;\n isValidating: (path?: string) => boolean;\n setFieldValidating: (path: string, validating: boolean) => void;\n setFormValidating: (validating: boolean) => void;\n getAbortSignal: (path: string) => AbortSignal;\n clearValidating: () => void;\n}\n\nexport function useFormValidating(): $FormValidating {\n const [validatingFields, setValidatingFields] = useState<Record<string, boolean>>({});\n const [formValidating, setFormValidatingState] = useState(false);\n const validatingRef = useRef<Record<string, boolean>>({});\n const formValidatingRef = useRef(false);\n const abortControllers = useRef<Record<string, AbortController>>({});\n\n const setFieldValidating = useCallback((path: string, value: boolean) => {\n validatingRef.current = { ...validatingRef.current, [path]: value };\n setValidatingFields({ ...validatingRef.current });\n }, []);\n\n const setFormValidating = useCallback((value: boolean) => {\n formValidatingRef.current = value;\n setFormValidatingState(value);\n }, []);\n\n const isValidating = useCallback((path?: string) => {\n if (path) {\n return !!validatingRef.current[path];\n }\n if (formValidatingRef.current) {\n return true;\n }\n return Object.values(validatingRef.current).some(Boolean);\n }, []);\n\n const getAbortSignal = useCallback((path: string) => {\n abortControllers.current[path]?.abort();\n abortControllers.current[path] = new AbortController();\n return abortControllers.current[path].signal;\n }, []);\n\n const clearValidating = useCallback(() => {\n validatingRef.current = {};\n setValidatingFields({});\n formValidatingRef.current = false;\n setFormValidatingState(false);\n Object.values(abortControllers.current).forEach((c) => c.abort());\n abortControllers.current = {};\n }, []);\n\n const validating = formValidating || Object.values(validatingFields).some(Boolean);\n\n return {\n validating,\n isValidating,\n setFieldValidating,\n setFormValidating,\n getAbortSignal,\n clearValidating,\n };\n}\n"],"mappings":"
|
|
1
|
+
{"version":3,"file":"use-form-validating.cjs","names":[],"sources":["../../../src/hooks/use-form-validating/use-form-validating.ts"],"sourcesContent":["import { useCallback, useRef, useState } from 'react';\n\nexport interface $FormValidating {\n validating: boolean;\n isValidating: (path?: string) => boolean;\n setFieldValidating: (path: string, validating: boolean) => void;\n setFormValidating: (validating: boolean) => void;\n getAbortSignal: (path: string) => AbortSignal;\n clearValidating: () => void;\n}\n\nexport function useFormValidating(): $FormValidating {\n const [validatingFields, setValidatingFields] = useState<Record<string, boolean>>({});\n const [formValidating, setFormValidatingState] = useState(false);\n const validatingRef = useRef<Record<string, boolean>>({});\n const formValidatingRef = useRef(false);\n const abortControllers = useRef<Record<string, AbortController>>({});\n\n const setFieldValidating = useCallback((path: string, value: boolean) => {\n validatingRef.current = { ...validatingRef.current, [path]: value };\n setValidatingFields({ ...validatingRef.current });\n }, []);\n\n const setFormValidating = useCallback((value: boolean) => {\n formValidatingRef.current = value;\n setFormValidatingState(value);\n }, []);\n\n const isValidating = useCallback((path?: string) => {\n if (path) {\n return !!validatingRef.current[path];\n }\n if (formValidatingRef.current) {\n return true;\n }\n return Object.values(validatingRef.current).some(Boolean);\n }, []);\n\n const getAbortSignal = useCallback((path: string) => {\n abortControllers.current[path]?.abort();\n abortControllers.current[path] = new AbortController();\n return abortControllers.current[path].signal;\n }, []);\n\n const clearValidating = useCallback(() => {\n validatingRef.current = {};\n setValidatingFields({});\n formValidatingRef.current = false;\n setFormValidatingState(false);\n Object.values(abortControllers.current).forEach((c) => c.abort());\n abortControllers.current = {};\n }, []);\n\n const validating = formValidating || Object.values(validatingFields).some(Boolean);\n\n return {\n validating,\n isValidating,\n setFieldValidating,\n setFormValidating,\n getAbortSignal,\n clearValidating,\n };\n}\n"],"mappings":";;;AAWA,SAAgB,oBAAqC;CACnD,MAAM,CAAC,kBAAkB,wBAAA,GAAA,MAAA,UAAyD,CAAC,CAAC;CACpF,MAAM,CAAC,gBAAgB,2BAAA,GAAA,MAAA,UAAmC,KAAK;CAC/D,MAAM,iBAAA,GAAA,MAAA,QAAgD,CAAC,CAAC;CACxD,MAAM,qBAAA,GAAA,MAAA,QAA2B,KAAK;CACtC,MAAM,oBAAA,GAAA,MAAA,QAA2D,CAAC,CAAC;CAEnE,MAAM,sBAAA,GAAA,MAAA,cAAkC,MAAc,UAAmB;EACvE,cAAc,UAAU;GAAE,GAAG,cAAc;IAAU,OAAO;EAAM;EAClE,oBAAoB,EAAE,GAAG,cAAc,QAAQ,CAAC;CAClD,GAAG,CAAC,CAAC;CAEL,MAAM,qBAAA,GAAA,MAAA,cAAiC,UAAmB;EACxD,kBAAkB,UAAU;EAC5B,uBAAuB,KAAK;CAC9B,GAAG,CAAC,CAAC;CAEL,MAAM,gBAAA,GAAA,MAAA,cAA4B,SAAkB;EAClD,IAAI,MACF,OAAO,CAAC,CAAC,cAAc,QAAQ;EAEjC,IAAI,kBAAkB,SACpB,OAAO;EAET,OAAO,OAAO,OAAO,cAAc,OAAO,EAAE,KAAK,OAAO;CAC1D,GAAG,CAAC,CAAC;CAEL,MAAM,kBAAA,GAAA,MAAA,cAA8B,SAAiB;EACnD,iBAAiB,QAAQ,OAAO,MAAM;EACtC,iBAAiB,QAAQ,QAAQ,IAAI,gBAAgB;EACrD,OAAO,iBAAiB,QAAQ,MAAM;CACxC,GAAG,CAAC,CAAC;CAEL,MAAM,mBAAA,GAAA,MAAA,mBAAoC;EACxC,cAAc,UAAU,CAAC;EACzB,oBAAoB,CAAC,CAAC;EACtB,kBAAkB,UAAU;EAC5B,uBAAuB,KAAK;EAC5B,OAAO,OAAO,iBAAiB,OAAO,EAAE,SAAS,MAAM,EAAE,MAAM,CAAC;EAChE,iBAAiB,UAAU,CAAC;CAC9B,GAAG,CAAC,CAAC;CAIL,OAAO;EACL,YAHiB,kBAAkB,OAAO,OAAO,gBAAgB,EAAE,KAAK,OAAO;EAI/E;EACA;EACA;EACA;EACA;CACF;AACF"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"use-form-values.cjs","names":["getPath","setPath"],"sources":["../../../src/hooks/use-form-values/use-form-values.ts"],"sourcesContent":["import { useCallback, useRef, useState } from 'react';\nimport { getPath, setPath } from '../../paths';\nimport { FormMode } from '../../types';\n\nexport interface $FormValues<out Values extends Record<PropertyKey, any>> {\n initialized: React.RefObject<boolean>;\n stateValues: Values;\n refValues: React.RefObject<Values>;\n valuesSnapshot: React.RefObject<Values>;\n setValues: <TValues extends Values>(payload: SetValuesInput<TValues>) => void;\n setFieldValue: <TValues extends Values>(payload: SetFieldValueInput<TValues>) => void;\n resetValues: () => void;\n setValuesSnapshot: <TValues extends Values>(payload: TValues) => void;\n initialize: <TValues extends Values>(values: TValues, onInitialize: () => void) => void;\n getValues: () => Values;\n getValuesSnapshot: () => Values;\n resetField: <TValues extends Values>(\n path: PropertyKey,\n subscribers?: (SetFieldValueSubscriber<TValues> | null | undefined)[]\n ) => void;\n}\n\nexport interface SetValuesSubscriberPayload<Values> {\n path?: PropertyKey;\n updatedValues: Values;\n previousValues: Values;\n}\n\nexport interface SetValuesInput<out Values = Record<string, any>> {\n values: Partial<Values> | (<TValues extends Values>(values: TValues) => Partial<TValues>);\n mergeWithPreviousValues?: boolean;\n updateState?: boolean;\n subscribers?: (SetFieldValueSubscriber<Values> | null | undefined)[];\n}\n\nexport type SetFieldValueSubscriber<out Values> = <TValues extends Values>(\n payload: SetValuesSubscriberPayload<TValues>\n) => void;\n\nexport interface SetFieldValueInput<Values> {\n path: PropertyKey;\n value: any;\n updateState?: boolean;\n subscribers?: (SetFieldValueSubscriber<Values> | null | undefined)[];\n}\n\ninterface UseFormValuesInput<Values extends Record<PropertyKey, any>> {\n initialValues: Values | undefined;\n mode: FormMode;\n onValuesChange?: ((values: Values, previousValues: Values) => void) | undefined;\n}\n\nexport function useFormValues<Values extends Record<PropertyKey, any>>({\n initialValues,\n onValuesChange,\n mode,\n}: UseFormValuesInput<Values>): $FormValues<Values> {\n const initialized = useRef(false);\n const [stateValues, setStateValues] = useState<Values>(initialValues || ({} as Values));\n const refValues = useRef(stateValues);\n const valuesSnapshot = useRef(stateValues);\n\n const setValues = useCallback(\n ({\n values,\n subscribers,\n updateState = true,\n mergeWithPreviousValues = true,\n }: SetValuesInput<Values>) => {\n const previousValues = refValues.current;\n const resolvedValues = values instanceof Function ? values(refValues.current) : values;\n const updatedValues = mergeWithPreviousValues\n ? { ...previousValues, ...resolvedValues }\n : (resolvedValues as Values);\n refValues.current = updatedValues;\n if (updateState) {\n setStateValues(updatedValues);\n if (mode === 'uncontrolled') {\n refValues.current = updatedValues;\n }\n }\n onValuesChange?.(updatedValues, previousValues);\n subscribers\n ?.filter(Boolean)\n .forEach((subscriber) => subscriber!({ updatedValues, previousValues }));\n },\n [onValuesChange]\n );\n\n const setFieldValue = useCallback(\n (payload: SetFieldValueInput<Values>) => {\n const currentValue = getPath(payload.path, refValues.current);\n const updatedValue =\n payload.value instanceof Function ? payload.value(currentValue) : payload.value;\n\n if (currentValue !== updatedValue) {\n const previousValues = refValues.current;\n const updatedValues = setPath(payload.path, updatedValue, refValues.current);\n setValues({ values: updatedValues, updateState: payload.updateState });\n\n payload.subscribers\n ?.filter(Boolean)\n .forEach((subscriber) =>\n subscriber!({ path: payload.path, updatedValues, previousValues })\n );\n }\n },\n [setValues]\n );\n\n const setValuesSnapshot = useCallback((payload: Values) => {\n valuesSnapshot.current = payload;\n }, []);\n\n const initialize = useCallback(\n (values: Values, onInitialize: () => void) => {\n if (!initialized.current) {\n initialized.current = true;\n setValues({ values, updateState: mode === 'controlled' });\n setValuesSnapshot(values);\n onInitialize();\n }\n },\n [setValues]\n );\n\n const resetValues = useCallback(() => {\n setValues({\n values: valuesSnapshot.current,\n updateState: true,\n mergeWithPreviousValues: false,\n });\n }, [setValues]);\n\n const getValues = useCallback(() => refValues.current, []);\n const getValuesSnapshot = useCallback(() => valuesSnapshot.current, []);\n\n const resetField = useCallback(\n (path: PropertyKey, subscribers?: (SetFieldValueSubscriber<Values> | null | undefined)[]) => {\n const snapshotValue = getPath(path, valuesSnapshot.current);\n if (typeof snapshotValue === 'undefined') {\n return;\n }\n setFieldValue({\n path,\n value: snapshotValue,\n updateState: mode === 'controlled',\n subscribers,\n });\n },\n [setFieldValue, mode]\n );\n\n return {\n initialized,\n stateValues,\n refValues,\n valuesSnapshot,\n setValues,\n setFieldValue,\n resetValues,\n setValuesSnapshot,\n initialize,\n getValues,\n getValuesSnapshot,\n resetField,\n };\n}\n"],"mappings":"
|
|
1
|
+
{"version":3,"file":"use-form-values.cjs","names":["getPath","setPath"],"sources":["../../../src/hooks/use-form-values/use-form-values.ts"],"sourcesContent":["import { useCallback, useRef, useState } from 'react';\nimport { getPath, setPath } from '../../paths';\nimport { FormMode } from '../../types';\n\nexport interface $FormValues<out Values extends Record<PropertyKey, any>> {\n initialized: React.RefObject<boolean>;\n stateValues: Values;\n refValues: React.RefObject<Values>;\n valuesSnapshot: React.RefObject<Values>;\n setValues: <TValues extends Values>(payload: SetValuesInput<TValues>) => void;\n setFieldValue: <TValues extends Values>(payload: SetFieldValueInput<TValues>) => void;\n resetValues: () => void;\n setValuesSnapshot: <TValues extends Values>(payload: TValues) => void;\n initialize: <TValues extends Values>(values: TValues, onInitialize: () => void) => void;\n getValues: () => Values;\n getValuesSnapshot: () => Values;\n resetField: <TValues extends Values>(\n path: PropertyKey,\n subscribers?: (SetFieldValueSubscriber<TValues> | null | undefined)[]\n ) => void;\n}\n\nexport interface SetValuesSubscriberPayload<Values> {\n path?: PropertyKey;\n updatedValues: Values;\n previousValues: Values;\n}\n\nexport interface SetValuesInput<out Values = Record<string, any>> {\n values: Partial<Values> | (<TValues extends Values>(values: TValues) => Partial<TValues>);\n mergeWithPreviousValues?: boolean;\n updateState?: boolean;\n subscribers?: (SetFieldValueSubscriber<Values> | null | undefined)[];\n}\n\nexport type SetFieldValueSubscriber<out Values> = <TValues extends Values>(\n payload: SetValuesSubscriberPayload<TValues>\n) => void;\n\nexport interface SetFieldValueInput<Values> {\n path: PropertyKey;\n value: any;\n updateState?: boolean;\n subscribers?: (SetFieldValueSubscriber<Values> | null | undefined)[];\n}\n\ninterface UseFormValuesInput<Values extends Record<PropertyKey, any>> {\n initialValues: Values | undefined;\n mode: FormMode;\n onValuesChange?: ((values: Values, previousValues: Values) => void) | undefined;\n}\n\nexport function useFormValues<Values extends Record<PropertyKey, any>>({\n initialValues,\n onValuesChange,\n mode,\n}: UseFormValuesInput<Values>): $FormValues<Values> {\n const initialized = useRef(false);\n const [stateValues, setStateValues] = useState<Values>(initialValues || ({} as Values));\n const refValues = useRef(stateValues);\n const valuesSnapshot = useRef(stateValues);\n\n const setValues = useCallback(\n ({\n values,\n subscribers,\n updateState = true,\n mergeWithPreviousValues = true,\n }: SetValuesInput<Values>) => {\n const previousValues = refValues.current;\n const resolvedValues = values instanceof Function ? values(refValues.current) : values;\n const updatedValues = mergeWithPreviousValues\n ? { ...previousValues, ...resolvedValues }\n : (resolvedValues as Values);\n refValues.current = updatedValues;\n if (updateState) {\n setStateValues(updatedValues);\n if (mode === 'uncontrolled') {\n refValues.current = updatedValues;\n }\n }\n onValuesChange?.(updatedValues, previousValues);\n subscribers\n ?.filter(Boolean)\n .forEach((subscriber) => subscriber!({ updatedValues, previousValues }));\n },\n [onValuesChange]\n );\n\n const setFieldValue = useCallback(\n (payload: SetFieldValueInput<Values>) => {\n const currentValue = getPath(payload.path, refValues.current);\n const updatedValue =\n payload.value instanceof Function ? payload.value(currentValue) : payload.value;\n\n if (currentValue !== updatedValue) {\n const previousValues = refValues.current;\n const updatedValues = setPath(payload.path, updatedValue, refValues.current);\n setValues({ values: updatedValues, updateState: payload.updateState });\n\n payload.subscribers\n ?.filter(Boolean)\n .forEach((subscriber) =>\n subscriber!({ path: payload.path, updatedValues, previousValues })\n );\n }\n },\n [setValues]\n );\n\n const setValuesSnapshot = useCallback((payload: Values) => {\n valuesSnapshot.current = payload;\n }, []);\n\n const initialize = useCallback(\n (values: Values, onInitialize: () => void) => {\n if (!initialized.current) {\n initialized.current = true;\n setValues({ values, updateState: mode === 'controlled' });\n setValuesSnapshot(values);\n onInitialize();\n }\n },\n [setValues]\n );\n\n const resetValues = useCallback(() => {\n setValues({\n values: valuesSnapshot.current,\n updateState: true,\n mergeWithPreviousValues: false,\n });\n }, [setValues]);\n\n const getValues = useCallback(() => refValues.current, []);\n const getValuesSnapshot = useCallback(() => valuesSnapshot.current, []);\n\n const resetField = useCallback(\n (path: PropertyKey, subscribers?: (SetFieldValueSubscriber<Values> | null | undefined)[]) => {\n const snapshotValue = getPath(path, valuesSnapshot.current);\n if (typeof snapshotValue === 'undefined') {\n return;\n }\n setFieldValue({\n path,\n value: snapshotValue,\n updateState: mode === 'controlled',\n subscribers,\n });\n },\n [setFieldValue, mode]\n );\n\n return {\n initialized,\n stateValues,\n refValues,\n valuesSnapshot,\n setValues,\n setFieldValue,\n resetValues,\n setValuesSnapshot,\n initialize,\n getValues,\n getValuesSnapshot,\n resetField,\n };\n}\n"],"mappings":";;;;;AAoDA,SAAgB,cAAuD,EACrE,eACA,gBACA,QACkD;CAClD,MAAM,eAAA,GAAA,MAAA,QAAqB,KAAK;CAChC,MAAM,CAAC,aAAa,mBAAA,GAAA,MAAA,UAAmC,iBAAkB,CAAC,CAAY;CACtF,MAAM,aAAA,GAAA,MAAA,QAAmB,WAAW;CACpC,MAAM,kBAAA,GAAA,MAAA,QAAwB,WAAW;CAEzC,MAAM,aAAA,GAAA,MAAA,cACH,EACC,QACA,aACA,cAAc,MACd,0BAA0B,WACE;EAC5B,MAAM,iBAAiB,UAAU;EACjC,MAAM,iBAAiB,kBAAkB,WAAW,OAAO,UAAU,OAAO,IAAI;EAChF,MAAM,gBAAgB,0BAClB;GAAE,GAAG;GAAgB,GAAG;EAAe,IACtC;EACL,UAAU,UAAU;EACpB,IAAI,aAAa;GACf,eAAe,aAAa;GAC5B,IAAI,SAAS,gBACX,UAAU,UAAU;EAExB;EACA,iBAAiB,eAAe,cAAc;EAC9C,aACI,OAAO,OAAO,EACf,SAAS,eAAe,WAAY;GAAE;GAAe;EAAe,CAAC,CAAC;CAC3E,GACA,CAAC,cAAc,CACjB;CAEA,MAAM,iBAAA,GAAA,MAAA,cACH,YAAwC;EACvC,MAAM,eAAeA,iBAAAA,QAAQ,QAAQ,MAAM,UAAU,OAAO;EAC5D,MAAM,eACJ,QAAQ,iBAAiB,WAAW,QAAQ,MAAM,YAAY,IAAI,QAAQ;EAE5E,IAAI,i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src/hooks/use-form-watch/use-form-watch.ts"],"sourcesContent":["import { useCallback, useEffect, useRef } from 'react';\nimport { getPath } from '../../paths';\nimport { FormPathValue, LooseKeys } from '../../paths.types';\nimport { FormFieldSubscriber, Watch } from '../../types';\nimport { $FormStatus } from '../use-form-status/use-form-status';\nimport { $FormValues, SetValuesSubscriberPayload } from '../use-form-values/use-form-values';\n\ninterface UseFormWatchInput<out Values extends Record<PropertyKey, any>> {\n $values: $FormValues<Values>;\n $status: $FormStatus<Values>;\n cascadeUpdates?: boolean;\n}\n\nexport interface $FormWatch<Values extends Record<PropertyKey, any>> {\n subscribers: React.RefObject<Record<string, FormFieldSubscriber<Values, any>[]>>;\n watch: Watch<Values, any>;\n getFieldSubscribers: (path: any) => ((input: SetValuesSubscriberPayload<Values>) => void)[];\n notifyWatchSubscribers: (previousValues: Values) => void;\n}\n\nexport function useFormWatch<\n Values extends Record<PropertyKey, any>,\n Field extends LooseKeys<Values> = LooseKeys<Values>,\n>({ $values, $status, cascadeUpdates }: UseFormWatchInput<Values>) {\n const subscribers = useRef<Record<Field, FormFieldSubscriber<Values, Field>[]>>({} as any);\n\n const watch: Watch<Values, Field> = useCallback((path, callback) => {\n useEffect(() => {\n subscribers.current[path] = subscribers.current[path] || [];\n subscribers.current[path].push(callback);\n\n return () => {\n subscribers.current[path] = subscribers.current[path].filter((cb) => cb !== callback);\n };\n }, [callback]);\n }, []);\n\n const getFieldSubscribers = useCallback((path: Field) => {\n const result: ((input: SetValuesSubscriberPayload<Values>) => void)[] =\n subscribers.current[path]?.map(\n (callback) => (input: SetValuesSubscriberPayload<Values>) =>\n callback({\n previousValue: getPath(path, input.previousValues) as any,\n value: getPath(path, input.updatedValues) as any,\n touched: $status.isTouched(path),\n dirty: $status.isDirty(path),\n })\n ) ?? [];\n\n for (const subscriptionKey in subscribers.current) {\n const isParent = String(path).startsWith(`${subscriptionKey}.`);\n const isChild = String(subscriptionKey).startsWith(`${path}.`);\n\n if (isParent || (cascadeUpdates && isChild)) {\n result.push(\n ...subscribers.current[subscriptionKey].map(\n (cb) => (input: SetValuesSubscriberPayload<Values>) =>\n cb({\n previousValue: getPath(subscriptionKey, input.previousValues) as any,\n value: getPath(subscriptionKey, input.updatedValues) as any,\n touched: $status.isTouched(subscriptionKey),\n dirty: $status.isDirty(subscriptionKey),\n })\n )\n );\n }\n }\n\n return result;\n }, []);\n\n const notifyWatchSubscribers = useCallback((previousValues: Values) => {\n Object.keys(subscribers.current).forEach((path) => {\n const value = getPath(path, $values.refValues.current);\n const previousValue = getPath(path, previousValues);\n\n if (value !== previousValue) {\n subscribers.current[path as Field]?.forEach((cb) =>\n cb({\n previousValue: getPath(path, previousValues) as FormPathValue<Values, Field>,\n value: getPath(path, $values.refValues.current) as FormPathValue<Values, Field>,\n touched: $status.isTouched(path),\n dirty: $status.isDirty(path),\n })\n );\n }\n });\n }, []);\n\n return {\n subscribers,\n watch,\n getFieldSubscribers,\n notifyWatchSubscribers,\n };\n}\n"],"mappings":"
|
|
1
|
+
{"version":3,"file":"use-form-watch.cjs","names":["getPath"],"sources":["../../../src/hooks/use-form-watch/use-form-watch.ts"],"sourcesContent":["import { useCallback, useEffect, useRef } from 'react';\nimport { getPath } from '../../paths';\nimport { FormPathValue, LooseKeys } from '../../paths.types';\nimport { FormFieldSubscriber, Watch } from '../../types';\nimport { $FormStatus } from '../use-form-status/use-form-status';\nimport { $FormValues, SetValuesSubscriberPayload } from '../use-form-values/use-form-values';\n\ninterface UseFormWatchInput<out Values extends Record<PropertyKey, any>> {\n $values: $FormValues<Values>;\n $status: $FormStatus<Values>;\n cascadeUpdates?: boolean;\n}\n\nexport interface $FormWatch<Values extends Record<PropertyKey, any>> {\n subscribers: React.RefObject<Record<string, FormFieldSubscriber<Values, any>[]>>;\n watch: Watch<Values, any>;\n getFieldSubscribers: (path: any) => ((input: SetValuesSubscriberPayload<Values>) => void)[];\n notifyWatchSubscribers: (previousValues: Values) => void;\n}\n\nexport function useFormWatch<\n Values extends Record<PropertyKey, any>,\n Field extends LooseKeys<Values> = LooseKeys<Values>,\n>({ $values, $status, cascadeUpdates }: UseFormWatchInput<Values>) {\n const subscribers = useRef<Record<Field, FormFieldSubscriber<Values, Field>[]>>({} as any);\n\n const watch: Watch<Values, Field> = useCallback((path, callback) => {\n useEffect(() => {\n subscribers.current[path] = subscribers.current[path] || [];\n subscribers.current[path].push(callback);\n\n return () => {\n subscribers.current[path] = subscribers.current[path].filter((cb) => cb !== callback);\n };\n }, [callback]);\n }, []);\n\n const getFieldSubscribers = useCallback((path: Field) => {\n const result: ((input: SetValuesSubscriberPayload<Values>) => void)[] =\n subscribers.current[path]?.map(\n (callback) => (input: SetValuesSubscriberPayload<Values>) =>\n callback({\n previousValue: getPath(path, input.previousValues) as any,\n value: getPath(path, input.updatedValues) as any,\n touched: $status.isTouched(path),\n dirty: $status.isDirty(path),\n })\n ) ?? [];\n\n for (const subscriptionKey in subscribers.current) {\n const isParent = String(path).startsWith(`${subscriptionKey}.`);\n const isChild = String(subscriptionKey).startsWith(`${path}.`);\n\n if (isParent || (cascadeUpdates && isChild)) {\n result.push(\n ...subscribers.current[subscriptionKey].map(\n (cb) => (input: SetValuesSubscriberPayload<Values>) =>\n cb({\n previousValue: getPath(subscriptionKey, input.previousValues) as any,\n value: getPath(subscriptionKey, input.updatedValues) as any,\n touched: $status.isTouched(subscriptionKey),\n dirty: $status.isDirty(subscriptionKey),\n })\n )\n );\n }\n }\n\n return result;\n }, []);\n\n const notifyWatchSubscribers = useCallback((previousValues: Values) => {\n Object.keys(subscribers.current).forEach((path) => {\n const value = getPath(path, $values.refValues.current);\n const previousValue = getPath(path, previousValues);\n\n if (value !== previousValue) {\n subscribers.current[path as Field]?.forEach((cb) =>\n cb({\n previousValue: getPath(path, previousValues) as FormPathValue<Values, Field>,\n value: getPath(path, $values.refValues.current) as FormPathValue<Values, Field>,\n touched: $status.isTouched(path),\n dirty: $status.isDirty(path),\n })\n );\n }\n });\n }, []);\n\n return {\n subscribers,\n watch,\n getField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AG,CAAC,CAuBgB;EAClB,yBAAA,GAAA,MAAA,cAtB0C,mBAA2B;GACrE,OAAO,KAAK,YAAY,OAAO,EAAE,SAAS,SAAS;IAIjD,IAHcA,iBAAAA,QAAQ,MAAM,QAAQ,UAAU,OAGtC,MAFcA,iBAAAA,QAAQ,MAAM,cAEV,GACxB,YAAY,QAAQ,OAAgB,SAAS,OAC3C,GAAG;KACD,eAAeA,iBAAAA,QAAQ,MAAM,cAAc;KAC3C,OAAOA,iBAAAA,QAAQ,MAAM,QAAQ,UAAU,OAAO;KAC9C,SAAS,QAAQ,UAAU,IAAI;KAC/B,OAAO,QAAQ,QAAQ,IAAI;IAC7B,CAAC,CACH;GAEJ,CAAC;EACH,GAAG,CAAC,CAMmB;CACvB;AACF"}
|
package/cjs/paths/set-path.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"set-path.cjs","names":["getSplittedPath"],"sources":["../../src/paths/set-path.ts"],"sourcesContent":["import { klona } from 'klona/full';\nimport { getSplittedPath } from './get-splitted-path';\n\nexport function setPath<T>(path: unknown, value: unknown, values: T): T {\n const splittedPath = getSplittedPath(path);\n\n if (splittedPath.length === 0) {\n return values;\n }\n\n const cloned: any = klona(values);\n\n if (splittedPath.length === 1) {\n cloned[splittedPath[0]] = value;\n return cloned;\n }\n\n let val = cloned[splittedPath[0]];\n\n for (let i = 1; i < splittedPath.length - 1; i += 1) {\n if (val === undefined) {\n return cloned;\n }\n\n val = val[splittedPath[i]];\n }\n\n val[splittedPath[splittedPath.length - 1]] = value;\n\n return cloned;\n}\n"],"mappings":"
|
|
1
|
+
{"version":3,"file":"set-path.cjs","names":["getSplittedPath"],"sources":["../../src/paths/set-path.ts"],"sourcesContent":["import { klona } from 'klona/full';\nimport { getSplittedPath } from './get-splitted-path';\n\nexport function setPath<T>(path: unknown, value: unknown, values: T): T {\n const splittedPath = getSplittedPath(path);\n\n if (splittedPath.length === 0) {\n return values;\n }\n\n const cloned: any = klona(values);\n\n if (splittedPath.length === 1) {\n cloned[splittedPath[0]] = value;\n return cloned;\n }\n\n let val = cloned[splittedPath[0]];\n\n for (let i = 1; i < splittedPath.length - 1; i += 1) {\n if (val === undefined) {\n return cloned;\n }\n\n val = val[splittedPath[i]];\n }\n\n val[splittedPath[splittedPath.length - 1]] = value;\n\n return cloned;\n}\n"],"mappings":";;;;AAGA,SAAgB,QAAW,MAAe,OAAgB,QAAc;CACtE,MAAM,eAAeA,0BAAAA,gBAAgB,IAAI;CAEzC,IAAI,aAAa,WAAW,GAC1B,OAAO;CAGT,MAAM,UAAA,GAAA,WAAA,OAAoB,MAAM;CAEhC,IAAI,aAAa,WAAW,GAAG;EAC7B,OAAO,aAAa,MAAM;EAC1B,OAAO;CACT;CAEA,IAAI,MAAM,OAAO,aAAa;CAE9B,KAAK,IAAI,IAAI,GAAG,IAAI,aAAa,SAAS,GAAG,KAAK,GAAG;EACnD,IAAI,QAAQ,KAAA,GACV,OAAO;EAGT,MAAM,IAAI,aAAa;CACzB;CAEA,IAAI,aAAa,aAAa,SAAS,MAAM;CAE7C,OAAO;AACT"}
|
package/cjs/use-field.cjs
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
"use client";
|
|
2
|
-
require("./_virtual/_rolldown/runtime.cjs");
|
|
3
2
|
const require_get_input_on_change = require("./get-input-on-change/get-input-on-change.cjs");
|
|
4
3
|
const require_should_validate_on_change = require("./validate/should-validate-on-change.cjs");
|
|
5
4
|
let react = require("react");
|
package/cjs/use-field.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"use-field.cjs","names":["getInputOnChange","shouldValidateOnChange"],"sources":["../src/use-field.ts"],"sourcesContent":["import { useCallback, useMemo, useRef, useState } from 'react';\nimport { getInputOnChange } from './get-input-on-change';\nimport { FormMode, GetInputPropsType } from './types';\nimport { shouldValidateOnChange } from './validate';\n\ntype UseFieldErrorResolver = (error: unknown) => React.ReactNode;\n\nexport interface UseFieldInput<\n T,\n FieldType extends GetInputPropsType = 'input',\n Mode extends FormMode = 'controlled',\n> {\n /** Field mode, controlled by default */\n mode?: Mode;\n\n /** Initial field value */\n initialValue: T;\n\n /** Initial touched value */\n initialTouched?: boolean;\n\n /** Initial field error message */\n initialError?: React.ReactNode;\n\n /** Called with updated value when the field value changes */\n onValueChange?: (value: T) => void;\n\n /** Determines whether the field should be validated when value changes, false by default */\n validateOnChange?: boolean;\n\n /** Determines whether the field should be validated when it loses focus, false by default */\n validateOnBlur?: boolean;\n\n /** Determines whether the field should clear error message when value changes, true by default */\n clearErrorOnChange?: boolean;\n\n /** A function to validate field value, can be sync or async */\n validate?: (value: T) => React.ReactNode | Promise<React.ReactNode>;\n\n /** Field type, input by default */\n type?: FieldType;\n\n /** A function to resolve validation error from the result returned from validate function, should return react node */\n resolveValidationError?: UseFieldErrorResolver;\n}\n\ninterface SetValueOptions {\n updateState?: boolean;\n updateKey?: boolean;\n}\n\ninterface GetInputPropsOptions {\n withError?: boolean;\n withFocus?: boolean;\n [key: string]: any;\n}\n\ninterface GetInputPropsSharedReturn {\n error?: React.ReactNode;\n onFocus?: () => void;\n onBlur: () => void;\n onChange: (value: any) => void;\n}\n\ntype GetInputPropsTypeValue<\n T,\n FieldType extends GetInputPropsType,\n Mode extends FormMode,\n> = FieldType extends 'checkbox'\n ? Mode extends 'controlled'\n ? { checked: boolean }\n : { defaultChecked: boolean }\n : FieldType extends 'radio'\n ? Mode extends 'controlled'\n ? { checked: boolean; value: T }\n : { defaultChecked: boolean; value: T }\n : Mode extends 'controlled'\n ? { value: T }\n : { defaultValue: T };\n\ntype GetInputPropsReturnType<\n T,\n FieldType extends GetInputPropsType,\n Mode extends FormMode,\n> = GetInputPropsSharedReturn & GetInputPropsTypeValue<T, FieldType, Mode>;\n\nexport interface UseFieldReturnType<\n T,\n FieldType extends GetInputPropsType = 'input',\n Mode extends FormMode = 'controlled',\n> {\n /** Returns props to pass to the input element */\n getInputProps: (options?: GetInputPropsOptions) => GetInputPropsReturnType<T, FieldType, Mode>;\n\n /** Returns current input value */\n getValue: () => T;\n\n /** Sets input value to the given value */\n setValue: (value: T) => void;\n\n /** Resets field value to initial state, sets touched state to false, sets error to null */\n reset: () => void;\n\n /** Validates current input value when called */\n validate: () => Promise<React.ReactNode | void>;\n\n /** Set to true when async validate function is called, stays true until the returned promise resolves */\n isValidating: boolean;\n\n /** Current error message */\n error: React.ReactNode;\n\n /** Sets error message to the given react node */\n setError: (error: React.ReactNode) => void;\n\n /** Returns true if the input has been focused at least once */\n isTouched: () => boolean;\n\n /** Returns true if input value is different from the initial value */\n isDirty: () => boolean;\n\n /** Resets touched state to false */\n resetTouched: () => void;\n\n /** Key that should be added to the input when mode is uncontrolled */\n key: number;\n}\n\nexport function useField<\n T,\n Mode extends FormMode = 'controlled',\n FieldType extends GetInputPropsType = 'input',\n>({\n mode = 'controlled' as Mode,\n clearErrorOnChange = true,\n initialValue,\n initialError = null,\n initialTouched = false,\n onValueChange,\n validateOnChange = false,\n validateOnBlur = false,\n validate,\n resolveValidationError,\n type = 'input' as FieldType,\n}: UseFieldInput<T, FieldType, Mode>): UseFieldReturnType<T, FieldType, Mode> {\n const [valueState, setValueState] = useState(initialValue);\n const valueRef = useRef(valueState);\n const [key, setKey] = useState(0);\n const [error, setError] = useState<React.ReactNode>(initialError || null);\n const touchedRef = useRef(initialTouched || false);\n const [, setTouchedState] = useState(touchedRef.current);\n const [isValidating, setIsValidating] = useState(false);\n const errorResolver: UseFieldErrorResolver = useMemo(\n () => resolveValidationError || ((err) => err as React.ReactNode),\n [resolveValidationError]\n );\n\n const setTouched = useCallback((val: boolean, { updateState = mode === 'controlled' } = {}) => {\n touchedRef.current = val;\n updateState && setTouchedState(val);\n }, []);\n\n const setValue = useCallback(\n (\n value: T,\n {\n updateKey = mode === 'uncontrolled',\n updateState = mode === 'controlled',\n }: SetValueOptions = {}\n ) => {\n if (valueRef.current === value) {\n return;\n }\n\n valueRef.current = value;\n\n onValueChange?.(value);\n\n if (clearErrorOnChange && error !== null) {\n setError(null);\n }\n\n if (updateState) {\n setValueState(value);\n }\n\n if (updateKey) {\n setKey((currentKey) => currentKey + 1);\n }\n\n if (validateOnChange) {\n _validate();\n }\n },\n [error, clearErrorOnChange, onValueChange]\n );\n\n const reset = useCallback(() => {\n setValue(initialValue);\n setError(null);\n setTouched(false);\n }, [initialValue]);\n\n const getValue = useCallback(() => valueRef.current, []);\n\n const isTouched = useCallback(() => touchedRef.current, []);\n\n const isDirty = useCallback(() => valueRef.current !== initialValue, [initialValue]);\n\n const _validate = useCallback(async () => {\n const validationResult = validate?.(valueRef.current);\n\n if (validationResult instanceof Promise) {\n setIsValidating(true);\n try {\n const result = await validationResult;\n setIsValidating(false);\n setError(result);\n } catch (err) {\n setIsValidating(false);\n const resolvedError = errorResolver(err);\n setError(resolvedError);\n return resolvedError;\n }\n } else {\n setError(validationResult);\n return validationResult;\n }\n }, []);\n\n const getInputProps = ({ withError = true, withFocus = true, ...otherOptions }: any = {}) => {\n const onChange = getInputOnChange<T>((val) => setValue(val as any, { updateKey: false }));\n\n const payload: any = { onChange };\n\n if (withError) {\n payload.error = error;\n }\n\n if (type === 'checkbox') {\n payload[mode === 'controlled' ? 'checked' : 'defaultChecked'] = valueRef.current;\n } else if (type === 'radio') {\n payload[mode === 'controlled' ? 'checked' : 'defaultChecked'] =\n valueRef.current === otherOptions.value;\n payload.value = otherOptions.value;\n } else {\n payload[mode === 'controlled' ? 'value' : 'defaultValue'] = valueRef.current;\n }\n\n if (withFocus) {\n payload.onFocus = () => {\n setTouched(true);\n };\n\n payload.onBlur = () => {\n if (shouldValidateOnChange('', !!validateOnBlur)) {\n _validate();\n }\n };\n }\n\n return payload;\n };\n\n const resetTouched = useCallback(() => setTouched(false), []);\n\n return {\n key,\n getValue,\n setValue,\n reset,\n getInputProps,\n\n isValidating,\n validate: _validate,\n\n error,\n setError,\n\n isTouched,\n isDirty,\n resetTouched,\n };\n}\n"],"mappings":";;;;;;AAgIA,SAAgB,SAId,EACA,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A;EAEA;EACA;EACA,eAAA,GAAA,MAAA,mBAjBqC,WAAW,KAAK,GAAG,CAAC,CAiB9C;CACb;AACF"}
|
|
1
|
+
{"version":3,"file":"use-field.cjs","names":["getInputOnChange","shouldValidateOnChange"],"sources":["../src/use-field.ts"],"sourcesContent":["import { useCallback, useMemo, useRef, useState } from 'react';\nimport { getInputOnChange } from './get-input-on-change';\nimport { FormMode, GetInputPropsType } from './types';\nimport { shouldValidateOnChange } from './validate';\n\ntype UseFieldErrorResolver = (error: unknown) => React.ReactNode;\n\nexport interface UseFieldInput<\n T,\n FieldType extends GetInputPropsType = 'input',\n Mode extends FormMode = 'controlled',\n> {\n /** Field mode, controlled by default */\n mode?: Mode;\n\n /** Initial field value */\n initialValue: T;\n\n /** Initial touched value */\n initialTouched?: boolean;\n\n /** Initial field error message */\n initialError?: React.ReactNode;\n\n /** Called with updated value when the field value changes */\n onValueChange?: (value: T) => void;\n\n /** Determines whether the field should be validated when value changes, false by default */\n validateOnChange?: boolean;\n\n /** Determines whether the field should be validated when it loses focus, false by default */\n validateOnBlur?: boolean;\n\n /** Determines whether the field should clear error message when value changes, true by default */\n clearErrorOnChange?: boolean;\n\n /** A function to validate field value, can be sync or async */\n validate?: (value: T) => React.ReactNode | Promise<React.ReactNode>;\n\n /** Field type, input by default */\n type?: FieldType;\n\n /** A function to resolve validation error from the result returned from validate function, should return react node */\n resolveValidationError?: UseFieldErrorResolver;\n}\n\ninterface SetValueOptions {\n updateState?: boolean;\n updateKey?: boolean;\n}\n\ninterface GetInputPropsOptions {\n withError?: boolean;\n withFocus?: boolean;\n [key: string]: any;\n}\n\ninterface GetInputPropsSharedReturn {\n error?: React.ReactNode;\n onFocus?: () => void;\n onBlur: () => void;\n onChange: (value: any) => void;\n}\n\ntype GetInputPropsTypeValue<\n T,\n FieldType extends GetInputPropsType,\n Mode extends FormMode,\n> = FieldType extends 'checkbox'\n ? Mode extends 'controlled'\n ? { checked: boolean }\n : { defaultChecked: boolean }\n : FieldType extends 'radio'\n ? Mode extends 'controlled'\n ? { checked: boolean; value: T }\n : { defaultChecked: boolean; value: T }\n : Mode extends 'controlled'\n ? { value: T }\n : { defaultValue: T };\n\ntype GetInputPropsReturnType<\n T,\n FieldType extends GetInputPropsType,\n Mode extends FormMode,\n> = GetInputPropsSharedReturn & GetInputPropsTypeValue<T, FieldType, Mode>;\n\nexport interface UseFieldReturnType<\n T,\n FieldType extends GetInputPropsType = 'input',\n Mode extends FormMode = 'controlled',\n> {\n /** Returns props to pass to the input element */\n getInputProps: (options?: GetInputPropsOptions) => GetInputPropsReturnType<T, FieldType, Mode>;\n\n /** Returns current input value */\n getValue: () => T;\n\n /** Sets input value to the given value */\n setValue: (value: T) => void;\n\n /** Resets field value to initial state, sets touched state to false, sets error to null */\n reset: () => void;\n\n /** Validates current input value when called */\n validate: () => Promise<React.ReactNode | void>;\n\n /** Set to true when async validate function is called, stays true until the returned promise resolves */\n isValidating: boolean;\n\n /** Current error message */\n error: React.ReactNode;\n\n /** Sets error message to the given react node */\n setError: (error: React.ReactNode) => void;\n\n /** Returns true if the input has been focused at least once */\n isTouched: () => boolean;\n\n /** Returns true if input value is different from the initial value */\n isDirty: () => boolean;\n\n /** Resets touched state to false */\n resetTouched: () => void;\n\n /** Key that should be added to the input when mode is uncontrolled */\n key: number;\n}\n\nexport function useField<\n T,\n Mode extends FormMode = 'controlled',\n FieldType extends GetInputPropsType = 'input',\n>({\n mode = 'controlled' as Mode,\n clearErrorOnChange = true,\n initialValue,\n initialError = null,\n initialTouched = false,\n onValueChange,\n validateOnChange = false,\n validateOnBlur = false,\n validate,\n resolveValidationError,\n type = 'input' as FieldType,\n}: UseFieldInput<T, FieldType, Mode>): UseFieldReturnType<T, FieldType, Mode> {\n const [valueState, setValueState] = useState(initialValue);\n const valueRef = useRef(valueState);\n const [key, setKey] = useState(0);\n const [error, setError] = useState<React.ReactNode>(initialError || null);\n const touchedRef = useRef(initialTouched || false);\n const [, setTouchedState] = useState(touchedRef.current);\n const [isValidating, setIsValidating] = useState(false);\n const errorResolver: UseFieldErrorResolver = useMemo(\n () => resolveValidationError || ((err) => err as React.ReactNode),\n [resolveValidationError]\n );\n\n const setTouched = useCallback((val: boolean, { updateState = mode === 'controlled' } = {}) => {\n touchedRef.current = val;\n updateState && setTouchedState(val);\n }, []);\n\n const setValue = useCallback(\n (\n value: T,\n {\n updateKey = mode === 'uncontrolled',\n updateState = mode === 'controlled',\n }: SetValueOptions = {}\n ) => {\n if (valueRef.current === value) {\n return;\n }\n\n valueRef.current = value;\n\n onValueChange?.(value);\n\n if (clearErrorOnChange && error !== null) {\n setError(null);\n }\n\n if (updateState) {\n setValueState(value);\n }\n\n if (updateKey) {\n setKey((currentKey) => currentKey + 1);\n }\n\n if (validateOnChange) {\n _validate();\n }\n },\n [error, clearErrorOnChange, onValueChange]\n );\n\n const reset = useCallback(() => {\n setValue(initialValue);\n setError(null);\n setTouched(false);\n }, [initialValue]);\n\n const getValue = useCallback(() => valueRef.current, []);\n\n const isTouched = useCallback(() => touchedRef.current, []);\n\n const isDirty = useCallback(() => valueRef.current !== initialValue, [initialValue]);\n\n const _validate = useCallback(async () => {\n const validationResult = validate?.(valueRef.current);\n\n if (validationResult instanceof Promise) {\n setIsValidating(true);\n try {\n const result = await validationResult;\n setIsValidating(false);\n setError(result);\n } catch (err) {\n setIsValidating(false);\n const resolvedError = errorResolver(err);\n setError(resolvedError);\n return resolvedError;\n }\n } else {\n setError(validationResult);\n return validationResult;\n }\n }, []);\n\n const getInputProps = ({ withError = true, withFocus = true, ...otherOptions }: any = {}) => {\n const onChange = getInputOnChange<T>((val) => setValue(val as any, { updateKey: false }));\n\n const payload: any = { onChange };\n\n if (withError) {\n payload.error = error;\n }\n\n if (type === 'checkbox') {\n payload[mode === 'controlled' ? 'checked' : 'defaultChecked'] = valueRef.current;\n } else if (type === 'radio') {\n payload[mode === 'controlled' ? 'checked' : 'defaultChecked'] =\n valueRef.current === otherOptions.value;\n payload.value = otherOptions.value;\n } else {\n payload[mode === 'controlled' ? 'value' : 'defaultValue'] = valueRef.current;\n }\n\n if (withFocus) {\n payload.onFocus = () => {\n setTouched(true);\n };\n\n payload.onBlur = () => {\n if (shouldValidateOnChange('', !!validateOnBlur)) {\n _validate();\n }\n };\n }\n\n return payload;\n };\n\n const resetTouched = useCallback(() => setTouched(false), []);\n\n return {\n key,\n getValue,\n setValue,\n reset,\n getInputProps,\n\n isValidating,\n validate: _validate,\n\n error,\n setError,\n\n isTouched,\n isDirty,\n resetTouched,\n };\n}\n"],"mappings":";;;;;AAgIA,SAAgB,SAId,EACA,OAAO,cACP,qBAAqB,MACrB,cACA,eAAe,MACf,iBAAiB,OACjB,eACA,mBAAmB,OACnB,iBAAiB,OACjB,UACA,wBACA,OAAO,WACqE;CAC5E,MAAM,CAAC,YAAY,kBAAA,GAAA,MAAA,UAA0B,YAAY;CACzD,MAAM,YAAA,GAAA,MAAA,QAAkB,UAAU;CAClC,MAAM,CAAC,KAAK,WAAA,GAAA,MAAA,UAAmB,CAAC;CAChC,MAAM,CAAC,OAAO,aAAA,GAAA,MAAA,UAAsC,gBAAgB,IAAI;CACxE,MAAM,cAAA,GAAA,MAAA,QAAoB,kBAAkB,KAAK;CACjD,MAAM,GAAG,oBAAA,GAAA,MAAA,UAA4B,WAAW,OAAO;CACvD,MAAM,CAAC,cAAc,oBAAA,GAAA,MAAA,UAA4B,KAAK;CACtD,MAAM,iBAAA,GAAA,MAAA,eACE,4BAA4B,QAAQ,MAC1C,CAAC,sBAAsB,CACzB;CAEA,MAAM,cAAA,GAAA,MAAA,cAA0B,KAAc,EAAE,cAAc,SAAS,iBAAiB,CAAC,MAAM;EAC7F,WAAW,UAAU;EACrB,eAAe,gBAAgB,GAAG;CACpC,GAAG,CAAC,CAAC;CAEL,MAAM,YAAA,GAAA,MAAA,cAEF,OACA,EACE,YAAY,SAAS,gBACrB,cAAc,SAAS,iBACJ,CAAC,MACnB;EACH,IAAI,SAAS,YAAY,OACvB;EAGF,SAAS,UAAU;EAE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,eAAe,YAAY,oBAC1C,SAAS,YAAY,aAAa;GACpC,QAAQ,QAAQ,aAAa;EAC/B,OACE,QAAQ,SAAS,eAAe,UAAU,kBAAkB,SAAS;EAGvE,IAAI,WAAW;GACb,QAAQ,gBAAgB;IACtB,WAAW,IAAI;GACjB;GAEA,QAAQ,eAAe;IACrB,IAAIC,kCAAAA,uBAAuB,IAAI,CAAC,CAAC,cAAc,GAC7C,UAAU;GAEd;EACF;EAEA,OAAO;CACT;CAIA,OAAO;EACL;EACA;EACA;EACA;EACA;EAEA;EACA,UAAU;EAEV;EACA;EAEA;EACA;EACA,eAAA,GAAA,MAAA,mBAjBqC,WAAW,KAAK,GAAG,CAAC,CAiB9C;CACb;AACF"}
|
package/cjs/use-form.cjs
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
"use client";
|
|
2
|
-
require("./_virtual/_rolldown/runtime.cjs");
|
|
3
2
|
const require_actions = require("./actions/actions.cjs");
|
|
4
3
|
const require_get_input_on_change = require("./get-input-on-change/get-input-on-change.cjs");
|
|
5
4
|
const require_use_form_errors = require("./hooks/use-form-errors/use-form-errors.cjs");
|
package/cjs/use-form.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"use-form.cjs","names":["useFormErrors","useFormValues","useFormStatus","useFormWatch","useFormList","useFormValidating","validateFieldValue","shouldValidateOnChange","getPath","validateValues","getInputOnChange","getDataPath"],"sources":["../src/use-form.ts"],"sourcesContent":["import { useCallback, useMemo, useRef, useState } from 'react';\nimport { useFormActions } from './actions';\nimport { getInputOnChange } from './get-input-on-change';\nimport { useFormErrors } from './hooks/use-form-errors/use-form-errors';\nimport { useFormList } from './hooks/use-form-list/use-form-list';\nimport { useFormStatus } from './hooks/use-form-status/use-form-status';\nimport { useFormValidating } from './hooks/use-form-validating/use-form-validating';\nimport { useFormValues } from './hooks/use-form-values/use-form-values';\nimport { useFormWatch } from './hooks/use-form-watch/use-form-watch';\nimport { getDataPath, getPath } from './paths';\nimport {\n FormErrors,\n FormRulesRecord,\n GetInputNode,\n GetInputProps,\n GetTransformedValues,\n Initialize,\n IsValidating,\n Key,\n OnReset,\n OnSubmit,\n Reset,\n SetFieldValue,\n SetValues,\n UseFormInput,\n UseFormReturnType,\n} from './types';\nimport { shouldValidateOnChange, validateFieldValue, validateValues } from './validate';\n\nconst defaultResolveValidationError = (err: unknown) =>\n err instanceof Error ? err.message : String(err);\n\nexport function useForm<\n Values extends Record<string, any>,\n TransformedValues = Values,\n R extends FormErrors | Promise<FormErrors> = FormErrors,\n>(\n input: UseFormInput<Values, TransformedValues> & { validate: (values: Values) => R }\n): UseFormReturnType<Values, TransformedValues, (values: Values) => R>;\n\nexport function useForm<\n Values extends Record<string, any>,\n TransformedValues = Values,\n Rules extends FormRulesRecord<Values> = FormRulesRecord<Values>,\n>(\n input: UseFormInput<Values, TransformedValues> & { validate: Rules }\n): UseFormReturnType<Values, TransformedValues, Rules>;\n\nexport function useForm<\n Values extends Record<string, any> = Record<string, any>,\n TransformedValues = Values,\n>(\n input?: UseFormInput<Values, TransformedValues>\n): UseFormReturnType<Values, TransformedValues, undefined>;\n\nexport function useForm<\n Values extends Record<PropertyKey, any> = Record<string, any>,\n TransformedValues = Values,\n>({\n name,\n mode = 'controlled',\n initialValues,\n initialErrors = {},\n initialDirty = {},\n initialTouched = {},\n clearInputErrorOnChange = true,\n validateInputOnChange = false,\n validateInputOnBlur = false,\n onValuesChange,\n transformValues = ((values: Values) => values) as any,\n enhanceGetInputProps,\n validate: rules,\n onSubmitPreventDefault = 'always',\n touchTrigger = 'change',\n cascadeUpdates = false,\n validateDebounce = 0,\n resolveValidationError = defaultResolveValidationError,\n}: UseFormInput<Values, TransformedValues> = {}): UseFormReturnType<Values, TransformedValues> {\n const $errors = useFormErrors<Values>(initialErrors);\n const $values = useFormValues<Values>({ initialValues, onValuesChange, mode });\n const $status = useFormStatus<Values>({ initialDirty, initialTouched, $values, mode });\n const $watch = useFormWatch<Values>({ $values, $status, cascadeUpdates });\n const $list = useFormList<Values>({ $values, $errors, $status, $watch });\n const $validating = useFormValidating();\n const [formKey, setFormKey] = useState(0);\n const [fieldKeys, setFieldKeys] = useState<Record<string, number>>({});\n const [submitting, setSubmitting] = useState(false);\n const validateGeneration = useRef(0);\n\n const reset: Reset = useCallback(() => {\n $values.resetValues();\n $errors.clearErrors();\n $status.resetDirty();\n $status.resetTouched();\n $validating.clearValidating();\n mode === 'uncontrolled' && setFormKey((key) => key + 1);\n }, []);\n\n const handleValuesChanges = useCallback(\n (previousValues: Values) => {\n clearInputErrorOnChange && $errors.clearErrors();\n mode === 'uncontrolled' && setFormKey((key) => key + 1);\n $watch.notifyWatchSubscribers(previousValues);\n },\n [clearInputErrorOnChange]\n );\n\n const initialize: Initialize<Values> = useCallback(\n (values) => {\n const previousValues = $values.refValues.current;\n $values.initialize(values, () => mode === 'uncontrolled' && setFormKey((key) => key + 1));\n handleValuesChanges(previousValues);\n },\n [handleValuesChanges]\n );\n\n const debouncedValidateField = useMemo(() => {\n const timers: Record<string, ReturnType<typeof setTimeout>> = {};\n\n const handleValidation = (path: string) => {\n const signal = $validating.getAbortSignal(path);\n const result = validateFieldValue(\n path,\n rules,\n $values.refValues.current,\n resolveValidationError,\n signal\n );\n\n const applyResult = (results: { hasError: boolean; error: React.ReactNode }) => {\n if (signal.aborted) {\n return;\n }\n if (results.hasError) {\n $errors.setFieldError(path as any, results.error);\n } else {\n $errors.clearFieldError(path);\n }\n };\n\n const cleanup = () => {\n if (!signal.aborted) {\n $validating.setFieldValidating(path, false);\n }\n };\n\n if (result instanceof Promise) {\n $validating.setFieldValidating(path, true);\n result.then(applyResult).finally(cleanup);\n } else {\n applyResult(result);\n }\n };\n\n return (path: string) => {\n clearTimeout(timers[path]);\n if (validateDebounce > 0) {\n timers[path] = setTimeout(() => handleValidation(path), validateDebounce);\n } else {\n handleValidation(path);\n }\n };\n }, [validateDebounce, rules, resolveValidationError]);\n\n const setFieldValue: SetFieldValue<Values> = useCallback(\n (path, value, options) => {\n const shouldValidate = shouldValidateOnChange(path, validateInputOnChange);\n const resolvedValue =\n value instanceof Function ? value(getPath(path, $values.refValues.current) as any) : value;\n\n $status.setCalculatedFieldDirty(path, resolvedValue);\n touchTrigger === 'change' && $status.setFieldTouched(path, true);\n !shouldValidate && clearInputErrorOnChange && $errors.clearFieldError(path);\n\n $values.setFieldValue({\n path,\n value,\n updateState: mode === 'controlled',\n subscribers: [\n ...$watch.getFieldSubscribers(path),\n shouldValidate ? () => debouncedValidateField(String(path)) : null,\n options?.forceUpdate !== false && mode !== 'controlled'\n ? () =>\n setFieldKeys((keys) => ({\n ...keys,\n [path as string]: (keys[path as string] || 0) + 1,\n }))\n : null,\n ],\n });\n },\n [onValuesChange, rules, debouncedValidateField]\n );\n\n const setValues: SetValues<Values> = useCallback(\n (values) => {\n const previousValues = $values.refValues.current;\n $values.setValues({ values, updateState: mode === 'controlled' });\n handleValuesChanges(previousValues);\n },\n [onValuesChange, handleValuesChanges]\n );\n\n const validate = useCallback(() => {\n const generation = ++validateGeneration.current;\n const signal = $validating.getAbortSignal('__form__');\n\n const handleResult = (results: { hasErrors: boolean; errors: Record<string, any> }) => {\n if (generation !== validateGeneration.current) {\n return { hasErrors: false, errors: {} };\n }\n $errors.setErrors(results.errors);\n return results;\n };\n\n const cleanup = () => {\n if (generation === validateGeneration.current) {\n $validating.setFormValidating(false);\n }\n };\n\n const result = validateValues(rules, $values.refValues.current, resolveValidationError, signal);\n\n if (result instanceof Promise) {\n $validating.setFormValidating(true);\n return result.then(handleResult).finally(cleanup);\n }\n\n return handleResult(result);\n }, [rules, resolveValidationError]);\n\n const validateField = useCallback(\n (path: string) => {\n const signal = $validating.getAbortSignal(String(path));\n\n const applyResult = (results: { hasError: boolean; error: React.ReactNode }) => {\n if (signal.aborted) {\n return { hasError: false, error: null };\n }\n if (results.hasError) {\n $errors.setFieldError(path, results.error);\n } else {\n $errors.clearFieldError(path);\n }\n return results;\n };\n\n const cleanup = () => {\n if (!signal.aborted) {\n $validating.setFieldValidating(String(path), false);\n }\n };\n\n const result = validateFieldValue(\n path,\n rules,\n $values.refValues.current,\n resolveValidationError,\n signal\n );\n\n if (result instanceof Promise) {\n $validating.setFieldValidating(String(path), true);\n return result.then(applyResult).finally(cleanup);\n }\n\n return applyResult(result);\n },\n [rules, resolveValidationError]\n );\n\n const getInputProps: GetInputProps<Values> = (\n path,\n { type = 'input', withError = true, withFocus, ...otherOptions } = {}\n ) => {\n const _withFocus = withFocus ?? type !== 'radio';\n const onChange = getInputOnChange((value) =>\n setFieldValue(path, value as any, { forceUpdate: false })\n );\n\n const payload: any = { onChange, 'data-path': getDataPath(name, path) };\n\n if (withError) {\n payload.error = $errors.errorsState[path];\n }\n\n if (type === 'checkbox') {\n payload[mode === 'controlled' ? 'checked' : 'defaultChecked'] = getPath(\n path,\n $values.refValues.current\n );\n } else if (type === 'radio') {\n payload[mode === 'controlled' ? 'checked' : 'defaultChecked'] =\n getPath(path, $values.refValues.current) === otherOptions.value;\n payload.value = otherOptions.value;\n } else {\n payload[mode === 'controlled' ? 'value' : 'defaultValue'] = getPath(\n path,\n $values.refValues.current\n );\n }\n\n if (_withFocus) {\n payload.onFocus = () => $status.setFieldTouched(path, true);\n payload.onBlur = () => {\n if (shouldValidateOnChange(path, validateInputOnBlur)) {\n debouncedValidateField(String(path));\n }\n };\n }\n\n return Object.assign(\n payload,\n enhanceGetInputProps?.({\n inputProps: payload,\n field: path,\n options: { type, withError, withFocus: _withFocus, ...otherOptions },\n form: form as any,\n })\n );\n };\n\n const onSubmit: OnSubmit<Values, TransformedValues> =\n (handleSubmit, handleValidationFailure) => (event) => {\n if (onSubmitPreventDefault === 'always') {\n event?.preventDefault();\n }\n\n setSubmitting(true);\n\n const handleValidation = (results: { hasErrors: boolean; errors: Record<string, any> }) => {\n if (results.hasErrors) {\n if (onSubmitPreventDefault === 'validation-failed') {\n event?.preventDefault();\n }\n\n handleValidationFailure?.(results.errors, $values.refValues.current, event);\n setSubmitting(false);\n } else {\n const submitResult = handleSubmit?.(\n transformValues($values.refValues.current) as any,\n event\n );\n\n if (submitResult instanceof Promise) {\n submitResult.finally(() => setSubmitting(false));\n } else {\n setSubmitting(false);\n }\n }\n };\n\n const result = validate();\n if (result instanceof Promise) {\n result.then(handleValidation).catch(() => {\n setSubmitting(false);\n });\n } else {\n handleValidation(result);\n }\n };\n\n const getTransformedValues: GetTransformedValues<Values, TransformedValues> = (input) =>\n (transformValues as any)(input || $values.refValues.current);\n\n const onReset: OnReset = useCallback((event) => {\n event.preventDefault();\n reset();\n }, []);\n\n const isValid = useCallback(\n (path?: string) => {\n const signal = new AbortController().signal;\n if (path) {\n const result = validateFieldValue(\n path,\n rules,\n $values.refValues.current,\n resolveValidationError,\n signal\n );\n if (result instanceof Promise) {\n return result.then((r) => !r.hasError);\n }\n return !result.hasError;\n }\n const result = validateValues(\n rules,\n $values.refValues.current,\n resolveValidationError,\n signal\n );\n if (result instanceof Promise) {\n return result.then((r) => !r.hasErrors);\n }\n return !result.hasErrors;\n },\n [rules, resolveValidationError]\n );\n\n const key: Key<Values> = (path) => `${formKey}-${String(path)}-${fieldKeys[String(path)] || 0}`;\n\n const getInputNode: GetInputNode<Values> = useCallback(\n (path) => document.querySelector(`[data-path=\"${getDataPath(name, path)}\"]`),\n []\n );\n\n const resetField = useCallback(\n (path: PropertyKey) => {\n $values.resetField(path, [\n mode !== 'controlled'\n ? () =>\n setFieldKeys((keys) => ({\n ...keys,\n [path as string]: (keys[path as string] || 0) + 1,\n }))\n : null,\n ]);\n },\n [$values.resetField, mode, setFieldKeys]\n );\n\n const form = {\n watch: $watch.watch,\n\n initialized: $values.initialized.current,\n values: mode === 'uncontrolled' ? $values.refValues.current : $values.stateValues,\n getValues: $values.getValues,\n getInitialValues: $values.getValuesSnapshot,\n setInitialValues: $values.setValuesSnapshot,\n resetField,\n initialize,\n setValues,\n setFieldValue,\n\n submitting,\n setSubmitting,\n\n validating: $validating.validating,\n isValidating: $validating.isValidating as IsValidating<Values>,\n\n errors: $errors.errorsState,\n setErrors: $errors.setErrors,\n setFieldError: $errors.setFieldError,\n clearFieldError: $errors.clearFieldError,\n clearErrors: $errors.clearErrors,\n\n resetDirty: $status.resetDirty,\n setTouched: $status.setTouched,\n setDirty: $status.setDirty,\n isTouched: $status.isTouched,\n resetTouched: $status.resetTouched,\n isDirty: $status.isDirty,\n getTouched: $status.getTouched,\n getDirty: $status.getDirty,\n\n reorderListItem: $list.reorderListItem,\n insertListItem: $list.insertListItem,\n removeListItem: $list.removeListItem,\n replaceListItem: $list.replaceListItem,\n\n reset,\n validate,\n validateField,\n getInputProps,\n onSubmit,\n onReset,\n isValid,\n getTransformedValues,\n key,\n\n getInputNode,\n };\n\n useFormActions(name, form as any);\n\n return form as any;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;AA6BA,MAAM,iCAAiC,QACrC,eAAe,QAAQ,IAAI,UAAU,OAAO,GAAG;AAyBjD,SAAgB,QAGd,EACA,MACA,OAAO,cACP,eACA,gBAAgB,CAAC,GACjB,eAAe,CAAC,GAChB,iBAAiB,CAAC,GAClB,0BAA0B,MAC1B,wBAAwB,OACxB,sBAAsB,OACtB,gBACA,oBAAoB,WAAmB,SACvC,sBACA,UAAU,OACV,yBAAyB,UACzB,eAAe,UACf,iBAAiB,OACjB,mBAAmB,GACnB,yBAAyB,kCACkB,CAAC,GAAiD;CAC7F,MAAM,UAAUA,wBAAAA,cAAsB,aAAa;CACnD,MAAM,UAAUC,wBAAAA,cAAsB;EAAE;EAAe;EAAgB;CAAK,CAAC;CAC7E,MAAM,UAAUC,wBAAAA,cAAsB;EAAE;EAAc;EAAgB;EAAS;CAAK,CAAC;CACrF,MAAM,SAASC,uBAAAA,aAAqB;EAAE;EAAS;EAAS;CAAe,CAAC;CACxE,MAAM,QAAQC,sBAAAA,YAAoB;EAAE;EAAS;EAAS;EAAS;CAAO,CAAC;CACvE,MAAM,cAAcC,4BAAAA,kBAAkB;CACtC,MAAM,CAAC,SAAS,eAAA,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;CAAY,CACzC;CAEA,MAAM,OAAO;EACX,OAAO,OAAO;EAEd,aAAa,QAAQ,YAAY;EACjC,QAAQ,SAAS,iBAAiB,QAAQ,UAAU,UAAU,QAAQ;EACtE,WAAW,QAAQ;EACnB,kBAAkB,QAAQ;EAC1B,kBAAkB,QAAQ;EAC1B;EACA;EACA;EACA;EAEA;EACA;EAEA,YAAY,YAAY;EACxB,cAAc,YAAY;EAE1B,QAAQ,QAAQ;EAChB,WAAW,QAAQ;EACnB,eAAe,QAAQ;EACvB,iBAAiB,QAAQ;EACzB,aAAa,QAAQ;EAErB,YAAY,QAAQ;EACpB,YAAY,QAAQ;EACpB,UAAU,QAAQ;EAClB,WAAW,QAAQ;EACnB,cAAc,QAAQ;EACtB,SAAS,QAAQ;EACjB,YAAY,QAAQ;EACpB,UAAU,QAAQ;EAElB,iBAAiB,MAAM;EACvB,gBAAgB,MAAM;EACtB,gBAAgB,MAAM;EACtB,iBAAiB,MAAM;EAEvB;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EAEA;CACF;CAEA,gBAAA,eAAe,MAAM,IAAW;CAEhC,OAAO;AACT"}
|
|
1
|
+
{"version":3,"file":"use-form.cjs","names":["useFormErrors","useFormValues","useFormStatus","useFormWatch","useFormList","useFormValidating","validateFieldValue","shouldValidateOnChange","getPath","validateValues","getInputOnChange","getDataPath"],"sources":["../src/use-form.ts"],"sourcesContent":["import { useCallback, useMemo, useRef, useState } from 'react';\nimport { useFormActions } from './actions';\nimport { getInputOnChange } from './get-input-on-change';\nimport { useFormErrors } from './hooks/use-form-errors/use-form-errors';\nimport { useFormList } from './hooks/use-form-list/use-form-list';\nimport { useFormStatus } from './hooks/use-form-status/use-form-status';\nimport { useFormValidating } from './hooks/use-form-validating/use-form-validating';\nimport { useFormValues } from './hooks/use-form-values/use-form-values';\nimport { useFormWatch } from './hooks/use-form-watch/use-form-watch';\nimport { getDataPath, getPath } from './paths';\nimport {\n FormErrors,\n FormRulesRecord,\n GetInputNode,\n GetInputProps,\n GetTransformedValues,\n Initialize,\n IsValidating,\n Key,\n OnReset,\n OnSubmit,\n Reset,\n SetFieldValue,\n SetValues,\n UseFormInput,\n UseFormReturnType,\n} from './types';\nimport { shouldValidateOnChange, validateFieldValue, validateValues } from './validate';\n\nconst defaultResolveValidationError = (err: unknown) =>\n err instanceof Error ? err.message : String(err);\n\nexport function useForm<\n Values extends Record<string, any>,\n TransformedValues = Values,\n R extends FormErrors | Promise<FormErrors> = FormErrors,\n>(\n input: UseFormInput<Values, TransformedValues> & { validate: (values: Values) => R }\n): UseFormReturnType<Values, TransformedValues, (values: Values) => R>;\n\nexport function useForm<\n Values extends Record<string, any>,\n TransformedValues = Values,\n Rules extends FormRulesRecord<Values> = FormRulesRecord<Values>,\n>(\n input: UseFormInput<Values, TransformedValues> & { validate: Rules }\n): UseFormReturnType<Values, TransformedValues, Rules>;\n\nexport function useForm<\n Values extends Record<string, any> = Record<string, any>,\n TransformedValues = Values,\n>(\n input?: UseFormInput<Values, TransformedValues>\n): UseFormReturnType<Values, TransformedValues, undefined>;\n\nexport function useForm<\n Values extends Record<PropertyKey, any> = Record<string, any>,\n TransformedValues = Values,\n>({\n name,\n mode = 'controlled',\n initialValues,\n initialErrors = {},\n initialDirty = {},\n initialTouched = {},\n clearInputErrorOnChange = true,\n validateInputOnChange = false,\n validateInputOnBlur = false,\n onValuesChange,\n transformValues = ((values: Values) => values) as any,\n enhanceGetInputProps,\n validate: rules,\n onSubmitPreventDefault = 'always',\n touchTrigger = 'change',\n cascadeUpdates = false,\n validateDebounce = 0,\n resolveValidationError = defaultResolveValidationError,\n}: UseFormInput<Values, TransformedValues> = {}): UseFormReturnType<Values, TransformedValues> {\n const $errors = useFormErrors<Values>(initialErrors);\n const $values = useFormValues<Values>({ initialValues, onValuesChange, mode });\n const $status = useFormStatus<Values>({ initialDirty, initialTouched, $values, mode });\n const $watch = useFormWatch<Values>({ $values, $status, cascadeUpdates });\n const $list = useFormList<Values>({ $values, $errors, $status, $watch });\n const $validating = useFormValidating();\n const [formKey, setFormKey] = useState(0);\n const [fieldKeys, setFieldKeys] = useState<Record<string, number>>({});\n const [submitting, setSubmitting] = useState(false);\n const validateGeneration = useRef(0);\n\n const reset: Reset = useCallback(() => {\n $values.resetValues();\n $errors.clearErrors();\n $status.resetDirty();\n $status.resetTouched();\n $validating.clearValidating();\n mode === 'uncontrolled' && setFormKey((key) => key + 1);\n }, []);\n\n const handleValuesChanges = useCallback(\n (previousValues: Values) => {\n clearInputErrorOnChange && $errors.clearErrors();\n mode === 'uncontrolled' && setFormKey((key) => key + 1);\n $watch.notifyWatchSubscribers(previousValues);\n },\n [clearInputErrorOnChange]\n );\n\n const initialize: Initialize<Values> = useCallback(\n (values) => {\n const previousValues = $values.refValues.current;\n $values.initialize(values, () => mode === 'uncontrolled' && setFormKey((key) => key + 1));\n handleValuesChanges(previousValues);\n },\n [handleValuesChanges]\n );\n\n const debouncedValidateField = useMemo(() => {\n const timers: Record<string, ReturnType<typeof setTimeout>> = {};\n\n const handleValidation = (path: string) => {\n const signal = $validating.getAbortSignal(path);\n const result = validateFieldValue(\n path,\n rules,\n $values.refValues.current,\n resolveValidationError,\n signal\n );\n\n const applyResult = (results: { hasError: boolean; error: React.ReactNode }) => {\n if (signal.aborted) {\n return;\n }\n if (results.hasError) {\n $errors.setFieldError(path as any, results.error);\n } else {\n $errors.clearFieldError(path);\n }\n };\n\n const cleanup = () => {\n if (!signal.aborted) {\n $validating.setFieldValidating(path, false);\n }\n };\n\n if (result instanceof Promise) {\n $validating.setFieldValidating(path, true);\n result.then(applyResult).finally(cleanup);\n } else {\n applyResult(result);\n }\n };\n\n return (path: string) => {\n clearTimeout(timers[path]);\n if (validateDebounce > 0) {\n timers[path] = setTimeout(() => handleValidation(path), validateDebounce);\n } else {\n handleValidation(path);\n }\n };\n }, [validateDebounce, rules, resolveValidationError]);\n\n const setFieldValue: SetFieldValue<Values> = useCallback(\n (path, value, options) => {\n const shouldValidate = shouldValidateOnChange(path, validateInputOnChange);\n const resolvedValue =\n value instanceof Function ? value(getPath(path, $values.refValues.current) as any) : value;\n\n $status.setCalculatedFieldDirty(path, resolvedValue);\n touchTrigger === 'change' && $status.setFieldTouched(path, true);\n !shouldValidate && clearInputErrorOnChange && $errors.clearFieldError(path);\n\n $values.setFieldValue({\n path,\n value,\n updateState: mode === 'controlled',\n subscribers: [\n ...$watch.getFieldSubscribers(path),\n shouldValidate ? () => debouncedValidateField(String(path)) : null,\n options?.forceUpdate !== false && mode !== 'controlled'\n ? () =>\n setFieldKeys((keys) => ({\n ...keys,\n [path as string]: (keys[path as string] || 0) + 1,\n }))\n : null,\n ],\n });\n },\n [onValuesChange, rules, debouncedValidateField]\n );\n\n const setValues: SetValues<Values> = useCallback(\n (values) => {\n const previousValues = $values.refValues.current;\n $values.setValues({ values, updateState: mode === 'controlled' });\n handleValuesChanges(previousValues);\n },\n [onValuesChange, handleValuesChanges]\n );\n\n const validate = useCallback(() => {\n const generation = ++validateGeneration.current;\n const signal = $validating.getAbortSignal('__form__');\n\n const handleResult = (results: { hasErrors: boolean; errors: Record<string, any> }) => {\n if (generation !== validateGeneration.current) {\n return { hasErrors: false, errors: {} };\n }\n $errors.setErrors(results.errors);\n return results;\n };\n\n const cleanup = () => {\n if (generation === validateGeneration.current) {\n $validating.setFormValidating(false);\n }\n };\n\n const result = validateValues(rules, $values.refValues.current, resolveValidationError, signal);\n\n if (result instanceof Promise) {\n $validating.setFormValidating(true);\n return result.then(handleResult).finally(cleanup);\n }\n\n return handleResult(result);\n }, [rules, resolveValidationError]);\n\n const validateField = useCallback(\n (path: string) => {\n const signal = $validating.getAbortSignal(String(path));\n\n const applyResult = (results: { hasError: boolean; error: React.ReactNode }) => {\n if (signal.aborted) {\n return { hasError: false, error: null };\n }\n if (results.hasError) {\n $errors.setFieldError(path, results.error);\n } else {\n $errors.clearFieldError(path);\n }\n return results;\n };\n\n const cleanup = () => {\n if (!signal.aborted) {\n $validating.setFieldValidating(String(path), false);\n }\n };\n\n const result = validateFieldValue(\n path,\n rules,\n $values.refValues.current,\n resolveValidationError,\n signal\n );\n\n if (result instanceof Promise) {\n $validating.setFieldValidating(String(path), true);\n return result.then(applyResult).finally(cleanup);\n }\n\n return applyResult(result);\n },\n [rules, resolveValidationError]\n );\n\n const getInputProps: GetInputProps<Values> = (\n path,\n { type = 'input', withError = true, withFocus, ...otherOptions } = {}\n ) => {\n const _withFocus = withFocus ?? type !== 'radio';\n const onChange = getInputOnChange((value) =>\n setFieldValue(path, value as any, { forceUpdate: false })\n );\n\n const payload: any = { onChange, 'data-path': getDataPath(name, path) };\n\n if (withError) {\n payload.error = $errors.errorsState[path];\n }\n\n if (type === 'checkbox') {\n payload[mode === 'controlled' ? 'checked' : 'defaultChecked'] = getPath(\n path,\n $values.refValues.current\n );\n } else if (type === 'radio') {\n payload[mode === 'controlled' ? 'checked' : 'defaultChecked'] =\n getPath(path, $values.refValues.current) === otherOptions.value;\n payload.value = otherOptions.value;\n } else {\n payload[mode === 'controlled' ? 'value' : 'defaultValue'] = getPath(\n path,\n $values.refValues.current\n );\n }\n\n if (_withFocus) {\n payload.onFocus = () => $status.setFieldTouched(path, true);\n payload.onBlur = () => {\n if (shouldValidateOnChange(path, validateInputOnBlur)) {\n debouncedValidateField(String(path));\n }\n };\n }\n\n return Object.assign(\n payload,\n enhanceGetInputProps?.({\n inputProps: payload,\n field: path,\n options: { type, withError, withFocus: _withFocus, ...otherOptions },\n form: form as any,\n })\n );\n };\n\n const onSubmit: OnSubmit<Values, TransformedValues> =\n (handleSubmit, handleValidationFailure) => (event) => {\n if (onSubmitPreventDefault === 'always') {\n event?.preventDefault();\n }\n\n setSubmitting(true);\n\n const handleValidation = (results: { hasErrors: boolean; errors: Record<string, any> }) => {\n if (results.hasErrors) {\n if (onSubmitPreventDefault === 'validation-failed') {\n event?.preventDefault();\n }\n\n handleValidationFailure?.(results.errors, $values.refValues.current, event);\n setSubmitting(false);\n } else {\n const submitResult = handleSubmit?.(\n transformValues($values.refValues.current) as any,\n event\n );\n\n if (submitResult instanceof Promise) {\n submitResult.finally(() => setSubmitting(false));\n } else {\n setSubmitting(false);\n }\n }\n };\n\n const result = validate();\n if (result instanceof Promise) {\n result.then(handleValidation).catch(() => {\n setSubmitting(false);\n });\n } else {\n handleValidation(result);\n }\n };\n\n const getTransformedValues: GetTransformedValues<Values, TransformedValues> = (input) =>\n (transformValues as any)(input || $values.refValues.current);\n\n const onReset: OnReset = useCallback((event) => {\n event.preventDefault();\n reset();\n }, []);\n\n const isValid = useCallback(\n (path?: string) => {\n const signal = new AbortController().signal;\n if (path) {\n const result = validateFieldValue(\n path,\n rules,\n $values.refValues.current,\n resolveValidationError,\n signal\n );\n if (result instanceof Promise) {\n return result.then((r) => !r.hasError);\n }\n return !result.hasError;\n }\n const result = validateValues(\n rules,\n $values.refValues.current,\n resolveValidationError,\n signal\n );\n if (result instanceof Promise) {\n return result.then((r) => !r.hasErrors);\n }\n return !result.hasErrors;\n },\n [rules, resolveValidationError]\n );\n\n const key: Key<Values> = (path) => `${formKey}-${String(path)}-${fieldKeys[String(path)] || 0}`;\n\n const getInputNode: GetInputNode<Values> = useCallback(\n (path) => document.querySelector(`[data-path=\"${getDataPath(name, path)}\"]`),\n []\n );\n\n const resetField = useCallback(\n (path: PropertyKey) => {\n $values.resetField(path, [\n mode !== 'controlled'\n ? () =>\n setFieldKeys((keys) => ({\n ...keys,\n [path as string]: (keys[path as string] || 0) + 1,\n }))\n : null,\n ]);\n },\n [$values.resetField, mode, setFieldKeys]\n );\n\n const form = {\n watch: $watch.watch,\n\n initialized: $values.initialized.current,\n values: mode === 'uncontrolled' ? $values.refValues.current : $values.stateValues,\n getValues: $values.getValues,\n getInitialValues: $values.getValuesSnapshot,\n setInitialValues: $values.setValuesSnapshot,\n resetField,\n initialize,\n setValues,\n setFieldValue,\n\n submitting,\n setSubmitting,\n\n validating: $validating.validating,\n isValidating: $validating.isValidating as IsValidating<Values>,\n\n errors: $errors.errorsState,\n setErrors: $errors.setErrors,\n setFieldError: $errors.setFieldError,\n clearFieldError: $errors.clearFieldError,\n clearErrors: $errors.clearErrors,\n\n resetDirty: $status.resetDirty,\n setTouched: $status.setTouched,\n setDirty: $status.setDirty,\n isTouched: $status.isTouched,\n resetTouched: $status.resetTouched,\n isDirty: $status.isDirty,\n getTouched: $status.getTouched,\n getDirty: $status.getDirty,\n\n reorderListItem: $list.reorderListItem,\n insertListItem: $list.insertListItem,\n removeListItem: $list.removeListItem,\n replaceListItem: $list.replaceListItem,\n\n reset,\n validate,\n validateField,\n getInputProps,\n onSubmit,\n onReset,\n isValid,\n getTransformedValues,\n key,\n\n getInputNode,\n };\n\n useFormActions(name, form as any);\n\n return form as any;\n}\n"],"mappings":";;;;;;;;;;;;;;;;AA6BA,MAAM,iCAAiC,QACrC,eAAe,QAAQ,IAAI,UAAU,OAAO,GAAG;AAyBjD,SAAgB,QAGd,EACA,MACA,OAAO,cACP,eACA,gBAAgB,CAAC,GACjB,eAAe,CAAC,GAChB,iBAAiB,CAAC,GAClB,0BAA0B,MAC1B,wBAAwB,OACxB,sBAAsB,OACtB,gBACA,oBAAoB,WAAmB,SACvC,sBACA,UAAU,OACV,yBAAyB,UACzB,eAAe,UACf,iBAAiB,OACjB,mBAAmB,GACnB,yBAAyB,kCACkB,CAAC,GAAiD;CAC7F,MAAM,UAAUA,wBAAAA,cAAsB,aAAa;CACnD,MAAM,UAAUC,wBAAAA,cAAsB;EAAE;EAAe;EAAgB;CAAK,CAAC;CAC7E,MAAM,UAAUC,wBAAAA,cAAsB;EAAE;EAAc;EAAgB;EAAS;CAAK,CAAC;CACrF,MAAM,SAASC,uBAAAA,aAAqB;EAAE;EAAS;EAAS;CAAe,CAAC;CACxE,MAAM,QAAQC,sBAAAA,YAAoB;EAAE;EAAS;EAAS;EAAS;CAAO,CAAC;CACvE,MAAM,cAAcC,4BAAAA,kBAAkB;CACtC,MAAM,CAAC,SAAS,eAAA,GAAA,MAAA,UAAuB,CAAC;CACxC,MAAM,CAAC,WA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