@felte/common
Advanced tools
Comparing version 0.1.6 to 0.2.0
# @felte/common | ||
## 0.2.0 | ||
### Minor Changes | ||
- f09b65b: BREAKING: `reporter` renamed to `extend` since its current API can be used to extende Felte's behaviour freely | ||
## 0.1.6 | ||
@@ -4,0 +10,0 @@ |
@@ -23,2 +23,7 @@ import { Readable, Writable } from "svelte/store"; | ||
type Reporter<Data extends Obj = Obj> = (currentForm: CurrentForm<Data>) => ReporterHandler<Data>; | ||
type ExtenderHandler<Data extends Obj> = { | ||
destroy?: () => void; | ||
onSubmitError?: (state: OnSubmitErrorState<Data>) => void; | ||
}; | ||
type Extender<Data extends Obj = Obj> = (currentForm: CurrentForm<Data>) => ExtenderHandler<Data>; | ||
/** `Record<string, unknown>` */ | ||
@@ -41,2 +46,4 @@ type Obj = Record<string, unknown>; | ||
reporter?: Reporter | Reporter[]; | ||
/** Optional function/s to extend Felte's functionality. */ | ||
extend?: Extender | Extender[]; | ||
} | ||
@@ -67,6 +74,4 @@ /** | ||
}; | ||
/** The return type for the `createForm` function. */ | ||
interface Form<Data extends Obj> { | ||
/** Action function to be used with the `use` directive on your `form` elements. */ | ||
form: FormAction; | ||
/** The stores that `createForm` creates. */ | ||
type Stores<Data extends Obj> = { | ||
/** Writable store that contains the form's data. */ | ||
@@ -78,2 +83,11 @@ data: Writable<Data>; | ||
touched: Writable<Touched<Data>>; | ||
/** Writable store containing only a boolean that represents if the form is submitting. */ | ||
isSubmitting: Writable<boolean>; | ||
/** Readable store containing only a boolean that represents if the form is valid. */ | ||
isValid: Readable<boolean>; | ||
}; | ||
/** The return type for the `createForm` function. */ | ||
type Form<Data extends Obj> = { | ||
/** Action function to be used with the `use` directive on your `form` elements. */ | ||
form: FormAction; | ||
/** Function to handle submit to be passed to the on:submit event. Not necessary if using the `form` action. */ | ||
@@ -85,6 +99,2 @@ handleSubmit: (e?: Event) => void; | ||
reset: () => void; | ||
/** Readable store containing only a boolean that represents if the form is valid. */ | ||
isValid: Readable<boolean>; | ||
/** Writable store containing only a boolean that represents if the form is submitting. */ | ||
isSubmitting: Writable<boolean>; | ||
/** Helper function to touch a specific field. */ | ||
@@ -98,3 +108,3 @@ setTouched: (path: string) => void; | ||
validate: () => Promise<Errors<Data> | void>; | ||
} | ||
} & Stores<Data>; | ||
type DeepSetResult<Data extends Obj, Value> = { | ||
@@ -170,3 +180,3 @@ [key in keyof Data]: Data[key] extends Obj ? DeepSetResult<Data[key], Value> : Value; | ||
declare function setForm<Data extends Obj>(node: HTMLFormElement, data: Data): void; | ||
export { _some, _mapValues, _get, _set, _unset, _update, _isPlainObject, deepSet, deepSome, isInputElement, isTextAreaElement, isSelectElement, isFieldSetElement, isFormControl, isElement, getPath, getFormControls, addAttrsFromFieldset, getFormDefaultValues, setForm, CreateSubmitHandlerConfig, CurrentForm, OnSubmitErrorState, ReporterHandler, Reporter, Obj, FieldValue, FormControl, FormConfigWithoutInitialValues, FormConfigWithInitialValues, FormConfig, Errors, Touched, FormAction, Form }; | ||
export { _some, _mapValues, _get, _set, _unset, _update, _isPlainObject, deepSet, deepSome, isInputElement, isTextAreaElement, isSelectElement, isFieldSetElement, isFormControl, isElement, getPath, getFormControls, addAttrsFromFieldset, getFormDefaultValues, setForm, CreateSubmitHandlerConfig, CurrentForm, OnSubmitErrorState, ReporterHandler, Reporter, ExtenderHandler, Extender, Obj, FieldValue, FormControl, FormConfigWithoutInitialValues, FormConfigWithInitialValues, FormConfig, Errors, Touched, FormAction, Stores, Form }; | ||
//# sourceMappingURL=index.d.ts.map |
@@ -1,2 +0,2 @@ | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).Common={})}(this,(function(e){"use strict";function t(e,t){return Object.keys(e).some((n=>t(e[n])))}function n(e,t){return Object.keys(e).reduce(((n,o)=>Object.assign(Object.assign({},n),{[o]:t(e[o])})),{})}function o(e,t,n){const o=t.split(".");let i=e;try{for(const e of o)i=i[e]}catch(e){return n}return function(e){return Array.isArray(e)?0===e.length||e.some((e=>e instanceof File||"string"==typeof e)):"string"==typeof e||"number"==typeof e||"boolean"==typeof e||e instanceof File}(i)?i:n}function i(e,t,n){const o=t.split(".");let i=e;for(;o.length-1;){const e=o.shift();e&&(e in i||(i[e]={}),i=i[e])}return i[o[0]]=n,e}function r(e,t,n){const o=t.split(".");let i=e;for(;o.length-1;){const e=o.shift();e&&(e in i||(i[e]={}),i=i[e])}return i[o[0]]=n(i[o[0]]),e}function l(e){return"[object Object]"===Object.prototype.toString.call(e)}function c(e){var t;return"INPUT"===(null===(t=e)||void 0===t?void 0:t.nodeName)}function s(e){var t;return"TEXTAREA"===(null===(t=e)||void 0===t?void 0:t.nodeName)}function u(e){var t;return"SELECT"===(null===(t=e)||void 0===t?void 0:t.nodeName)}function f(e){var t;return"FIELDSET"===(null===(t=e)||void 0===t?void 0:t.nodeName)}function a(e){return c(e)||s(e)||u(e)}function d(e){const t=e.dataset.felteFieldset;return t?`${t}.${e.name}`:e.name}function m(e){for(const t of e.elements)(a(t)||f(t))&&(e.name&&t.name&&(t.dataset.felteFieldset=e.dataset.felteFieldset?`${e.dataset.felteFieldset}.${e.name}`:e.name),"true"!==e.dataset.felteUnsetOnRemove||t.hasAttribute("data-felte-unset-on-remove")||(t.dataset.felteUnsetOnRemove="true"))}e._get=o,e._isPlainObject=l,e._mapValues=n,e._set=i,e._some=t,e._unset=function(e,t){const n=t.split(".");let o=e;for(;n.length-1;){const e=n.shift();e&&(e in o||(o[e]={}),o=o[e])}return delete o[n[0]],e},e._update=r,e.addAttrsFromFieldset=m,e.deepSet=function e(t,o){return n(t,(t=>l(t)?e(t,o):o))},e.deepSome=function e(n,o){return t(n,(t=>l(t)?e(t,o):o(t)))},e.getFormControls=function e(t){if(a(t))return[t];if(0===t.childElementCount)return[];const n=[];for(const o of t.children){if(a(o)&&n.push(o),f(o))for(const e of o.elements)a(e)&&n.push(e);o.childElementCount>0&&n.push(...e(o))}return n},e.getFormDefaultValues=function(e){var t;const n={},l={};for(const s of e.elements){if(f(s)&&m(s),!a(s)||!s.name)continue;const u=d(s);if(i(l,u,!1),c(s)&&"checkbox"===s.type){if(void 0===o(n,u)){if(1===e.querySelectorAll(`[name="${s.name}"]`).length){i(n,u,s.checked);continue}i(n,u,s.checked?[s.value]:[]);continue}Array.isArray(o(n,u))&&s.checked&&r(n,u,(e=>[...e,s.value]))}else if(c(s)&&"radio"===s.type){if(o(n,u))continue;i(n,u,s.checked?s.value:void 0)}else c(s)&&"file"===s.type?i(n,u,s.multiple?Array.from(s.files||[]):null===(t=s.files)||void 0===t?void 0:t[0]):i(n,u,s.type.match(/^(number|range)$/)?+s.value:s.value)}return{defaultData:n,defaultTouched:l}},e.getPath=d,e.isElement=function(e){return e.nodeType===Node.ELEMENT_NODE},e.isFieldSetElement=f,e.isFormControl=a,e.isInputElement=c,e.isSelectElement=u,e.isTextAreaElement=s,e.setForm=function(e,t){for(const n of e.elements){if(f(n)&&m(n),!a(n)||!n.name)continue;const e=d(n);if(c(n)&&"checkbox"===n.type){const i=o(t,e);if(void 0===i||"boolean"==typeof i){n.checked=!!i;continue}Array.isArray(i)&&(i.includes(n.value)?n.checked=!0:n.checked=!1)}else if(c(n)&&"radio"===n.type){const i=o(t,e);n.value===i?n.checked=!0:n.checked=!1}else c(n)&&"file"===n.type?(n.files=null,n.value=""):n.value=String(o(t,e,""))}},Object.defineProperty(e,"__esModule",{value:!0})})); | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).Common={})}(this,(function(e){"use strict";function t(e,t){return Object.keys(e).some((n=>t(e[n])))}function n(e,t){return Object.keys(e).reduce(((n,o)=>Object.assign(Object.assign({},n),{[o]:t(e[o])})),{})}function o(e,t,n){const o=t.split(".");let i=e;try{for(const e of o)i=i[e]}catch(e){return n}return function(e){return Array.isArray(e)?0===e.length||e.some((e=>e instanceof File||"string"==typeof e)):"string"==typeof e||"number"==typeof e||"boolean"==typeof e||e instanceof File}(i)?i:n}function i(e,t,n){e=null!=e?e:{};const o=t.split(".");let i=e;for(;o.length-1;){const e=o.shift();e&&(e in i||(i[e]={}),i=i[e])}return i[o[0]]=n,e}function r(e,t,n){const o=t.split(".");let i=e;for(;o.length-1;){const e=o.shift();e&&(e in i||(i[e]={}),i=i[e])}return i[o[0]]=n(i[o[0]]),e}function l(e){return"[object Object]"===Object.prototype.toString.call(e)}function u(e){var t;return"INPUT"===(null===(t=e)||void 0===t?void 0:t.nodeName)}function c(e){var t;return"TEXTAREA"===(null===(t=e)||void 0===t?void 0:t.nodeName)}function s(e){var t;return"SELECT"===(null===(t=e)||void 0===t?void 0:t.nodeName)}function f(e){var t;return"FIELDSET"===(null===(t=e)||void 0===t?void 0:t.nodeName)}function a(e){return u(e)||c(e)||s(e)}function d(e){const t=e.dataset.felteFieldset;return t?`${t}.${e.name}`:e.name}function m(e){for(const t of e.elements)(a(t)||f(t))&&(e.name&&t.name&&(t.dataset.felteFieldset=e.dataset.felteFieldset?`${e.dataset.felteFieldset}.${e.name}`:e.name),"true"!==e.dataset.felteUnsetOnRemove||t.hasAttribute("data-felte-unset-on-remove")||(t.dataset.felteUnsetOnRemove="true"))}e._get=o,e._isPlainObject=l,e._mapValues=n,e._set=i,e._some=t,e._unset=function(e,t){const n=t.split(".");let o=e;for(;n.length-1;){const e=n.shift();e&&(e in o||(o[e]={}),o=o[e])}return delete o[n[0]],e},e._update=r,e.addAttrsFromFieldset=m,e.deepSet=function e(t,o){return n(t,(t=>l(t)?e(t,o):o))},e.deepSome=function e(n,o){return t(n,(t=>l(t)?e(t,o):o(t)))},e.getFormControls=function e(t){if(a(t))return[t];if(0===t.childElementCount)return[];const n=[];for(const o of t.children){if(a(o)&&n.push(o),f(o))for(const e of o.elements)a(e)&&n.push(e);o.childElementCount>0&&n.push(...e(o))}return n},e.getFormDefaultValues=function(e){var t;const n={},l={};for(const c of e.elements){if(f(c)&&m(c),!a(c)||!c.name)continue;const s=d(c);if(i(l,s,!1),u(c)&&"checkbox"===c.type){if(void 0===o(n,s)){if(1===e.querySelectorAll(`[name="${c.name}"]`).length){i(n,s,c.checked);continue}i(n,s,c.checked?[c.value]:[]);continue}Array.isArray(o(n,s))&&c.checked&&r(n,s,(e=>[...e,c.value]))}else if(u(c)&&"radio"===c.type){if(o(n,s))continue;i(n,s,c.checked?c.value:void 0)}else u(c)&&"file"===c.type?i(n,s,c.multiple?Array.from(c.files||[]):null===(t=c.files)||void 0===t?void 0:t[0]):i(n,s,c.type.match(/^(number|range)$/)?+c.value:c.value)}return{defaultData:n,defaultTouched:l}},e.getPath=d,e.isElement=function(e){return e.nodeType===Node.ELEMENT_NODE},e.isFieldSetElement=f,e.isFormControl=a,e.isInputElement=u,e.isSelectElement=s,e.isTextAreaElement=c,e.setForm=function(e,t){for(const n of e.elements){if(f(n)&&m(n),!a(n)||!n.name)continue;const e=d(n);if(u(n)&&"checkbox"===n.type){const i=o(t,e);if(void 0===i||"boolean"==typeof i){n.checked=!!i;continue}Array.isArray(i)&&(i.includes(n.value)?n.checked=!0:n.checked=!1)}else if(u(n)&&"radio"===n.type){const i=o(t,e);n.value===i?n.checked=!0:n.checked=!1}else u(n)&&"file"===n.type?(n.files=null,n.value=""):n.value=String(o(t,e,""))}},Object.defineProperty(e,"__esModule",{value:!0})})); | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "@felte/common", | ||
"version": "0.1.6", | ||
"version": "0.2.0", | ||
"description": "Common utilities for Felte packages", | ||
@@ -5,0 +5,0 @@ "author": "Pablo Berganza <pablo@berganza.dev>", |
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
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
44632
206