@felte/core
Advanced tools
Comparing version 1.0.0-next.5 to 1.0.0-next.6
@@ -1,2 +0,9 @@ | ||
import { Form, FormConfig, FormConfigWithTransformFn, FormConfigWithoutTransformFn, StoreFactory, Obj, UnknownStores, Stores, KnownStores, Helpers, UnknownHelpers, KnownHelpers } from "@felte/common"; | ||
import { Obj, Stores, FormConfig, Form, FormConfigWithTransformFn, FormConfigWithoutTransformFn, StoreFactory, UnknownStores, KnownStores, Helpers, UnknownHelpers, KnownHelpers } from "@felte/common"; | ||
type FailResponse = Omit<Response, "ok"> & { | ||
ok: false; | ||
}; | ||
declare class FelteSubmitError extends Error { | ||
constructor(message: string, response: FailResponse); | ||
response: FailResponse; | ||
} | ||
type Adapters<StoreExt = Record<string, any>> = { | ||
@@ -11,4 +18,4 @@ storeFactory: StoreFactory<StoreExt>; | ||
declare function createForm<Data extends Obj = Obj, Ext extends Obj = Obj, StoreExt = Record<string, any>>(config: FormConfig<Data> & Ext, adapters: Adapters<StoreExt>): CoreForm<Data> & Helpers<Data> & Stores<Data, StoreExt>; | ||
export { Adapters, CoreForm, createForm }; | ||
export { FelteSubmitError, Adapters, CoreForm, createForm }; | ||
export * from '@felte/common'; | ||
//# sourceMappingURL=index.d.ts.map |
@@ -514,156 +514,47 @@ 'use strict'; | ||
function addAtIndex(storeValue, path, value, index) { | ||
return _update(storeValue, path, (oldValue) => { | ||
if (!Array.isArray(oldValue)) | ||
return oldValue; | ||
if (typeof index === 'undefined') { | ||
oldValue.push(value); | ||
class FelteSubmitError extends Error { | ||
constructor(message, response) { | ||
super(message); | ||
this.name = 'FelteSubmitError'; | ||
this.response = response; | ||
} | ||
} | ||
function createDefaultSubmitHandler(form) { | ||
if (!form) | ||
return; | ||
return async function onSubmit() { | ||
let body = new FormData(form); | ||
const action = new URL(form.action); | ||
const method = form.method.toLowerCase() === 'get' | ||
? 'get' | ||
: action.searchParams.get('_method') || form.method; | ||
let enctype = form.enctype; | ||
if (form.querySelector('input[type="file"]')) { | ||
enctype = 'multipart/form-data'; | ||
} | ||
else { | ||
oldValue.splice(index, 0, value); | ||
if (method === 'get' || enctype === 'application/x-www-form-urlencoded') { | ||
body = new URLSearchParams(body); | ||
} | ||
return oldValue; | ||
}); | ||
} | ||
function isUpdater(value) { | ||
return typeof value === 'function'; | ||
} | ||
function createSetHelper(storeSetter) { | ||
const setHelper = (pathOrValue, valueOrUpdater) => { | ||
if (typeof pathOrValue === 'string') { | ||
const path = pathOrValue; | ||
storeSetter((oldValue) => { | ||
const newValue = isUpdater(valueOrUpdater) | ||
? valueOrUpdater(_get(oldValue, path)) | ||
: valueOrUpdater; | ||
return _set(oldValue, path, newValue); | ||
let fetchOptions; | ||
if (method === 'get') { | ||
body.forEach((value, key) => { | ||
action.searchParams.append(key, value); | ||
}); | ||
fetchOptions = { method }; | ||
} | ||
else { | ||
storeSetter((oldValue) => isUpdater(pathOrValue) ? pathOrValue(oldValue) : pathOrValue); | ||
fetchOptions = { | ||
method, | ||
body, | ||
headers: { | ||
'Content-Type': enctype, | ||
}, | ||
}; | ||
} | ||
const response = await fetch(action.toString(), fetchOptions); | ||
if (response.ok) | ||
return response; | ||
throw new FelteSubmitError('An error occurred while submitting the form', response); | ||
}; | ||
return setHelper; | ||
} | ||
function createHelpers({ stores, config, }) { | ||
var _a; | ||
const { data, touched, errors, warnings, isDirty, isSubmitting } = stores; | ||
const setData = createSetHelper(data.update); | ||
const setTouched = createSetHelper(touched.update); | ||
const setErrors = createSetHelper(errors.update); | ||
const setWarnings = createSetHelper(warnings.update); | ||
function updateFields(updater) { | ||
setData((oldData) => { | ||
const newData = updater(oldData); | ||
if (formNode) | ||
setForm(formNode, newData); | ||
return newData; | ||
}); | ||
} | ||
const setFields = (pathOrValue, valueOrUpdater, shouldTouch) => { | ||
const fieldsSetter = createSetHelper(updateFields); | ||
fieldsSetter(pathOrValue, valueOrUpdater); | ||
if (typeof pathOrValue === 'string' && shouldTouch) { | ||
setTouched(pathOrValue, true); | ||
} | ||
}; | ||
function unsetField(path) { | ||
errors.update(($errors) => { | ||
return _unset($errors, path); | ||
}); | ||
warnings.update(($warnings) => { | ||
return _unset($warnings, path); | ||
}); | ||
touched.update(($touched) => { | ||
return _unset($touched, path); | ||
}); | ||
data.update(($data) => { | ||
const newData = _unset($data, path); | ||
if (formNode) | ||
setForm(formNode, newData); | ||
return newData; | ||
}); | ||
} | ||
function addField(path, value, index) { | ||
errors.update(($errors) => { | ||
return addAtIndex($errors, path, null, index); | ||
}); | ||
warnings.update(($warnings) => { | ||
return addAtIndex($warnings, path, null, index); | ||
}); | ||
touched.update(($touched) => { | ||
return addAtIndex($touched, path, false, index); | ||
}); | ||
data.update(($data) => { | ||
const newData = addAtIndex($data, path, value, index); | ||
setTimeout(() => formNode && setForm(formNode, newData)); | ||
return newData; | ||
}); | ||
} | ||
function resetField(path) { | ||
const initialValue = _get(initialValues, path); | ||
data.update(($data) => { | ||
const newData = _set($data, path, initialValue); | ||
if (formNode) | ||
setForm(formNode, newData); | ||
return newData; | ||
}); | ||
touched.update(($touched) => { | ||
return _set($touched, path, false); | ||
}); | ||
errors.update(($errors) => { | ||
return _set($errors, path, null); | ||
}); | ||
warnings.update(($warnings) => { | ||
return _set($warnings, path, null); | ||
}); | ||
} | ||
const setIsSubmitting = createSetHelper(isSubmitting.update); | ||
const setIsDirty = createSetHelper(isDirty.update); | ||
async function validate() { | ||
const currentData = get(data); | ||
setTouched((t) => { | ||
return deepSet(t, true); | ||
}); | ||
const currentErrors = await executeValidation(currentData, config.validate); | ||
const currentWarnings = await executeValidation(currentData, config.warn); | ||
warnings.set(_merge(deepSet(currentData, null), currentWarnings || {})); | ||
errors.set(currentErrors || {}); | ||
return currentErrors; | ||
} | ||
let formNode; | ||
let initialValues = ((_a = config.initialValues) !== null && _a !== void 0 ? _a : {}); | ||
function reset() { | ||
setFields(_cloneDeep(initialValues)); | ||
setTouched(($touched) => deepSet($touched, false)); | ||
isDirty.set(false); | ||
} | ||
return { | ||
public: { | ||
setData, | ||
setFields, | ||
setTouched, | ||
setErrors, | ||
setWarnings, | ||
setIsSubmitting, | ||
setIsDirty, | ||
validate, | ||
reset, | ||
unsetField, | ||
resetField, | ||
addField, | ||
setInitialValues: (values) => { | ||
initialValues = values; | ||
}, | ||
}, | ||
private: { | ||
_setFormNode: (node) => { | ||
formNode = node; | ||
}, | ||
_getFormNode: () => formNode, | ||
_getInitialValues: () => initialValues, | ||
}, | ||
}; | ||
} | ||
function createFormAction({ helpers, stores, config, extender, _setFormNode, _getFormNode, _getInitialValues, _setCurrentExtenders, _getCurrentExtenders, }) { | ||
@@ -674,10 +565,12 @@ const { setFields, setTouched, reset, validate, addValidator, addWarnValidator, addTransformer, setInitialValues, } = helpers; | ||
var _a, _b, _c, _d; | ||
const onSubmit = (_a = altConfig === null || altConfig === void 0 ? void 0 : altConfig.onSubmit) !== null && _a !== void 0 ? _a : config.onSubmit; | ||
const validate = (_b = altConfig === null || altConfig === void 0 ? void 0 : altConfig.validate) !== null && _b !== void 0 ? _b : config.validate; | ||
const warn = (_c = altConfig === null || altConfig === void 0 ? void 0 : altConfig.warn) !== null && _c !== void 0 ? _c : config.warn; | ||
const onError = (_d = altConfig === null || altConfig === void 0 ? void 0 : altConfig.onError) !== null && _d !== void 0 ? _d : config.onError; | ||
const validate = (_a = altConfig === null || altConfig === void 0 ? void 0 : altConfig.validate) !== null && _a !== void 0 ? _a : config.validate; | ||
const warn = (_b = altConfig === null || altConfig === void 0 ? void 0 : altConfig.warn) !== null && _b !== void 0 ? _b : config.warn; | ||
const onError = (_c = altConfig === null || altConfig === void 0 ? void 0 : altConfig.onError) !== null && _c !== void 0 ? _c : config.onError; | ||
const onSuccess = (_d = altConfig === null || altConfig === void 0 ? void 0 : altConfig.onSuccess) !== null && _d !== void 0 ? _d : config.onSuccess; | ||
return async function handleSubmit(event) { | ||
var _a, _b; | ||
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); | ||
if (!onSubmit) | ||
return; | ||
const formNode = _getFormNode(); | ||
event === null || event === void 0 ? void 0 : event.preventDefault(); | ||
@@ -709,3 +602,3 @@ isSubmitting.set(true); | ||
try { | ||
await onSubmit(currentData, { | ||
const response = await onSubmit(currentData, { | ||
form: formNode, | ||
@@ -715,7 +608,14 @@ controls: formNode && Array.from(formNode.elements).filter(isFormControl), | ||
}); | ||
formNode === null || formNode === void 0 ? void 0 : formNode.dispatchEvent(new CustomEvent('feltesuccess', { | ||
detail: response, | ||
})); | ||
await (onSuccess === null || onSuccess === void 0 ? void 0 : onSuccess(response)); | ||
} | ||
catch (e) { | ||
formNode === null || formNode === void 0 ? void 0 : formNode.dispatchEvent(new CustomEvent('felteerror', { | ||
detail: e, | ||
})); | ||
if (!onError) | ||
throw e; | ||
const serverErrors = onError(e); | ||
return; | ||
const serverErrors = await onError(e); | ||
if (serverErrors) { | ||
@@ -798,5 +698,6 @@ errors.set(serverErrors); | ||
function setFileValue(target) { | ||
const files = target.files; | ||
var _a; | ||
const files = Array.from((_a = target.files) !== null && _a !== void 0 ? _a : []); | ||
data.update(($data) => { | ||
return _set($data, getPath(target), target.multiple ? Array.from(files !== null && files !== void 0 ? files : []) : files === null || files === void 0 ? void 0 : files[0]); | ||
return _set($data, getPath(target), target.multiple ? files : files[0]); | ||
}); | ||
@@ -966,2 +867,156 @@ } | ||
function addAtIndex(storeValue, path, value, index) { | ||
return _update(storeValue, path, (oldValue) => { | ||
if (!Array.isArray(oldValue)) | ||
return oldValue; | ||
if (typeof index === 'undefined') { | ||
oldValue.push(value); | ||
} | ||
else { | ||
oldValue.splice(index, 0, value); | ||
} | ||
return oldValue; | ||
}); | ||
} | ||
function isUpdater(value) { | ||
return typeof value === 'function'; | ||
} | ||
function createSetHelper(storeSetter) { | ||
const setHelper = (pathOrValue, valueOrUpdater) => { | ||
if (typeof pathOrValue === 'string') { | ||
const path = pathOrValue; | ||
storeSetter((oldValue) => { | ||
const newValue = isUpdater(valueOrUpdater) | ||
? valueOrUpdater(_get(oldValue, path)) | ||
: valueOrUpdater; | ||
return _set(oldValue, path, newValue); | ||
}); | ||
} | ||
else { | ||
storeSetter((oldValue) => isUpdater(pathOrValue) ? pathOrValue(oldValue) : pathOrValue); | ||
} | ||
}; | ||
return setHelper; | ||
} | ||
function createHelpers({ stores, config, }) { | ||
var _a; | ||
const { data, touched, errors, warnings, isDirty, isSubmitting } = stores; | ||
const setData = createSetHelper(data.update); | ||
const setTouched = createSetHelper(touched.update); | ||
const setErrors = createSetHelper(errors.update); | ||
const setWarnings = createSetHelper(warnings.update); | ||
function updateFields(updater) { | ||
setData((oldData) => { | ||
const newData = updater(oldData); | ||
if (formNode) | ||
setForm(formNode, newData); | ||
return newData; | ||
}); | ||
} | ||
const setFields = (pathOrValue, valueOrUpdater, shouldTouch) => { | ||
const fieldsSetter = createSetHelper(updateFields); | ||
fieldsSetter(pathOrValue, valueOrUpdater); | ||
if (typeof pathOrValue === 'string' && shouldTouch) { | ||
setTouched(pathOrValue, true); | ||
} | ||
}; | ||
function unsetField(path) { | ||
errors.update(($errors) => { | ||
return _unset($errors, path); | ||
}); | ||
warnings.update(($warnings) => { | ||
return _unset($warnings, path); | ||
}); | ||
touched.update(($touched) => { | ||
return _unset($touched, path); | ||
}); | ||
data.update(($data) => { | ||
const newData = _unset($data, path); | ||
if (formNode) | ||
setForm(formNode, newData); | ||
return newData; | ||
}); | ||
} | ||
function addField(path, value, index) { | ||
errors.update(($errors) => { | ||
return addAtIndex($errors, path, null, index); | ||
}); | ||
warnings.update(($warnings) => { | ||
return addAtIndex($warnings, path, null, index); | ||
}); | ||
touched.update(($touched) => { | ||
return addAtIndex($touched, path, false, index); | ||
}); | ||
data.update(($data) => { | ||
const newData = addAtIndex($data, path, value, index); | ||
setTimeout(() => formNode && setForm(formNode, newData)); | ||
return newData; | ||
}); | ||
} | ||
function resetField(path) { | ||
const initialValue = _get(initialValues, path); | ||
data.update(($data) => { | ||
const newData = _set($data, path, initialValue); | ||
if (formNode) | ||
setForm(formNode, newData); | ||
return newData; | ||
}); | ||
touched.update(($touched) => { | ||
return _set($touched, path, false); | ||
}); | ||
errors.update(($errors) => { | ||
return _set($errors, path, null); | ||
}); | ||
warnings.update(($warnings) => { | ||
return _set($warnings, path, null); | ||
}); | ||
} | ||
const setIsSubmitting = createSetHelper(isSubmitting.update); | ||
const setIsDirty = createSetHelper(isDirty.update); | ||
async function validate() { | ||
const currentData = get(data); | ||
setTouched((t) => { | ||
return deepSet(t, true); | ||
}); | ||
const currentErrors = await executeValidation(currentData, config.validate); | ||
const currentWarnings = await executeValidation(currentData, config.warn); | ||
warnings.set(_merge(deepSet(currentData, null), currentWarnings || {})); | ||
errors.set(currentErrors || {}); | ||
return currentErrors; | ||
} | ||
let formNode; | ||
let initialValues = ((_a = config.initialValues) !== null && _a !== void 0 ? _a : {}); | ||
function reset() { | ||
setFields(_cloneDeep(initialValues)); | ||
setTouched(($touched) => deepSet($touched, false)); | ||
isDirty.set(false); | ||
} | ||
return { | ||
public: { | ||
setData, | ||
setFields, | ||
setTouched, | ||
setErrors, | ||
setWarnings, | ||
setIsSubmitting, | ||
setIsDirty, | ||
validate, | ||
reset, | ||
unsetField, | ||
resetField, | ||
addField, | ||
setInitialValues: (values) => { | ||
initialValues = values; | ||
}, | ||
}, | ||
private: { | ||
_setFormNode: (node) => { | ||
formNode = node; | ||
}, | ||
_getFormNode: () => formNode, | ||
_getInitialValues: () => initialValues, | ||
}, | ||
}; | ||
} | ||
function errorFilterer(errValue, touchValue) { | ||
@@ -1181,2 +1236,3 @@ if (_isPlainObject(touchValue)) | ||
exports.FelteSubmitError = FelteSubmitError; | ||
exports._cloneDeep = _cloneDeep; | ||
@@ -1183,0 +1239,0 @@ exports._defaultsDeep = _defaultsDeep; |
{ | ||
"name": "@felte/core", | ||
"version": "1.0.0-next.5", | ||
"version": "1.0.0-next.6", | ||
"description": "Core package for FelteJS", | ||
@@ -27,3 +27,3 @@ "main": "dist/index.js", | ||
"dependencies": { | ||
"@felte/common": "1.0.0-next.5" | ||
"@felte/common": "1.0.0-next.6" | ||
}, | ||
@@ -30,0 +30,0 @@ "publishConfig": { |
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
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
285564
2458
2
+ Added@felte/common@1.0.0-next.6(transitive)
- Removed@felte/common@1.0.0-next.5(transitive)
Updated@felte/common@1.0.0-next.6