-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdist.js
1 lines (1 loc) · 2.9 KB
/
dist.js
1
import{h as e,text as r}from"https://unpkg.com/hyperapp";var t=t=>({errors:a})=>{let s=Object.entries(a).reduce((e,[r,t])=>e||t,"");return e("p",{...t,class:"error",hidden:!s},[r(s)])},a=(e,r,t)=>({values:a,errors:s,submitted:o,register:l,SetValues:n,SetErrors:i})=>{r&&l(t=>({...t,[e]:r(a[e],a)}));const u=r?(t,o)=>[i,{...s,[e]:r(o,a)}]:e=>e;return t({disabled:o,value:a[e],error:s[e],Set:(r,t)=>[n,{...a,[e]:t}],Validate:u})};const s=(o=(e,{f:r,a:t})=>r(e,...t),e=>(...r)=>[o,{f:e,a:r}]);var o;const l=s((e,r)=>r.preventDefault()),n=s((e,r,t)=>e(r,t));var i=(...e)=>(r,t)=>[r,...e.map(e=>n(e,t))],u=r=>"checkbox"===r.type?(r=>a(r.name,r.validator,({value:t,error:a,disabled:s,Set:o,Validate:l},n=r.value||"on")=>e("input",{...r,type:"checkbox",class:[r.class,{error:a}],disabled:s,checked:Array.isArray(t)?t.indexOf(n)>=0:t===n,name:r.name,value:n,onchange:(e,r,a=[...(t?Array.isArray(t)?t:[t]:[]).filter(e=>e!==n),...r.target.checked?[n]:[]])=>[i(o,l),a.length>1?a:1==a.length?a[0]:""]})))(r):"radio"===r.type?(r=>a(r.name,r.validator,({value:t,error:a,disabled:s,Set:o,Validate:l})=>e("input",{...r,type:"radio",class:[r.class,{error:a}],value:r.value||"on",disabled:s,checked:t===(r.value||"on"),onchange:[i(o,l),r.value||"on"]})))(r):a(r.name,r.validator,({value:t,error:a,disabled:s,Set:o,Validate:l})=>e("input",{...r,disabled:s,type:r.type||"text",class:[r.class,{error:!!a}],value:t,oninput:(e,r)=>[a?i(o,l):o,r.target.value],...void 0===t?{}:{onblur:[l,t]}})),d=(r,t)=>({submitted:a})=>e("button",{...r,type:r.type||"button",disabled:a},t),c=(e={},r={})=>({values:e,errors:r,submitted:!1});const m=(e,{event:r,onsubmit:t,validators:a,getFormState:s,setFormState:o})=>{let i=s(e);if(i.submitted)return e;let u=a.reduce((e,r)=>r(e),{}),d=Object.entries(u).reduce((e,[r,t])=>e&&!t,!0);return[o(e,{...i,errors:u,submitted:d}),d&&n(t,i.values),l(r)]},v=(e,r,t,a=r.getFormState(e))=>a.submitted?e:[r.setFormState,{...a,[t]:r[t]}],p=(e,r)=>v(e,r,"values"),b=(e,r)=>v(e,r,"errors"),S=(e,r)=>r?Array.isArray(r)?r.map(r=>S(e,r)).flat():"function"==typeof r?S(e,r(e)):r.children?{...r,children:S(e,r.children)}:r:r,h=(e,r,t,a)=>{const s=(o=e,l=r,(e,r)=>(t,a)=>[e,{...r(a),getFormState:o,setFormState:l}]);var o,l;return{Submit:s(m,e=>({event:e,onsubmit:t,validators:a})),SetValues:s(p,e=>({values:e})),SetErrors:s(b,e=>({errors:e}))}};var y=({state:r,getFormState:t,setFormState:a,onsubmit:s},o)=>{const l=[],{Submit:n,SetValues:i,SetErrors:u}=h(t,a,s,l);return e("form",{onsubmit:n},S({...r,register:e=>l.push(e),SetValues:i,SetErrors:u},o))},g=(r,t)=>a(r.name,r.validator,({value:a,error:s,disabled:o,Set:l,Validate:n})=>e("select",{...r,class:[r.class,{error:s}],name:r.name,value:a,disabled:o,oninput:(e,r)=>[i(l,n),r.target.value]},t.map(e=>"option"!==e.name||!a||e.props.value!==a&&e.children[0].name!==a?e:{...e,props:{...e.props,selected:!0}})));export{i as batch,d as button,t as error,y as form,c as init,u as input,S as provide,g as select,a as widget};