UNPKG

@sparkstone/solid-validation

Version:

A lightweight form validation library for Solid.js with PocketBase integration.

3 lines (2 loc) 4.48 kB
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports,require("solid-js"),require("solid-js/store")):"function"==typeof define&&define.amd?define(["exports","solid-js","solid-js/store"],e):e((t||self).solidValidation={},t.solidJs,t.store)}(this,function(t,e,n){const r="undefined"!=typeof Symbol?Symbol.iterator||(Symbol.iterator=Symbol("Symbol.iterator")):"@@iterator";function i(t,e,n){if(!t.s){if(n instanceof o){if(!n.s)return void(n.o=i.bind(null,t,e));1&e&&(e=n.s),n=n.v}if(n&&n.then)return void n.then(i.bind(null,t,e),i.bind(null,t,2));t.s=e,t.v=n;var r=t.o;r&&r(t)}}var o=/*#__PURE__*/function(){function t(){}return t.prototype.then=function(e,n){var r=new t,o=this.s;if(o){var u=1&o?e:n;if(u){try{i(r,1,u(this.v))}catch(t){i(r,2,t)}return r}return this}return this.o=function(t){try{var o=t.v;1&t.s?i(r,1,e?e(o):o):n?i(r,1,n(o)):i(r,2,o)}catch(t){i(r,2,t)}},r},t}();function u(t){return t instanceof o&&1&t.s}function a(t,e,n){var r,a,s=-1;return function l(c){try{for(;++s<t.length&&(!n||!n());)if((c=e(s))&&c.then){if(!u(c))return void c.then(l,a||(a=i.bind(null,r=new o,2)));c=c.v}r?i(r,1,c):r=c}catch(t){i(r||(r=new o),2,t)}}(),r}function s(t,e,n){var s=t.element,l=t.validators,c=void 0===l?[]:l;return function(){try{var t=function(){var t,r;return l&&(n&&s.classList.toggle(n,!0),s.setAttribute("aria-invalid","true"),e(((r={})[null!=(t=s.name)?t:s.dataset.name]=l,r))),l};null==s.setCustomValidity||s.setCustomValidity(""),null==s.checkValidity||s.checkValidity();var l=s.validationMessage,f=function(){if(!l){var t,e=function(t,e,n){if("function"==typeof t[r]){var s,l,c,f=function(t){try{for(;!((s=v.next()).done||n&&n());)if((t=e(s.value))&&t.then){if(!u(t))return void t.then(f,c||(c=i.bind(null,l=new o,2)));t=t.v}l?i(l,1,t):l=t}catch(t){i(l||(l=new o),2,t)}},v=t[r]();if(f(),v.return){var d=function(t){try{s.done||v.return()}catch(t){}return t};if(l&&l.then)return l.then(d,function(t){throw d(t)});d()}return l}if(!("length"in t))throw new TypeError("Object is not iterable");for(var m=[],h=0;h<t.length;h++)m.push(t[h]);return a(m,function(t){return e(m[t])},n)}(c,function(e){if(e)return Promise.resolve(e(s)).then(function(e){e&&(l=e,null==s.setCustomValidity||s.setCustomValidity(e),t=1)})},function(){return t});if(e&&e.then)return e.then(function(){})}}();return Promise.resolve(f&&f.then?f.then(t):t())}catch(t){return Promise.reject(t)}}}t.useForm=function(t){var r=function(t,e){try{var n=function(){if(!r)return d(!0),Promise.resolve(t(e)).then(function(t){if(d(!1),t instanceof Object){for(var e in t)e in b&&b[e].element.setAttribute("aria-invalid","true");c(t)}else p(),h(!0)})},r=!1,i=function(t){var e=[];for(var n in t)e.push(n);return a(e,function(t){return function(t){var e=b[t];if(e)return Promise.resolve(s(e,c,o)()).then(function(n){r||!e.element.validationMessage&&!n||(e.element.focus(),e.element.scrollIntoView({behavior:"smooth"}),document.contains(e.element)?r=!0:delete b[t])})}(e[t])},void 0)}(b);return Promise.resolve(i&&i.then?i.then(n):n())}catch(t){return Promise.reject(t)}},i=(void 0===t?{}:t).errorClass,o=void 0===i?"":i,u=n.createStore({}),l=u[0],c=u[1],f=e.createSignal(!1),v=f[0],d=f[1],m=e.createSignal(!1),h=m[1],b={},y=function(t,e){void 0===e&&(e=function(){}),queueMicrotask(function(){var n,r,i=null!=(n=t.name)?n:t.dataset.name,u=e(),a=Array.isArray(u)?u:[];b[i]=r={element:t,validators:a},t.onblur=function(){return h(!1),s(r,c,o)},t.oninput=function(){var e;h(!1),l[i]&&(c(((e={})[i]=void 0,e)),t.setAttribute("aria-invalid","false"),o&&t.classList.toggle(o,!1))}})};function p(){c(function(t){return Object.fromEntries(Object.entries(t).map(function(t){return[t[0],void 0]}))})}return{validate:y,formSubmit:function(t,e){var n=e()||function(){};h(!1),t.setAttribute("novalidate",""),t.onsubmit=function(e){try{return e.preventDefault(),Promise.resolve(r(n,t)).then(function(){})}catch(e){return Promise.reject(e)}},t.onreset=function(){p()}},submit:r,errors:l,isSubmitting:v,isSubmitted:m[0],validateField:function(t){try{var e=b[t];return e?Promise.resolve(s(e,c,o)()).then(function(n){return(e.element.validationMessage||n)&&(e.element.focus(),e.element.scrollIntoView({behavior:"smooth"})),!l[t]}):Promise.resolve(!1)}catch(t){return Promise.reject(t)}},getFieldValue:function(t){var e=b[t];return null==e?void 0:e.element.value},validateRef:function(){var t=arguments;return function(e){return y(e,function(){return[].slice.call(t)})}}}}}); //# sourceMappingURL=main.umd.js.map