@sciphergfx/json-fields
Version:
UI-agnostic React component for generating Form Fields from JSON with support for Chakra UI, Tailwind CSS, and shadcn/ui
3 lines (2 loc) • 27.6 kB
JavaScript
import{useState as e,useCallback as t,useEffect as r}from"react";import{jsx as n,jsxs as o}from"react/jsx-runtime";function a(e,t){(null==t||t>e.length)&&(t=e.length);for(var r=0,n=Array(t);r<t;r++)n[r]=e[r];return n}function i(e,t,r){return(t=function(e){var t=function(e,t){if("object"!=typeof e||!e)return e;var r=e[Symbol.toPrimitive];if(void 0!==r){var n=r.call(e,t);if("object"!=typeof n)return n;throw new TypeError("@@toPrimitive must return a primitive value.")}return("string"===t?String:Number)(e)}(e,"string");return"symbol"==typeof t?t:t+""}(t))in e?Object.defineProperty(e,t,{value:r,enumerable:!0,configurable:!0,writable:!0}):e[t]=r,e}function l(e,t){var r=Object.keys(e);if(Object.getOwnPropertySymbols){var n=Object.getOwnPropertySymbols(e);t&&(n=n.filter(function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable})),r.push.apply(r,n)}return r}function c(e){for(var t=1;t<arguments.length;t++){var r=null!=arguments[t]?arguments[t]:{};t%2?l(Object(r),!0).forEach(function(t){i(e,t,r[t])}):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(r)):l(Object(r)).forEach(function(t){Object.defineProperty(e,t,Object.getOwnPropertyDescriptor(r,t))})}return e}function s(e,t){return function(e){if(Array.isArray(e))return e}(e)||function(e,t){var r=null==e?null:"undefined"!=typeof Symbol&&e[Symbol.iterator]||e["@@iterator"];if(null!=r){var n,o,a,i,l=[],c=!0,s=!1;try{if(a=(r=r.call(e)).next,0===t);else for(;!(c=(n=a.call(r)).done)&&(l.push(n.value),l.length!==t);c=!0);}catch(e){s=!0,o=e}finally{try{if(!c&&null!=r.return&&(i=r.return(),Object(i)!==i))return}finally{if(s)throw o}}return l}}(e,t)||f(e,t)||function(){throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}()}function u(e){return function(e){if(Array.isArray(e))return a(e)}(e)||function(e){if("undefined"!=typeof Symbol&&null!=e[Symbol.iterator]||null!=e["@@iterator"])return Array.from(e)}(e)||f(e)||function(){throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}()}function d(e){return d="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e},d(e)}function f(e,t){if(e){if("string"==typeof e)return a(e,t);var r={}.toString.call(e).slice(8,-1);return"Object"===r&&e.constructor&&(r=e.constructor.name),"Map"===r||"Set"===r?Array.from(e):"Arguments"===r||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(r)?a(e,t):void 0}}var m=function(e){var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:"",r={};for(var n in e)if(Object.prototype.hasOwnProperty.call(e,n)){var o=t?"".concat(t,".").concat(n):n;"object"!==d(e[n])||null===e[n]||Array.isArray(e[n])?r[o]=e[n]:Object.assign(r,m(e[n],o))}return r},p=function(e){var t={};for(var r in e){for(var n=r.split("."),o=t,a=0;a<n.length-1;a++)o[n[a]]||(o[n[a]]={}),o=o[n[a]];o[n[n.length-1]]=e[r]}return t},g=function(e){var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:"",r=arguments.length>2&&void 0!==arguments[2]?arguments[2]:{};if(r[t])return c({type:r[t].type},r[t]);if("boolean"==typeof e)return{type:"checkbox"};if("number"==typeof e)return{type:"number"};if(Array.isArray(e))return{type:"array",items:e};if("object"===d(e)&&null!==e)return{type:"object"};if("string"==typeof e){if(e.length>100)return{type:"textarea",rows:4};if(e.includes("@")||t.toLowerCase().includes("email"))return{type:"email"};if(e.startsWith("http")||t.toLowerCase().includes("url"))return{type:"url"};if(e.match(/^\d{4}-\d{2}-\d{2}$/)||t.toLowerCase().includes("date"))return{type:"date"};if(t.toLowerCase().includes("password"))return{type:"password"}}return{type:"text"}},y=function(e){try{return{success:!0,data:JSON.parse(e)}}catch(e){return{success:!1,error:e.message}}},b=function(e){var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:2;return JSON.stringify(e,null,t)},h=function(e){return e.split(".").length-1},v=function(e){return e.split(".").pop().replace(/([A-Z])/g," $1").replace(/[_-]/g," ").replace(/\b\w/g,function(e){return e.toUpperCase()}).trim()},x=["_classNames","styles","renderers","uiLibrary","onSave","onCancel","onFieldChange","saveButtonText","cancelButtonText","initialJson","customStyles","showControls","columns","fieldConfig","sections","includeUnsectioned","unsectionedTitle"],k=function(a){a._classNames,a.styles;var l=a.renderers,f=void 0===l?{}:l;a.uiLibrary;var b=a.onSave,h=a.onCancel,k=a.onFieldChange,w=a.saveButtonText,B=void 0===w?"Save Changes":w,S=a.cancelButtonText,C=void 0===S?"Reset Form":S,N=a.initialJson,T=void 0===N?"":N,O=a.customStyles,A=void 0===O?{}:O,j=a.showControls,L=void 0===j||j,I=a.columns,H=void 0===I?2:I,W=a.fieldConfig,P=void 0===W?{}:W,E=a.sections,V=void 0===E?null:E,F=a.includeUnsectioned,D=void 0!==F&&F,G=a.unsectionedTitle,J=void 0===G?"Other":G,z=function(e,t){if(null==e)return{};var r,n,o=function(e,t){if(null==e)return{};var r={};for(var n in e)if({}.hasOwnProperty.call(e,n)){if(-1!==t.indexOf(n))continue;r[n]=e[n]}return r}(e,t);if(Object.getOwnPropertySymbols){var a=Object.getOwnPropertySymbols(e);for(n=0;n<a.length;n++)r=a[n],-1===t.indexOf(r)&&{}.propertyIsEnumerable.call(e,r)&&(o[r]=e[r])}return o}(a,x),M=s(e(T),2),R=M[0],_=M[1],U=s(e({}),2),$=U[0],K=U[1],Z=s(e({}),2),q=Z[0],Q=Z[1],X=s(e(null),2)[1],Y=s(e(""),2),ee=Y[0],te=Y[1],re=s(e({}),2),ne=re[0],oe=re[1],ae=s(e(function(){return new Set}),2),ie=ae[0],le=ae[1],ce={Container:f.Container||"div",Box:f.Box||"div",Button:f.Button||"button",Input:f.Input||"input",Select:f.Select||"select",Textarea:f.Textarea||"textarea",Text:f.Text||"span",Heading:f.Heading||"h2",VStack:f.VStack||"div",HStack:f.HStack||"div",Card:f.Card||"div",Alert:f.Alert||"div",Label:f.Label||"label"},se=function(e,t,r){var a=arguments.length>3&&void 0!==arguments[3]?arguments[3]:{},l=Array.isArray(t),f=!l&&"object"===d(t)&&null!==t,m=l?t:f?Object.entries(t).map(function(e){var t=s(e,2);return{key:t[0],value:t[1]}}):[],p=[];if(m.length){var g=new Set;m.forEach(function(e){return Object.keys(e||{}).forEach(function(e){return g.add(e)})}),p=Array.from(g)}else p=Array.isArray(a.columns)?a.columns:["key","value"];var y=function(t){if(l)fe(e,t);else if(f){var r={};t.forEach(function(e){var t,n,o=null!==(t=e.key)&&void 0!==t?t:e.name,a=null!==(n=e.value)&&void 0!==n?n:e.val;void 0!==o&&""!==o&&(r[o]=a)}),fe(e,r)}else fe(e,t)},b={display:"grid",gridTemplateColumns:"".concat("1fr ".repeat(p.length),"auto").trim(),gap:"0.5rem",alignItems:"center",padding:"3px 0px"};return o(ce.Box,{className:ue(),style:c({marginBottom:"1rem"},A.fieldContainer),children:[n(ce.Label,{className:ue(),style:c({display:"block",fontWeight:"600",marginBottom:"0.5rem"},A.fieldLabel),children:r}),!1!==a.showHeader&&o(ce.Box,{style:c(c({},b),{},{fontSize:"12px",opacity:.8}),children:[p.map(function(e){return n(ce.Text,{children:e},"hdr-".concat(e))}),n("span",{})]}),o(ce.VStack,{style:{gap:"0.5rem"},children:[m.map(function(t,a){return o(ce.Box,{style:b,children:[p.map(function(r){var o;return n(ce.Input,{type:"text",placeholder:r,value:null!==(o=null==t?void 0:t[r])&&void 0!==o?o:"",onChange:function(e){return t=a,n=r,o=e.target.value,l=m.map(function(e,r){return r===t?c(c({},e),{},i({},n,o)):e}),void y(l);var t,n,o,l},className:ue(),style:A.input},"".concat(e,"-").concat(a,"-").concat(r))}),n(ce.Button,{"aria-label":"Remove ".concat(r," row ").concat(a+1),onClick:function(){return function(e){var t=m.filter(function(t,r){return r!==e});y(t)}(a)},className:ue(),style:{padding:"6px 10px"},children:"×"})]},"".concat(e,"-").concat(a))}),o(ce.Button,{onClick:function(){var e=p.reduce(function(e,t){return c(c({},e),{},i({},t,""))},{});y([].concat(u(m),[e]))},className:ue(),style:{alignSelf:"flex-start",padding:"6px 10px"},children:["+ Add ",r]})]})]},e)},ue=function(){return""},de=t(function(){var e=y(arguments.length>0&&void 0!==arguments[0]?arguments[0]:R);if(e.success){X(e.data);var t=m(e.data);K(t),Q(c({},t)),te("")}else te(e.error),X(null),K({}),Q({})},[R]);r(function(){T?(_(T),de(T)):""===T&&(_(""),K({}),Q({}),X(null),te(""))},[T,de]);var fe=function(e,t){var r=c(c({},$),{},i({},e,t));if(K(r),k){var n=p(r);k(e,t,n)}},me=function(e,t){var r=g(t,e,P),n=v(e);switch(r.type){case"checkbox":return ge(e,t,n);case"select":return ye(e,t,n,r);case"multi-select":return be(e,t,n,r);case"textarea":return he(e,t,n,r);case"email":case"url":case"date":case"password":return ve(e,t,n,r);case"number":return xe(e,t,n);case"slider":return pe(e,t,n,r);case"tags":return we(e,t,n,r);case"key-value-list":return se(e,t,n,r);case"array":return ke(e,t,n,r);case"object":return Be(e,t,n);default:return Se(e,t,n)}},pe=function(e,t,r,a){var i,l,s,u,d=null!==(i=a.min)&&void 0!==i?i:0,f=null!==(l=a.max)&&void 0!==l?l:100,m=null!==(s=a.step)&&void 0!==s?s:1,p="number"==typeof $[e]?$[e]:null!==(u=a.default)&&void 0!==u?u:d;return o(ce.Box,{className:ue(),style:c({marginBottom:"1rem"},A.fieldContainer),children:[n(ce.Label,{className:ue(),style:A.label,children:n(ce.Text,{className:ue(),style:c({fontWeight:"500"},A.fieldLabel),children:r})}),o("div",{style:{display:"flex",alignItems:"center",gap:"0.75rem"},children:[n("input",{type:"range",min:d,max:f,step:m,value:p,onChange:function(t){return fe(e,Number(t.target.value))},className:ue(),style:{width:"100%"}}),n(ce.Text,{className:ue(),style:{minWidth:"3rem",textAlign:"right"},children:p})]})]},e)},ge=function(e,t,r){return n(ce.Box,{className:ue(),style:c({marginBottom:"1rem"},A.fieldContainer),children:o(ce.Label,{className:ue(),style:c({display:"flex",alignItems:"center",gap:"0.5rem",cursor:"pointer"},A.label),children:[n("input",{type:"checkbox",checked:$[e]||!1,onChange:function(t){return fe(e,t.target.checked)},className:ue(),style:A.checkbox}),n(ce.Text,{className:ue(),style:c({fontWeight:"500"},A.fieldLabel),children:r})]})},e)},ye=function(e,t,r,a){var i;return o(ce.Box,{className:ue(),style:c({marginBottom:"1rem"},A.fieldContainer),children:[n(ce.Label,{className:ue(),style:c({display:"block",fontWeight:"600",marginBottom:"0.5rem"},A.fieldLabel),children:r}),o(ce.Select,{value:$[e]||"",onChange:function(t){return fe(e,t.target.value)},className:ue(),style:c({width:"100%"},A.select),children:[o("option",{value:"",children:["Select ",r]}),null===(i=a.options)||void 0===i?void 0:i.map(function(e){return n("option",{value:e,children:e},e)})]})]},e)},be=function(e,t,r,a){var i,l=Array.isArray($[e])?$[e]:[];return o(ce.Box,{className:ue(),style:c({marginBottom:"1rem"},A.fieldContainer),children:[n(ce.Label,{className:ue(),style:c({display:"block",fontWeight:"600",marginBottom:"0.5rem"},A.fieldLabel),children:r}),n(ce.Box,{style:{display:"flex",flexDirection:"column",gap:"0.5rem"},children:null===(i=a.options)||void 0===i?void 0:i.map(function(t){return o(ce.Label,{style:{display:"flex",alignItems:"center",gap:"0.5rem",cursor:"pointer"},children:[n("input",{type:"checkbox",checked:l.includes(t),onChange:function(r){var n=r.target.checked?[].concat(u(l),[t]):l.filter(function(e){return e!==t});fe(e,n)},style:A.checkbox}),n(ce.Text,{style:c({fontSize:"14px"},A.text),children:t})]},t)})})]},e)},he=function(e,t,r,a){return o(ce.Box,{className:ue(),style:c({marginBottom:"1rem"},A.fieldContainer),children:[n(ce.Label,{className:ue(),style:c({display:"block",fontWeight:"600",marginBottom:"0.5rem"},A.fieldLabel),children:r}),n(ce.Textarea,{value:$[e]||"",onChange:function(t){return fe(e,t.target.value)},className:ue(),style:c({width:"100%",minHeight:"".concat(1.5*(a.rows||4),"rem")},A.textarea)})]},e)},ve=function(e,t,r,a){return o(ce.Box,{className:ue(),style:c({marginBottom:"1rem"},A.fieldContainer),children:[n(ce.Label,{className:ue(),style:c({display:"block",fontWeight:"600",marginBottom:"0.5rem"},A.fieldLabel),children:r}),n(ce.Input,{type:a.type,value:$[e]||"",onChange:function(t){return fe(e,t.target.value)},className:ue(),style:c({width:"100%"},A.input)})]},e)},xe=function(e,t,r){return o(ce.Box,{className:ue(),style:c({marginBottom:"1rem"},A.fieldContainer),children:[n(ce.Label,{className:ue(),style:c({display:"block",fontWeight:"600",marginBottom:"0.5rem"},A.fieldLabel),children:r}),n(ce.Input,{type:"number",value:$[e]||"",onChange:function(t){var r=parseFloat(t.target.value)||0;fe(e,r)},className:ue(),style:c({width:"100%"},A.input)})]},e)},ke=function(e,t,r,a){if((Array.isArray(t)?t:[]).every(function(e){return"string"==typeof e})){var l=Array.isArray($[e])?$[e]:[],s=ne[e]||"",d=function(t){var r=(t||"").trim();r&&(l.includes(r)||(fe(e,[].concat(u(l),[r])),oe(function(t){return c(c({},t),{},i({},e,""))})))},f=function(t){fe(e,l.filter(function(e){return e!==t}))},m={display:"inline-flex",alignItems:"center",gap:"6px",padding:"4px 8px",borderRadius:"999px",background:"#eef2ff",color:"#3730a3",border:"1px solid #c7d2fe",fontSize:"12px"},p={cursor:"pointer",border:"none",background:"transparent",color:"#4338ca",fontSize:"14px",lineHeight:1};return o(ce.Box,{className:ue(),style:c({marginBottom:"1rem"},A.fieldContainer),children:[n(ce.Label,{className:ue(),style:c({display:"block",fontWeight:"600",marginBottom:"0.5rem"},A.fieldLabel),children:r}),o(ce.Box,{style:{display:"flex",flexWrap:"wrap",gap:"8px",alignItems:"center"},children:[l.map(function(e){return o("span",{style:m,children:[e,n("button",{"aria-label":"Remove ".concat(e),onClick:function(){return f(e)},style:p,children:"×"})]},e)}),n(ce.Input,{type:"text",value:s,onChange:function(t){return oe(function(r){return c(c({},r),{},i({},e,t.target.value))})},onKeyDown:function(e){"Enter"===e.key||","===e.key?(e.preventDefault(),d(s)):"Backspace"===e.key&&!s&&l.length&&f(l[l.length-1])},placeholder:a.placeholder||"Add ".concat(r," and press Enter"),className:ue(),style:c({minWidth:"140px",flex:"0 1 auto"},A.input)}),n(ce.Button,{onClick:function(){return d(s)},className:ue(),style:{padding:"6px 10px"},children:"Add"})]})]},e)}return o(ce.Box,{className:ue(),style:c({marginBottom:"1rem"},A.fieldContainer),children:[o(ce.Label,{className:ue(),style:c({display:"block",fontWeight:"600",marginBottom:"0.5rem"},A.fieldLabel),children:[r," (Array)"]}),n(ce.Textarea,{value:JSON.stringify(t,null,2),onChange:function(t){try{var r=JSON.parse(t.target.value);fe(e,r)}catch(r){fe(e,t.target.value)}},className:ue(),style:c({fontFamily:"monospace",minHeight:"100px",width:"100%"},A.textarea)})]},e)},we=function(e,t,r,a){var l=Array.isArray($[e])?$[e]:[],s=ne[e]||"",d=function(t){var r=(t||"").trim();r&&(l.includes(r)||(fe(e,[].concat(u(l),[r])),oe(function(t){return c(c({},t),{},i({},e,""))})))},f=function(t){fe(e,l.filter(function(e){return e!==t}))},m={display:"inline-flex",alignItems:"center",gap:"6px",padding:"4px 8px",borderRadius:"999px",background:"#eef2ff",color:"#3730a3",border:"1px solid #c7d2fe",fontSize:"12px"},p={cursor:"pointer",border:"none",background:"transparent",color:"#4338ca",fontSize:"14px",lineHeight:1};return o(ce.Box,{className:ue(),style:c({marginBottom:"1rem"},A.fieldContainer),children:[n(ce.Label,{className:ue(),style:c({display:"block",fontWeight:"600",marginBottom:"0.5rem"},A.fieldLabel),children:r}),o(ce.Box,{style:{display:"flex",flexWrap:"wrap",gap:"8px",alignItems:"center"},children:[l.map(function(e){return o("span",{style:m,children:[e,n("button",{"aria-label":"Remove ".concat(e),onClick:function(){return f(e)},style:p,children:"×"})]},e)}),n(ce.Input,{type:"text",value:s,onChange:function(t){return oe(function(r){return c(c({},r),{},i({},e,t.target.value))})},onKeyDown:function(e){"Enter"===e.key||","===e.key?(e.preventDefault(),d(s)):"Backspace"===e.key&&!s&&l.length&&f(l[l.length-1])},placeholder:a.placeholder||"Add tag and press Enter",className:ue(),style:c({minWidth:"140px",flex:"0 1 auto"},A.input)}),n(ce.Button,{onClick:function(){return d(s)},className:ue(),style:{padding:"6px 10px"},children:"Add"})]})]},e)},Be=function(e,t,r){return o(ce.Box,{className:ue(),style:c({marginBottom:"1rem"},A.fieldContainer),children:[o(ce.Label,{className:ue(),style:c({display:"block",fontWeight:"600",marginBottom:"0.5rem"},A.fieldLabel),children:[r," (Object)"]}),n(ce.Textarea,{value:JSON.stringify(t,null,2),onChange:function(t){try{var r=JSON.parse(t.target.value);fe(e,r)}catch(r){fe(e,t.target.value)}},className:ue(),style:c({fontFamily:"monospace",minHeight:"100px",width:"100%"},A.textarea)})]},e)},Se=function(e,t,r){return o(ce.Box,{className:ue(),style:c({marginBottom:"1rem"},A.fieldContainer),children:[n(ce.Label,{className:ue(),style:c({display:"block",fontWeight:"600",marginBottom:"0.5rem"},A.fieldLabel),children:r}),n(ce.Input,{type:"text",value:$[e]||"",onChange:function(t){return fe(e,t.target.value)},className:ue(),style:c({width:"100%"},A.input)})]},e)};return n(ce.Container,c(c({className:ue(),style:A.container},z),{},{children:o(ce.VStack,{className:ue(),style:c({gap:"1rem"},A.stack),children:[ee&&n(ce.Alert,{className:ue(),style:A.alert,children:n(ce.Text,{style:A.errorText,children:ee})}),Object.keys($).length>0&&n(ce.Card,{className:ue(),style:c({width:"100%",padding:"1rem"},A.formCard),children:function(){if(Array.isArray(V)&&V.length){var e=Object.keys($),t=new Set,r=V.map(function(r,a){var i=r.id||"section-".concat(a),l=Array.isArray(r.fields)?r.fields:[];l.forEach(function(e){return t.add(e)});var s=l.filter(function(t){return e.includes(t)}),u=function(){if(H<=1)return n(ce.VStack,{style:{gap:"1rem"},children:s.map(function(e){return me(e,$[e])})},i);for(var e=Math.ceil(s.length/H),t=[],r=0;r<H;r++){var o=r*e,a=Math.min(o+e,s.length);t.push(s.slice(o,a))}var l=c({display:"grid",gridTemplateColumns:"repeat(".concat(H,", 1fr)"),gap:"2rem",width:"100%"},A.formGrid);return n(ce.Box,{className:ue(),style:l,children:t.map(function(e,t){return n(ce.VStack,{className:ue(),style:c({gap:"1rem"},A.formColumn),children:e.map(function(e){return me(e,$[e])})},t)})},i)}(),d=!(!r.defaultOpen&&r.collapsible),f=ie.has(i)?!d:d;return o(ce.Box,{style:{marginBottom:"1.25rem"},children:[o(ce.Box,{style:{display:"flex",alignItems:"center",justifyContent:"space-between",marginBottom:"0.5rem"},children:[o(ce.Box,{style:{display:"flex",alignItems:"center",gap:"8px"},children:[r.collapsible&&n("button",{"aria-label":"Toggle ".concat(r.title),onClick:function(){return le(function(e){var t=new Set(e);return t.has(i)?t.delete(i):t.add(i),t})},style:{border:"none",background:"transparent",cursor:"pointer"},children:n("span",{children:f?"▼":"▶"})}),n(ce.Heading,{className:ue(),style:{margin:0},children:r.title})]}),r.description&&n(ce.Text,{className:ue(),style:{opacity:.8},children:r.description})]}),(!r.collapsible||f)&&u]},"wrap-".concat(i))}),a=null;if(D){var i=e.filter(function(e){return!t.has(e)});if(i.length){var l="unsectioned",u=!0,d=!ie.has(l)&&u;a=o(ce.Box,{style:{marginBottom:"1.25rem"},children:[n(ce.Box,{style:{display:"flex",alignItems:"center",justifyContent:"space-between",marginBottom:"0.5rem"},children:o(ce.Box,{style:{display:"flex",alignItems:"center",gap:"8px"},children:[n("button",{"aria-label":"Toggle ".concat(J),onClick:function(){return le(function(e){var t=new Set(e);return t.has(l)?t.delete(l):t.add(l),t})},style:{border:"none",background:"transparent",cursor:"pointer"},children:n("span",{children:d?"▼":"▶"})}),n(ce.Heading,{className:ue(),style:{margin:0},children:J})]})}),d&&function(){if(H<=1)return n(ce.VStack,{style:{gap:"1rem"},children:i.map(function(e){return me(e,$[e])})});for(var e=Math.ceil(i.length/H),t=[],r=0;r<H;r++){var o=r*e,a=Math.min(o+e,i.length);t.push(i.slice(o,a))}var l=c({display:"grid",gridTemplateColumns:"repeat(".concat(H,", 1fr)"),gap:"2rem",width:"100%"},A.formGrid);return n(ce.Box,{className:ue(),style:l,children:t.map(function(e,t){return n(ce.VStack,{className:ue(),style:c({gap:"1rem"},A.formColumn),children:e.map(function(e){return me(e,$[e])})},t)})})}()]},"wrap-".concat(l))}}return o(ce.VStack,{className:ue(),style:c({gap:"1rem"},A.formStack),children:[r,a]})}var f=Object.entries($);if(H<=1)return n(ce.VStack,{className:ue(),style:c({gap:"1rem"},A.formStack),children:f.map(function(e){var t=s(e,2),r=t[0],n=t[1];return me(r,n)})});for(var m=Math.ceil(f.length/H),p=[],g=0;g<H;g++){var y=g*m,b=Math.min(y+m,f.length);p.push(f.slice(y,b))}var h=c({display:"grid",gridTemplateColumns:"repeat(".concat(H,", 1fr)"),gap:"2rem",width:"100%"},A.formGrid);return n(ce.Box,{className:ue(),style:h,children:p.map(function(e,t){return n(ce.VStack,{className:ue(),style:c({gap:"1rem"},A.formColumn),children:e.map(function(e){var t=s(e,2),r=t[0],n=t[1];return me(r,n)})},t)})})}()}),L&&Object.keys($).length>0&&o(ce.HStack,{className:ue(),style:c({gap:"0.5rem",marginTop:"1rem"},A.controlButtons),children:[n(ce.Button,{onClick:function(){if(b){var e=p($);b(e,$)}},className:ue(),style:A.saveButton,children:B}),n(ce.Button,{onClick:function(){K(c({},q)),h&&h()},className:ue(),style:A.cancelButton,children:C})]})]})}))},w=function(){return{Container:"div",Box:"div",Button:"button",Input:"input",Select:"select",Textarea:"textarea",Table:"table",Thead:"thead",Tbody:"tbody",Tr:"tr",Th:"th",Td:"td",Text:"span",Heading:"h2",Stack:"div",HStack:"div",VStack:"div",Card:"div",Alert:"div",Badge:"span",Grid:"div",GridItem:"div",Flex:"div",Label:"label"}},B=function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:"chakra",t=arguments.length>1?arguments[1]:void 0,r=arguments.length>2&&void 0!==arguments[2]?arguments[2]:"default";if("chakra"===e)return"";var n={tailwind:{Container:"max-w-7xl mx-auto px-4 sm:px-6 lg:px-8",Box:"block",Button:{default:"inline-flex items-center px-4 py-2 border border-transparent text-sm font-medium rounded-md shadow-sm text-white bg-blue-600 hover:bg-blue-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-blue-500",secondary:"inline-flex items-center px-4 py-2 border border-gray-300 text-sm font-medium rounded-md text-gray-700 bg-white hover:bg-gray-50 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-blue-500"},Input:{default:"block w-full px-3 py-2 border border-gray-300 rounded-md shadow-sm placeholder-gray-400 focus:outline-none focus:ring-blue-500 focus:border-blue-500 sm:text-sm",checkbox:"h-4 w-4 text-blue-600 focus:ring-blue-500 border-gray-300 rounded"},Select:"block w-full px-3 py-2 border border-gray-300 rounded-md shadow-sm focus:outline-none focus:ring-blue-500 focus:border-blue-500 sm:text-sm",Textarea:"block w-full px-3 py-2 border border-gray-300 rounded-md shadow-sm placeholder-gray-400 focus:outline-none focus:ring-blue-500 focus:border-blue-500 sm:text-sm resize-vertical",Table:"min-w-full divide-y divide-gray-200",Thead:"bg-gray-50",Tbody:"bg-white divide-y divide-gray-200",Tr:"hover:bg-gray-50",Th:"px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider",Td:"px-6 py-4 whitespace-nowrap text-sm text-gray-900",Text:"text-gray-900",Heading:"text-2xl font-bold text-gray-900 mb-4",Stack:"space-y-4",HStack:"flex space-x-4 items-center",VStack:"flex flex-col space-y-4",Card:"bg-white overflow-hidden shadow rounded-lg p-6",Alert:{error:"bg-red-50 border border-red-200 rounded-md p-4 text-red-800",success:"bg-green-50 border border-green-200 rounded-md p-4 text-green-800",warning:"bg-yellow-50 border border-yellow-200 rounded-md p-4 text-yellow-800",info:"bg-blue-50 border border-blue-200 rounded-md p-4 text-blue-800"},Badge:"inline-flex items-center px-2.5 py-0.5 rounded-full text-xs font-medium bg-blue-100 text-blue-800",Grid:"grid",GridItem:"",Flex:"flex",Label:"block text-sm font-medium text-gray-700"},shadcn:{Container:"container mx-auto px-4",Box:"block",Button:{default:"inline-flex items-center justify-center rounded-md text-sm font-medium ring-offset-background transition-colors focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 disabled:pointer-events-none disabled:opacity-50 bg-primary text-primary-foreground hover:bg-primary/90 h-10 px-4 py-2",secondary:"inline-flex items-center justify-center rounded-md text-sm font-medium ring-offset-background transition-colors focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 disabled:pointer-events-none disabled:opacity-50 bg-secondary text-secondary-foreground hover:bg-secondary/80 h-10 px-4 py-2"},Input:{default:"flex h-10 w-full rounded-md border border-input bg-background px-3 py-2 text-sm ring-offset-background file:border-0 file:bg-transparent file:text-sm file:font-medium placeholder:text-muted-foreground focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 disabled:cursor-not-allowed disabled:opacity-50",checkbox:"h-4 w-4 rounded border border-primary text-primary ring-offset-background focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 disabled:cursor-not-allowed disabled:opacity-50"},Select:"flex h-10 w-full rounded-md border border-input bg-background px-3 py-2 text-sm ring-offset-background focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 disabled:cursor-not-allowed disabled:opacity-50",Textarea:"flex min-h-[80px] w-full rounded-md border border-input bg-background px-3 py-2 text-sm ring-offset-background placeholder:text-muted-foreground focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 disabled:cursor-not-allowed disabled:opacity-50",Table:"w-full caption-bottom text-sm",Thead:"[&_tr]:border-b",Tbody:"[&_tr:last-child]:border-0",Tr:"border-b transition-colors hover:bg-muted/50 data-[state=selected]:bg-muted",Th:"h-12 px-4 text-left align-middle font-medium text-muted-foreground [&:has([role=checkbox])]:pr-0",Td:"p-4 align-middle [&:has([role=checkbox])]:pr-0",Text:"text-sm text-muted-foreground",Heading:"scroll-m-20 text-2xl font-semibold tracking-tight",Stack:"flex flex-col space-y-4",HStack:"flex space-x-4",VStack:"flex flex-col space-y-4",Card:"rounded-lg border bg-card text-card-foreground shadow-sm",Alert:{error:"relative w-full rounded-lg border p-4 [&>svg~*]:pl-7 [&>svg+div]:translate-y-[-3px] [&>svg]:absolute [&>svg]:left-4 [&>svg]:top-4 [&>svg]:text-foreground border-destructive/50 text-destructive dark:border-destructive [&>svg]:text-destructive",success:"relative w-full rounded-lg border p-4 [&>svg~*]:pl-7 [&>svg+div]:translate-y-[-3px] [&>svg]:absolute [&>svg]:left-4 [&>svg]:top-4 [&>svg]:text-foreground",warning:"relative w-full rounded-lg border p-4 [&>svg~*]:pl-7 [&>svg+div]:translate-y-[-3px] [&>svg]:absolute [&>svg]:left-4 [&>svg]:top-4 [&>svg]:text-foreground",info:"relative w-full rounded-lg border p-4 [&>svg~*]:pl-7 [&>svg+div]:translate-y-[-3px] [&>svg]:absolute [&>svg]:left-4 [&>svg]:top-4 [&>svg]:text-foreground"},Badge:"inline-flex items-center rounded-full border px-2.5 py-0.5 text-xs font-semibold transition-colors focus:outline-none focus:ring-2 focus:ring-ring focus:ring-offset-2",Grid:"grid",GridItem:"",Flex:"flex",Label:"text-sm font-medium leading-none peer-disabled:cursor-not-allowed peer-disabled:opacity-70"}}[e];if(!n||!n[t])return"";var o=n[t];return"object"===d(o)?o[r]||o.default||"":o};export{k as Fields,k as JsonFields,m as flattenObject,b as formatJson,v as getDisplayName,g as getInputType,h as getNestedLevel,B as getUIClasses,w as getUIComponents,y as parseJsonSafely,p as unflattenObject};
//# sourceMappingURL=index.esm.js.map