@overmap-ai/forms 1.0.0 → 1.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1 +1 @@
1
- (function(y,n){typeof exports=="object"&&typeof module<"u"?n(exports,require("react/jsx-runtime"),require("@overmap-ai/blocks"),require("formik"),require("react"),require("@hello-pangea/dnd"),require("@overmap-ai/core"),require("lodash.get"),require("lodash.set")):typeof define=="function"&&define.amd?define(["exports","react/jsx-runtime","@overmap-ai/blocks","formik","react","@hello-pangea/dnd","@overmap-ai/core","lodash.get","lodash.set"],n):(y=typeof globalThis<"u"?globalThis:y||self,n(y.forms={},y.jsxRuntime,y.blocks,y.formik,y.React,y.dnd,y.core,y.get,y.set))})(this,function(y,n,s,A,c,N,C,Q,Fe){"use strict";var _t=Object.defineProperty;var qt=(y,n,s)=>n in y?_t(y,n,{enumerable:!0,configurable:!0,writable:!0,value:s}):y[n]=s;var g=(y,n,s)=>(qt(y,typeof n!="symbol"?n+"":n,s),s);class Ve{constructor(i){g(this,"type");g(this,"identifier");g(this,"description");const{description:e=null,identifier:t,type:r}=i;this.identifier=t,this.description=e,this.type=r}getId(){return this.identifier}static deserialize(i){throw new Error(`${this.name} must implement deserialize.`)}_serialize(){if(!this.identifier)throw new Error("Field identifier must be set before serializing.");return{type:this.type,identifier:this.identifier,description:this.description}}}class $ extends Ve{constructor(e){const{label:t,required:r,fieldValidators:o=[],formValidators:d=[],...a}=e;super(a);g(this,"required");g(this,"formValidators");g(this,"fieldValidators");g(this,"label");this.label=t,this.required=r,this.fieldValidators=o,this.formValidators=d}static getFieldCreationSchema(){return[]}isBlank(e){return e==null||e===""}getError(e,t){if(this.required&&this.isBlank(e))return"This field is required.";for(const r of this.getFieldValidators()){const o=r(e);if(o)return o}if(t)for(const r of this.getFormValidators()){const o=r(e,t);if(o)return o}}_serialize(){return{...super._serialize(),label:this.label,required:this.required}}getFieldValidators(){return[...this.fieldValidators]}getFormValidators(){return[...this.formValidators]}}g($,"fieldTypeName"),g($,"fieldTypeDescription");const ue={description:"_description_17zed_1"},H=l=>{const{label:i,children:e,severity:t,inputId:r,labelId:o,flexProps:d}=l;return n.jsx(s.Flex,{direction:"column",gap:"1",asChild:!0,...d,children:n.jsxs("label",{htmlFor:r,children:[n.jsx(s.Text,{severity:t,id:o,children:i}),e]})})},j=l=>{const{helpText:i,children:e,severity:t}=l;return n.jsxs(s.Flex,{direction:"column",gap:"1",children:[e,n.jsx(s.Flex,{direction:"column",children:n.jsx(s.Text,{size:"1",severity:t,className:ue.description,children:i})})]})},W=l=>{const{id:i,field:e,formId:t,...r}=l,[o,d,a]=A.useField(e.getId()),{touched:u}=d,p=d.error??e.description,h=d.error?"danger":void 0,f=i??`${t}-${e.getId()}-input`,m=`${f}-label`,v=e.required?`${e.label} *`:e.label,x=c.useMemo(()=>({...o,onChange:I=>{a.setValue(I.target.value,!1).then(),u&&a.setError(e.getError(I.target.value))},onBlur:I=>{a.setTouched(!0,!1).then(),a.setError(e.getError(I.target.value))}}),[e,o,a,u]);return[{helpText:p,severity:h,inputId:f,labelId:m,label:v,fieldProps:x,helpers:a,field:e},{...r,"aria-labelledby":m}]},nt=[!0,"true"],Be=c.memo(function(i){const[{inputId:e,labelId:t,severity:r,helpText:o,field:d,label:a,fieldProps:u,helpers:p},h]=W(i),f=s.useSeverityColor(r),{setValue:m,setTouched:v,setError:x}=p,w=nt.includes(u.value),F=c.useCallback(I=>{if(I==="indeterminate")throw new Error("indeterminate CheckedState value is not supported");m(I,!1).then(),v(!0,!1).then(),x(d.getError(I))},[m,v,x,d]);return n.jsx(j,{helpText:o,severity:r,children:n.jsx(H,{severity:r,inputId:e,labelId:t,label:a,flexProps:{direction:"row-reverse",justify:"end",align:"center",gap:"2"},children:n.jsx(s.Checkbox,{...h,...u,id:e,color:f,value:w.toString(),checked:w,onCheckedChange:F,onChange:void 0,onBlur:void 0})})})}),k=class k extends ${constructor(i){super({...i,type:"boolean"})}isBlank(i){return this.required&&!i}serialize(){return super._serialize()}static deserialize(i){if(i.type!=="boolean")throw new Error("Type mismatch.");return new k(i)}getInput(i){return n.jsx(Be,{...i,field:this})}};g(k,"fieldTypeName","Checkbox"),g(k,"fieldTypeDescription","Perfect for both optional and required yes/no questions."),g(k,"Icon",s.CheckCircledIcon);let K=k;const $e=c.memo(function(i){const[{inputId:e,labelId:t,severity:r,helpText:o,label:d,fieldProps:a,field:u},p]=W(i),h=s.useSeverityColor(r);return n.jsx(j,{helpText:o,severity:r,children:n.jsx(H,{severity:r,inputId:e,labelId:t,label:d,children:n.jsx(s.TextField.Input,{...p,...a,type:"number",id:e,min:u.minimum,max:u.maximum,step:u.integers?1:.1,color:h})})})}),_=class _ extends ${constructor(e){const{minimum:t=Number.MIN_SAFE_INTEGER,maximum:r=Number.MAX_SAFE_INTEGER,integers:o=!1,...d}=e;super({...d,type:"number"});g(this,"minimum");g(this,"maximum");g(this,"integers");this.minimum=t,this.maximum=r,this.integers=o}static getFieldCreationSchema(){return[new _({label:"Minimum",description:"Minimum value",integers:!0,required:!1,identifier:"minimum",formValidators:[this._validateMin]}),new _({label:"Maximum",description:"Maximum value",integers:!0,required:!1,identifier:"maximum",formValidators:[this._validateMax]}),new K({label:"Integers",description:"Whole numbers only",required:!1,identifier:"integers"})]}getFieldValidators(){const e=super.getFieldValidators(),t=this.minimum,r=this.maximum;return typeof t=="number"&&e.push(o=>{if(typeof o=="number"&&o<t)return`Must be at least ${this.minimum}.`}),typeof r=="number"&&e.push(o=>{if(typeof o=="number"&&o>r)return`Must be at most ${this.maximum}.`}),this.integers&&e.push(o=>{if(typeof o=="number"&&!Number.isInteger(o))return"Must be a whole number."}),e}serialize(){return{...super._serialize(),minimum:this.minimum,maximum:this.maximum,integers:this.integers}}static deserialize(e){if(e.type!=="number")throw new Error("Type mismatch.");return new _(e)}getInput(e){return n.jsx($e,{field:this,...e})}};g(_,"fieldTypeName","Number"),g(_,"fieldTypeDescription","Allows specifying a number within a given range."),g(_,"Icon",s.FontFamilyIcon),g(_,"_validateMin",(e,t)=>typeof t.maximum=="number"&&typeof e=="number"&&t.maximum<e?"Minimum cannot be greater than minimum.":null),g(_,"_validateMax",(e,t)=>typeof t.minimum=="number"&&typeof e=="number"&&t.minimum>e?"Maximum cannot be less than minimum.":null);let X=_;const _e=c.memo(function(i){const[{inputId:e,labelId:t,severity:r,helpText:o,label:d,field:a,fieldProps:u,helpers:p},h]=W(i),f=s.useSeverityColor(r),{setValue:m,setError:v}=p,x=u.value?u.value.split("T")[0]:"",w=c.useCallback(F=>{const I=new Date(F.target.value).toISOString();m(I,!1).then(),v(a.getError(I))},[a,v,m]);return n.jsx(j,{helpText:o,severity:r,children:n.jsx(H,{severity:r,inputId:e,labelId:t,label:d,children:n.jsx(s.TextField.Input,{...h,...u,type:"date",id:e,color:f,value:x,onChange:w})})})}),R=class R extends ${constructor(i){super({...i,type:"date"})}serialize(){return super._serialize()}static deserialize(i){if(i.type!=="date")throw new Error("Type mismatch.");return new R(i)}getInput(i){return n.jsx(_e,{field:this,...i})}};g(R,"fieldTypeName","Date"),g(R,"fieldTypeDescription","Allows specifying a date."),g(R,"Icon",s.CalendarIcon);let fe=R;class he extends ${constructor(e){const{minLength:t,maxLength:r=5e3,...o}=e;super(o);g(this,"minLength");g(this,"maxLength");this.minLength=t?Math.max(t,0):void 0,this.maxLength=r?Math.max(r,0):5e3}static getFieldCreationSchema(){return[new X({label:"Minimum length",description:"Minimum number of characters",required:!1,identifier:"minimum_length",minimum:0,maximum:100,formValidators:[this._validateMin],integers:!0}),new X({label:"Maximum length",description:"Maximum number of characters",required:!1,identifier:"maximum_length",minimum:1,maximum:5e3,formValidators:[this._validateMax],integers:!0})]}getFieldValidators(){const e=super.getFieldValidators();return this.minLength&&e.push(t=>{if(this.minLength&&(!t||t.length<this.minLength))return!this.required&&!t?null:`Minimum ${this.minLength} character(s).`}),this.maxLength&&e.push(t=>{if(typeof t=="string"&&this.maxLength&&t.length>this.maxLength)return`Maximum ${this.maxLength} character(s).`}),e}_serialize(){if(!this.identifier)throw new Error("Field identifier must be set before serializing.");return{...super._serialize(),minimum_length:this.minLength,maximum_length:this.maxLength}}}g(he,"_validateMin",(e,t)=>typeof t.maximum_length=="number"&&typeof e=="number"&&t.maximum_length<e?"Minimum cannot be greater than maximum.":null),g(he,"_validateMax",(e,t)=>{if(typeof e!="number")return null;const{minimum_length:r}=t;return typeof r!="number"?null:r>e?"Maximum cannot be less than minimum.":null});const qe=c.memo(function(i){const[{inputId:e,labelId:t,severity:r,helpText:o,label:d,fieldProps:a,field:u},p]=W(i),h=s.useSeverityColor(r);return n.jsx(j,{helpText:o,severity:r,children:n.jsx(H,{severity:r,inputId:e,labelId:t,label:d,children:n.jsx(s.TextField.Input,{...p,...a,type:u.inputType,id:e,color:h})})})}),ee=class ee extends he{constructor(e){const{inputType:t="text",...r}=e,o=e.maxLength?Math.min(500,e.maxLength):500,d=e.minLength?Math.min(e.minLength,o):void 0;super({...r,maxLength:o,minLength:d,type:"string"});g(this,"inputType");this.inputType=t}serialize(){return{...super._serialize(),input_type:this.inputType}}static deserialize(e){if(e.type!=="string")throw new Error("Type mismatch.");const{maximum_length:t,minimum_length:r,input_type:o,...d}=e;return new ee({...d,maxLength:t,minLength:r,inputType:o})}getInput(e){return n.jsx(qe,{field:this,...e})}};g(ee,"fieldTypeName","Short Text"),g(ee,"fieldTypeDescription","Short text fields can hold up to 500 characters on a single line."),g(ee,"Icon",s.InputIcon);let J=ee;const Ne=c.memo(function(i){const[{inputId:e,labelId:t,severity:r,helpText:o,label:d,fieldProps:a},u]=W(i);return n.jsx(j,{helpText:o,severity:r,children:n.jsx(H,{severity:r,inputId:e,labelId:t,label:d,children:n.jsx(s.TextArea,{...u,...a,resize:"vertical",id:e,severity:r})})})}),te=class te extends he{constructor(i){const e=i.maxLength?Math.min(5e3,i.maxLength):5e3,t=i.minLength?Math.min(i.minLength,e):void 0;super({...i,maxLength:e,minLength:t,type:"text"})}serialize(){return super._serialize()}static deserialize(i){if(i.type!=="text")throw new Error("Type mismatch.");const{maximum_length:e,minimum_length:t,...r}=i;return new te({...r,maxLength:e,minLength:t})}getInput(i){return n.jsx(Ne,{field:this,...i})}};g(te,"fieldTypeName","Paragraph"),g(te,"fieldTypeDescription","Paragraph fields can hold up to 5000 characters and can have multiple lines."),g(te,"Icon",s.RowsIcon);let Z=te;const He=c.memo(function(i){const[{inputId:e,labelId:t,severity:r,helpText:o,label:d,fieldProps:a,field:u,helpers:p},h]=W(i),{setValue:f,setTouched:m,setError:v}=p,x=c.useMemo(()=>u.options.map(F=>({value:F.value,itemContent:F.label})),[u.options]),w=c.useCallback(F=>{f(F,!1).then(),m(!0,!1).then(),v(u.getError(F))},[m,f,v,u]);return n.jsx(j,{helpText:o,severity:r,children:n.jsx(H,{severity:r,inputId:e,labelId:t,label:d,children:n.jsx(s.Select,{items:x,...a,onValueChange:w,placeholder:"Select one...",id:e,severity:r,...h})})})}),pe=(l="",i=[])=>({type:"section",fields:i,identifier:l,label:null,condition:null,conditional:!1}),rt=l=>{if(!l)return;const i=l.fields;let e=[];const t=[];for(const r of i)r.type==="section"?(e.length>0&&(t.push(pe(`AUTO_section-${i.indexOf(r)}`,e)),e=[]),t.push(r)):e.push(r);return e.length>0&&t.push(pe("AUTO_section-last",e)),{...l,fields:t}};function Ce(l,i,e){const t=Array.from(l),[r]=t.splice(i,1);if(!r)throw new Error("Could not find field to reorder.");return t.splice(e,0,r),t}function st(l,i,e){const t=Array.from(l);return t[i]=e,t}function je(l,i,e){const t=Array.from(l??[]);return t.splice(i,0,e),t}function me(l,i){const e=Array.from(l);return e.splice(i,1),e}const ot=(l,i)=>typeof l=="string"&&l.length>0?l:C.slugify(i),We=(l,i)=>{if(!i)return null;for(const e of l)if(e.type==="section"){const t=We(e.fields,i);if(t)return t}else if(e.identifier===i)return e;return null},be=(l,i)=>l.filter((e,t)=>t<i).flatMap(e=>e.fields),Ue=c.memo(function(i){const[{inputId:e,labelId:t,severity:r,helpText:o,label:d,fieldProps:a,field:u,helpers:p},h]=W(i),f=s.useSeverityColor(r),m=c.useMemo(()=>Array.isArray(a.value)?a.value:[],[a.value]),{setValue:v,setTouched:x,setError:w}=p,F=`${e}-droppable`,{disabled:I}=h,[b,z]=c.useState(""),[P,M]=c.useState(""),O=P||o,G=P?"red":f,V=c.useCallback(E=>{v(E,!1).then(),x(!0,!1).then(),w(u.getError(E))},[v,x,w,u]),S=c.useCallback(E=>{m.findIndex(B=>B.value===E.target.value.trim())>=0?M("All options must be unique"):E.target.value?M(""):M("Option cannot be empty"),z(E.target.value)},[z,m]),T=c.useCallback(()=>{if(P)return;if(!b.trim())return M("Option cannot be empty");const E=b.trim();V([...m,{value:E,label:E}]),z("")},[b,P,V,m]),D=c.useCallback(E=>{E.key==="Enter"&&(E.preventDefault(),T())},[T]),L=c.useCallback(E=>{V(me(m,E))},[m,V]),q=c.useCallback(E=>{if(!E.destination)return;const B=E.source.index,Y=E.destination.index;V(Ce(m,B,Y))},[V,m]);return n.jsx(N.DragDropContext,{onDragEnd:q,children:n.jsxs(s.Flex,{direction:"column",gap:"2",children:[n.jsx(j,{helpText:O,severity:r,children:n.jsx(H,{severity:r,inputId:e,labelId:t,label:d,children:(!I||m.length===0)&&n.jsxs(s.Flex,{gap:"2",children:[n.jsx(s.Box,{grow:"1",children:n.jsx(s.TextField.Input,{placeholder:"Press enter to add a new option",...h,...a,value:b,onChange:S,onKeyDown:D,id:e,color:G,onBlur:void 0})}),n.jsx(s.IconButton,{type:"button","aria-label":"Add option",disabled:!!P||I,onClick:T,children:n.jsx(s.PlusIcon,{})})]})})}),n.jsx(N.Droppable,{droppableId:F,children:E=>n.jsxs(s.Flex,{...E.droppableProps,ref:E.innerRef,direction:"column",children:[m.map((B,Y)=>n.jsx(N.Draggable,{draggableId:`${B.value}-draggable`,index:Y,isDragDisabled:I,children:({draggableProps:oe,dragHandleProps:Oe,innerRef:$t})=>n.jsx(s.Flex,{...Oe,...oe,ref:$t,gap:"2",align:"center",justify:"between",mb:"1",asChild:!0,children:n.jsxs(s.Badge,{color:"gray",size:"2",children:[n.jsx("span",{children:B.label}),n.jsx(s.IconButton,{size:"small",variant:"ghost",type:"button","aria-label":"Delete option",severity:"info",disabled:I,onClick:()=>L(Y),children:n.jsx(s.Cross1Icon,{})})]})})},B.value)),E.placeholder]})})]})})}),ie=class ie extends ${constructor(e){const{minimum_length:t,maximum_length:r,...o}=e;super({...o,type:"multi-string"});g(this,"minLength");g(this,"maxLength");this.minLength=t??0,this.maxLength=r??1/0}getInput(e){return n.jsx(Ue,{field:this,...e})}serialize(){return{...super._serialize(),minimum_length:this.minLength,maximum_length:this.maxLength}}isBlank(e){return super.isBlank(e)||e.length===0}getFieldValidators(){const e=super.getFieldValidators();return e.push(t=>{if(Array.isArray(t)&&t.length<this.minLength)return`Must have at least ${this.minLength} options.`}),e.push(t=>{if(Array.isArray(t)&&t.length>this.maxLength)return`Must have at most ${this.maxLength} options.`}),e}static deserialize(e){if(e.type!=="multi-string")throw new Error("Type mismatch.");return new ie(e)}};g(ie,"fieldTypeName","Multi-string"),g(ie,"fieldTypeDescription","Allows the user to provide multiple unique strings."),g(ie,"Icon",s.ListBulletIcon);let le=ie;class Ge extends ${constructor(e){super(e);g(this,"options");const t=new Set;this.options=e.options.map(r=>(typeof r=="string"&&(r={label:r,value:r}),t.add(r.label),r)),t.size!==e.options.length&&console.error(`${e.options.length-t.size} duplicate identifiers found in options. This may cause unexpected behavior. Options:`,e.options)}_serialize(){return{...super._serialize(),options:this.options}}static getFieldCreationSchema(){return[new le({label:"Options",description:"List possible options for the user to select from.",required:!0,identifier:"options",minimum_length:2,maximum_length:20})]}}const ne=class ne extends Ge{constructor(i){super({...i,type:"select"})}serialize(){return super._serialize()}static deserialize(i){if(i.type!=="select")throw new Error("Type mismatch.");return new ne(i)}getInput(i){return n.jsx(He,{field:this,...i})}};g(ne,"fieldTypeName","Dropdown"),g(ne,"fieldTypeDescription","Allows the user to select a single option from a list of options."),g(ne,"Icon",s.DropdownMenuIcon);let ae=ne;const Ke=c.memo(function(i){const[{inputId:e,labelId:t,severity:r,helpText:o,label:d,fieldProps:a,field:u,helpers:p},h]=W(i),{setValue:f,setTouched:m,setError:v}=p,x=c.useMemo(()=>Array.isArray(a.value)?a.value:[],[a.value]),w=c.useCallback(F=>{f(F,!1).then(),m(!0,!1).then(),v(u.getError(F))},[m,f,v,u]);return n.jsx(j,{helpText:o,severity:r,children:n.jsx(H,{severity:r,inputId:e,labelId:t,label:d,children:n.jsx(s.MultiSelect,{value:x,onValueChange:w,options:u.options,name:a.name,placeholder:"Select one or more...",id:e,severity:r,...h})})})}),re=class re extends Ge{constructor(i){super({...i,type:"multi-select"})}isBlank(i){return super.isBlank(i)||i.length===0}serialize(){return super._serialize()}static deserialize(i){if(i.type!=="multi-select")throw new Error("Type mismatch.");return new re(i)}getInput(i){return n.jsx(Ke,{field:this,...i})}};g(re,"fieldTypeName","Multi-select"),g(re,"fieldTypeDescription","Allows the user to select a multiple options from a list of options."),g(re,"Icon",s.CheckboxIcon);let ge=re;const lt=c.memo(function({field:i,...e}){const[{value:t}]=A.useField(i.options.clonedFieldIdentifier),r=c.useMemo(()=>{const o=i.options.getFieldToClone(t);return o?de(o):null},[i.options,t]);return ce(r,e)});class ye extends ${constructor(e,t){super({...e,type:"custom"});g(this,"Component");g(this,"options");this.options=e,this.Component=t}serialize(){throw new Error("Serializing only supported for public input types.")}getInput(e){const t=this.Component;return n.jsx(t,{field:this,...e})}}g(ye,"fieldTypeName","Custom"),g(ye,"fieldTypeDescription","Allows re-rendering of field already in the form");class at extends ye{constructor(i){super(i,lt)}}const dt=c.memo(function(i){const{field:e,...t}=i,{label:r,description:o,fields:d,condition:a}=e,{values:u,setFieldValue:p}=A.useFormikContext(),h=a!=null&&a.identifier?Q(u,a.identifier):void 0,f=c.useMemo(()=>Me(a,h),[a,h]);c.useEffect(()=>{if(!f)for(const v of d)p(v.getId(),"").then()},[f,d,p]);const m=ze(d,t);return f?r?n.jsx(s.Card,{children:n.jsxs(s.Flex,{direction:"column",gap:"3",children:[n.jsxs(s.Flex,{direction:"column",children:[n.jsx(s.Heading,{as:"h3",size:"3",children:r}),n.jsx(s.Text,{className:ue.description,children:o})]}),m]})}):m:null}),se=class se extends Ve{constructor(e){const{label:t=null,fields:r,condition:o=null,conditional:d,...a}=e;super({...a,type:"section"});g(this,"label");g(this,"fields");g(this,"condition");this.fields=r,this.condition=o,this.label=t,d===!1&&(this.condition=null)}static getFieldCreationSchema(e){return e.length===0?[]:[new K({label:"Conditional",description:"Conditionally show or hide this section.",identifier:"conditional",required:!1}),new se({label:"Conditional settings",identifier:"conditional-settings",condition:{identifier:"conditional",value:!0},fields:[new ae({label:"Field",description:"The field to use for the condition.",options:e.map(t=>t.label?{label:t.label,value:t.identifier}:null).filter(t=>!!t),identifier:"condition.identifier",required:!0}),new at({label:"Value",identifier:"condition.value",required:!0,clonedFieldIdentifier:"condition.identifier",getFieldToClone(t){if(!t)return null;const r=e.find(o=>o.identifier===t);return r?{...r,label:"Value",identifier:"condition.value",description:"The value to compare against.",required:r.type!=="boolean"}:(console.error("Could not find field with identifier",t),null)}})]})]}static deserialize(e){var r;if(e.type!=="section")throw new Error("Invalid type");const t=((r=e.fields)==null?void 0:r.map(Se))??[];return new se({...e,fields:t})}conditional(){return this.condition!==null}serialize(){return{...super._serialize(),label:this.label,condition:this.condition,conditional:this.conditional(),fields:this.fields.map(e=>e.serialize())}}getErrors(e){const t={};for(const r of this.fields){const o=r.getId(),d=r.getError(Q(e,o),e);d&&Fe(t,r.getId(),d)}return t}getInput(e){return n.jsx(dt,{field:this,...e})}};g(se,"fieldTypeName","Section"),g(se,"fieldTypeDescription","Sections can be useful for grouping fields together. They can also be conditionally shown or hidden.");let U=se;const Te={date:fe,number:X,boolean:K,select:ae,string:J,text:Z,custom:ye,"multi-string":le,"multi-select":ge},Se=l=>{const i=l.type;return Te[i].deserialize(l)},de=l=>l.type==="section"?U.deserialize(l):Se(l);function Ee(l,i={}){const{readonly:e=!1}=i;return{title:l.title,description:l.description,fields:l.fields.map(t=>de(t)),meta:{readonly:e}}}function Me(l,i){if(!l)return!0;const e=Array.isArray(i)?i.map(r=>typeof r=="string"?r:r.value):i,t=Array.isArray(l.value)?l.value.map(r=>typeof r=="string"?r:r.value):l.value;if(Array.isArray(t)&&Array.isArray(e)){for(const r of t)if(!e.includes(r))return!1;return!0}return t===i}const ce=(l,i)=>c.useMemo(()=>!i||!l?null:l.getInput(i),[l,i]),ze=(l,i)=>{const e=c.useMemo(()=>l.map(t=>n.jsx("div",{children:t.getInput(i)},t.getId())),[l,i]);return n.jsx(s.Flex,{direction:"column",gap:"2",children:e})},ve=l=>Object.keys(l).length>0,Ye=async(l,i)=>{const e={};for(const t of l.fields)if(t instanceof U){if(t.condition){const{identifier:r}=t.condition;if(!Me(t.condition,Q(i,r)))continue}Object.assign(e,t.getErrors(i))}else{if(!(t instanceof $))throw new Error("Invalid field type");const r=t.getId(),o=t.getError(Q(i,r),i);o&&Fe(e,r,o)}if(ve(e))return e},ct=[null,void 0],Pe=(l,i)=>l.reduce((e,t)=>t instanceof U?{...e,...Pe(t.fields,i)}:(ct.includes(Q(e,t.getId()))&&Fe(e,t.getId(),""),e),i),ut=()=>{throw new Error("onSubmit must be provided if form is not readonly.")},xe=c.memo(c.forwardRef((l,i)=>{const{schema:e,values:t={},onSubmit:r=ut,submitText:o="Submit",cancelText:d,onCancel:a,onDirty:u,hideTitle:p=!e.title,hideDescription:h,className:f}=l,{readonly:m}=e.meta,v=c.useMemo(()=>crypto.randomUUID(),[]),x=A.useFormik({initialValues:Pe(e.fields,t),onSubmit:r,validate:z=>Ye(e,z),validateOnBlur:!1,validateOnChange:!1}),{dirty:w}=x,F=c.useMemo(()=>typeof e.title=="string"?n.jsx(s.Heading,{children:e.title}):e.title,[e.title]),I=c.useMemo(()=>typeof e.description=="string"?n.jsx(s.Text,{className:ue.description,children:e.description}):e.description,[e.description]),b=ze(e.fields,{formId:v,disabled:m});return c.useEffect(()=>{w&&u&&u()},[w,u]),n.jsx(A.FormikProvider,{value:x,children:n.jsx(s.Flex,{ref:i,direction:"column",gap:"2",className:f,asChild:!0,children:n.jsxs("form",{id:v,onSubmit:x.handleSubmit,children:[!p&&n.jsx(s.Card,{children:n.jsxs(s.Flex,{direction:"column",gap:"1",children:[F,!h&&I]})}),b,!m&&n.jsxs(s.Flex,{justify:"end",gap:"2",children:[d&&n.jsx(s.Button,{type:"button",variant:"soft",onClick:a,children:d}),n.jsx(s.Button,{type:"submit",disabled:!x.isValid,children:o})]})]})})})})),ft=c.memo(c.forwardRef((l,i)=>{const{submission:e,showFormDescription:t=!1,showFormTitle:r=!0}=l,o=C.useAppSelector(C.selectFormRevision(e.form_revision));if(!o)throw new Error(`Could not find revision ${e.form_revision} for submission ${e.offline_id}.`);const d=c.useMemo(()=>Ee(o,{readonly:!0}),[o]);return n.jsx(xe,{ref:i,schema:d,values:e.values,hideDescription:!t,hideTitle:!r})})),Qe={favoriteIcon:"_favoriteIcon_1bixi_1",regularIcon:"_regularIcon_1bixi_9"},Ae="organization:",Le="user:",ht=c.memo(c.forwardRef((l,i)=>{const{maxResults:e=20,...t}=l,[r,o]=c.useState(""),[d,a]=c.useState(""),{sdk:u}=C.useSDK(),p=c.useMemo(()=>{const b={maxResults:e,searchTerm:r};return d&&(d.startsWith(Ae)?b.owner_organization=parseInt(d.slice(Ae.length)):d.startsWith(Le)&&(b.owner_user=parseInt(d.slice(Le.length)))),b},[r,e,d]),h=C.useAppSelector(C.selectFilteredUserForms(p))??[],f=C.useAppSelector(C.selectUserFormMapping),m=c.useCallback(b=>{b.favorite?u.userForms.unfavorite(b.offline_id).then():u.userForms.favorite(b.offline_id).then()},[u]),v=c.useMemo(()=>{const b=u.store.getState(),z={};for(const P of Object.values(f)){const M=C.selectOrganization(P.owner_organization||-1)(b);M&&(z[`${Ae}${M.id}`]=M.name);const O=C.selectUser(P.owner_user||-1)(b);O&&(z[`${Le}${O.id}`]=O.username)}return Object.entries(z).map(([P,M])=>({itemContent:M,value:P}))},[f,u.store]),x=c.useCallback(b=>{o(b.currentTarget.value)},[]),F=(C.useAppSelector(C.selectNumberOfUserForms)||0)-h.length,I=h.length==e&&F>0?`Only the first ${e} results are shown (${F} hidden)`:F>0&&`${F} hidden forms`;return n.jsxs(s.Flex,{ref:i,direction:"column",gap:"2",children:[n.jsxs(s.Flex,{gap:"2",grow:"1",children:[n.jsx(s.Box,{grow:"1",asChild:!0,children:n.jsx(s.TextField.Root,{size:"3",children:n.jsx(s.TextField.Input,{placeholder:"Filter",value:r,onChange:x})})}),n.jsx(s.Select,{items:v,value:d,onValueChange:a,placeholder:"Owner",size:"large"})]}),h.length>0&&n.jsx(s.ButtonList.Root,{children:h.map(b=>n.jsx(pt,{...t,form:b,handleToggleFavorite:()=>m(b)},b.offline_id))}),n.jsx(s.Box,{px:"3",children:n.jsx(s.Text,{size:"2",severity:"info",children:I})})]})})),pt=l=>{var m;const{form:i,onSelectForm:e,isFavoriteEditable:t,handleToggleFavorite:r}=l,o=(m=C.useAppSelector(C.selectOrganization(i.owner_organization||-1)))==null?void 0:m.name,d=C.useAppSelector(C.selectUser(i.owner_user||-1)),a=C.useAppSelector(C.selectCurrentUser).id,u=!!d&&d.id===a,p=o??(u?"You":d==null?void 0:d.username)??"Unknown",h=c.useCallback(v=>{v.stopPropagation(),r()},[r]),f=n.jsx(s.ButtonList.Item,{onClick:()=>e(i),asChild:!0,children:n.jsxs(s.Flex,{justify:"between",gap:"2",py:"2",px:"3",...s.divButtonProps,children:[n.jsxs(s.Flex,{grow:"1",align:"center",gap:"2",children:[n.jsx(s.IconButton,{className:C.classNames(i.favorite?Qe.favoriteIcon:Qe.regularIcon),variant:"ghost",onClick:h,"aria-label":i.favorite?"Favorite form":"Standard form",disabled:!t,children:i.favorite?n.jsx(s.StarFilledIcon,{}):n.jsx(s.StarIcon,{})}),n.jsx(s.Text,{noWrap:!0,children:i.latestRevision.title}),i.latestRevision.description&&n.jsx(s.QuestionMarkCircledIcon,{})]}),p&&n.jsxs(s.Flex,{align:"center",gap:"2",children:[n.jsx(s.PersonIcon,{})," ",p]})]})});return i.latestRevision.description?n.jsx(s.Tooltip,{content:i.latestRevision.description,children:f},i.offline_id):f},Xe={submissionsContainer:"_submissionsContainer_9iirt_1",stopHorizontalOverflow:"_stopHorizontalOverflow_9iirt_6"},mt=c.memo(function(i){var b;const{submission:e,onSubmissionClick:t,compact:r,labelType:o,rowDecorator:d}=i,a=C.useAppSelector(C.selectCurrentUser),u=C.useAppSelector(C.selectUser("created_by"in e?e.created_by:a.id)),p=De(e),h=C.isToday(p)?p.toLocaleTimeString([],{hour:"2-digit",minute:"2-digit"}):C.getLocalDateString(p),f=C.useAppSelector(C.selectFormRevision(e.form_revision));if(!f)throw new Error(`Could not find revision ${e.form_revision} for submission ${e.offline_id}.`);const m=(b=C.useAppSelector(C.selectLatestFormRevision(f.form)))==null?void 0:b.revision,v=C.useFileSrc({file:(u==null?void 0:u.profile.file)??null,fileSha1:(u==null?void 0:u.profile.file_sha1)??null}),x=(u==null?void 0:u.username.charAt(0).toUpperCase())??"?",w=f.revision===m,F=c.useCallback(()=>{t&&t({submission:e})},[e,t]),I=n.jsx(s.ButtonList.Item,{onClick:F,asChild:!0,children:n.jsxs(s.Flex,{grow:"1",width:"100%",p:"2",gap:"2",justify:"between",children:[n.jsxs(s.Flex,{gap:"2",align:"center",className:Xe.stopHorizontalOverflow,children:[n.jsx(s.Avatar,{src:v,size:"1",fallback:x}),n.jsx(s.Text,{size:"2",noWrap:!0,children:o==="creator"?(u||a).username:f.title})]}),n.jsxs(s.Flex,{gap:"2",align:"center",children:[!r&&(f.revision?n.jsx(s.Badge,{variant:"soft",severity:w?"primary":"info",children:r?f.revision.toString():`Revision #${f.revision}`}):!!m&&n.jsx(s.Badge,{children:"Original"})),n.jsx(s.Text,{size:"2",noWrap:!0,children:h})]})]})});return d?d(e,I):I}),De=l=>{const i="created_at"in l?l.created_at:l.submitted_at;return new Date(i)},gt=c.memo(function(i){const{formId:e,submissions:t,compact:r=!1,className:o,after:d,variant:a="outline",...u}=i;if(!!e==!!t)throw new Error("Either formId or submissions must be provided, but not both.");const p=C.useAppSelector(t?()=>t:C.selectSubmissionsForForm(e)),h=c.useMemo(()=>p==null?void 0:p.sort((f,m)=>De(m).getTime()-De(f).getTime()),[p]);return n.jsx(s.ButtonList.Root,{className:C.classNames(Xe.submissionsContainer,o),size:"small",variant:a,before:!r&&n.jsxs(s.Text,{severity:"info",children:["There are ",((p==null?void 0:p.length)||0).toString()," submissions of this form."]}),after:d,children:h==null?void 0:h.map((f,m)=>n.jsx(mt,{submission:f,compact:r,...u},m))})}),yt=c.memo(function(i){const{name:e,render:t}=i,{submitForm:r}=A.useFormikContext(),[o,d,a]=A.useField(e);return c.useMemo(()=>{const u=p=>a.setValue(p,!1);return t({value:o.value,setValue:u,patchValue:r})},[r,a,o.value,t])}),vt=c.memo(c.forwardRef((l,i)=>{const{children:e,schema:t,values:r,onPatch:o,onError:d,...a}=l,u=c.useMemo(()=>Pe(t.fields,r),[t.fields,r]),p=c.useCallback(x=>{const w={};for(const F in x){const I=x[F];I!==u[F]&&I!==void 0&&(w[F]=I)}ve(w)&&o(w)},[u,o]),h=c.useCallback(async x=>{const w=await Ye(t,x);return w&&d(w),w},[t,d]),f=A.useFormik({initialValues:u,onSubmit:p,validate:h,validateOnBlur:!1,validateOnChange:!1}),{errors:m,resetForm:v}=f;return c.useEffect(()=>{ve(m)&&v({values:u,errors:{}})},[m,u,v]),n.jsx(A.FormikProvider,{value:f,children:n.jsx("form",{...a,ref:i,onSubmit:f.handleSubmit,children:e})})})),Je={...Te,section:U},xt=c.memo(function(i){const{field:e,setFieldType:t}=i,r=e.fieldTypeName,o=e.fieldTypeDescription,d=e.Icon;return n.jsxs(s.Flex,{gap:"4",align:"center",children:[n.jsx(s.Button,{type:"button",variant:"surface",onClick:t,style:{width:"135px"},children:n.jsxs(s.Flex,{gap:"3",align:"center",grow:"1",children:[n.jsx(d,{}),r]})}),n.jsx(s.Text,{children:o})]})}),Ze=[["string","text"],["select","multi-select"],["boolean","date","number","multi-string"]],wt=Ze.length-1,It=c.memo(function(i){const{setFieldType:e}=i;return n.jsx(s.Flex,{direction:"column",gap:"3",children:Ze.map((t,r)=>n.jsxs(s.Flex,{direction:"column",gap:"3",children:[n.jsx(s.Flex,{direction:"column",gap:"2",children:t.map(o=>n.jsx(xt,{field:Te[o],setFieldType:()=>e(o)},o))}),r<wt&&n.jsx(s.Separator,{size:"4"})]},r))})}),Ft=l=>i=>{if(!i||typeof i!="string")return;const e=C.slugify(i);if(l.includes(e))return"This name is already taken."},Ct=(l,i)=>{const e=[new J({label:"Label",required:!0,maxLength:100,identifier:"label",fieldValidators:[Ft(l)]}),new Z({label:"Description",required:!1,maxLength:1e3,identifier:"description"})];return i==="section"?e:[...e,new K({label:"Required",description:null,required:!1,identifier:"required"})]},bt=c.memo(function(i){const{fieldType:e,handleCancel:t,handleCreateField:r,defaultField:o,conditionalSourceFields:d}=i,a=Je[e],u=A.useFormikContext(),p=c.useMemo(()=>{const h=u.values.fields.flatMap(m=>m.type==="section"?[...m.fields.map(v=>v.identifier),m.identifier]:m.identifier).filter(m=>m!==(o==null?void 0:o.identifier));let f=Ct(h,e);if(a===U){if(d===void 0)throw new Error("Conditional source fields must be provided when changing sections.");f=f.concat(a.getFieldCreationSchema(d))}else{if(!(a.prototype instanceof $))throw new Error(`Field must be an instance of BaseField. Got ${a}.`);f=[...f,...a.getFieldCreationSchema()]}return{fields:f,meta:{readonly:!1},title:null}},[u.values.fields,e,a,o==null?void 0:o.identifier,d]);return n.jsx(xe,{schema:p,values:o,onSubmit:r,cancelText:o?void 0:"Back",onCancel:t})}),we=c.memo(function(i){const{parentPath:e,index:t,children:r,initial:o,editing:d,conditionalSourceFields:a}=i,[u,p]=c.useState(),h=(o==null?void 0:o.type)??u,f=h?Je[h].fieldTypeName:void 0,{setFieldValue:m,values:v}=A.useFormikContext();if(d&&!o)throw new Error("Initial field must be provided if editing is true.");const x=!h&&!d&&!o,w=x?"Choose a field type":`${f} settings`,F=x?"Select a field type to add to this section.":(f==null?void 0:f.toLowerCase())==="section"?"Customize your section":`Customize your ${f==null?void 0:f.toLowerCase()} field.`,I=c.useCallback(()=>p(void 0),[]),b=c.useCallback(M=>{p(void 0),M()},[]),z=c.useCallback((M,O)=>{const{label:G}=M;if(!h)throw new Error("Field type must be selected before creating a field.");if(!G||typeof G!="string")throw new Error("Label must be provided before creating a field.");const V=de({type:h,...M,identifier:ot(M.identifier,G)}).serialize(),S=Q(v,e);if(S===void 0)throw new Error("Parent path must point to an existing field.");let T;if(!Array.isArray(S))throw new Error("Parent path must point to an array.");d?T=st(S,t,V):T=je(S,t,V),m(e,T).then(),O()},[d,h,v,e,m,t]),P=c.useCallback(M=>x?n.jsx(It,{setFieldType:p}):n.jsx(bt,{conditionalSourceFields:a,handleCancel:I,handleCreateField:O=>z(O,M),fieldType:h,defaultField:o}),[a,I,z,o,x,h]);return n.jsx(s.Dialog,{onCloseInterrupt:b,title:w,description:F,content:P,children:r})}),ke=({children:l})=>n.jsx(n.Fragment,{children:l}),Re=(l,i)=>({initial:l?i:"none",sm:l?"none":i}),et=c.memo(function(i){const{remove:e,dragHandleProps:t,editProps:r,insertAfterProps:o}=i,d=c.useMemo(()=>[{Wrapper:we,wrapperProps:r,Icon:s.Pencil1Icon,text:"Edit"},{Icon:s.TrashIcon,buttonProps:{onClick:e},text:"Delete"},{Wrapper:we,wrapperProps:o,Icon:s.PlusIcon,text:"Add after"},{Icon:a=>n.jsx("div",{...a,children:n.jsx(s.DragHandleDots2Icon,{})}),text:"Reorder",disableOnMobile:!0,buttonProps:{...t,asChild:!0}}],[t,r,o,e]);return n.jsxs(n.Fragment,{children:[n.jsx(s.Flex,{gap:"4",display:Re(!1,"flex"),children:d.map(a=>{const u=a.Wrapper??ke;return n.jsx(u,{...a.wrapperProps,children:n.jsx(s.IconButton,{type:"button",variant:"ghost","aria-label":a.text,...a.buttonProps,children:n.jsx(a.Icon,{})})},a.text)})}),n.jsx(s.Box,{display:Re(!0,"block"),children:n.jsx(s.DropdownMenu,{trigger:n.jsx(s.IconButton,{variant:"ghost","aria-label":"Actions menu",children:n.jsx(s.DotsVerticalIcon,{})}),closeOnSelect:!1,items:d.map(a=>{var p;if(a.disableOnMobile)return null;const u=a.Wrapper??ke;return{...a.buttonProps,onSelect:(p=a.buttonProps)==null?void 0:p.onClick,content:n.jsx(u,{...a.wrapperProps,children:n.jsxs(s.Flex,{gap:"2",align:"center",children:[n.jsx(a.Icon,{}),a.text]})})}}).filter(a=>a!==null)})})]})}),Ie="form-builder",Tt=c.memo(function(i){const{field:e,index:t,sectionIndex:r,remove:o}=i,d=c.useMemo(()=>de(e),[e]),a=ce(d,{formId:Ie,disabled:!0}),u=c.useMemo(()=>({index:t,parentPath:`fields.${r}.fields`,initial:e,editing:!0}),[e,t,r]),p=c.useMemo(()=>({parentPath:`fields.${r}.fields`,index:t+1,initial:void 0}),[t,r]);return n.jsx(N.Draggable,{draggableId:e.identifier,index:t,children:h=>n.jsx(s.Card,{ref:h.innerRef,...h.draggableProps,...h.dragHandleProps,mb:"4",children:n.jsxs(s.Flex,{gap:"4",justify:"between",align:"center",children:[a,n.jsx(et,{remove:o,editProps:u,insertAfterProps:p,dragHandleProps:h.dragHandleProps})]})})})}),St=c.memo(function(i){var z,P,M,O,G,V;const{field:e,index:t,dropState:r}=i,o=(z=r[e.identifier])==null?void 0:z.disabled,{setFieldValue:d,values:a}=A.useFormikContext(),u=s.useAlertDialog(),p=c.useCallback((S,T)=>{for(const D of S){const L=T.indexOf(D);d(`fields.${L}.condition`,null).then(),d(`fields.${L}.conditional`,!1).then()}},[d]),h=c.useCallback(S=>{var L;const T=e.fields[S];if(!T)throw new Error("Could not find field to remove.");const D=[];for(const q of a.fields)((L=q.condition)==null?void 0:L.identifier)===T.identifier&&D.push(q);return{removing:T,affectedSections:D,action:()=>d(`fields.${t}.fields`,me(e.fields,S))}},[e.fields,a.fields,d,t]),f=c.useCallback(S=>{const{affectedSections:T,action:D,removing:L}=h(S),q=()=>{D().then(),p(T,a.fields)};if(T.length>0){const E=T.map(B=>B.label).join(", ");return u({title:"Remove condition?",description:`${L.label} is being used as a condition, deleting it will remove the condition from the ${E} section(s).`,severity:"danger",actionText:"Remove",onAction:q})}q()},[h,p,a.fields,u]),m=c.useCallback(()=>{const T=e.fields.map((oe,Oe)=>h(Oe)).flatMap(oe=>oe.affectedSections),D=T.length?"Remove fields and conditions?":"Remove fields?",L=e.fields.length,q=T.map(oe=>oe.label).join(", "),E=T.length?`Deleting this section will remove the ${L} field(s) it contains and will remove the conditions from following sections: ${q}`:`Deleting this section will remove the ${L} field(s) it contains.`,B=me(a.fields,t),Y=()=>d("fields",B);if(T.length>0)return u({title:D,description:E,severity:"danger",actionText:"Remove",onAction:()=>{Y().then(()=>{p(T,B)})}});Y().then()},[e.fields,a.fields,t,h,d,u,p]),v=c.useMemo(()=>({index:t,parentPath:"fields",initial:e,editing:!0,conditionalSourceFields:be(a.fields,t)}),[e,t,a.fields]),x=c.useMemo(()=>({index:t+1,parentPath:"fields",initial:pe(),conditionalSourceFields:be(a.fields,t+1)}),[t,a.fields]),w=c.useMemo(()=>({parentPath:`fields.${t}.fields`,index:e.fields.length,initial:void 0}),[e.fields.length,t]),F=c.useMemo(()=>{var S,T;return(T=We(a.fields,(S=e.condition)==null?void 0:S.identifier))==null?void 0:T.label},[(P=e.condition)==null?void 0:P.identifier,a.fields]),I=Array.isArray((M=e.condition)==null?void 0:M.value)?"contains all of":"equals",b=Array.isArray((O=e.condition)==null?void 0:O.value)?(G=e.condition)==null?void 0:G.value.map(S=>typeof S=="string"?S:S.label).join(", "):(V=e.condition)==null?void 0:V.value.toString();return n.jsx(N.Draggable,{draggableId:e.identifier,index:t,children:S=>n.jsx(s.Card,{ref:S.innerRef,...S.draggableProps,...S.dragHandleProps,mb:"4",children:n.jsxs(s.Flex,{gap:"3",justify:"between",align:"center",children:[n.jsxs(s.Flex,{direction:"column",gap:"2",grow:"1",children:[n.jsxs(s.Flex,{direction:"column",children:[n.jsx(s.Heading,{as:"h3",size:"3",children:e.label}),n.jsx(s.Text,{className:ue.description,children:e.description})]}),e.condition&&n.jsx(s.Text,{size:"1",children:n.jsxs(s.Em,{children:["Display only if ",n.jsx(s.Strong,{children:F})," ",I," ",n.jsx(s.Strong,{children:b})]})}),n.jsx(N.Droppable,{droppableId:e.identifier,type:"SECTION",isDropDisabled:o,children:T=>n.jsxs(s.Flex,{ref:T.innerRef,...T.droppableProps,direction:"column",gap:"0",children:[e.fields.map((D,L)=>n.jsx(Tt,{field:D,index:L,sectionIndex:t,remove:()=>f(L)},D.identifier)),T.placeholder,n.jsx(we,{...w,children:n.jsxs(s.Button,{type:"button",variant:"outline",children:[n.jsx(s.PlusIcon,{})," Add a field"]})})]})})]}),n.jsx(et,{remove:m,insertAfterProps:x,dragHandleProps:S.dragHandleProps,editProps:v})]})})})}),Et=(l,i)=>{var t;const e={...l};switch(i.type){case"release":for(const r in e)e[r].disabled=!1;return e;case"hold":for(const r in e)(t=e[r])!=null&&t.conditionFields.has(i.fieldId)&&(e[r].disabled=!0);return e;case"update":return i.state}},Mt=(l,i)=>{if(i)for(let e=0;e<l.length;e++){const t=l[e];if(t){for(const r of t.fields)if(r.identifier===i)return e}}},tt=l=>{var e,t,r;const i={};for(let o=0;o<l.length;o++){const d=l[o];if(!d)throw new Error("Field is undefined.");const a=o>0?(e=i[l[o-1].identifier])==null?void 0:e.conditionFields:void 0,u=new Set(a);(t=d.condition)!=null&&t.identifier&&u.add(d.condition.identifier),i[d.identifier]={disabled:!1,conditionFields:u,conditionIndex:Mt(l,(r=d.condition)==null?void 0:r.identifier),index:o,label:d.label}}return i},it=(l,i)=>{for(const[e,t]of Object.entries(l))if(t.identifier===i)return[t,e]},zt=c.memo(function(){const{values:i,setFieldValue:e}=A.useFormikContext(),[t,r]=c.useReducer(Et,i.fields,tt),{showInfo:o}=s.useToast();c.useEffect(()=>{r({type:"update",state:tt(i.fields)})},[r,i.fields]);const d=c.useCallback(p=>{p.type==="SECTION"&&r({type:"hold",fieldId:p.draggableId})},[]),a=c.useCallback(p=>{const{source:h,destination:f,type:m,reason:v,draggableId:x}=p;if(r({type:"release"}),!f||v==="CANCEL")return;if(m==="ROOT"){const z=t[x];if(!z)throw new Error("Could not find section context.");let P=typeof z.conditionIndex<"u"?Math.max(z.conditionIndex+1,f.index):f.index;for(const M of Object.values(t))M.conditionIndex===h.index&&(P=Math.min(P,M.index-1));return P!=f.index&&o({title:"Reordered sections",description:"Sections with conditions must be below the fields they reference."}),e("fields",Ce(i.fields,h.index,P))}if(m!=="SECTION")throw new Error("Unexpected droppable type.");const[w,F]=it(i.fields,h.droppableId)??[],[I,b]=it(i.fields,f.droppableId)??[];if(!(w!=null&&w.fields)||!I)throw new Error("Could not find section with fields.");if(w.identifier===I.identifier)e(`fields.${F}.fields`,Ce(w.fields,h.index,f.index)).then();else{const z=w.fields[h.index];if(!z)throw new Error("Could not find field to reorder.");e(`fields.${F}.fields`,me(w.fields,h.index)).then(),e(`fields.${b}.fields`,je(I.fields,f.index,z)).then()}},[i.fields,e,t,o]),u=c.useMemo(()=>({index:i.fields.length,parentPath:"fields",initial:pe(),conditionalSourceFields:be(i.fields,i.fields.length)}),[i.fields]);return n.jsx(N.DragDropContext,{onDragStart:d,onDragEnd:a,children:n.jsx(N.Droppable,{droppableId:"droppable",type:"ROOT",children:p=>n.jsxs(s.Flex,{ref:p.innerRef,...p.droppableProps,direction:"column",gap:"0",children:[i.fields.map((h,f)=>n.jsx(St,{field:h,index:f,dropState:t},h.label)),p.placeholder,n.jsx(we,{...u,children:n.jsxs(s.Button,{type:"button",variant:"outline",children:[n.jsx(s.PlusIcon,{})," Add a section"]})})]})})})}),Pt={title:"",description:"",fields:[]},At=new J({label:"Title",minLength:0,maxLength:100,required:!0,identifier:"title"}),Lt={formId:Ie,placeholder:"Give your form a title."},Dt=new Z({label:"Description",minLength:0,maxLength:1e3,required:!1,identifier:"description"}),Ot={formId:Ie,placeholder:"Explain the purpose of this form."},Vt=()=>{alert("This is a form preview, your data will not be saved.")},Bt=c.memo(c.forwardRef((l,i)=>{const{onCancel:e,onSave:t,revision:r}=l,o=r?"Edit form":"Create a new form",{heading:d=o}=l,a=c.useCallback(v=>{const x={};if(v.title||(x.title="Title is required."),(!v.fields||v.fields.length===0)&&(x.fields="At least one field is required."),ve(x))return x},[]),u=A.useFormik({initialValues:rt(r)??Pt,validate:a,onSubmit:v=>t(v),validateOnChange:!1,validateOnBlur:!1}),p=c.useMemo(()=>Ee(u.values),[u.values]),h=ce(At,Lt),f=ce(Dt,Ot),m=c.useMemo(()=>typeof d=="object"?d:n.jsx(s.Heading,{children:d}),[d]);return n.jsx(s.Tabs.Root,{ref:i,defaultValue:"edit",children:n.jsxs(s.Flex,{direction:"column",gap:"2",children:[n.jsxs(s.Tabs.List,{children:[n.jsx(s.Tabs.Trigger,{value:"edit",children:"Edit"}),n.jsx(s.Tabs.Trigger,{value:"preview",children:"Preview"})]}),n.jsxs(s.Tabs.Content,{value:"edit",children:[m,n.jsxs(s.Text,{children:["Add a new form field by clicking a + button. Specify options for each field, then drag and drop to rearrange them. You can see what a submitted form might look like in the"," ",n.jsx("em",{children:"Preview"})," tab, but"," ",n.jsx("strong",{children:"field values entered on this page will not be saved."})]}),n.jsx(s.Flex,{asChild:!0,direction:"column",gap:"2",mt:"3",children:n.jsxs("form",{id:Ie,onSubmit:u.handleSubmit,children:[n.jsxs(A.FormikProvider,{value:u,children:[h,f,n.jsx(zt,{}),n.jsx(s.Text,{severity:"danger",size:"1",children:typeof u.errors.fields=="string"&&u.errors.fields})]}),n.jsxs(s.Flex,{justify:"end",gap:"2",children:[n.jsx(s.Button,{type:"button",variant:"soft",onClick:e,children:"Cancel"}),n.jsx(s.Button,{type:"submit",disabled:!u.isValid,children:"Save"})]})]})})]}),n.jsx(s.Tabs.Content,{value:"preview",children:n.jsx(xe,{schema:p,onSubmit:Vt})})]})})}));y.BooleanField=K,y.BooleanInput=Be,y.DateField=fe,y.DateInput=_e,y.FieldSection=U,y.FormBrowser=ht,y.FormBuilder=Bt,y.FormRenderer=xe,y.FormSubmissionBrowser=gt,y.FormSubmissionViewer=ft,y.MultiSelectField=ge,y.MultiSelectInput=Ke,y.MultiStringField=le,y.MultiStringInput=Ue,y.NumberField=X,y.NumberInput=$e,y.PatchField=yt,y.PatchFormProvider=vt,y.SelectField=ae,y.SelectInput=He,y.StringField=J,y.StringInput=qe,y.TextField=Z,y.TextInput=Ne,y.deserialize=de,y.deserializeField=Se,y.formRevisionToSchema=Ee,y.isConditionMet=Me,y.useFieldInput=ce,y.useFieldInputs=ze,Object.defineProperty(y,Symbol.toStringTag,{value:"Module"})});
1
+ (function(x,i){typeof exports=="object"&&typeof module<"u"?i(exports,require("react/jsx-runtime"),require("@overmap-ai/blocks"),require("formik"),require("react"),require("@hello-pangea/dnd"),require("@overmap-ai/core"),require("lodash.get"),require("lodash.set")):typeof define=="function"&&define.amd?define(["exports","react/jsx-runtime","@overmap-ai/blocks","formik","react","@hello-pangea/dnd","@overmap-ai/core","lodash.get","lodash.set"],i):(x=typeof globalThis<"u"?globalThis:x||self,i(x.forms={},x.jsxRuntime,x.blocks,x.formik,x.React,x.dnd,x.core,x.get,x.set))})(this,function(x,i,s,D,c,j,I,Y,be){"use strict";var Yt=Object.defineProperty;var Qt=(x,i,s)=>i in x?Yt(x,i,{enumerable:!0,configurable:!0,writable:!0,value:s}):x[i]=s;var m=(x,i,s)=>(Qt(x,typeof i!="symbol"?i+"":i,s),s);class Oe{constructor(n){m(this,"type");m(this,"identifier");m(this,"description");const{description:e=null,identifier:t,type:r}=n;this.identifier=t,this.description=e,this.type=r}getId(){return this.identifier}static deserialize(n){throw new Error(`${this.name} must implement deserialize.`)}_serialize(){if(!this.identifier)throw new Error("Field identifier must be set before serializing.");return{type:this.type,identifier:this.identifier,description:this.description}}}class V extends Oe{constructor(e){const{label:t,required:r,fieldValidators:o=[],formValidators:d=[],...a}=e;super(a);m(this,"required");m(this,"formValidators");m(this,"fieldValidators");m(this,"label");m(this,"onlyValidateAfterTouched",!0);this.label=t,this.required=r,this.fieldValidators=o,this.formValidators=d}static getFieldCreationSchema(){return[]}isBlank(e){return e==null||e===""}getValueFromChangeEvent(e){return e.target.value}getError(e,t){if(this.required&&this.isBlank(e))return"This field is required.";for(const r of this.getFieldValidators()){const o=r(e);if(o)return o}if(t)for(const r of this.getFormValidators()){const o=r(e,t);if(o)return o}}_serialize(){return{...super._serialize(),label:this.label,required:this.required}}getFieldValidators(){return[...this.fieldValidators]}getFormValidators(){return[...this.formValidators]}}m(V,"fieldTypeName"),m(V,"fieldTypeDescription");const fe={description:"_description_17zed_1"},$=l=>{const{label:n,children:e,severity:t,inputId:r,labelId:o,flexProps:d}=l;return i.jsx(s.Flex,{direction:"column",gap:"1",asChild:!0,...d,children:i.jsxs("label",{htmlFor:r,children:[i.jsx(s.Text,{severity:t,id:o,children:n}),e]})})},O=l=>{const{helpText:n,children:e,severity:t}=l;return i.jsxs(s.Flex,{direction:"column",gap:"1",children:[e,i.jsx(s.Flex,{direction:"column",children:i.jsx(s.Text,{size:"1",severity:t,className:fe.description,children:n})})]})},q=l=>{const{id:n,field:e,formId:t,...r}=l,[o,d,a]=D.useField(e.getId()),{touched:u}=d,p=d.error??e.description,f=d.error?"danger":void 0,h=n??`${t}-${e.getId()}-input`,g=`${h}-label`,y=e.required?`${e.label} *`:e.label,v=c.useMemo(()=>({...o,onChange:T=>{const F=e.getValueFromChangeEvent(T);a.setValue(F,!1).then(),(u||!e.onlyValidateAfterTouched)&&a.setError(e.getError(F))},onBlur:T=>{a.setTouched(!0,!1).then(),a.setError(e.getError(e.getValueFromChangeEvent(T)))}}),[e,o,a,u]);return[{helpText:p,severity:f,inputId:h,labelId:g,label:y,fieldProps:v,helpers:a,field:e},{...r,"aria-labelledby":g}]},at=[!0,"true"],qe=c.memo(function(n){const[{inputId:e,labelId:t,severity:r,helpText:o,label:d,fieldProps:a},u]=q(n),p=s.useSeverityColor(r),f=at.includes(a.value);return i.jsx(O,{helpText:o,severity:r,children:i.jsx($,{severity:r,inputId:e,labelId:t,label:d,flexProps:{direction:"row-reverse",justify:"end",align:"center",gap:"2"},children:i.jsx(s.Checkbox,{...u,...a,id:e,color:p,value:f.toString(),checked:f,onCheckedChange:a.onChange,onChange:void 0,onBlur:void 0})})})}),J=class J extends V{constructor(e){super({...e,type:"boolean"});m(this,"onlyValidateAfterTouched",!1)}isBlank(e){return this.required&&!e}getValueFromChangeEvent(e){return typeof e=="boolean"?e:e.target.checked}serialize(){return super._serialize()}static deserialize(e){if(e.type!=="boolean")throw new Error("Type mismatch.");return new J(e)}getInput(e){return i.jsx(qe,{...e,field:this})}};m(J,"fieldTypeName","Checkbox"),m(J,"fieldTypeDescription","Perfect for both optional and required yes/no questions."),m(J,"Icon",s.CheckCircledIcon);let G=J;const Ne=c.memo(function(n){const[{inputId:e,labelId:t,severity:r,helpText:o,label:d,fieldProps:a,field:u},p]=q(n),f=s.useSeverityColor(r);return i.jsx(O,{helpText:o,severity:r,children:i.jsx($,{severity:r,inputId:e,labelId:t,label:d,children:i.jsx(s.TextField.Input,{...p,...a,type:"number",id:e,min:u.minimum,max:u.maximum,step:u.integers?1:.1,color:f})})})}),_=class _ extends V{constructor(e){const{minimum:t=Number.MIN_SAFE_INTEGER,maximum:r=Number.MAX_SAFE_INTEGER,integers:o=!1,...d}=e;super({...d,type:"number"});m(this,"minimum");m(this,"maximum");m(this,"integers");this.minimum=t,this.maximum=r,this.integers=o}getValueFromChangeEvent(e){const t=Number.parseFloat(e.target.value);return Number.isNaN(t)?"":t}static getFieldCreationSchema(){return[new _({label:"Minimum",description:"Minimum value",integers:!0,required:!1,identifier:"minimum",formValidators:[this._validateMin]}),new _({label:"Maximum",description:"Maximum value",integers:!0,required:!1,identifier:"maximum",formValidators:[this._validateMax]}),new G({label:"Integers",description:"Whole numbers only",required:!1,identifier:"integers"})]}getFieldValidators(){const e=super.getFieldValidators(),t=this.minimum,r=this.maximum;return typeof t=="number"&&e.push(o=>{if(typeof o=="number"&&o<t)return`Must be at least ${this.minimum}.`}),typeof r=="number"&&e.push(o=>{if(typeof o=="number"&&o>r)return`Must be at most ${this.maximum}.`}),this.integers&&e.push(o=>{if(typeof o=="number"&&!Number.isInteger(o))return"Must be a whole number."}),e}serialize(){return{...super._serialize(),minimum:this.minimum,maximum:this.maximum,integers:this.integers}}static deserialize(e){if(e.type!=="number")throw new Error("Type mismatch.");return new _(e)}getInput(e){return i.jsx(Ne,{field:this,...e})}};m(_,"fieldTypeName","Number"),m(_,"fieldTypeDescription","Allows specifying a number within a given range."),m(_,"Icon",s.FontFamilyIcon),m(_,"_validateMin",(e,t)=>typeof t.maximum=="number"&&typeof e=="number"&&t.maximum<e?"Minimum cannot be greater than minimum.":null),m(_,"_validateMax",(e,t)=>typeof t.minimum=="number"&&typeof e=="number"&&t.minimum>e?"Maximum cannot be less than minimum.":null);let H=_;const We=c.memo(function(n){const[{inputId:e,labelId:t,severity:r,helpText:o,label:d,fieldProps:a},u]=q(n),p=s.useSeverityColor(r),f=a.value?a.value.split("T")[0]:"";return i.jsx(O,{helpText:o,severity:r,children:i.jsx($,{severity:r,inputId:e,labelId:t,label:d,children:i.jsx(s.TextField.Input,{...u,...a,type:"date",id:e,color:p,value:f})})})}),Z=class Z extends V{constructor(e){super({...e,type:"date"});m(this,"onlyValidateAfterTouched",!1)}serialize(){return super._serialize()}getValueFromChangeEvent(e){return new Date(e.target.value).toISOString()}static deserialize(e){if(e.type!=="date")throw new Error("Type mismatch.");return new Z(e)}getInput(e){return i.jsx(We,{field:this,...e})}};m(Z,"fieldTypeName","Date"),m(Z,"fieldTypeDescription","Allows specifying a date."),m(Z,"Icon",s.CalendarIcon);let he=Z;class pe extends V{constructor(e){const{minLength:t,maxLength:r=5e3,...o}=e;super(o);m(this,"minLength");m(this,"maxLength");this.minLength=t?Math.max(t,0):void 0,this.maxLength=r?Math.max(r,0):5e3}static getFieldCreationSchema(){return[new H({label:"Minimum length",description:"Minimum number of characters",required:!1,identifier:"minimum_length",minimum:0,maximum:100,formValidators:[this._validateMin],integers:!0}),new H({label:"Maximum length",description:"Maximum number of characters",required:!1,identifier:"maximum_length",minimum:1,maximum:5e3,formValidators:[this._validateMax],integers:!0})]}getFieldValidators(){const e=super.getFieldValidators();return this.minLength&&e.push(t=>{if(this.minLength&&(!t||t.length<this.minLength))return!this.required&&!t?null:`Minimum ${this.minLength} character(s).`}),this.maxLength&&e.push(t=>{if(typeof t=="string"&&this.maxLength&&t.length>this.maxLength)return`Maximum ${this.maxLength} character(s).`}),e}_serialize(){if(!this.identifier)throw new Error("Field identifier must be set before serializing.");return{...super._serialize(),minimum_length:this.minLength,maximum_length:this.maxLength}}}m(pe,"_validateMin",(e,t)=>typeof t.maximum_length=="number"&&typeof e=="number"&&t.maximum_length<e?"Minimum cannot be greater than maximum.":null),m(pe,"_validateMax",(e,t)=>{if(typeof e!="number")return null;const{minimum_length:r}=t;return typeof r!="number"?null:r>e?"Maximum cannot be less than minimum.":null});const je=c.memo(function(n){const[{inputId:e,labelId:t,severity:r,helpText:o,label:d,fieldProps:a,field:u},p]=q(n),f=s.useSeverityColor(r);return i.jsx(O,{helpText:o,severity:r,children:i.jsx($,{severity:r,inputId:e,labelId:t,label:d,children:i.jsx(s.TextField.Input,{...p,...a,type:u.inputType,id:e,color:f})})})}),k=class k extends pe{constructor(e){const{inputType:t="text",...r}=e,o=e.maxLength?Math.min(500,e.maxLength):500,d=e.minLength?Math.min(e.minLength,o):void 0;super({...r,maxLength:o,minLength:d,type:"string"});m(this,"inputType");this.inputType=t}serialize(){return{...super._serialize(),input_type:this.inputType}}static deserialize(e){if(e.type!=="string")throw new Error("Type mismatch.");const{maximum_length:t,minimum_length:r,input_type:o,...d}=e;return new k({...d,maxLength:t,minLength:r,inputType:o})}getInput(e){return i.jsx(je,{field:this,...e})}};m(k,"fieldTypeName","Short Text"),m(k,"fieldTypeDescription","Short text fields can hold up to 500 characters on a single line."),m(k,"Icon",s.InputIcon);let Q=k;const He=c.memo(function(n){const[{inputId:e,labelId:t,severity:r,helpText:o,label:d,fieldProps:a},u]=q(n);return i.jsx(O,{helpText:o,severity:r,children:i.jsx($,{severity:r,inputId:e,labelId:t,label:d,children:i.jsx(s.TextArea,{...u,...a,resize:"vertical",id:e,severity:r})})})}),R=class R extends pe{constructor(n){const e=n.maxLength?Math.min(5e3,n.maxLength):5e3,t=n.minLength?Math.min(n.minLength,e):void 0;super({...n,maxLength:e,minLength:t,type:"text"})}serialize(){return super._serialize()}static deserialize(n){if(n.type!=="text")throw new Error("Type mismatch.");const{maximum_length:e,minimum_length:t,...r}=n;return new R({...r,maxLength:e,minLength:t})}getInput(n){return i.jsx(He,{field:this,...n})}};m(R,"fieldTypeName","Paragraph"),m(R,"fieldTypeDescription","Paragraph fields can hold up to 5000 characters and can have multiple lines."),m(R,"Icon",s.RowsIcon);let X=R;const Ue=c.memo(function(n){const[{inputId:e,labelId:t,severity:r,helpText:o,label:d,fieldProps:a,field:u},p]=q(n),{onChange:f,onBlur:h}=a,g=c.useMemo(()=>u.options.map(v=>({value:v.value,itemContent:v.label})),[u.options]),y=c.useCallback(v=>{f(v),h(v)},[f,h]);return i.jsx(O,{helpText:o,severity:r,children:i.jsx($,{severity:r,inputId:e,labelId:t,label:d,children:i.jsx(s.Select,{items:g,...a,onValueChange:y,placeholder:"Select one...",id:e,severity:r,...p})})})}),me=(l="",n=[])=>({type:"section",fields:n,identifier:l,label:null,condition:null,conditional:!1}),dt=l=>{if(!l)return;const n=l.fields;let e=[];const t=[];for(const r of n)r.type==="section"?(e.length>0&&(t.push(me(`AUTO_section-${n.indexOf(r)}`,e)),e=[]),t.push(r)):e.push(r);return e.length>0&&t.push(me("AUTO_section-last",e)),{...l,fields:t,description:l.description??""}};function Te(l,n,e){const t=Array.from(l),[r]=t.splice(n,1);if(!r)throw new Error("Could not find field to reorder.");return t.splice(e,0,r),t}function ct(l,n,e){const t=Array.from(l);return t[n]=e,t}function Ge(l,n,e){const t=Array.from(l??[]);return t.splice(n,0,e),t}function ge(l,n){const e=Array.from(l);return e.splice(n,1),e}const ut=(l,n)=>{if(typeof l=="string"&&l.length>0)return l;const e=new Date;return`${I.slugify(n)}-${e.getTime()}`},Ke=(l,n)=>{if(!n)return null;for(const e of l)if(e.type==="section"){const t=Ke(e.fields,n);if(t)return t}else if(e.identifier===n)return e;return null},Se=(l,n)=>l.filter((e,t)=>t<n).flatMap(e=>e.fields),Ye=c.memo(function(n){const[{inputId:e,labelId:t,severity:r,helpText:o,label:d,fieldProps:a},u]=q(n),p=s.useSeverityColor(r),f=c.useMemo(()=>Array.isArray(a.value)?a.value:[],[a.value]),{onChange:h,onBlur:g}=a,y=`${e}-droppable`,{disabled:v}=u,[b,S]=c.useState(""),[T,F]=c.useState(""),E=T||o,A=T?"red":p,C=c.useCallback(w=>{h(w),g(w)},[h,g]),P=c.useCallback(w=>{f.findIndex(M=>M.value===w.target.value.trim())>=0?F("All options must be unique"):w.target.value?F(""):F("Option cannot be empty"),S(w.target.value)},[S,f]),B=c.useCallback(()=>{if(T)return;if(!b.trim())return F("Option cannot be empty");const w=b.trim();C([...f,{value:w,label:w}]),S("")},[b,T,C,f]),K=c.useCallback(w=>{w.key==="Enter"&&(w.preventDefault(),B())},[B]),N=c.useCallback(w=>{C(ge(f,w))},[f,C]),z=c.useCallback(w=>{if(!w.destination)return;const M=w.source.index,L=w.destination.index;C(Te(f,M,L))},[C,f]);return i.jsx(j.DragDropContext,{onDragEnd:z,children:i.jsxs(s.Flex,{direction:"column",gap:"2",children:[i.jsx(O,{helpText:E,severity:r,children:i.jsx($,{severity:r,inputId:e,labelId:t,label:d,children:(!v||f.length===0)&&i.jsxs(s.Flex,{gap:"2",children:[i.jsx(s.Box,{grow:"1",children:i.jsx(s.TextField.Input,{placeholder:"Press enter to add a new option",...u,...a,value:b,onChange:P,onKeyDown:K,id:e,color:A,onBlur:void 0})}),i.jsx(s.IconButton,{type:"button","aria-label":"Add option",disabled:!!T||v,onClick:B,children:i.jsx(s.PlusIcon,{})})]})})}),i.jsx(j.Droppable,{droppableId:y,children:w=>i.jsxs(s.Flex,{...w.droppableProps,ref:w.innerRef,direction:"column",children:[f.map((M,L)=>i.jsx(j.Draggable,{draggableId:`${M.value}-draggable`,index:L,isDragDisabled:v,children:({draggableProps:W,dragHandleProps:ue,innerRef:se})=>i.jsx(s.Flex,{...ue,...W,ref:se,gap:"2",align:"center",justify:"between",mb:"1",asChild:!0,children:i.jsxs(s.Badge,{color:"gray",size:"2",children:[i.jsx("span",{children:M.label}),i.jsx(s.IconButton,{size:"small",variant:"ghost",type:"button","aria-label":"Delete option",severity:"info",disabled:v,onClick:()=>N(L),children:i.jsx(s.Cross1Icon,{})})]})})},M.value)),w.placeholder]})})]})})}),ee=class ee extends V{constructor(e){const{minimum_length:t,maximum_length:r,...o}=e;super({...o,type:"multi-string"});m(this,"minLength");m(this,"maxLength");m(this,"onlyValidateAfterTouched",!1);this.minLength=t??0,this.maxLength=r??1/0}getValueFromChangeEvent(e){if(Array.isArray(e))return e;throw new Error("Expected an array.")}getInput(e){return i.jsx(Ye,{field:this,...e})}serialize(){return{...super._serialize(),minimum_length:this.minLength,maximum_length:this.maxLength}}isBlank(e){return super.isBlank(e)||e.length===0}getFieldValidators(){const e=super.getFieldValidators();return e.push(t=>{if(Array.isArray(t)&&t.length<this.minLength)return`Must have at least ${this.minLength} options.`}),e.push(t=>{if(Array.isArray(t)&&t.length>this.maxLength)return`Must have at most ${this.maxLength} options.`}),e}static deserialize(e){if(e.type!=="multi-string")throw new Error("Type mismatch.");return new ee(e)}};m(ee,"fieldTypeName","Multi-string"),m(ee,"fieldTypeDescription","Allows the user to provide multiple unique strings."),m(ee,"Icon",s.ListBulletIcon);let oe=ee;class Qe extends V{constructor(e){super(e);m(this,"options");m(this,"onlyValidateAfterTouched",!1);const t=new Set;this.options=e.options.map(r=>(typeof r=="string"&&(r={label:r,value:r}),t.add(r.label),r)),t.size!==e.options.length&&console.error(`${e.options.length-t.size} duplicate identifiers found in options. This may cause unexpected behavior. Options:`,e.options)}_serialize(){return{...super._serialize(),options:this.options}}static getFieldCreationSchema(){return[new oe({label:"Options",description:"List possible options for the user to select from.",required:!0,identifier:"options",minimum_length:2,maximum_length:20})]}}const te=class te extends Qe{constructor(n){super({...n,type:"select"})}getValueFromChangeEvent(n){return typeof n=="string"?n:n.target.value}serialize(){return super._serialize()}static deserialize(n){if(n.type!=="select")throw new Error("Type mismatch.");return new te(n)}getInput(n){return i.jsx(Ue,{field:this,...n})}};m(te,"fieldTypeName","Dropdown"),m(te,"fieldTypeDescription","Allows the user to select a single option from a list of options."),m(te,"Icon",s.DropdownMenuIcon);let le=te;const ft=l=>l?Array.isArray(l)?l:[l]:[],Xe=c.memo(function(n){const[{inputId:e,labelId:t,severity:r,helpText:o,label:d,fieldProps:a,field:u},p]=q(n),{onChange:f,onBlur:h}=a,g=c.useMemo(()=>ft(a.value),[a.value]),y=c.useCallback(v=>{f(v),h(v)},[f,h]);return i.jsx(O,{helpText:o,severity:r,children:i.jsx($,{severity:r,inputId:e,labelId:t,label:d,children:i.jsx(s.MultiSelect,{value:g,onValueChange:y,options:u.options,name:a.name,placeholder:"Select one or more...",id:e,severity:r,...p})})})}),ie=class ie extends Qe{constructor(n){super({...n,type:"multi-select"})}getValueFromChangeEvent(n){if(Array.isArray(n))return n;throw new Error("Expected an array.")}isBlank(n){return super.isBlank(n)||n.length===0}serialize(){return super._serialize()}static deserialize(n){if(n.type!=="multi-select")throw new Error("Type mismatch.");return new ie(n)}getInput(n){return i.jsx(Xe,{field:this,...n})}};m(ie,"fieldTypeName","Multi-select"),m(ie,"fieldTypeDescription","Allows the user to select a multiple options from a list of options."),m(ie,"Icon",s.CheckboxIcon);let ae=ie;const ht=c.memo(function({field:n,...e}){const[{value:t}]=D.useField(n.options.clonedFieldIdentifier),r=c.useMemo(()=>{const o=n.options.getFieldToClone(t);return o?de(o):null},[n.options,t]);return ce(r,e)});class ye extends V{constructor(e,t){super({...e,type:"custom"});m(this,"Component");m(this,"options");this.options=e,this.Component=t}serialize(){throw new Error("Serializing only supported for public input types.")}getInput(e){const t=this.Component;return i.jsx(t,{field:this,...e})}}m(ye,"fieldTypeName","Custom"),m(ye,"fieldTypeDescription","Allows re-rendering of field already in the form");class pt extends ye{constructor(n){super(n,ht)}}const mt=c.memo(function(n){const{field:e,...t}=n,{label:r,description:o,fields:d,condition:a}=e,{values:u,setFieldValue:p}=D.useFormikContext(),f=a!=null&&a.identifier?Y(u,a.identifier):void 0,h=c.useMemo(()=>Le(a,f),[a,f]);c.useEffect(()=>{if(!h)for(const y of d)p(y.getId(),"").then()},[h,d,p]);const g=De(d,t);return h?r?i.jsx(s.Card,{children:i.jsxs(s.Flex,{direction:"column",gap:"3",children:[i.jsxs(s.Flex,{direction:"column",children:[i.jsx(s.Heading,{as:"h3",size:"3",children:r}),i.jsx(s.Text,{className:fe.description,children:o})]}),g]})}):g:null}),ne=class ne extends Oe{constructor(e){const{label:t=null,fields:r,condition:o=null,conditional:d,...a}=e;super({...a,type:"section"});m(this,"label");m(this,"fields");m(this,"condition");this.fields=r,this.condition=o,this.label=t,d===!1&&(this.condition=null)}static getFieldCreationSchema(e){return e.length===0?[]:[new G({label:"Conditional",description:"Conditionally show or hide this section.",identifier:"conditional",required:!1}),new ne({label:"Conditional settings",identifier:"conditional-settings",condition:{identifier:"conditional",value:!0},fields:[new le({label:"Field",description:"The field to use for the condition.",options:e.map(t=>!t.label||t.type==="upload"?null:{label:t.label,value:t.identifier}).filter(t=>!!t),identifier:"condition.identifier",required:!0}),new pt({label:"Value",identifier:"condition.value",required:!0,clonedFieldIdentifier:"condition.identifier",getFieldToClone(t){if(!t)return null;const r=e.find(o=>o.identifier===t);return r?{...r,label:"Value",identifier:"condition.value",description:"The value to compare against.",required:r.type!=="boolean"}:(console.error("Could not find field with identifier",t),null)}})]})]}static deserialize(e){var r;if(e.type!=="section")throw new Error("Invalid type");const t=((r=e.fields)==null?void 0:r.map(Me))??[];return new ne({...e,fields:t})}conditional(){return this.condition!==null}serialize(){return{...super._serialize(),label:this.label,condition:this.condition,conditional:this.conditional(),fields:this.fields.map(e=>e.serialize())}}getErrors(e){const t={};for(const r of this.fields){const o=r.getId(),d=r.getError(Y(e,o),e);d&&be(t,r.getId(),d)}return t}getInput(e){return i.jsx(mt,{field:this,...e})}};m(ne,"fieldTypeName","Section"),m(ne,"fieldTypeDescription","Sections can be useful for grouping fields together. They can also be conditionally shown or hidden.");let U=ne;const gt={previewImage:"_previewImage_1ig84_1"},ze=l=>{const n=["byte","kilobyte","megabyte"];let e=l,t=0;for(;e>1024&&t<n.length-1;)e/=1024,t++;return new Intl.NumberFormat([],{maximumFractionDigits:2,style:"unit",unit:n[t]}).format(e)},yt=c.memo(function(n){var A;const[{inputId:e,labelId:t,severity:r,helpText:o,label:d,fieldProps:a,field:u},p]=q(n),{onChange:f}=a,h=s.useSeverityColor(r),g=c.useRef(null),{value:y}=a,v=c.useMemo(()=>o||(u.maxFileSize?`Maximum file size: ${ze(u.maxFileSize)}`:null),[u.maxFileSize,o]),b=c.useCallback(()=>{var C;(C=g.current)==null||C.click()},[]),S=c.useCallback(C=>{const P=[...y];P.splice(C,1),f({target:{files:P}})},[y,f]),T=y?"Select new files":"Select files",F=y?"Select new file":"Select a file",E=u.maxFiles>1?T:F;return i.jsxs(s.Flex,{direction:"column",gap:"2",children:[i.jsx(O,{helpText:v,severity:r,children:i.jsxs($,{severity:r,inputId:e,labelId:t,label:d,children:[i.jsx(s.Flex,{direction:"row",gap:"2",children:i.jsx(s.Box,{width:"max-content",asChild:!0,children:i.jsxs(s.Button,{...p,onClick:b,children:[i.jsx(s.UploadIcon,{})," ",E]})})}),i.jsx("input",{...p,type:"file",ref:g,id:e,accept:(A=u.extensions)==null?void 0:A.join(","),multiple:u.maxFiles>1,color:h,style:{display:"none"},...a,value:""})]})}),Array.isArray(y)&&y.length>0&&i.jsx(s.Flex,{direction:"column",gap:"2",children:y.map((C,P)=>i.jsx(xt,{field:u,file:C,onRemove:()=>S(P),disabled:p.disabled},P))})]})}),xt=c.memo(function({file:n,field:e,onRemove:t,disabled:r}){const[o,d]=c.useState(null),a=c.useMemo(()=>o&&e.getError([o]),[e,o]),{url:u,name:p,size:f}=c.useMemo(()=>{let h=null,g,y;return o!=null&&o.type.startsWith("image/")&&(h=URL.createObjectURL(o)),o?(g=o.name,y=ze(o.size)):(g="Downloading...",y="..."),{url:h,name:g,size:y}},[o]);return c.useEffect(()=>{n instanceof Promise?n.then(d):d(n)},[n]),i.jsx(s.Card,{children:i.jsxs(s.Flex,{direction:{initial:"column",sm:"row"},gap:"3",justify:"between",children:[i.jsxs(s.Flex,{direction:"row",gap:"3",align:"center",grow:"1",shrink:"0",children:[i.jsx(s.IconButton,{severity:"info",variant:"outline","aria-label":`Remove ${p}`,disabled:r,onClick:t,children:i.jsx(s.Cross1Icon,{})}),i.jsxs(s.Flex,{direction:"column",gap:"1",children:[i.jsx(s.Text,{children:p}),i.jsx(s.Text,{size:"1",children:f}),a&&i.jsx(s.Text,{size:"1",severity:"danger",children:a})]})]}),u&&i.jsx("img",{className:gt.previewImage,src:u,alt:p})]})})}),Je=50*1024*1024,re=class re extends V{constructor(e){const{extensions:t,maximum_files:r,maximum_size:o,...d}=e;super({...d,type:"upload"});m(this,"extensions");m(this,"maxFileSize");m(this,"maxFiles");m(this,"onlyValidateAfterTouched",!1);this.maxFileSize=typeof o=="number"?o:void 0,this.maxFiles=Math.max(typeof r=="number"?r:1,1),this.extensions=t}getValueFromChangeEvent(e){return Array.from(e.target.files||[])}isBlank(e){return super.isBlank(e)||e.length===0}static getFieldCreationSchema(){return[new H({label:"How many files can be uploaded?",description:"By default, only one file can be uploaded.",required:!1,minimum:1,maximum:10,identifier:"maximum_files"}),new H({label:"What is the maximum size of each file?",description:"Maximum file size in bytes.",required:!1,identifier:"maximum_size",minimum:1,maximum:Je,integers:!0}),new ae({label:"Accepted file types",description:"Types of allowed files to upload. If left blank, all files will be accepted.",required:!1,identifier:"extensions",options:[{value:"image/*",label:"Images"},{value:"audio/*",label:"Audio files"},{value:"video/*",label:"Videos"},{value:"text/*",label:"Text files"},{value:"application/*",label:"Application files (includes PDFs and Word documents)"}]})]}getFieldValidators(){const e=super.getFieldValidators(),t=this.maxFileSize??Je,r=this.maxFiles??1;return e.push(o=>{if(o&&o.some(d=>d.size>t))return`Files must be at most ${ze(t)}.`}),e.push(o=>{if(o&&o.length>r)return`You can only upload ${r} files.`}),e}serialize(){return{...super._serialize(),extensions:this.extensions,maximum_size:this.maxFileSize,maximum_files:this.maxFiles}}static deserialize(e){if(e.type!=="upload")throw new Error("Type mismatch.");return new re(e)}getInput(e){return i.jsx(yt,{field:this,...e})}};m(re,"fieldTypeName","Upload"),m(re,"fieldTypeDescription","Allows a file to be uploaded."),m(re,"Icon",s.UploadIcon);let Ee=re;const Ae={date:he,number:H,boolean:G,select:le,string:Q,text:X,custom:ye,upload:Ee,"multi-string":oe,"multi-select":ae},Me=l=>{const n=l.type;return Ae[n].deserialize(l)},de=l=>l.type==="section"?U.deserialize(l):Me(l);function Pe(l,n={}){const{readonly:e=!1}=n;return{title:l.title,description:l.description,fields:l.fields.map(t=>de(t)),meta:{readonly:e}}}function xe(l){return!!(Array.isArray(l)&&l.some(n=>n instanceof File||n instanceof Promise))}function Le(l,n){if(!l)return!0;if(xe(n)||xe(l.value))throw new Error("Conditions do not support file uploads");const e=Array.isArray(n)?n.map(r=>typeof r=="string"?r:r.value):n,t=Array.isArray(l.value)?l.value.map(r=>typeof r=="string"?r:r.value):l.value;if(Array.isArray(t)&&Array.isArray(e)){for(const r of t)if(!e.includes(r))return!1;return!0}return t===n}const ce=(l,n)=>c.useMemo(()=>!n||!l?null:l.getInput(n),[l,n]),De=(l,n)=>{const e=c.useMemo(()=>l.map(t=>i.jsx("div",{children:t.getInput(n)},t.getId())),[l,n]);return i.jsx(s.Flex,{direction:"column",gap:"2",children:e})},ve=l=>Object.keys(l).length>0,Ze=async(l,n)=>{const e={};for(const t of l.fields)if(t instanceof U){if(t.condition){const{identifier:r}=t.condition;if(!Le(t.condition,Y(n,r)))continue}Object.assign(e,t.getErrors(n))}else{if(!(t instanceof V))throw new Error("Invalid field type");const r=t.getId(),o=t.getError(Y(n,r),n);o&&be(e,r,o)}if(ve(e))return e},vt=[null,void 0],Ve=(l,n)=>l.reduce((e,t)=>t instanceof U?{...e,...Ve(t.fields,n)}:(vt.includes(Y(e,t.getId()))&&be(e,t.getId(),""),e),n),wt=()=>{throw new Error("onSubmit must be provided if form is not readonly.")},we=c.memo(c.forwardRef((l,n)=>{const{schema:e,values:t={},onSubmit:r=wt,submitText:o="Submit",cancelText:d,onCancel:a,onDirty:u,hideTitle:p=!e.title,hideDescription:f,className:h}=l,{readonly:g}=e.meta,y=c.useMemo(()=>crypto.randomUUID(),[]),v=D.useFormik({initialValues:Ve(e.fields,t),onSubmit:r,validate:E=>Ze(e,E),validateOnBlur:!1,validateOnChange:!1}),{dirty:b}=v,S=c.useMemo(()=>typeof e.title=="string"?i.jsx(s.Heading,{children:e.title}):e.title,[e.title]),T=c.useMemo(()=>typeof e.description=="string"?i.jsx(s.Text,{className:fe.description,children:e.description}):e.description,[e.description]),F=De(e.fields,{formId:y,disabled:g});return c.useEffect(()=>{b&&u&&u()},[b,u]),i.jsx(D.FormikProvider,{value:v,children:i.jsx(s.Flex,{ref:n,direction:"column",gap:"2",className:h,asChild:!0,children:i.jsxs("form",{id:y,onSubmit:v.handleSubmit,children:[!p&&i.jsx(s.Card,{children:i.jsxs(s.Flex,{direction:"column",gap:"1",children:[S,!f&&T]})}),F,!g&&i.jsxs(s.Flex,{justify:"end",gap:"2",children:[d&&i.jsx(s.Button,{type:"button",variant:"soft",onClick:a,children:d}),i.jsx(s.Button,{type:"submit",disabled:!v.isValid,children:o})]})]})})})})),Ft=c.memo(c.forwardRef((l,n)=>{const{submission:e,showFormDescription:t=!1,showFormTitle:r=!0}=l,o=I.useAppSelector(I.selectFormRevision(e.form_revision)),{sdk:d}=I.useSDK();if(!o)throw new Error(`Could not find revision ${e.form_revision} for submission ${e.offline_id}.`);const a=c.useMemo(()=>Pe(o,{readonly:!0}),[o]),u=c.useMemo(()=>{const p=I.selectSubmissionAttachments(e.offline_id)(d.store.getState())??[],f={};for(const h of p){const g=d.files.fetchFileFromUrl(h.file,h.file_sha1,h.file_name).then(v=>{if(!v.success)throw new Error(`Failed to download attachment ${h.file_name}.`);return v.body}),y=f[h.field_identifier];y?y.push(g):f[h.field_identifier]=[g]}return{...e.values,...f}},[d.files,d.store,e.offline_id,e.values]);return i.jsx(we,{ref:n,schema:a,values:u,hideDescription:!t,hideTitle:!r})})),ke={favoriteIcon:"_favoriteIcon_1bixi_1",regularIcon:"_regularIcon_1bixi_9"},Be="organization:",_e="user:",It=c.memo(c.forwardRef((l,n)=>{const{maxResults:e=20,...t}=l,[r,o]=c.useState(""),[d,a]=c.useState(""),{sdk:u}=I.useSDK(),p=c.useMemo(()=>{const F={maxResults:e,searchTerm:r};return d&&(d.startsWith(Be)?F.owner_organization=parseInt(d.slice(Be.length)):d.startsWith(_e)&&(F.owner_user=parseInt(d.slice(_e.length)))),F},[r,e,d]),f=I.useAppSelector(I.selectFilteredUserForms(p))??[],h=I.useAppSelector(I.selectUserFormMapping),g=c.useCallback(F=>{F.favorite?u.userForms.unfavorite(F.offline_id).then():u.userForms.favorite(F.offline_id).then()},[u]),y=c.useMemo(()=>{const F=u.store.getState(),E={};for(const A of Object.values(h)){const C=I.selectOrganization(A.owner_organization||-1)(F);C&&(E[`${Be}${C.id}`]=C.name);const P=I.selectUser(A.owner_user||-1)(F);P&&(E[`${_e}${P.id}`]=P.username)}return Object.entries(E).map(([A,C])=>({itemContent:C,value:A}))},[h,u.store]),v=c.useCallback(F=>{o(F.currentTarget.value)},[]),S=(I.useAppSelector(I.selectNumberOfUserForms)||0)-f.length,T=f.length==e&&S>0?`Only the first ${e} results are shown (${S} hidden)`:S>0&&`${S} hidden forms`;return i.jsxs(s.Flex,{ref:n,direction:"column",gap:"2",children:[i.jsxs(s.Flex,{gap:"2",grow:"1",children:[i.jsx(s.Box,{grow:"1",asChild:!0,children:i.jsx(s.TextField.Root,{size:"3",children:i.jsx(s.TextField.Input,{placeholder:"Filter",value:r,onChange:v})})}),i.jsx(s.Select,{items:y,value:d,onValueChange:a,placeholder:"Owner",size:"large"})]}),f.length>0&&i.jsx(s.ButtonList.Root,{children:f.map(F=>i.jsx(Ct,{...t,form:F,handleToggleFavorite:()=>g(F)},F.offline_id))}),i.jsx(s.Box,{px:"3",children:i.jsx(s.Text,{size:"2",severity:"info",children:T})})]})})),Ct=l=>{var g;const{form:n,onSelectForm:e,isFavoriteEditable:t,handleToggleFavorite:r}=l,o=(g=I.useAppSelector(I.selectOrganization(n.owner_organization||-1)))==null?void 0:g.name,d=I.useAppSelector(I.selectUser(n.owner_user||-1)),a=I.useAppSelector(I.selectCurrentUser).id,u=!!d&&d.id===a,p=o??(u?"You":d==null?void 0:d.username)??"Unknown",f=c.useCallback(y=>{y.stopPropagation(),r()},[r]),h=i.jsx(s.ButtonList.Item,{onClick:()=>e(n),asChild:!0,children:i.jsxs(s.Flex,{justify:"between",gap:"2",py:"2",px:"3",...s.divButtonProps,children:[i.jsxs(s.Flex,{grow:"1",align:"center",gap:"2",children:[i.jsx(s.IconButton,{className:I.classNames(n.favorite?ke.favoriteIcon:ke.regularIcon),variant:"ghost",onClick:f,"aria-label":n.favorite?"Favorite form":"Standard form",disabled:!t,children:n.favorite?i.jsx(s.StarFilledIcon,{}):i.jsx(s.StarIcon,{})}),i.jsx(s.Text,{noWrap:!0,children:n.latestRevision.title}),n.latestRevision.description&&i.jsx(s.QuestionMarkCircledIcon,{})]}),p&&i.jsxs(s.Flex,{align:"center",gap:"2",children:[i.jsx(s.PersonIcon,{})," ",p]})]})});return n.latestRevision.description?i.jsx(s.Tooltip,{content:n.latestRevision.description,children:h},n.offline_id):h},Re={submissionsContainer:"_submissionsContainer_9iirt_1",stopHorizontalOverflow:"_stopHorizontalOverflow_9iirt_6"},bt=c.memo(function(n){var F;const{submission:e,onSubmissionClick:t,compact:r,labelType:o,rowDecorator:d}=n,a=I.useAppSelector(I.selectCurrentUser),u=I.useAppSelector(I.selectUser("created_by"in e?e.created_by:a.id)),p=$e(e),f=I.isToday(p)?p.toLocaleTimeString([],{hour:"2-digit",minute:"2-digit"}):I.getLocalDateString(p),h=I.useAppSelector(I.selectFormRevision(e.form_revision));if(!h)throw new Error(`Could not find revision ${e.form_revision} for submission ${e.offline_id}.`);const g=(F=I.useAppSelector(I.selectLatestFormRevision(h.form)))==null?void 0:F.revision,y=I.useFileSrc({file:(u==null?void 0:u.profile.file)??null,fileSha1:(u==null?void 0:u.profile.file_sha1)??null}),v=(u==null?void 0:u.username.charAt(0).toUpperCase())??"?",b=h.revision===g,S=c.useCallback(()=>{t&&t({submission:e})},[e,t]),T=i.jsx(s.ButtonList.Item,{onClick:S,asChild:!0,children:i.jsxs(s.Flex,{grow:"1",width:"100%",p:"2",gap:"2",justify:"between",children:[i.jsxs(s.Flex,{gap:"2",align:"center",className:Re.stopHorizontalOverflow,children:[i.jsx(s.Avatar,{src:y,size:"1",fallback:v}),i.jsx(s.Text,{size:"2",noWrap:!0,children:o==="creator"?(u||a).username:h.title})]}),i.jsxs(s.Flex,{gap:"2",align:"center",children:[!r&&(h.revision?i.jsx(s.Badge,{variant:"soft",severity:b?"primary":"info",children:r?h.revision.toString():`Revision #${h.revision}`}):!!g&&i.jsx(s.Badge,{children:"Original"})),i.jsx(s.Text,{size:"2",noWrap:!0,children:f})]})]})});return d?d(e,T):T}),$e=l=>{const n="created_at"in l?l.created_at:l.submitted_at;return new Date(n)},Tt=c.memo(function(n){const{formId:e,submissions:t,compact:r=!1,className:o,after:d,variant:a="outline",...u}=n;if(!!e==!!t)throw new Error("Either formId or submissions must be provided, but not both.");const p=I.useAppSelector(t?()=>t:I.selectSubmissionsForForm(e)),f=c.useMemo(()=>p==null?void 0:p.sort((h,g)=>$e(g).getTime()-$e(h).getTime()),[p]);return i.jsx(s.ButtonList.Root,{className:I.classNames(Re.submissionsContainer,o),size:"small",variant:a,before:!r&&i.jsxs(s.Text,{severity:"info",children:["There are ",((p==null?void 0:p.length)||0).toString()," submissions of this form."]}),after:d,children:f==null?void 0:f.map((h,g)=>i.jsx(bt,{submission:h,compact:r,...u},g))})}),St=c.memo(function(n){const{name:e,render:t}=n,{submitForm:r}=D.useFormikContext(),[o,d,a]=D.useField(e);return c.useMemo(()=>{const u=p=>a.setValue(p,!1);return t({value:o.value,setValue:u,patchValue:r})},[r,a,o.value,t])}),zt=c.memo(c.forwardRef((l,n)=>{const{children:e,schema:t,values:r,onPatch:o,onError:d,...a}=l,u=c.useMemo(()=>Ve(t.fields,r),[t.fields,r]),p=c.useCallback(v=>{const b={};for(const S in v){const T=v[S];T!==u[S]&&T!==void 0&&(b[S]=T)}ve(b)&&o(b)},[u,o]),f=c.useCallback(async v=>{const b=await Ze(t,v);return b&&d(b),b},[t,d]),h=D.useFormik({initialValues:u,onSubmit:p,validate:f,validateOnBlur:!1,validateOnChange:!1}),{errors:g,resetForm:y}=h;return c.useEffect(()=>{ve(g)&&y({values:u,errors:{}})},[g,u,y]),i.jsx(D.FormikProvider,{value:h,children:i.jsx("form",{...a,ref:n,onSubmit:h.handleSubmit,children:e})})})),et={...Ae,section:U},Et=c.memo(function(n){const{field:e,setFieldType:t}=n,r=e.fieldTypeName,o=e.fieldTypeDescription,d=e.Icon;return i.jsxs(s.Flex,{gap:"4",align:"center",children:[i.jsx(s.Button,{type:"button",variant:"surface",onClick:t,style:{width:"135px"},children:i.jsxs(s.Flex,{gap:"3",align:"center",grow:"1",children:[i.jsx(d,{}),r]})}),i.jsx(s.Text,{children:o})]})}),tt=[["string","text"],["select","multi-select","upload"],["boolean","date","number","multi-string"]],At=tt.length-1,Mt=c.memo(function(n){const{setFieldType:e}=n;return i.jsx(s.Flex,{direction:"column",gap:"3",children:tt.map((t,r)=>i.jsxs(s.Flex,{direction:"column",gap:"3",children:[i.jsx(s.Flex,{direction:"column",gap:"2",children:t.map(o=>i.jsx(Et,{field:Ae[o],setFieldType:()=>e(o)},o))}),r<At&&i.jsx(s.Separator,{size:"4"})]},r))})}),Pt=l=>n=>{if(!(!n||typeof n!="string")&&l.includes(n.trim()))return"This name is already taken."},Lt=(l,n)=>{const e=[new Q({label:"Label",required:!0,maxLength:200,identifier:"label",fieldValidators:[Pt(l)]}),new X({label:"Description",required:!1,maxLength:1e3,identifier:"description"})];return n==="section"?e:[...e,new G({label:"Required",description:null,required:!1,identifier:"required"})]},Dt=c.memo(function(n){const{fieldType:e,handleCancel:t,handleCreateField:r,defaultField:o,conditionalSourceFields:d}=n,a=et[e],u=D.useFormikContext(),p=c.useMemo(()=>{const f=u.values.fields.flatMap(g=>g.type==="section"?[...g.fields.map(y=>y.label),g.label]:g.label).filter(g=>g!==(o==null?void 0:o.label)&&g!==null);let h=Lt(f,e);if(a===U){if(d===void 0)throw new Error("Conditional source fields must be provided when changing sections.");h=h.concat(a.getFieldCreationSchema(d))}else{if(!(a.prototype instanceof V))throw new Error(`Field must be an instance of BaseField. Got ${a}.`);h=[...h,...a.getFieldCreationSchema()]}return{fields:h,meta:{readonly:!1},title:null}},[u.values.fields,e,a,o==null?void 0:o.label,d]);return i.jsx(we,{schema:p,values:o,onSubmit:r,cancelText:o?void 0:"Back",onCancel:t})}),Fe=c.memo(function(n){const{parentPath:e,index:t,children:r,initial:o,editing:d,conditionalSourceFields:a}=n,[u,p]=c.useState(),f=(o==null?void 0:o.type)??u,h=f?et[f].fieldTypeName:void 0,{setFieldValue:g,values:y}=D.useFormikContext();if(d&&!o)throw new Error("Initial field must be provided if editing is true.");const v=!f&&!d&&!o,b=v?"Choose a field type":`${h} settings`,S=v?"Select a field type to add to this section.":(h==null?void 0:h.toLowerCase())==="section"?"Customize your section":`Customize your ${h==null?void 0:h.toLowerCase()} field.`,T=c.useCallback(()=>p(void 0),[]),F=c.useCallback(C=>{p(void 0),C()},[]),E=c.useCallback((C,P)=>{const{label:B}=C;if(!f)throw new Error("Field type must be selected before creating a field.");if(!B||typeof B!="string")throw new Error("Label must be provided before creating a field.");const K=de({type:f,...C,identifier:ut(C.identifier,B)}).serialize(),N=Y(y,e);if(N===void 0)throw new Error("Parent path must point to an existing field.");let z;if(!Array.isArray(N))throw new Error("Parent path must point to an array.");d?z=ct(N,t,K):z=Ge(N,t,K),g(e,z).then(),P()},[d,f,y,e,g,t]),A=c.useCallback(C=>v?i.jsx(Mt,{setFieldType:p}):i.jsx(Dt,{conditionalSourceFields:a,handleCancel:T,handleCreateField:P=>E(P,C),fieldType:f,defaultField:o}),[a,T,E,o,v,f]);return i.jsx(s.Dialog,{onCloseInterrupt:F,title:b,description:S,content:A,children:r})}),it=({children:l})=>i.jsx(i.Fragment,{children:l}),nt=(l,n)=>({initial:l?n:"none",sm:l?"none":n}),rt=c.memo(function(n){const{remove:e,dragHandleProps:t,editProps:r,insertAfterProps:o}=n,d=c.useMemo(()=>[{Wrapper:Fe,wrapperProps:r,Icon:s.Pencil1Icon,text:"Edit"},{Icon:s.TrashIcon,buttonProps:{onClick:e},text:"Delete"},{Wrapper:Fe,wrapperProps:o,Icon:s.PlusIcon,text:"Add after"},{Icon:a=>i.jsx("div",{...a,children:i.jsx(s.DragHandleDots2Icon,{})}),text:"Reorder",disableOnMobile:!0,buttonProps:{...t,asChild:!0}}],[t,r,o,e]);return i.jsxs(i.Fragment,{children:[i.jsx(s.Flex,{gap:"4",display:nt(!1,"flex"),children:d.map(a=>{const u=a.Wrapper??it;return i.jsx(u,{...a.wrapperProps,children:i.jsx(s.IconButton,{type:"button",variant:"ghost","aria-label":a.text,...a.buttonProps,children:i.jsx(a.Icon,{})})},a.text)})}),i.jsx(s.Box,{display:nt(!0,"block"),children:i.jsx(s.DropdownMenu,{trigger:i.jsx(s.IconButton,{variant:"ghost","aria-label":"Actions menu",children:i.jsx(s.DotsVerticalIcon,{})}),closeOnSelect:!1,items:d.map(a=>{var p;if(a.disableOnMobile)return null;const u=a.Wrapper??it;return{...a.buttonProps,onSelect:(p=a.buttonProps)==null?void 0:p.onClick,content:i.jsx(u,{...a.wrapperProps,children:i.jsxs(s.Flex,{gap:"2",align:"center",children:[i.jsx(a.Icon,{}),a.text]})})}}).filter(a=>a!==null)})})]})}),Ie="form-builder",Vt=c.memo(function(n){const{field:e,index:t,sectionIndex:r,remove:o}=n,d=c.useMemo(()=>de(e),[e]),a=ce(d,{formId:Ie,disabled:!0}),u=c.useMemo(()=>({index:t,parentPath:`fields.${r}.fields`,initial:e,editing:!0}),[e,t,r]),p=c.useMemo(()=>({parentPath:`fields.${r}.fields`,index:t+1,initial:void 0}),[t,r]);return i.jsx(j.Draggable,{draggableId:e.identifier,index:t,children:f=>i.jsx(s.Card,{ref:f.innerRef,...f.draggableProps,...f.dragHandleProps,mb:"4",children:i.jsxs(s.Flex,{gap:"4",justify:"between",align:"center",children:[a,i.jsx(rt,{remove:o,editProps:u,insertAfterProps:p,dragHandleProps:f.dragHandleProps})]})})})}),Bt=c.memo(function(n){var E,A,C,P,B,K,N;const{field:e,index:t,dropState:r}=n,o=(E=r[e.identifier])==null?void 0:E.disabled,{setFieldValue:d,values:a}=D.useFormikContext(),u=s.useAlertDialog(),p=c.useCallback((z,w)=>{for(const M of z){const L=w.indexOf(M);d(`fields.${L}.condition`,null).then(),d(`fields.${L}.conditional`,!1).then()}},[d]),f=c.useCallback(z=>{var L;const w=e.fields[z];if(!w)throw new Error("Could not find field to remove.");const M=[];for(const W of a.fields)((L=W.condition)==null?void 0:L.identifier)===w.identifier&&M.push(W);return{removing:w,affectedSections:M,action:()=>d(`fields.${t}.fields`,ge(e.fields,z))}},[e.fields,a.fields,d,t]),h=c.useCallback(z=>{const{affectedSections:w,action:M,removing:L}=f(z),W=()=>{M().then(),p(w,a.fields)};if(w.length>0){const ue=w.map(se=>se.label).join(", ");return u({title:"Remove condition?",description:`${L.label} is being used as a condition, deleting it will remove the condition from the ${ue} section(s).`,severity:"danger",actionText:"Remove",onAction:W})}W()},[f,p,a.fields,u]),g=c.useCallback(()=>{const w=e.fields.map((Ce,Kt)=>f(Kt)).flatMap(Ce=>Ce.affectedSections),M=w.length?"Remove fields and conditions?":"Remove fields?",L=e.fields.length,W=w.map(Ce=>Ce.label).join(", "),ue=w.length?`Deleting this section will remove the ${L} field(s) it contains and will remove the conditions from following sections: ${W}`:`Deleting this section will remove the ${L} field(s) it contains.`,se=ge(a.fields,t),lt=()=>d("fields",se);if(w.length>0)return u({title:M,description:ue,severity:"danger",actionText:"Remove",onAction:()=>{lt().then(()=>{p(w,se)})}});lt().then()},[e.fields,a.fields,t,f,d,u,p]),y=c.useMemo(()=>({index:t,parentPath:"fields",initial:e,editing:!0,conditionalSourceFields:Se(a.fields,t)}),[e,t,a.fields]),v=c.useMemo(()=>({index:t+1,parentPath:"fields",initial:me(),conditionalSourceFields:Se(a.fields,t+1)}),[t,a.fields]),b=c.useMemo(()=>({parentPath:`fields.${t}.fields`,index:e.fields.length,initial:void 0}),[e.fields.length,t]),S=c.useMemo(()=>{var z,w;return(w=Ke(a.fields,(z=e.condition)==null?void 0:z.identifier))==null?void 0:w.label},[(A=e.condition)==null?void 0:A.identifier,a.fields]),T=Array.isArray((C=e.condition)==null?void 0:C.value)?"contains all of":"equals";if(xe((P=e.condition)==null?void 0:P.value))throw new Error("File values are not supported for conditions.");const F=Array.isArray((B=e.condition)==null?void 0:B.value)?(K=e.condition)==null?void 0:K.value.map(z=>typeof z=="string"?z:z.label).join(", "):(N=e.condition)==null?void 0:N.value.toString();return i.jsx(j.Draggable,{draggableId:e.identifier,index:t,children:z=>i.jsx(s.Card,{ref:z.innerRef,...z.draggableProps,...z.dragHandleProps,mb:"4",children:i.jsxs(s.Flex,{gap:"3",justify:"between",align:"center",children:[i.jsxs(s.Flex,{direction:"column",gap:"2",grow:"1",children:[i.jsxs(s.Flex,{direction:"column",children:[i.jsx(s.Heading,{as:"h3",size:"3",children:e.label}),i.jsx(s.Text,{className:fe.description,children:e.description})]}),e.condition&&i.jsx(s.Text,{size:"1",children:i.jsxs(s.Em,{children:["Display only if ",i.jsx(s.Strong,{children:S})," ",T," ",i.jsx(s.Strong,{children:F})]})}),i.jsx(j.Droppable,{droppableId:e.identifier,type:"SECTION",isDropDisabled:o,children:w=>i.jsxs(s.Flex,{ref:w.innerRef,...w.droppableProps,direction:"column",gap:"0",children:[e.fields.map((M,L)=>i.jsx(Vt,{field:M,index:L,sectionIndex:t,remove:()=>h(L)},M.identifier)),w.placeholder,i.jsx(Fe,{...b,children:i.jsxs(s.Button,{type:"button",variant:"outline",children:[i.jsx(s.PlusIcon,{})," Add a field"]})})]})})]}),i.jsx(rt,{remove:g,insertAfterProps:v,dragHandleProps:z.dragHandleProps,editProps:y})]})})})}),_t=(l,n)=>{var t;const e={...l};switch(n.type){case"release":for(const r in e)e[r].disabled=!1;return e;case"hold":for(const r in e)(t=e[r])!=null&&t.conditionFields.has(n.fieldId)&&(e[r].disabled=!0);return e;case"update":return n.state}},$t=(l,n)=>{if(n)for(let e=0;e<l.length;e++){const t=l[e];if(t){for(const r of t.fields)if(r.identifier===n)return e}}},st=l=>{var e,t,r;const n={};for(let o=0;o<l.length;o++){const d=l[o];if(!d)throw new Error("Field is undefined.");const a=o>0?(e=n[l[o-1].identifier])==null?void 0:e.conditionFields:void 0,u=new Set(a);(t=d.condition)!=null&&t.identifier&&u.add(d.condition.identifier),n[d.identifier]={disabled:!1,conditionFields:u,conditionIndex:$t(l,(r=d.condition)==null?void 0:r.identifier),index:o,label:d.label}}return n},ot=(l,n)=>{for(const[e,t]of Object.entries(l))if(t.identifier===n)return[t,e]},Ot=c.memo(function(){const{values:n,setFieldValue:e}=D.useFormikContext(),[t,r]=c.useReducer(_t,n.fields,st),{showInfo:o}=s.useToast();c.useEffect(()=>{r({type:"update",state:st(n.fields)})},[r,n.fields]);const d=c.useCallback(p=>{p.type==="SECTION"&&r({type:"hold",fieldId:p.draggableId})},[]),a=c.useCallback(p=>{const{source:f,destination:h,type:g,reason:y,draggableId:v}=p;if(r({type:"release"}),!h||y==="CANCEL")return;if(g==="ROOT"){const E=t[v];if(!E)throw new Error("Could not find section context.");let A=typeof E.conditionIndex<"u"?Math.max(E.conditionIndex+1,h.index):h.index;for(const C of Object.values(t))C.conditionIndex===f.index&&(A=Math.min(A,C.index-1));return A!=h.index&&o({title:"Reordered sections",description:"Sections with conditions must be below the fields they reference."}),e("fields",Te(n.fields,f.index,A))}if(g!=="SECTION")throw new Error("Unexpected droppable type.");const[b,S]=ot(n.fields,f.droppableId)??[],[T,F]=ot(n.fields,h.droppableId)??[];if(!(b!=null&&b.fields)||!T)throw new Error("Could not find section with fields.");if(b.identifier===T.identifier)e(`fields.${S}.fields`,Te(b.fields,f.index,h.index)).then();else{const E=b.fields[f.index];if(!E)throw new Error("Could not find field to reorder.");e(`fields.${S}.fields`,ge(b.fields,f.index)).then(),e(`fields.${F}.fields`,Ge(T.fields,h.index,E)).then()}},[n.fields,e,t,o]),u=c.useMemo(()=>({index:n.fields.length,parentPath:"fields",initial:me(),conditionalSourceFields:Se(n.fields,n.fields.length)}),[n.fields]);return i.jsx(j.DragDropContext,{onDragStart:d,onDragEnd:a,children:i.jsx(j.Droppable,{droppableId:"droppable",type:"ROOT",children:p=>i.jsxs(s.Flex,{ref:p.innerRef,...p.droppableProps,direction:"column",gap:"0",children:[n.fields.map((f,h)=>i.jsx(Bt,{field:f,index:h,dropState:t},f.label)),p.placeholder,i.jsx(Fe,{...u,children:i.jsxs(s.Button,{type:"button",variant:"outline",children:[i.jsx(s.PlusIcon,{})," Add a section"]})})]})})})}),qt={title:"",description:"",fields:[]},Nt=new Q({label:"Title",minLength:0,maxLength:100,required:!0,identifier:"title"}),Wt={formId:Ie,placeholder:"Give your form a title."},jt=new X({label:"Description",minLength:0,maxLength:1e3,required:!1,identifier:"description"}),Ht={formId:Ie,placeholder:"Explain the purpose of this form."},Ut=()=>{alert("This is a form preview, your data will not be saved.")},Gt=c.memo(c.forwardRef((l,n)=>{const{onCancel:e,onSave:t,revision:r}=l,o=r?"Edit form":"Create a new form",{heading:d=o}=l,a=c.useCallback(y=>{const v={};if(y.title||(v.title="Title is required."),(!y.fields||y.fields.length===0)&&(v.fields="At least one field is required."),ve(v))return v},[]),u=D.useFormik({initialValues:dt(r)??qt,validate:a,onSubmit:y=>t(y),validateOnChange:!1,validateOnBlur:!1}),p=c.useMemo(()=>Pe(u.values),[u.values]),f=ce(Nt,Wt),h=ce(jt,Ht),g=c.useMemo(()=>typeof d=="object"?d:i.jsx(s.Heading,{children:d}),[d]);return i.jsx(s.Tabs.Root,{ref:n,defaultValue:"edit",children:i.jsxs(s.Flex,{direction:"column",gap:"2",children:[i.jsxs(s.Tabs.List,{children:[i.jsx(s.Tabs.Trigger,{value:"edit",children:"Edit"}),i.jsx(s.Tabs.Trigger,{value:"preview",children:"Preview"})]}),i.jsxs(s.Tabs.Content,{value:"edit",children:[g,i.jsxs(s.Text,{children:["Add a new form field by clicking a + button. Specify options for each field, then drag and drop to rearrange them. You can see what a submitted form might look like in the"," ",i.jsx("em",{children:"Preview"})," tab, but"," ",i.jsx("strong",{children:"field values entered on this page will not be saved."})]}),i.jsx(s.Flex,{asChild:!0,direction:"column",gap:"2",mt:"3",children:i.jsxs("form",{id:Ie,onSubmit:u.handleSubmit,children:[i.jsxs(D.FormikProvider,{value:u,children:[f,h,i.jsx(Ot,{}),i.jsx(s.Text,{severity:"danger",size:"1",children:typeof u.errors.fields=="string"&&u.errors.fields})]}),i.jsxs(s.Flex,{justify:"end",gap:"2",children:[i.jsx(s.Button,{type:"button",variant:"soft",onClick:e,children:"Cancel"}),i.jsx(s.Button,{type:"submit",disabled:!u.isValid,children:"Save"})]})]})})]}),i.jsx(s.Tabs.Content,{value:"preview",children:i.jsx(we,{schema:p,onSubmit:Ut})})]})})}));x.BooleanField=G,x.BooleanInput=qe,x.DateField=he,x.DateInput=We,x.FieldSection=U,x.FormBrowser=It,x.FormBuilder=Gt,x.FormRenderer=we,x.FormSubmissionBrowser=Tt,x.FormSubmissionViewer=Ft,x.MultiSelectField=ae,x.MultiSelectInput=Xe,x.MultiStringField=oe,x.MultiStringInput=Ye,x.NumberField=H,x.NumberInput=Ne,x.PatchField=St,x.PatchFormProvider=zt,x.SelectField=le,x.SelectInput=Ue,x.StringField=Q,x.StringInput=je,x.TextField=X,x.TextInput=He,x.deserialize=de,x.deserializeField=Me,x.formRevisionToSchema=Pe,x.isConditionMet=Le,x.useFieldInput=ce,x.useFieldInputs=De,x.valueIsFile=xe,Object.defineProperty(x,Symbol.toStringTag,{value:"Module"})});
package/dist/style.css CHANGED
@@ -1 +1 @@
1
- ._description_17zed_1{white-space:pre-line}._favoriteIcon_1bixi_1._favoriteIcon_1bixi_1{color:var(--yellow-a9)}._favoriteIcon_1bixi_1._favoriteIcon_1bixi_1:hover:not(:disabled){background-color:transparent;color:var(--gray-a9)}._regularIcon_1bixi_9._regularIcon_1bixi_9:hover:not(:disabled){color:var(--yellow-a9);background-color:transparent}._submissionsContainer_9iirt_1{overflow-y:auto;max-height:min(369px,100vh - 200px)}._stopHorizontalOverflow_9iirt_6{min-width:0}
1
+ ._description_17zed_1{white-space:pre-line}._previewImage_1ig84_1{max-height:100px;border-radius:max(var(--radius-2),var(--radius-4));flex-basis:50%;object-fit:cover}._favoriteIcon_1bixi_1._favoriteIcon_1bixi_1{color:var(--yellow-a9)}._favoriteIcon_1bixi_1._favoriteIcon_1bixi_1:hover:not(:disabled){background-color:transparent;color:var(--gray-a9)}._regularIcon_1bixi_9._regularIcon_1bixi_9:hover:not(:disabled){color:var(--yellow-a9);background-color:transparent}._submissionsContainer_9iirt_1{overflow-y:auto;max-height:min(369px,100vh - 200px)}._stopHorizontalOverflow_9iirt_6{min-width:0}
package/dist/typings.d.ts CHANGED
@@ -1,5 +1,11 @@
1
1
  import { FieldValue } from "@overmap-ai/core";
2
2
  import { AnyField, BaseField } from "./fields";
3
- export type Form = Record<string, FieldValue>;
3
+ /**
4
+ * When upload fields are given initial values, that value will be
5
+ * a `Promise<File>[]` instead of a `File[]` as the files must be downloaded.
6
+ * `Promise<File>[]` is not add to the `FieldValue` type as it is not a valid
7
+ * value for submitted forms as the files must be present to be submitted
8
+ */
9
+ export type Form = Record<string, FieldValue | Promise<File>[]>;
4
10
  /** Helper type that extracts the TValue type from a BaseField. */
5
11
  export type ValueOfField<Type extends AnyField> = Type extends BaseField<infer TValue> ? TValue : never;
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "description": "Form functionality for Overmap",
4
4
  "author": "Wôrdn Inc.",
5
5
  "license": "UNLICENSED",
6
- "version": "1.0.0",
6
+ "version": "1.0.2",
7
7
  "type": "module",
8
8
  "main": "dist/forms.umd.cjs",
9
9
  "module": "dist/forms.js",
@@ -35,7 +35,7 @@
35
35
  },
36
36
  "devDependencies": {
37
37
  "@overmap-ai/blocks": "^1.0.1",
38
- "@overmap-ai/core": "^1.0.1",
38
+ "@overmap-ai/core": "^1.0.2-wor-154-upload-field-types.1",
39
39
  "@rollup/plugin-commonjs": "^25.0.4",
40
40
  "@storybook/addon-a11y": "^7.4.5",
41
41
  "@storybook/addon-essentials": "^7.4.5",