@felte/core
Advanced tools
Comparing version
import { __rest } from './external/.pnpm/tslib@2.3.1/external/tslib/tslib.es6.js'; | ||
import { get } from './get.js'; | ||
import { FelteSubmitEvent, FelteSuccessEvent, FelteErrorEvent } from './events.js'; | ||
import { FelteSubmitError } from './error.js'; | ||
@@ -63,12 +64,15 @@ import { deepSetTouched } from './deep-set-touched.js'; | ||
function createSubmitHandler(altConfig) { | ||
var _a, _b; | ||
const onError = (_a = altConfig === null || altConfig === void 0 ? void 0 : altConfig.onError) !== null && _a !== void 0 ? _a : config.onError; | ||
const onSuccess = (_b = altConfig === null || altConfig === void 0 ? void 0 : altConfig.onSuccess) !== null && _b !== void 0 ? _b : config.onSuccess; | ||
return async function handleSubmit(event) { | ||
var _a, _b; | ||
var _a, _b, _c, _d, _e, _f, _g; | ||
const formNode = _getFormNode(); | ||
const onSubmit = (_b = (_a = altConfig === null || altConfig === void 0 ? void 0 : altConfig.onSubmit) !== null && _a !== void 0 ? _a : config.onSubmit) !== null && _b !== void 0 ? _b : createDefaultSubmitHandler(formNode); | ||
const submitEvent = new FelteSubmitEvent(); | ||
formNode === null || formNode === void 0 ? void 0 : formNode.dispatchEvent(submitEvent); | ||
const onError = (_b = (_a = submitEvent.onError) !== null && _a !== void 0 ? _a : altConfig === null || altConfig === void 0 ? void 0 : altConfig.onError) !== null && _b !== void 0 ? _b : config.onError; | ||
const onSuccess = (_d = (_c = submitEvent.onSuccess) !== null && _c !== void 0 ? _c : altConfig === null || altConfig === void 0 ? void 0 : altConfig.onSuccess) !== null && _d !== void 0 ? _d : config.onSuccess; | ||
const onSubmit = (_g = (_f = (_e = submitEvent.onSubmit) !== null && _e !== void 0 ? _e : altConfig === null || altConfig === void 0 ? void 0 : altConfig.onSubmit) !== null && _f !== void 0 ? _f : config.onSubmit) !== null && _g !== void 0 ? _g : createDefaultSubmitHandler(formNode); | ||
if (!onSubmit) | ||
return; | ||
event === null || event === void 0 ? void 0 : event.preventDefault(); | ||
if (submitEvent.defaultPrevented) | ||
return; | ||
isSubmitting.set(true); | ||
@@ -83,2 +87,3 @@ interacted.set(null); | ||
if (currentErrors) { | ||
touched.set(deepSetTouched(currentErrors, true)); | ||
const hasErrors = deepSome(currentErrors, (error) => Array.isArray(error) ? error.length >= 1 : !!error); | ||
@@ -101,15 +106,16 @@ if (hasErrors) { | ||
const response = await onSubmit(currentData, context); | ||
formNode === null || formNode === void 0 ? void 0 : formNode.dispatchEvent(new CustomEvent('feltesuccess', { | ||
detail: Object.assign({ response }, context), | ||
})); | ||
formNode === null || formNode === void 0 ? void 0 : formNode.dispatchEvent(new FelteSuccessEvent(Object.assign({ response }, context))); | ||
await (onSuccess === null || onSuccess === void 0 ? void 0 : onSuccess(response, context)); | ||
} | ||
catch (e) { | ||
formNode === null || formNode === void 0 ? void 0 : formNode.dispatchEvent(new CustomEvent('felteerror', { | ||
detail: Object.assign({ error: e }, context), | ||
})); | ||
if (!onError) | ||
const errorEvent = new FelteErrorEvent(Object.assign({ error: e }, context)); | ||
formNode === null || formNode === void 0 ? void 0 : formNode.dispatchEvent(errorEvent); | ||
if (!onError && !errorEvent.defaultPrevented) { | ||
throw e; | ||
} | ||
if (!onError && !errorEvent.errors) | ||
return; | ||
const serverErrors = await onError(e, context); | ||
const serverErrors = errorEvent.errors || (await (onError === null || onError === void 0 ? void 0 : onError(e, context))); | ||
if (serverErrors) { | ||
touched.set(deepSetTouched(serverErrors, true)); | ||
errors.set(serverErrors); | ||
@@ -121,3 +127,3 @@ await new Promise((r) => setTimeout(r)); | ||
data: currentData, | ||
errors: serverErrors, | ||
errors: get(errors), | ||
}); | ||
@@ -124,0 +130,0 @@ }); |
@@ -5,2 +5,3 @@ export { get as getValueFromStore } from './get.js'; | ||
export { createForm } from './create-form.js'; | ||
export { FelteErrorEvent, FelteSubmitEvent, FelteSuccessEvent } from './events.js'; | ||
export { _some } from './packages/common/dist/esm/utils/some.js'; | ||
@@ -7,0 +8,0 @@ export { _mapValues } from './packages/common/dist/esm/utils/mapValues.js'; |
import { Readable } from "svelte/store"; | ||
import { FieldValue, Form, FormConfig, FormConfigWithTransformFn, FormConfigWithoutTransformFn, StoreFactory, Obj, UnknownStores, Stores, KnownStores, Helpers, UnknownHelpers, KnownHelpers, SubmitContext } from "@felte/common"; | ||
import { FieldValue, Form, FormConfig, FormConfigWithTransformFn, FormConfigWithoutTransformFn, StoreFactory, Obj, UnknownStores, Stores, KnownStores, Helpers, UnknownHelpers, KnownHelpers, SubmitContext, CreateSubmitHandlerConfig, AssignableErrors } from "@felte/common"; | ||
type Store<Data> = Readable<Data> | { | ||
@@ -48,7 +48,23 @@ subscribe(subscriber: (value: Data) => void): { | ||
}; | ||
type FelteSuccessEvent<Data extends Obj = Obj> = CustomEvent<FelteSuccessDetail<Data>>; | ||
type FelteErrorEvent<Data extends Obj = Obj> = CustomEvent<FelteErrorDetail<Data>>; | ||
export { get as getValueFromStore, FelteSubmitError, createField, Adapters, CoreForm, createForm, FelteSuccessDetail, FelteErrorDetail, FelteSuccessEvent, FelteErrorEvent }; | ||
declare class FelteSuccessEvent<Data extends Obj = any> extends CustomEvent<FelteSuccessDetail<Data>> { | ||
constructor(detail: FelteSuccessDetail<Data>); | ||
} | ||
declare class FelteErrorEvent<Data extends Obj = any> extends CustomEvent<FelteErrorDetail<Data>> { | ||
constructor(detail: FelteErrorDetail<Data>); | ||
errors?: AssignableErrors<Data>; | ||
setErrors(errors: AssignableErrors<Data>): void; | ||
} | ||
declare class FelteSubmitEvent<Data extends Obj = any> extends Event { | ||
constructor(); | ||
target: HTMLFormElement; | ||
onSubmit?: CreateSubmitHandlerConfig<Data>["onSubmit"]; | ||
onError?: CreateSubmitHandlerConfig<Data>["onError"]; | ||
onSuccess?: CreateSubmitHandlerConfig<Data>["onSuccess"]; | ||
handleSubmit(onSubmit: CreateSubmitHandlerConfig<Data>["onSubmit"]): void; | ||
handleError(onError: CreateSubmitHandlerConfig<Data>["onError"]): void; | ||
handleSuccess(onSuccess: CreateSubmitHandlerConfig<Data>["onSuccess"]): void; | ||
} | ||
export { get as getValueFromStore, FelteSubmitError, createField, Adapters, CoreForm, createForm, FelteSuccessDetail, FelteErrorDetail, FelteSuccessEvent, FelteErrorEvent, FelteSubmitEvent }; | ||
export type { Field, FieldConfig }; | ||
export * from '@felte/common'; | ||
//# sourceMappingURL=index.cjs.d.ts.map |
@@ -7,3 +7,3 @@ export { get as getValueFromStore } from "./get"; | ||
export * from '@felte/common'; | ||
export * from "./events-793c50bc"; | ||
export * from "./events"; | ||
//# sourceMappingURL=index.d.ts.map |
import { StoreFactory, Obj, Keyed, FormConfig, Errors, Touched, ValidationFunction, PartialWritableErrors } from '@felte/common'; | ||
import { Writable, Readable } from 'svelte/store'; | ||
declare function errorFilterer(touchValue?: unknown, errValue?: unknown): any[] | import("@felte/common/dist/types/types-f8d7390f").DeepSetResult<Record<string, unknown>, null> | null | undefined; | ||
declare function warningFilterer(touchValue?: unknown, errValue?: unknown): any[] | import("@felte/common/dist/types/types-f8d7390f").DeepSetResult<Record<string, unknown>, null> | null | undefined; | ||
declare function errorFilterer(touchValue?: unknown, errValue?: unknown): any[] | import("@felte/common/dist/types/types-bdbfc971").DeepSetResult<Record<string, unknown>, null> | null | undefined; | ||
declare function warningFilterer(touchValue?: unknown, errValue?: unknown): any[] | import("@felte/common/dist/types/types-bdbfc971").DeepSetResult<Record<string, unknown>, null> | null | undefined; | ||
type Readables = Readable<any> | [Readable<any>, ...Array<Readable<any>>] | Array<Readable<any>>; | ||
@@ -6,0 +6,0 @@ type ReadableValues<T> = T extends Readable<infer U> ? [U] : { |
{ | ||
"name": "@felte/core", | ||
"version": "1.0.1", | ||
"version": "1.1.0", | ||
"description": "Core utility for Felte's integration with front-end frameworks", | ||
@@ -18,3 +18,2 @@ "main": "dist/cjs/index.cjs", | ||
"repository": "github:pablo-abc/felte", | ||
"funding": "https://www.buymeacoffee.com/pablo.abc", | ||
"keywords": [ | ||
@@ -28,3 +27,3 @@ "forms", | ||
"dependencies": { | ||
"@felte/common": "1.0.1" | ||
"@felte/common": "1.0.2" | ||
}, | ||
@@ -50,3 +49,3 @@ "publishConfig": { | ||
}, | ||
"readme": "# @felte/core\n\n[](https://bundlephobia.com/result?p=@felte/core)\n[](https://www.npmjs.com/package/@felte/core)\n\nThe core package containing the main functionality of Felte. This allows to make Felte compatible with multiple frameworks or vanilla javascript. More documenttion on this pending.\n\nSince this package is _bundled_ with other packages, breaking changes might occur in between minor versions, specially if they're required to _prevent_ breaking changes on the other packages.\n\nIf you're looking to use Felte for any of your apps, you're most likely looking for:\n\n- [felte](../felte/README.md) if you're working with Svelte.\n\n- [@felte/solid](../solid/README.md) if you're working with Solid.\n\n- [@felte/react](../react/README.md) if your're working with React.\n" | ||
"readme": "# @felte/core\n\n[](https://github.com/pablo-abc/felte/actions/workflows/test.yml)\n[](https://bundlephobia.com/result?p=@felte/core)\n[](https://www.npmjs.com/package/@felte/core)\n[](https://codecov.io/gh/pablo-abc/felte)\n\nThe core package containing the main functionality of Felte. This allows to make Felte compatible with multiple frameworks or vanilla javascript. More documenttion on this pending.\n\nSince this package is _bundled_ with other packages, breaking changes might occur in between minor versions, specially if they're required to _prevent_ breaking changes on the other packages.\n\nIf you're looking to use Felte for any of your apps, you're most likely looking for:\n\n- [felte](/packages/felte) if you're working with Svelte.\n\n- [@felte/solid](/packages/solid) if you're working with Solid.\n\n- [@felte/react](/packages/react) if your're working with React.\n\n- [@felte/element](/packages/element) if you're working with vanilla JS.\n" | ||
} |
# @felte/core | ||
[](https://github.com/pablo-abc/felte/actions/workflows/test.yml) | ||
[](https://bundlephobia.com/result?p=@felte/core) | ||
[](https://www.npmjs.com/package/@felte/core) | ||
[](https://codecov.io/gh/pablo-abc/felte) | ||
@@ -12,6 +14,8 @@ The core package containing the main functionality of Felte. This allows to make Felte compatible with multiple frameworks or vanilla javascript. More documenttion on this pending. | ||
- [felte](../felte/README.md) if you're working with Svelte. | ||
- [felte](/packages/felte) if you're working with Svelte. | ||
- [@felte/solid](../solid/README.md) if you're working with Solid. | ||
- [@felte/solid](/packages/solid) if you're working with Solid. | ||
- [@felte/react](../react/README.md) if your're working with React. | ||
- [@felte/react](/packages/react) if your're working with React. | ||
- [@felte/element](/packages/element) if you're working with vanilla JS. |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
442224
2.51%91
2.25%3937
2.74%21
23.53%+ Added
- Removed
Updated