@felte/core
Advanced tools
Comparing version 0.2.2 to 0.2.3
# @felte/core | ||
## 0.2.3 | ||
### Patch Changes | ||
- 3dcfe7c: Call submit handler on `HTMLFormElement.requestSubmit()` | ||
## 0.2.2 | ||
@@ -4,0 +10,0 @@ |
@@ -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).Core={})}(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,r)=>Object.assign(Object.assign({},n),{[r]:t(e[r])})),{})}function r(e){return"[object Object]"===Object.prototype.toString.call(e)}function o(e){return Object.keys(e||{}).reduce(((t,n)=>Object.assign(Object.assign({},t),{[n]:r(e[n])?o(e[n]):Array.isArray(e[n])?[...e[n]]:e[n]})),{})}function i(e,t){return n(e,(e=>r(e)?i(e,t):Array.isArray(e)?e.map(function(e){return function(t){return r(t)?i(t,e):e}}(t)):t))}function a(...e){const t=e.pop(),n=o(e.shift());if(0===e.length)return n;for(const u of e){if(!u)continue;const e=Object.keys(u);for(const l of e){const e=t(n[l],u[l]);if(void 0!==e)n[l]=e;else if(r(u[l])&&r(n[l]))n[l]=a(n[l],u[l],t);else if(Array.isArray(u[l]))n[l]=u[l].map(((e,o)=>r(e)?a(Array.isArray(n[l])?n[l][o]:n[l],e,t):e));else if(r(u[l])){const e=i(o(u[l]),void 0);n[l]=a(e,u[l],t)}else void 0!==u[l]&&(n[l]=u[l])}}return n}function u(e,t){if(!r(e)||!r(t)){if(Array.isArray(t)){if(t.some(r))return;const n=Array.isArray(e)?e:[];return t.map(((e,t)=>{var r;return null!==(r=n[t])&&void 0!==r?r:e}))}return void 0!==e?e:void 0}}function l(...e){return a(...e,u)}function c(...e){return a(...e,(()=>{}))}function s(e,t,n){const r=n=>String.prototype.split.call(t,n).filter(Boolean).reduce(((e,t)=>null!=e?e[t]:e),e),o=r(/[,[\]]+?/)||r(/[,[\].]+?/);return void 0===o||o===e?n:o}function d(e,t,n){Object(e)!==e?e={}:void 0!==e&&(e=o(e));let r=Array.isArray(t)?t:t.toString().match(/[^.[\]]+/g)||[];return r.slice(0,-1).reduce(((e,t,n)=>Object(e[t])===e[t]?e[t]:e[t]=Math.abs(Number(r[n+1]))>>0==+r[n+1]?[]:{}),e)[r[r.length-1]]=n,e}function f(e,t){var n;if(Object(e)!==e)return;void 0!==e&&(e=o(e));let r=Array.isArray(t)?t:t.toString().match(/[^.[\]]+/g)||[];return null===(n=r.slice(0,-1).reduce(((e,t)=>Object(e[t])===e[t]?e[t]:void 0),e))||void 0===n||delete n[r[r.length-1]],e}function v(e,t,n){Object(e)!==e&&(e={});let r=t.toString().match(/[^.[\]]+/g)||[];return r.slice(0,-1).reduce(((e,t,n)=>Object(e[t])===e[t]?e[t]:e[t]=Math.abs(Number(r[n+1]))>>0==+r[n+1]?[]:{}),e)[r[r.length-1]]=n(s(e,t)),e}function m(e,n){return t(e,(e=>r(e)?m(e,n):n(e)))}function y(e){return e.hasAttribute("data-felte-index")?Number(e.dataset.felteIndex):void 0}function p(e){var t;return"INPUT"===(null===(t=e)||void 0===t?void 0:t.nodeName)}function g(e){var t;return"TEXTAREA"===(null===(t=e)||void 0===t?void 0:t.nodeName)}function h(e){var t;return"SELECT"===(null===(t=e)||void 0===t?void 0:t.nodeName)}function b(e){var t;return"FIELDSET"===(null===(t=e)||void 0===t?void 0:t.nodeName)}function A(e){return p(e)||g(e)||h(e)}function E(e){return e.nodeType===Node.ELEMENT_NODE}function O(e,t){const n=y(e);let r="";t?r=t:A(e)&&(r=e.name),r=void 0===n?r:`${r}[${n}]`;let o=e.parentNode;if(!o)return r;for(;o&&"FORM"!==o.nodeName;){if(b(o)&&o.name){const e=y(o);r=`${void 0===e?o.name:`${o.name}[${e}]`}.${r}`}o=o.parentNode}return r}function j(e){const t=e.dataset.felteFieldset,n=y(e),r=void 0===n?e.name:`${e.name}[${n}]`;return t?`${t}.${r}`:r}function x(e){let t=e;for(;t&&"FORM"!==t.nodeName;){if(t.hasAttribute("data-felte-ignore"))return!0;t=t.parentElement}return!1}function S(e){if(A(e))return[e];if(0===e.childElementCount)return[];const t=new Set;for(const n of e.children){if(A(n)&&t.add(n),b(n))for(const e of n.elements)A(e)&&t.add(e);n.childElementCount>0&&S(n).forEach((e=>t.add(e)))}return Array.from(t)}function k(e){for(const t of e.elements)if(A(t)||b(t)){if(e.name&&t.name){const n=y(e),r=void 0===n?e.name:`${e.name}[${n}]`;t.dataset.felteFieldset=e.dataset.felteFieldset?`${e.dataset.felteFieldset}.${r}`:r}"true"!==e.dataset.felteUnsetOnRemove||t.hasAttribute("data-felte-unset-on-remove")||(t.dataset.felteUnsetOnRemove="true")}}function N(e){return e.type.match(/^(number|range)$/)?e.value?+e.value:void 0:e.value}function F(e){var t;let n={};for(const r of e.elements){if(b(r)&&k(r),!A(r)||!r.name)continue;const o=O(r),i=y(r);if(p(r)){if("checkbox"===r.type){if(void 0===s(n,o)){if(1===Array.from(e.querySelectorAll(`[name="${r.name}"]`)).filter((e=>!!A(e)&&(void 0!==i?Number(e.dataset.felteIndex)===i:o===O(e)))).length){n=d(n,o,r.checked);continue}n=d(n,o,r.checked?[r.value]:[]);continue}Array.isArray(s(n,o))&&r.checked&&v(n,o,(e=>(void 0===i||Array.isArray(e)||(e=[]),[...e,r.value])));continue}if("radio"===r.type){if(s(n,o))continue;n=d(n,o,r.checked?r.value:void 0);continue}if("file"===r.type){n=d(n,o,r.multiple?Array.from(r.files||[]):null===(t=r.files)||void 0===t?void 0:t[0]);continue}}const a=N(r);n=d(n,o,a)}return{defaultData:n}}function T(e,t){if(!A(e))return;const n=t;if(p(e)){if("checkbox"===e.type){const t=n;return void 0===t||"boolean"==typeof t?void(e.checked=!!t):void(Array.isArray(t)&&(t.includes(e.value)?e.checked=!0:e.checked=!1))}if("radio"===e.type){const t=n;return void(e.value===t?e.checked=!0:e.checked=!1)}if("file"===e.type)return e.files=null,void(e.value="")}e.value=String(null!=n?n:"")}function $(e,t){for(const n of e.elements){if(b(n)&&k(n),!A(n)||!n.name)continue;T(n,s(t,O(n)))}}function V(e,t){if(!r(e)&&!r(t)){if(null===e)return t;if(null===t)return e;if(e&&t)return Array.isArray(e)||(e=[e]),Array.isArray(t)||(t=[t]),[...e,...t]}}async function L(e,t){if(!t)return;if(!Array.isArray(t))return t(e);return a(...await Promise.all(t.map((t=>t(e)))),V)}function _(){}function M(e){let t;return function(e,...t){if(null==e)return _;const n=e.subscribe(...t);return n.unsubscribe?()=>n.unsubscribe():n}(e,(e=>t=e))(),t}e._cloneDeep=o,e._defaultsDeep=l,e._get=s,e._isPlainObject=r,e._mapValues=n,e._merge=c,e._mergeWith=a,e._set=d,e._some=t,e._unset=f,e._update=v,e.addAttrsFromFieldset=k,e.createForm=function(e,t){var n,u;function v(t){e.validate?e.validate=[...e.validate,t]:e.validate=[t]}null!==(n=e.extend)&&void 0!==n||(e.extend=[]),null!==(u=e.touchTriggerEvents)&&void 0!==u||(e.touchTriggerEvents={change:!0,blur:!0}),e.validate&&!Array.isArray(e.validate)&&(e.validate=[e.validate]);const b=Array.isArray(e.extend)?e.extend:[e.extend];let k=[];const{isSubmitting:V,data:_,errors:D,touched:I,isValid:w}=t.stores;k=b.map((t=>t({errors:D,touched:I,data:_,config:e,addValidator:v})));const P=function({stores:e,config:t,currentExtenders:n,extender:r,addValidator:a}){var u;const{isSubmitting:v,data:b,touched:k,errors:V}=e;function _(e){var r,o,a;const u=null!==(r=null==e?void 0:e.onSubmit)&&void 0!==r?r:t.onSubmit,l=null!==(o=null==e?void 0:e.validate)&&void 0!==o?o:t.validate,c=null!==(a=null==e?void 0:e.onError)&&void 0!==a?a:t.onError;return async function(r){null==r||r.preventDefault(),v.set(!0);const o=M(b),a=await L(o,l);if(k.update((e=>i(e,!0))),a&&(V.set(a),m(a,(e=>!!e))))return n.forEach((e=>{var t;return null===(t=null==e?void 0:e.onSubmitError)||void 0===t?void 0:t.call(e,{data:o,errors:a})})),void v.set(!1);try{await u(o,{form:w,controls:w&&Array.from(w.elements).filter(A),config:Object.assign(Object.assign({},t),e)})}catch(e){if(!c)throw e;const t=c(e);t&&(V.set(t),n.forEach((e=>{var n;return null===(n=null==e?void 0:e.onSubmitError)||void 0===n?void 0:n.call(e,{data:o,errors:t})})))}finally{v.set(!1)}}}function D(e,t){const n=void 0===t?e:`${e}[${t}]`;k.update((e=>d(e,n,!0)))}function I(e){b.set(o(e)),w&&$(w,e)}let w,P=null!==(u=t.initialValues)&&void 0!==u?u:{};const C=_();return{handleSubmit:C,createSubmitHandler:_,reset:function(){I(o(P)),k.update((e=>i(e,!1)))},setTouched:D,setError:function(e,t){V.update((n=>d(n,e,t)))},setField:function(e,t,n=!0){if(b.update((n=>d(n,e,t))),n&&D(e),w)for(const n of w.elements){if(!A(n)||!n.name)continue;const r=O(n);if(e===r)return void T(n,t)}},setFields:I,validate:async function(){const e=M(b);k.update((e=>i(e,!0)));const n=await L(e,t.validate);return V.set(n||{}),n},form:function(e){function u(n){return n({form:e,controls:Array.from(e.elements).filter(A),data:b,errors:V,touched:k,config:t,addValidator:a})}function v(){for(const t of Array.from(e.elements).filter(A)){if(x(t))continue;let e="value";p(t)&&["checkbox","radio"].includes(t.type)&&(e="checked"),p(t)&&"file"===t.type&&(e="files");const n=Object.getOwnPropertyDescriptor(h(t)?HTMLSelectElement.prototype:g(t)?HTMLTextAreaElement.prototype:HTMLInputElement.prototype,e);Object.defineProperty(t,e,{configurable:!0,set(e){var r;if(null===(r=null==n?void 0:n.set)||void 0===r||r.call(t,e),p(t)){if("checkbox"===t.type)return T(t);if("radio"===t.type)return $(t);if("file"===t.type)return L(t)}const o=h(t)?t.value:N(t);b.update((e=>d(e,O(t),o)))},get:null==n?void 0:n.get})}}n=r.map(u),e.noValidate=!!t.validate;const{defaultData:m}=F(e);function T(t){const n=y(t),r=O(t),o=Array.from(e.querySelectorAll(`[name="${t.name}"]`)).filter((e=>!!A(e)&&(void 0!==n?Number(e.dataset.felteIndex)===n:r===O(e))));return 1===o.length?b.update((e=>d(e,O(t),t.checked))):b.update((e=>d(e,O(t),o.filter(p).filter((e=>e.checked)).map((e=>e.value)))))}function $(t){const n=e.querySelectorAll(`[name="${t.name}"]`),r=Array.from(n).find((e=>p(e)&&e.checked));b.update((e=>d(e,O(t),null==r?void 0:r.value)))}function L(e){const t=e.files;b.update((n=>d(n,O(e),e.multiple?Array.from(null!=t?t:[]):null==t?void 0:t[0])))}function _(e){var n;const r=e.target;if(!r||!A(r)||h(r)||x(r))return;if(["checkbox","radio","file"].includes(r.type))return;if(!r.name)return;(null===(n=t.touchTriggerEvents)||void 0===n?void 0:n.input)&&D(O(r));const o=N(r);b.update((e=>d(e,O(r),o)))}function M(e){var n;const r=e.target;r&&A(r)&&!x(r)&&r.name&&((null===(n=t.touchTriggerEvents)||void 0===n?void 0:n.change)&&D(O(r)),h(r)&&b.update((e=>d(e,O(r),r.value))),p(r)&&("checkbox"===r.type&&T(r),"radio"===r.type&&$(r),"file"===r.type&&L(r)))}function R(e){var n;const r=e.target;r&&A(r)&&!x(r)&&r.name&&(null===(n=t.touchTriggerEvents)||void 0===n?void 0:n.blur)&&D(O(r))}function H(e){for(const t of e)"true"===t.dataset.felteUnsetOnRemove&&b.update((e=>f(e,j(t))))}w=e,P=c(o(m),P),I(P),k.set(i(P,!1));const U=new MutationObserver((function(t){for(const o of t)if("childList"===o.type){if(o.addedNodes.length>0){if(v(),!Array.from(o.addedNodes).some((e=>!!E(e)&&(!!A(e)||S(e).length>0))))continue;n.forEach((e=>{var t;return null===(t=null==e?void 0:e.destroy)||void 0===t?void 0:t.call(e)})),n=r.map(u);const{defaultData:t}=F(e),a=i(t,!1);b.update((e=>l(e,t))),k.update((e=>l(e,a)))}if(o.removedNodes.length>0)for(const e of o.removedNodes){if(!E(e))continue;const t=S(e);0!==t.length&&(n.forEach((e=>{var t;return null===(t=null==e?void 0:e.destroy)||void 0===t?void 0:t.call(e)})),n=r.map(u),H(t))}}}));U.observe(e,{childList:!0,subtree:!0}),v(),e.addEventListener("input",_),e.addEventListener("change",M),e.addEventListener("focusout",R),e.addEventListener("submit",C);const q=V.subscribe((t=>{for(const n of e.elements){if(!A(n)||!n.name)continue;const e=s(t,O(n)),r=Array.isArray(e)?e.join("\n"):"string"==typeof e?e:void 0;r!==n.dataset.felteValidationMessage&&(r?n.dataset.felteValidationMessage=r:delete n.dataset.felteValidationMessage)}}));return{destroy(){U.disconnect(),e.removeEventListener("input",_),e.removeEventListener("change",M),e.removeEventListener("focusout",R),e.removeEventListener("submit",C),q(),n.forEach((e=>{var t;return null===(t=null==e?void 0:e.destroy)||void 0===t?void 0:t.call(e)}))}}}}}({currentExtenders:k,extender:b,config:e,addValidator:v,stores:{data:_,errors:D,touched:I,isValid:w,isSubmitting:V}});function C(e,t){if(!r(e))return e!==t}function R(e,t){if(!r(e))return t||e}return Object.assign({data:Object.assign(Object.assign({},_),{set:function(t){return I.update((n=>{const r=a(o(t),e.initialValues,C);return a(r,n,R)})),_.set(t)}}),errors:D,touched:I,isValid:w,isSubmitting:V},P)},e.deepSet=i,e.deepSome=m,e.executeValidation=L,e.getFormControls=S,e.getFormDefaultValues=F,e.getIndex=y,e.getInputTextOrNumber=N,e.getPath=O,e.getPathFromDataset=j,e.isElement=E,e.isFieldSetElement=b,e.isFieldValue=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},e.isFormControl=A,e.isInputElement=p,e.isSelectElement=h,e.isTextAreaElement=g,e.setControlValue=T,e.setForm=$,e.shouldIgnore=x,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).Core={})}(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,r)=>Object.assign(Object.assign({},n),{[r]:t(e[r])})),{})}function r(e){return"[object Object]"===Object.prototype.toString.call(e)}function o(e){return Object.keys(e||{}).reduce(((t,n)=>Object.assign(Object.assign({},t),{[n]:r(e[n])?o(e[n]):Array.isArray(e[n])?[...e[n]]:e[n]})),{})}function i(e,t){return n(e,(e=>r(e)?i(e,t):Array.isArray(e)?e.map(function(e){return function(t){return r(t)?i(t,e):e}}(t)):t))}function a(...e){const t=e.pop(),n=o(e.shift());if(0===e.length)return n;for(const u of e){if(!u)continue;const e=Object.keys(u);for(const l of e){const e=t(n[l],u[l]);if(void 0!==e)n[l]=e;else if(r(u[l])&&r(n[l]))n[l]=a(n[l],u[l],t);else if(Array.isArray(u[l]))n[l]=u[l].map(((e,o)=>r(e)?a(Array.isArray(n[l])?n[l][o]:n[l],e,t):e));else if(r(u[l])){const e=i(o(u[l]),void 0);n[l]=a(e,u[l],t)}else void 0!==u[l]&&(n[l]=u[l])}}return n}function u(e,t){if(!r(e)||!r(t)){if(Array.isArray(t)){if(t.some(r))return;const n=Array.isArray(e)?e:[];return t.map(((e,t)=>{var r;return null!==(r=n[t])&&void 0!==r?r:e}))}return void 0!==e?e:void 0}}function l(...e){return a(...e,u)}function c(...e){return a(...e,(()=>{}))}function s(e,t,n){const r=n=>String.prototype.split.call(t,n).filter(Boolean).reduce(((e,t)=>null!=e?e[t]:e),e),o=r(/[,[\]]+?/)||r(/[,[\].]+?/);return void 0===o||o===e?n:o}function d(e,t,n){Object(e)!==e?e={}:void 0!==e&&(e=o(e));let r=Array.isArray(t)?t:t.toString().match(/[^.[\]]+/g)||[];return r.slice(0,-1).reduce(((e,t,n)=>Object(e[t])===e[t]?e[t]:e[t]=Math.abs(Number(r[n+1]))>>0==+r[n+1]?[]:{}),e)[r[r.length-1]]=n,e}function f(e,t){var n;if(Object(e)!==e)return;void 0!==e&&(e=o(e));let r=Array.isArray(t)?t:t.toString().match(/[^.[\]]+/g)||[];return null===(n=r.slice(0,-1).reduce(((e,t)=>Object(e[t])===e[t]?e[t]:void 0),e))||void 0===n||delete n[r[r.length-1]],e}function v(e,t,n){Object(e)!==e&&(e={});let r=t.toString().match(/[^.[\]]+/g)||[];return r.slice(0,-1).reduce(((e,t,n)=>Object(e[t])===e[t]?e[t]:e[t]=Math.abs(Number(r[n+1]))>>0==+r[n+1]?[]:{}),e)[r[r.length-1]]=n(s(e,t)),e}function m(e,n){return t(e,(e=>r(e)?m(e,n):n(e)))}function y(e){return e.hasAttribute("data-felte-index")?Number(e.dataset.felteIndex):void 0}function p(e){var t;return"INPUT"===(null===(t=e)||void 0===t?void 0:t.nodeName)}function g(e){var t;return"TEXTAREA"===(null===(t=e)||void 0===t?void 0:t.nodeName)}function b(e){var t;return"SELECT"===(null===(t=e)||void 0===t?void 0:t.nodeName)}function h(e){var t;return"FIELDSET"===(null===(t=e)||void 0===t?void 0:t.nodeName)}function A(e){return p(e)||g(e)||b(e)}function E(e){return e.nodeType===Node.ELEMENT_NODE}function O(e,t){const n=y(e);let r="";t?r=t:A(e)&&(r=e.name),r=void 0===n?r:`${r}[${n}]`;let o=e.parentNode;if(!o)return r;for(;o&&"FORM"!==o.nodeName;){if(h(o)&&o.name){const e=y(o);r=`${void 0===e?o.name:`${o.name}[${e}]`}.${r}`}o=o.parentNode}return r}function S(e){const t=e.dataset.felteFieldset,n=y(e),r=void 0===n?e.name:`${e.name}[${n}]`;return t?`${t}.${r}`:r}function j(e){let t=e;for(;t&&"FORM"!==t.nodeName;){if(t.hasAttribute("data-felte-ignore"))return!0;t=t.parentElement}return!1}function x(e){if(A(e))return[e];if(0===e.childElementCount)return[];const t=new Set;for(const n of e.children){if(A(n)&&t.add(n),h(n))for(const e of n.elements)A(e)&&t.add(e);n.childElementCount>0&&x(n).forEach((e=>t.add(e)))}return Array.from(t)}function k(e){for(const t of e.elements)if(A(t)||h(t)){if(e.name&&t.name){const n=y(e),r=void 0===n?e.name:`${e.name}[${n}]`;t.dataset.felteFieldset=e.dataset.felteFieldset?`${e.dataset.felteFieldset}.${r}`:r}"true"!==e.dataset.felteUnsetOnRemove||t.hasAttribute("data-felte-unset-on-remove")||(t.dataset.felteUnsetOnRemove="true")}}function N(e){return e.type.match(/^(number|range)$/)?e.value?+e.value:void 0:e.value}function F(e){var t;let n={};for(const r of e.elements){if(h(r)&&k(r),!A(r)||!r.name)continue;const o=O(r),i=y(r);if(p(r)){if("checkbox"===r.type){if(void 0===s(n,o)){if(1===Array.from(e.querySelectorAll(`[name="${r.name}"]`)).filter((e=>!!A(e)&&(void 0!==i?Number(e.dataset.felteIndex)===i:o===O(e)))).length){n=d(n,o,r.checked);continue}n=d(n,o,r.checked?[r.value]:[]);continue}Array.isArray(s(n,o))&&r.checked&&v(n,o,(e=>(void 0===i||Array.isArray(e)||(e=[]),[...e,r.value])));continue}if("radio"===r.type){if(s(n,o))continue;n=d(n,o,r.checked?r.value:void 0);continue}if("file"===r.type){n=d(n,o,r.multiple?Array.from(r.files||[]):null===(t=r.files)||void 0===t?void 0:t[0]);continue}}const a=N(r);n=d(n,o,a)}return{defaultData:n}}function T(e,t){if(!A(e))return;const n=t;if(p(e)){if("checkbox"===e.type){const t=n;return void 0===t||"boolean"==typeof t?void(e.checked=!!t):void(Array.isArray(t)&&(t.includes(e.value)?e.checked=!0:e.checked=!1))}if("radio"===e.type){const t=n;return void(e.value===t?e.checked=!0:e.checked=!1)}if("file"===e.type)return e.files=null,void(e.value="")}e.value=String(null!=n?n:"")}function $(e,t){for(const n of e.elements){if(h(n)&&k(n),!A(n)||!n.name)continue;T(n,s(t,O(n)))}}function V(e,t){if(!r(e)&&!r(t)){if(null===e)return t;if(null===t)return e;if(e&&t)return Array.isArray(e)||(e=[e]),Array.isArray(t)||(t=[t]),[...e,...t]}}async function L(e,t){if(!t)return;if(!Array.isArray(t))return t(e);return a(...await Promise.all(t.map((t=>t(e)))),V)}function _(){}function M(e){let t;return function(e,...t){if(null==e)return _;const n=e.subscribe(...t);return n.unsubscribe?()=>n.unsubscribe():n}(e,(e=>t=e))(),t}e._cloneDeep=o,e._defaultsDeep=l,e._get=s,e._isPlainObject=r,e._mapValues=n,e._merge=c,e._mergeWith=a,e._set=d,e._some=t,e._unset=f,e._update=v,e.addAttrsFromFieldset=k,e.createForm=function(e,t){var n,u;function v(t){e.validate?e.validate=[...e.validate,t]:e.validate=[t]}null!==(n=e.extend)&&void 0!==n||(e.extend=[]),null!==(u=e.touchTriggerEvents)&&void 0!==u||(e.touchTriggerEvents={change:!0,blur:!0}),e.validate&&!Array.isArray(e.validate)&&(e.validate=[e.validate]);const h=Array.isArray(e.extend)?e.extend:[e.extend];let k=[];const{isSubmitting:V,data:_,errors:D,touched:I,isValid:w}=t.stores;k=h.map((t=>t({errors:D,touched:I,data:_,config:e,addValidator:v})));const P=function({stores:e,config:t,currentExtenders:n,extender:r,addValidator:a}){var u;const{isSubmitting:v,data:h,touched:k,errors:V}=e;function _(e){var r,o,a;const u=null!==(r=null==e?void 0:e.onSubmit)&&void 0!==r?r:t.onSubmit,l=null!==(o=null==e?void 0:e.validate)&&void 0!==o?o:t.validate,c=null!==(a=null==e?void 0:e.onError)&&void 0!==a?a:t.onError;return async function(r){null==r||r.preventDefault(),v.set(!0);const o=M(h),a=await L(o,l);if(k.update((e=>i(e,!0))),a&&(V.set(a),m(a,(e=>!!e))))return n.forEach((e=>{var t;return null===(t=null==e?void 0:e.onSubmitError)||void 0===t?void 0:t.call(e,{data:o,errors:a})})),void v.set(!1);try{await u(o,{form:w,controls:w&&Array.from(w.elements).filter(A),config:Object.assign(Object.assign({},t),e)})}catch(e){if(!c)throw e;const t=c(e);t&&(V.set(t),n.forEach((e=>{var n;return null===(n=null==e?void 0:e.onSubmitError)||void 0===n?void 0:n.call(e,{data:o,errors:t})})))}finally{v.set(!1)}}}function D(e,t){const n=void 0===t?e:`${e}[${t}]`;k.update((e=>d(e,n,!0)))}function I(e){h.set(o(e)),w&&$(w,e)}let w,P=null!==(u=t.initialValues)&&void 0!==u?u:{};const C=_();return{handleSubmit:C,createSubmitHandler:_,reset:function(){I(o(P)),k.update((e=>i(e,!1)))},setTouched:D,setError:function(e,t){V.update((n=>d(n,e,t)))},setField:function(e,t,n=!0){if(h.update((n=>d(n,e,t))),n&&D(e),w)for(const n of w.elements){if(!A(n)||!n.name)continue;const r=O(n);if(e===r)return void T(n,t)}},setFields:I,validate:async function(){const e=M(h);k.update((e=>i(e,!0)));const n=await L(e,t.validate);return V.set(n||{}),n},form:function(e){function u(n){return n({form:e,controls:Array.from(e.elements).filter(A),data:h,errors:V,touched:k,config:t,addValidator:a})}function v(){for(const t of Array.from(e.elements).filter(A)){if(j(t))continue;let e="value";p(t)&&["checkbox","radio"].includes(t.type)&&(e="checked"),p(t)&&"file"===t.type&&(e="files");const n=Object.getOwnPropertyDescriptor(b(t)?HTMLSelectElement.prototype:g(t)?HTMLTextAreaElement.prototype:HTMLInputElement.prototype,e);Object.defineProperty(t,e,{configurable:!0,set(e){var r;if(null===(r=null==n?void 0:n.set)||void 0===r||r.call(t,e),p(t)){if("checkbox"===t.type)return T(t);if("radio"===t.type)return $(t);if("file"===t.type)return L(t)}const o=b(t)?t.value:N(t);h.update((e=>d(e,O(t),o)))},get:null==n?void 0:n.get})}}e.requestSubmit||(e.requestSubmit=C),n=r.map(u),e.noValidate=!!t.validate;const{defaultData:m}=F(e);function T(t){const n=y(t),r=O(t),o=Array.from(e.querySelectorAll(`[name="${t.name}"]`)).filter((e=>!!A(e)&&(void 0!==n?Number(e.dataset.felteIndex)===n:r===O(e))));return 1===o.length?h.update((e=>d(e,O(t),t.checked))):h.update((e=>d(e,O(t),o.filter(p).filter((e=>e.checked)).map((e=>e.value)))))}function $(t){const n=e.querySelectorAll(`[name="${t.name}"]`),r=Array.from(n).find((e=>p(e)&&e.checked));h.update((e=>d(e,O(t),null==r?void 0:r.value)))}function L(e){const t=e.files;h.update((n=>d(n,O(e),e.multiple?Array.from(null!=t?t:[]):null==t?void 0:t[0])))}function _(e){var n;const r=e.target;if(!r||!A(r)||b(r)||j(r))return;if(["checkbox","radio","file"].includes(r.type))return;if(!r.name)return;(null===(n=t.touchTriggerEvents)||void 0===n?void 0:n.input)&&D(O(r));const o=N(r);h.update((e=>d(e,O(r),o)))}function M(e){var n;const r=e.target;r&&A(r)&&!j(r)&&r.name&&((null===(n=t.touchTriggerEvents)||void 0===n?void 0:n.change)&&D(O(r)),b(r)&&h.update((e=>d(e,O(r),r.value))),p(r)&&("checkbox"===r.type&&T(r),"radio"===r.type&&$(r),"file"===r.type&&L(r)))}function R(e){var n;const r=e.target;r&&A(r)&&!j(r)&&r.name&&(null===(n=t.touchTriggerEvents)||void 0===n?void 0:n.blur)&&D(O(r))}function q(e){for(const t of e)"true"===t.dataset.felteUnsetOnRemove&&h.update((e=>f(e,S(t))))}w=e,P=c(o(m),P),I(P),k.set(i(P,!1));const H=new MutationObserver((function(t){for(const o of t)if("childList"===o.type){if(o.addedNodes.length>0){if(v(),!Array.from(o.addedNodes).some((e=>!!E(e)&&(!!A(e)||x(e).length>0))))continue;n.forEach((e=>{var t;return null===(t=null==e?void 0:e.destroy)||void 0===t?void 0:t.call(e)})),n=r.map(u);const{defaultData:t}=F(e),a=i(t,!1);h.update((e=>l(e,t))),k.update((e=>l(e,a)))}if(o.removedNodes.length>0)for(const e of o.removedNodes){if(!E(e))continue;const t=x(e);0!==t.length&&(n.forEach((e=>{var t;return null===(t=null==e?void 0:e.destroy)||void 0===t?void 0:t.call(e)})),n=r.map(u),q(t))}}}));H.observe(e,{childList:!0,subtree:!0}),v(),e.addEventListener("input",_),e.addEventListener("change",M),e.addEventListener("focusout",R),e.addEventListener("submit",C);const U=V.subscribe((t=>{for(const n of e.elements){if(!A(n)||!n.name)continue;const e=s(t,O(n)),r=Array.isArray(e)?e.join("\n"):"string"==typeof e?e:void 0;r!==n.dataset.felteValidationMessage&&(r?n.dataset.felteValidationMessage=r:delete n.dataset.felteValidationMessage)}}));return{destroy(){H.disconnect(),e.removeEventListener("input",_),e.removeEventListener("change",M),e.removeEventListener("focusout",R),e.removeEventListener("submit",C),U(),n.forEach((e=>{var t;return null===(t=null==e?void 0:e.destroy)||void 0===t?void 0:t.call(e)}))}}}}}({currentExtenders:k,extender:h,config:e,addValidator:v,stores:{data:_,errors:D,touched:I,isValid:w,isSubmitting:V}});function C(e,t){if(!r(e))return e!==t}function R(e,t){if(!r(e))return t||e}return Object.assign({data:Object.assign(Object.assign({},_),{set:function(t){return I.update((n=>{const r=a(o(t),e.initialValues,C);return a(r,n,R)})),_.set(t)}}),errors:D,touched:I,isValid:w,isSubmitting:V},P)},e.deepSet=i,e.deepSome=m,e.executeValidation=L,e.getFormControls=x,e.getFormDefaultValues=F,e.getIndex=y,e.getInputTextOrNumber=N,e.getPath=O,e.getPathFromDataset=S,e.isElement=E,e.isFieldSetElement=h,e.isFieldValue=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},e.isFormControl=A,e.isInputElement=p,e.isSelectElement=b,e.isTextAreaElement=g,e.setControlValue=T,e.setForm=$,e.shouldIgnore=j,Object.defineProperty(e,"__esModule",{value:!0})})); | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "@felte/core", | ||
"version": "0.2.2", | ||
"version": "0.2.3", | ||
"description": "Core package for FelteJS", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.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
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
232220