@modular-forms/preact
Advanced tools
Comparing version 0.7.0 to 0.8.0
@@ -0,1 +1,2 @@ | ||
import { safeParseAsync, } from 'valibot'; | ||
/** | ||
@@ -10,5 +11,7 @@ * Creates a validation functions that parses the Valibot schema of a field. | ||
return async (value) => { | ||
const result = await schema._parse(value, { abortPipeEarly: true }); | ||
const result = await safeParseAsync(schema, value, { | ||
abortPipeEarly: true, | ||
}); | ||
return result.issues?.[0]?.message || ''; | ||
}; | ||
} |
@@ -0,1 +1,2 @@ | ||
import { getDotPath, safeParseAsync, } from 'valibot'; | ||
/** | ||
@@ -10,10 +11,13 @@ * Creates a validation functions that parses the Valibot schema of a form. | ||
return async (values) => { | ||
const result = await schema._parse(values, { abortPipeEarly: true }); | ||
return result.issues | ||
? result.issues.reduce((errors, issue) => ({ | ||
...errors, | ||
[issue.path.map(({ key }) => key).join('.')]: issue.message, | ||
}), {}) | ||
: {}; | ||
const result = await safeParseAsync(schema, values, { | ||
abortPipeEarly: true, | ||
}); | ||
const formErrors = {}; | ||
if (result.issues) { | ||
for (const issue of result.issues) { | ||
formErrors[getDotPath(issue)] = issue.message; | ||
} | ||
} | ||
return formErrors; | ||
}; | ||
} |
@@ -11,12 +11,13 @@ /** | ||
const result = schema.safeParse(values); | ||
return result.success | ||
? {} | ||
: result.error.issues.reduce((errors, error) => { | ||
const path = error.path.join('.'); | ||
if (!errors[path]) { | ||
errors[path] = error.message; | ||
const formErrors = {}; | ||
if (!result.success) { | ||
for (const issue of result.error.issues) { | ||
const path = issue.path.join('.'); | ||
if (!formErrors[path]) { | ||
formErrors[path] = issue.message; | ||
} | ||
return errors; | ||
}, {}); | ||
} | ||
} | ||
return formErrors; | ||
}; | ||
} |
import { useMemo } from 'preact/hooks'; | ||
import { Form, Field, FieldArray } from '../components'; | ||
import { useFormStore } from './useFormStore'; | ||
/** | ||
* Creates and returns the store of the form as well as a linked Form, Field | ||
* and FieldArray component. | ||
* | ||
* @param options The form options. | ||
* | ||
* @returns The store and linked components. | ||
*/ | ||
export function useForm(options) { | ||
@@ -13,0 +5,0 @@ // Create form store |
@@ -5,7 +5,2 @@ import { batch } from '@preact/signals'; | ||
import { getUniqueId, updateFormState } from '../utils'; | ||
/** | ||
* Handles the lifecycle dependent state of a field or field array. | ||
* | ||
* @param props The lifecycle properties. | ||
*/ | ||
export function useLifecycle({ of: form, name, store, validate, transform, keepActive = false, keepState = true, }) { | ||
@@ -12,0 +7,0 @@ useEffect(() => { |
import { initializeFieldStore } from '../utils'; | ||
/** | ||
* Returns the value of the specified field. | ||
* | ||
* @param form The form of the field. | ||
* @param name The name of the field. | ||
* @param options The value options. | ||
* | ||
* @returns The value of the field. | ||
*/ | ||
export function getValue(form, name, { shouldActive = true, shouldTouched = false, shouldDirty = false, shouldValid = false, } = {}) { | ||
@@ -12,0 +3,0 @@ // Get store of specified field |
import { batch } from '@preact/signals'; | ||
import { initializeFieldStore, updateFieldDirty, validateIfRequired, } from '../utils'; | ||
/** | ||
* Sets the value of the specified field. | ||
* | ||
* @param form The form of the field. | ||
* @param name The name of the field. | ||
* @param value The value to bet set. | ||
* @param options The value options. | ||
*/ | ||
export function setValue(form, name, value, { shouldTouched = true, shouldDirty = true, shouldValidate = true, shouldFocus = true, } = {}) { | ||
@@ -12,0 +4,0 @@ batch(() => { |
import { batch } from '@preact/signals'; | ||
import { updateFieldDirty } from './updateFieldDirty'; | ||
import { validateIfRequired } from './validateIfRequired'; | ||
/** | ||
* Handles the input, change and blur event of a field. | ||
* | ||
* @param form The form of the field. | ||
* @param field The store of the field. | ||
* @param name The name of the field. | ||
* @param event The event of the field. | ||
* @param validationModes The modes of the validation. | ||
* @param inputValue The value of the input. | ||
*/ | ||
export function handleFieldEvent(form, field, name, event, validationModes, inputValue) { | ||
@@ -15,0 +5,0 @@ batch(() => { |
import { signal } from '@preact/signals'; | ||
import { getFieldStore } from './getFieldStore'; | ||
import { getPathValue } from './getPathValue'; | ||
/** | ||
* Initializes and returns the store of a field. | ||
* | ||
* @param form The form of the field. | ||
* @param name The name of the field. | ||
* | ||
* @returns The reactive store. | ||
*/ | ||
export function initializeFieldStore(form, name) { | ||
@@ -18,2 +10,3 @@ // Initialize store on first request | ||
// Add store of field to form | ||
// @ts-expect-error | ||
form.internal.fields[name] = { | ||
@@ -20,0 +13,0 @@ // Signals |
@@ -1,2 +0,2 @@ | ||
import type { BaseSchema, BaseSchemaAsync } from 'valibot'; | ||
import { type GenericSchema, type GenericSchemaAsync } from 'valibot'; | ||
import type { FieldValue, ValidateField } from '../types'; | ||
@@ -10,2 +10,2 @@ /** | ||
*/ | ||
export declare function valiField<TFieldValue extends FieldValue>(schema: BaseSchema<TFieldValue, any> | BaseSchemaAsync<TFieldValue, any>): ValidateField<TFieldValue>; | ||
export declare function valiField<TFieldValue extends FieldValue>(schema: GenericSchema | GenericSchemaAsync): ValidateField<TFieldValue>; |
@@ -1,2 +0,2 @@ | ||
import type { BaseSchema, BaseSchemaAsync } from 'valibot'; | ||
import { type GenericSchema, type GenericSchemaAsync } from 'valibot'; | ||
import type { FieldValues, ValidateForm } from '../types'; | ||
@@ -10,2 +10,2 @@ /** | ||
*/ | ||
export declare function valiForm<TFieldValues extends FieldValues>(schema: BaseSchema<TFieldValues, any> | BaseSchemaAsync<TFieldValues, any>): ValidateForm<TFieldValues>; | ||
export declare function valiForm<TFieldValues extends FieldValues>(schema: GenericSchema | GenericSchemaAsync): ValidateForm<TFieldValues>; |
@@ -19,3 +19,3 @@ import type { FieldArrayPath, FieldPath, FieldPathValue, FieldValues, FormStore, InternalFieldArrayStore, InternalFieldStore, Maybe, MaybeArray, ResponseData, TransformField, ValidateField, ValidateFieldArray } from '../types'; | ||
*/ | ||
export declare function useLifecycle<TFieldValues extends FieldValues, TResponseData extends ResponseData, TFieldName extends FieldPath<TFieldValues>>({ of: form, name, store, validate, transform, keepActive, keepState, }: LifecycleProps<TFieldValues, TResponseData, TFieldName>): void; | ||
export declare function useLifecycle<TFieldValues extends FieldValues, TResponseData extends ResponseData, TFieldName extends FieldPath<TFieldValues>>(props: LifecycleProps<TFieldValues, TResponseData, TFieldName>): void; | ||
export {}; |
@@ -20,2 +20,2 @@ import type { FieldPath, FieldPathValue, FieldValues, FormStore, Maybe, ResponseData } from '../types'; | ||
*/ | ||
export declare function getValue<TFieldValues extends FieldValues, TResponseData extends ResponseData, TFieldName extends FieldPath<TFieldValues>>(form: FormStore<TFieldValues, TResponseData>, name: TFieldName, { shouldActive, shouldTouched, shouldDirty, shouldValid, }?: Maybe<GetValueOptions>): Maybe<FieldPathValue<TFieldValues, TFieldName>>; | ||
export declare function getValue<TFieldValues extends FieldValues, TResponseData extends ResponseData, TFieldName extends FieldPath<TFieldValues>>(form: FormStore<TFieldValues, TResponseData>, name: TFieldName, options?: Maybe<GetValueOptions>): Maybe<FieldPathValue<TFieldValues, TFieldName>>; |
@@ -20,3 +20,3 @@ import type { FieldValues, FieldPath, FieldPathValue, ResponseData, FormStore, Maybe, FieldArrayPath, PartialValues } from '../types'; | ||
/** | ||
* Resets the entire form, several fields and field arrays or a singel field or | ||
* Resets the entire form, several fields and field arrays or a single field or | ||
* field array. | ||
@@ -29,3 +29,3 @@ * | ||
/** | ||
* Resets the entire form, several fields and field arrays or a singel field or | ||
* Resets the entire form, several fields and field arrays or a single field or | ||
* field array. | ||
@@ -39,3 +39,3 @@ * | ||
/** | ||
* Resets the entire form, several fields and field arrays or a singel field or | ||
* Resets the entire form, several fields and field arrays or a single field or | ||
* field array. | ||
@@ -42,0 +42,0 @@ * |
@@ -19,2 +19,2 @@ import type { FieldPath, FieldPathValue, FieldValues, FormStore, Maybe, ResponseData } from '../types'; | ||
*/ | ||
export declare function setValue<TFieldValues extends FieldValues, TResponseData extends ResponseData, TFieldName extends FieldPath<TFieldValues>>(form: FormStore<TFieldValues, TResponseData>, name: TFieldName, value: FieldPathValue<TFieldValues, TFieldName>, { shouldTouched, shouldDirty, shouldValidate, shouldFocus, }?: Maybe<SetValueOptions>): void; | ||
export declare function setValue<TFieldValues extends FieldValues, TResponseData extends ResponseData, TFieldName extends FieldPath<TFieldValues>>(form: FormStore<TFieldValues, TResponseData>, name: TFieldName, value: FieldPathValue<TFieldValues, TFieldName>, options?: Maybe<SetValueOptions>): void; |
{ | ||
"name": "@modular-forms/preact", | ||
"description": "The modular and type-safe form library for Preact", | ||
"version": "0.7.0", | ||
"version": "0.8.0", | ||
"license": "MIT", | ||
@@ -43,15 +43,24 @@ "author": "Fabian Hiller", | ||
}, | ||
"scripts": { | ||
"build": "vite build --mode lib && tsc", | ||
"format": "prettier --write \"./src\"", | ||
"format.check": "prettier --check \"./src\"", | ||
"lint": "eslint \"./src/**/*.ts*\" && tsc --noEmit", | ||
"publish": "npm publish" | ||
}, | ||
"dependencies": { | ||
"valibot": ">=0.31.0 <1" | ||
}, | ||
"devDependencies": { | ||
"@preact/preset-vite": "^2.5.0", | ||
"@preact/signals": "^1.1.5", | ||
"@types/eslint": "^8.44.0", | ||
"@typescript-eslint/eslint-plugin": "^5.62.0", | ||
"@typescript-eslint/parser": "^5.62.0", | ||
"eslint": "^8.44.0", | ||
"eslint-config-preact": "^1.3.0", | ||
"preact": "^10.16.0", | ||
"typescript": "4.9.5", | ||
"valibot": "^0.13.1", | ||
"vite": "^4.4.3", | ||
"zod": "^3.21.4" | ||
"@preact/preset-vite": "^2.8.2", | ||
"@preact/signals": "^1.2.3", | ||
"@types/eslint": "^8.56.10", | ||
"@typescript-eslint/eslint-plugin": "^7.12.0", | ||
"@typescript-eslint/parser": "^7.12.0", | ||
"eslint": "8.57.0", | ||
"eslint-config-preact": "^1.4.0", | ||
"preact": "^10.22.0", | ||
"typescript": "5.4.5", | ||
"vite": "^5.2.12", | ||
"zod": "^3.23.8" | ||
}, | ||
@@ -61,9 +70,3 @@ "peerDependencies": { | ||
"preact": "^10.0.0" | ||
}, | ||
"scripts": { | ||
"build": "vite build --mode lib && tsc", | ||
"format": "prettier --write .", | ||
"format.check": "prettier --check .", | ||
"lint": "eslint \"src/**/*.ts*\" && tsc --noEmit" | ||
} | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
11
245682
3
198
6378
+ Addedvalibot@>=0.31.0 <1
+ Addedvalibot@0.42.1(transitive)