@overmap-ai/forms 1.0.4-form-editor-revamp.0 → 1.0.4
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.
- package/dist/builder/FieldBuilder.d.ts +2 -2
- package/dist/builder/FieldSectionWithActions.d.ts +0 -1
- package/dist/builder/FieldWithActions.d.ts +0 -3
- package/dist/forms.js +1953 -1374
- package/dist/forms.js.map +1 -0
- package/dist/forms.umd.cjs +2667 -1
- package/dist/forms.umd.cjs.map +1 -0
- package/dist/style.css +26 -1
- package/package.json +1 -1
package/dist/forms.umd.cjs
CHANGED
|
@@ -1 +1,2667 @@
|
|
|
1
|
-
(function(x,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):(x=typeof globalThis<"u"?globalThis:x||self,n(x.forms={},x.jsxRuntime,x.blocks,x.formik,x.React,x.dnd,x.core,x.get,x.set))})(this,function(x,n,o,D,d,H,C,X,Ae){"use strict";var ri=Object.defineProperty;var oi=(x,n,o)=>n in x?ri(x,n,{enumerable:!0,configurable:!0,writable:!0,value:o}):x[n]=o;var m=(x,n,o)=>(oi(x,typeof n!="symbol"?n+"":n,o),o);class Ke{constructor(i){m(this,"type");m(this,"identifier");m(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 Ke{constructor(e){const{label:t,required:r,fieldValidators:s=[],formValidators:c=[],...u}=e;super(u);m(this,"required");m(this,"formValidators");m(this,"fieldValidators");m(this,"label");m(this,"onlyValidateAfterTouched",!0);this.label=t,this.required=r,this.fieldValidators=s,this.formValidators=c}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 s=r(e);if(s)return s}if(t)for(const r of this.getFormValidators()){const s=r(e,t);if(s)return s}}_serialize(){return{...super._serialize(),label:this.label,required:this.required}}getFieldValidators(){return[...this.fieldValidators]}getFormValidators(){return[...this.formValidators]}}m($,"fieldTypeName"),m($,"fieldTypeDescription");const pe={description:"_description_17zed_1"},N=l=>{const{label:i,children:e,severity:t,inputId:r,labelId:s,flexProps:c}=l;return n.jsx(o.Flex,{direction:"column",gap:"1",asChild:!0,...c,children:n.jsxs("label",{htmlFor:r,children:[n.jsx(o.Text,{severity:t,id:s,children:i}),e]})})},j=l=>{const{helpText:i,children:e,severity:t}=l;return n.jsxs(o.Flex,{direction:"column",gap:"1",children:[e,n.jsx(o.Flex,{direction:"column",children:n.jsx(o.Text,{size:"1",severity:t,className:pe.description,children:i})})]})},W=l=>{const{id:i,field:e,formId:t,...r}=l,[s,c,u]=D.useField(e.getId()),{touched:a}=c,f=c.error??e.description,p=c.error?"danger":void 0,h=i??`${t}-${e.getId()}-input`,g=`${h}-label`,y=e.required?`${e.label} *`:e.label,v=d.useMemo(()=>({...s,onChange:I=>{const F=e.getValueFromChangeEvent(I);u.setValue(F,!1).then(),(a||!e.onlyValidateAfterTouched)&&u.setError(e.getError(F))},onBlur:I=>{u.setTouched(!0,!1).then(),u.setError(e.getError(e.getValueFromChangeEvent(I)))}}),[e,s,u,a]);return[{helpText:f,severity:p,inputId:h,labelId:g,label:y,fieldProps:v,helpers:u,field:e},{...r,"aria-labelledby":g}]},yt=[!0,"true"],Ge=d.memo(function(i){const[{inputId:e,labelId:t,severity:r,helpText:s,label:c,fieldProps:u},a]=W(i),f=o.useSeverityColor(r),p=yt.includes(u.value);return n.jsx(j,{helpText:s,severity:r,children:n.jsx(N,{severity:r,inputId:e,labelId:t,label:c,flexProps:{direction:"row-reverse",justify:"end",align:"center",gap:"2"},children:n.jsx(o.Checkbox,{...a,...u,id:e,color:f,value:p.toString(),checked:p,onCheckedChange:u.onChange,onChange:void 0,onBlur:void 0})})})}),k=class k extends ${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 k(e)}getInput(e){return n.jsx(Ge,{...e,field:this})}};m(k,"fieldTypeName","Checkbox"),m(k,"fieldTypeDescription","Perfect for both optional and required yes/no questions."),m(k,"Icon",o.CheckCircledIcon);let G=k;const Ye=d.memo(function(i){const[{inputId:e,labelId:t,severity:r,helpText:s,label:c,fieldProps:u,field:a},f]=W(i),p=o.useSeverityColor(r);return n.jsx(j,{helpText:s,severity:r,children:n.jsx(N,{severity:r,inputId:e,labelId:t,label:c,children:n.jsx(o.TextField.Input,{...f,...u,type:"number",id:e,min:a.minimum,max:a.maximum,step:a.integers?1:.1,color:p})})})}),O=class O extends ${constructor(e){const{minimum:t=Number.MIN_SAFE_INTEGER,maximum:r=Number.MAX_SAFE_INTEGER,integers:s=!1,...c}=e;super({...c,type:"number"});m(this,"minimum");m(this,"maximum");m(this,"integers");this.minimum=t,this.maximum=r,this.integers=s}getValueFromChangeEvent(e){const t=Number.parseFloat(e.target.value);return Number.isNaN(t)?"":t}static getFieldCreationSchema(){return[new O({label:"Minimum",description:"Minimum value",integers:!0,required:!1,identifier:"minimum",formValidators:[this._validateMin]}),new O({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(s=>{if(typeof s=="number"&&s<t)return`Must be at least ${this.minimum}.`}),typeof r=="number"&&e.push(s=>{if(typeof s=="number"&&s>r)return`Must be at most ${this.maximum}.`}),this.integers&&e.push(s=>{if(typeof s=="number"&&!Number.isInteger(s))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 O(e)}getInput(e){return n.jsx(Ye,{field:this,...e})}};m(O,"fieldTypeName","Number"),m(O,"fieldTypeDescription","Allows specifying a number within a given range."),m(O,"Icon",o.FontFamilyIcon),m(O,"_validateMin",(e,t)=>typeof t.maximum=="number"&&typeof e=="number"&&t.maximum<e?"Minimum cannot be greater than minimum.":null),m(O,"_validateMax",(e,t)=>typeof t.minimum=="number"&&typeof e=="number"&&t.minimum>e?"Maximum cannot be less than minimum.":null);let U=O;const Qe=d.memo(function(i){const[{inputId:e,labelId:t,severity:r,helpText:s,label:c,fieldProps:u},a]=W(i),f=o.useSeverityColor(r),p=u.value?u.value.split("T")[0]:"";return n.jsx(j,{helpText:s,severity:r,children:n.jsx(N,{severity:r,inputId:e,labelId:t,label:c,children:n.jsx(o.TextField.Input,{...a,...u,type:"date",id:e,color:f,value:p})})})}),R=class R extends ${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 R(e)}getInput(e){return n.jsx(Qe,{field:this,...e})}};m(R,"fieldTypeName","Date"),m(R,"fieldTypeDescription","Allows specifying a date."),m(R,"Icon",o.CalendarIcon);let he=R;class me extends ${constructor(e){const{minLength:t,maxLength:r=5e3,...s}=e;super(s);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 U({label:"Minimum length",description:"Minimum number of characters",required:!1,identifier:"minimum_length",minimum:0,maximum:100,formValidators:[this._validateMin],integers:!0}),new U({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(me,"_validateMin",(e,t)=>typeof t.maximum_length=="number"&&typeof e=="number"&&t.maximum_length<e?"Minimum cannot be greater than maximum.":null),m(me,"_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 Xe=d.memo(function(i){const[{inputId:e,labelId:t,severity:r,helpText:s,label:c,fieldProps:u,field:a},f]=W(i),p=o.useSeverityColor(r);return n.jsx(j,{helpText:s,severity:r,children:n.jsx(N,{severity:r,inputId:e,labelId:t,label:c,children:n.jsx(o.TextField.Input,{...f,...u,type:a.inputType,id:e,color:p})})})}),ee=class ee extends me{constructor(e){const{inputType:t="text",...r}=e,s=e.maxLength?Math.min(500,e.maxLength):500,c=e.minLength?Math.min(e.minLength,s):void 0;super({...r,maxLength:s,minLength:c,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:s,...c}=e;return new ee({...c,maxLength:t,minLength:r,inputType:s})}getInput(e){return n.jsx(Xe,{field:this,...e})}};m(ee,"fieldTypeName","Short Text"),m(ee,"fieldTypeDescription","Short text fields can hold up to 500 characters on a single line."),m(ee,"Icon",o.InputIcon);let Z=ee;const Ze=d.memo(function(i){const[{inputId:e,labelId:t,severity:r,helpText:s,label:c,fieldProps:u},a]=W(i);return n.jsx(j,{helpText:s,severity:r,children:n.jsx(N,{severity:r,inputId:e,labelId:t,label:c,children:n.jsx(o.TextArea,{...a,...u,resize:"vertical",id:e,severity:r})})})}),te=class te extends me{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(Ze,{field:this,...i})}};m(te,"fieldTypeName","Paragraph"),m(te,"fieldTypeDescription","Paragraph fields can hold up to 5000 characters and can have multiple lines."),m(te,"Icon",o.RowsIcon);let J=te;const Je=d.memo(function(i){const[{inputId:e,labelId:t,severity:r,helpText:s,label:c,fieldProps:u,field:a},f]=W(i),{onChange:p,onBlur:h}=u,g=d.useMemo(()=>a.options.map(v=>({value:v.value,itemContent:v.label})),[a.options]),y=d.useCallback(v=>{p(v),h(v)},[p,h]);return n.jsx(j,{helpText:s,severity:r,children:n.jsx(N,{severity:r,inputId:e,labelId:t,label:c,children:n.jsx(o.Select,{items:g,...u,onValueChange:y,placeholder:"Select one...",id:e,severity:r,...f})})})}),ge=(l="",i=[])=>({type:"section",fields:i,identifier:l,label:null,condition:null,conditional:!1}),xt=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(ge(`AUTO_section-${i.indexOf(r)}`,e)),e=[]),t.push(r)):e.push(r);return e.length>0&&t.push(ge("AUTO_section-last",e)),{...l,fields:t,description:l.description??""}};function Le(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 vt(l,i,e){const t=Array.from(l);return t[i]=e,t}function ke(l,i,e){const t=Array.from(l??[]);return t.splice(i,0,e),t}function ye(l,i){const e=Array.from(l);return e.splice(i,1),e}const Re=(l,i)=>{if(typeof l=="string"&&l.length>0)return l;const e=new Date;return`${C.slugify(i)}-${e.getTime()}`},et=(l,i)=>{if(!i)return null;for(const e of l)if(e.type==="section"){const t=et(e.fields,i);if(t)return t}else if(e.identifier===i)return e;return null},xe=(l,i)=>l.filter((e,t)=>t<i).flatMap(e=>e.fields),tt=l=>l.flatMap(i=>i.type==="section"?[...i.fields.map(e=>e.label),i.label]:i.label).filter(i=>i!==null),Pe=(l,i)=>{let e=1,t=`${l} (${e})`;for(;i.includes(t);)t=`${l} (${++e})`;return t},it=d.memo(function(i){const[{inputId:e,labelId:t,severity:r,helpText:s,label:c,fieldProps:u},a]=W(i),f=o.useSeverityColor(r),p=d.useMemo(()=>Array.isArray(u.value)?u.value:[],[u.value]),{onChange:h,onBlur:g}=u,y=`${e}-droppable`,{disabled:v}=a,[w,S]=d.useState(""),[I,F]=d.useState(""),L=I||s,M=I?"red":f,b=d.useCallback(E=>{h(E),g(E)},[h,g]),P=d.useCallback(E=>{p.findIndex(q=>q.value===E.target.value.trim())>=0?F("All options must be unique"):E.target.value?F(""):F("Option cannot be empty"),S(E.target.value)},[S,p]),_=d.useCallback(()=>{if(I)return;if(!w.trim())return F("Option cannot be empty");const E=w.trim();b([...p,{value:E,label:E}]),S("")},[w,I,b,p]),Y=d.useCallback(E=>{E.key==="Enter"&&(E.preventDefault(),_())},[_]),be=d.useCallback(E=>{b(ye(p,E))},[p,b]),Te=d.useCallback(E=>{if(!E.destination)return;const q=E.source.index,Q=E.destination.index;b(Le(p,q,Q))},[b,p]);return n.jsx(H.DragDropContext,{onDragEnd:Te,children:n.jsxs(o.Flex,{direction:"column",gap:"2",children:[n.jsx(j,{helpText:L,severity:r,children:n.jsx(N,{severity:r,inputId:e,labelId:t,label:c,children:(!v||p.length===0)&&n.jsxs(o.Flex,{gap:"2",children:[n.jsx(o.Box,{grow:"1",children:n.jsx(o.TextField.Input,{placeholder:"Press enter to add a new option",...a,...u,value:w,onChange:P,onKeyDown:Y,id:e,color:M,onBlur:void 0})}),n.jsx(o.IconButton,{type:"button","aria-label":"Add option",disabled:!!I||v,onClick:_,children:n.jsx(o.PlusIcon,{})})]})})}),n.jsx(H.Droppable,{droppableId:y,children:E=>n.jsxs(o.Flex,{...E.droppableProps,ref:E.innerRef,direction:"column",children:[p.map((q,Q)=>n.jsx(H.Draggable,{draggableId:`${q.value}-draggable`,index:Q,isDragDisabled:v,children:({draggableProps:Se,dragHandleProps:ze,innerRef:T})=>n.jsx(o.Flex,{...ze,...Se,ref:T,gap:"2",align:"center",justify:"between",mb:"1",asChild:!0,children:n.jsxs(o.Badge,{color:"gray",size:"2",children:[n.jsx("span",{children:q.label}),n.jsx(o.IconButton,{size:"small",variant:"ghost",type:"button","aria-label":"Delete option",severity:"info",disabled:v,onClick:()=>be(Q),children:n.jsx(o.Cross1Icon,{})})]})})},q.value)),E.placeholder]})})]})})}),ie=class ie extends ${constructor(e){const{minimum_length:t,maximum_length:r,...s}=e;super({...s,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 n.jsx(it,{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)}};m(ie,"fieldTypeName","Multi-string"),m(ie,"fieldTypeDescription","Allows the user to provide multiple unique strings."),m(ie,"Icon",o.ListBulletIcon);let le=ie;class nt extends ${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 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 nt{constructor(i){super({...i,type:"select"})}getValueFromChangeEvent(i){return typeof i=="string"?i:i.target.value}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(Je,{field:this,...i})}};m(ne,"fieldTypeName","Dropdown"),m(ne,"fieldTypeDescription","Allows the user to select a single option from a list of options."),m(ne,"Icon",o.DropdownMenuIcon);let ae=ne;const wt=l=>l?Array.isArray(l)?l:[l]:[],rt=d.memo(function(i){const[{inputId:e,labelId:t,severity:r,helpText:s,label:c,fieldProps:u,field:a},f]=W(i),{onChange:p,onBlur:h}=u,g=d.useMemo(()=>wt(u.value),[u.value]),y=d.useCallback(v=>{p(v),h(v)},[p,h]);return n.jsx(j,{helpText:s,severity:r,children:n.jsx(N,{severity:r,inputId:e,labelId:t,label:c,children:n.jsx(o.MultiSelect,{value:g,onValueChange:y,options:a.options,name:u.name,placeholder:"Select one or more...",id:e,severity:r,...f})})})}),re=class re extends nt{constructor(i){super({...i,type:"multi-select"})}getValueFromChangeEvent(i){if(Array.isArray(i))return i;throw new Error("Expected an array.")}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(rt,{field:this,...i})}};m(re,"fieldTypeName","Multi-select"),m(re,"fieldTypeDescription","Allows the user to select a multiple options from a list of options."),m(re,"Icon",o.CheckboxIcon);let de=re;const Ft=d.memo(function({field:i,...e}){const[{value:t}]=D.useField(i.options.clonedFieldIdentifier),r=d.useMemo(()=>{const s=i.options.getFieldToClone(t);return s?ce(s):null},[i.options,t]);return ue(r,e)});class ve extends ${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 n.jsx(t,{field:this,...e})}}m(ve,"fieldTypeName","Custom"),m(ve,"fieldTypeDescription","Allows re-rendering of field already in the form");class Ct extends ve{constructor(i){super(i,Ft)}}const It=d.memo(function(i){const{field:e,...t}=i,{label:r,description:s,fields:c,condition:u}=e,{values:a,setFieldValue:f}=D.useFormikContext(),p=u!=null&&u.identifier?X(a,u.identifier):void 0,h=d.useMemo(()=>Oe(u,p),[u,p]);d.useEffect(()=>{if(!h)for(const y of c)f(y.getId(),"").then()},[h,c,f]);const g=qe(c,t);return h?r?n.jsx(o.Card,{children:n.jsxs(o.Flex,{direction:"column",gap:"3",children:[n.jsxs(o.Flex,{direction:"column",children:[n.jsx(o.Heading,{as:"h3",size:"3",children:r}),n.jsx(o.Text,{className:pe.description,children:s})]}),g]})}):g:null}),oe=class oe extends Ke{constructor(e){const{label:t=null,fields:r,condition:s=null,conditional:c,...u}=e;super({...u,type:"section"});m(this,"label");m(this,"fields");m(this,"condition");this.fields=r,this.condition=s,this.label=t,c===!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 oe({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||t.type==="upload"?null:{label:t.label,value:t.identifier}).filter(t=>!!t),identifier:"condition.identifier",required:!0}),new Ct({label:"Value",identifier:"condition.value",required:!0,clonedFieldIdentifier:"condition.identifier",getFieldToClone(t){if(!t)return null;const r=e.find(s=>s.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(Be))??[];return new oe({...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 s=r.getId(),c=r.getError(X(e,s),e);c&&Ae(t,r.getId(),c)}return t}getInput(e){return n.jsx(It,{field:this,...e})}};m(oe,"fieldTypeName","Section"),m(oe,"fieldTypeDescription","Sections can be useful for grouping fields together. They can also be conditionally shown or hidden.");let K=oe;const bt={previewImage:"_previewImage_1ig84_1"},De=l=>{const i=["byte","kilobyte","megabyte"];let e=l,t=0;for(;e>1024&&t<i.length-1;)e/=1024,t++;return new Intl.NumberFormat([],{maximumFractionDigits:2,style:"unit",unit:i[t]}).format(e)},Tt=d.memo(function(i){var M;const[{inputId:e,labelId:t,severity:r,helpText:s,label:c,fieldProps:u,field:a},f]=W(i),{onChange:p}=u,h=o.useSeverityColor(r),g=d.useRef(null),{value:y}=u,v=d.useMemo(()=>s||(a.maxFileSize?`Maximum file size: ${De(a.maxFileSize)}`:null),[a.maxFileSize,s]),w=d.useCallback(()=>{var b;(b=g.current)==null||b.click()},[]),S=d.useCallback(b=>{const P=[...y];P.splice(b,1),p({target:{files:P}})},[y,p]),I=y?"Select new files":"Select files",F=y?"Select new file":"Select a file",L=a.maxFiles>1?I:F;return n.jsxs(o.Flex,{direction:"column",gap:"2",children:[n.jsx(j,{helpText:v,severity:r,children:n.jsxs(N,{severity:r,inputId:e,labelId:t,label:c,children:[n.jsx(o.Flex,{direction:"row",gap:"2",children:n.jsx(o.Box,{width:"max-content",asChild:!0,children:n.jsxs(o.Button,{...f,onClick:w,children:[n.jsx(o.UploadIcon,{})," ",L]})})}),n.jsx("input",{...f,type:"file",ref:g,id:e,accept:(M=a.extensions)==null?void 0:M.join(","),multiple:a.maxFiles>1,color:h,style:{display:"none"},...u,value:""})]})}),Array.isArray(y)&&y.length>0&&n.jsx(o.Flex,{direction:"column",gap:"2",children:y.map((b,P)=>n.jsx(St,{field:a,file:b,onRemove:()=>S(P),disabled:f.disabled},P))})]})}),St=d.memo(function({file:i,field:e,onRemove:t,disabled:r}){const[s,c]=d.useState(null),u=d.useMemo(()=>s&&e.getError([s]),[e,s]),{url:a,name:f,size:p}=d.useMemo(()=>{let h=null,g,y;return s!=null&&s.type.startsWith("image/")&&(h=URL.createObjectURL(s)),s?(g=s.name,y=De(s.size)):(g="Downloading...",y="..."),{url:h,name:g,size:y}},[s]);return d.useEffect(()=>{i instanceof Promise?i.then(c):c(i)},[i]),n.jsx(o.Card,{children:n.jsxs(o.Flex,{direction:{initial:"column",sm:"row"},gap:"3",justify:"between",children:[n.jsxs(o.Flex,{direction:"row",gap:"3",align:"center",grow:"1",shrink:"0",children:[n.jsx(o.IconButton,{severity:"info",variant:"outline","aria-label":`Remove ${f}`,disabled:r,onClick:t,children:n.jsx(o.Cross1Icon,{})}),n.jsxs(o.Flex,{direction:"column",gap:"1",children:[n.jsx(o.Text,{children:f}),n.jsx(o.Text,{size:"1",children:p}),u&&n.jsx(o.Text,{size:"1",severity:"danger",children:u})]})]}),a&&n.jsx("img",{className:bt.previewImage,src:a,alt:f})]})})}),ot=50*1024*1024,se=class se extends ${constructor(e){const{extensions:t,maximum_files:r,maximum_size:s,...c}=e;super({...c,type:"upload"});m(this,"extensions");m(this,"maxFileSize");m(this,"maxFiles");m(this,"onlyValidateAfterTouched",!1);this.maxFileSize=typeof s=="number"?s: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 U({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 U({label:"What is the maximum size of each file?",description:"Maximum file size in bytes.",required:!1,identifier:"maximum_size",minimum:1,maximum:ot,integers:!0}),new de({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??ot,r=this.maxFiles??1;return e.push(s=>{if(s&&s.some(c=>c.size>t))return`Files must be at most ${De(t)}.`}),e.push(s=>{if(s&&s.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 se(e)}getInput(e){return n.jsx(Tt,{field:this,...e})}};m(se,"fieldTypeName","Upload"),m(se,"fieldTypeDescription","Allows a file to be uploaded."),m(se,"Icon",o.UploadIcon);let Ve=se;const $e={date:he,number:U,boolean:G,select:ae,string:Z,text:J,custom:ve,upload:Ve,"multi-string":le,"multi-select":de},Be=l=>{const i=l.type;return $e[i].deserialize(l)},ce=l=>l.type==="section"?K.deserialize(l):Be(l);function _e(l,i={}){const{readonly:e=!1}=i;return{title:l.title,description:l.description,fields:l.fields.map(t=>ce(t)),meta:{readonly:e}}}function we(l){return!!(Array.isArray(l)&&l.some(i=>i instanceof File||i instanceof Promise))}function Oe(l,i){if(!l)return!0;if(we(i)||we(l.value))throw new Error("Conditions do not support file uploads");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 ue=(l,i)=>d.useMemo(()=>!i||!l?null:l.getInput(i),[l,i]),qe=(l,i)=>{const e=d.useMemo(()=>l.map(t=>n.jsx("div",{children:t.getInput(i)},t.getId())),[l,i]);return n.jsx(o.Flex,{direction:"column",gap:"2",children:e})},Fe=l=>Object.keys(l).length>0,st=async(l,i)=>{const e={};for(const t of l.fields)if(t instanceof K){if(t.condition){const{identifier:r}=t.condition;if(!Oe(t.condition,X(i,r)))continue}Object.assign(e,t.getErrors(i))}else{if(!(t instanceof $))throw new Error("Invalid field type");const r=t.getId(),s=t.getError(X(i,r),i);s&&Ae(e,r,s)}if(Fe(e))return e},zt=[null,void 0],Ne=(l,i)=>l.reduce((e,t)=>t instanceof K?{...e,...Ne(t.fields,i)}:(zt.includes(X(e,t.getId()))&&Ae(e,t.getId(),""),e),i),Et=()=>{throw new Error("onSubmit must be provided if form is not readonly.")},Ce=d.memo(d.forwardRef((l,i)=>{const{schema:e,values:t={},onSubmit:r=Et,submitText:s="Submit",cancelText:c,onCancel:u,onDirty:a,hideTitle:f=!e.title,hideDescription:p,className:h}=l,{readonly:g}=e.meta,y=d.useMemo(()=>crypto.randomUUID(),[]),v=D.useFormik({initialValues:Ne(e.fields,t),onSubmit:r,validate:L=>st(e,L),validateOnBlur:!1,validateOnChange:!1}),{dirty:w}=v,S=d.useMemo(()=>typeof e.title=="string"?n.jsx(o.Heading,{children:e.title}):e.title,[e.title]),I=d.useMemo(()=>typeof e.description=="string"?n.jsx(o.Text,{className:pe.description,children:e.description}):e.description,[e.description]),F=qe(e.fields,{formId:y,disabled:g});return d.useEffect(()=>{w&&a&&a()},[w,a]),n.jsx(D.FormikProvider,{value:v,children:n.jsx(o.Flex,{ref:i,direction:"column",gap:"2",className:h,asChild:!0,children:n.jsxs("form",{id:y,onSubmit:v.handleSubmit,children:[!f&&n.jsx(o.Card,{children:n.jsxs(o.Flex,{direction:"column",gap:"1",children:[S,!p&&I]})}),F,!g&&n.jsxs(o.Flex,{justify:"end",gap:"2",children:[c&&n.jsx(o.Button,{type:"button",variant:"soft",onClick:u,children:c}),n.jsx(o.Button,{type:"submit",disabled:!v.isValid,children:s})]})]})})})})),Mt=d.memo(d.forwardRef((l,i)=>{const{submission:e,showFormDescription:t=!1,showFormTitle:r=!0}=l,s=C.useAppSelector(C.selectFormRevision(e.form_revision)),{sdk:c}=C.useSDK();if(!s)throw new Error(`Could not find revision ${e.form_revision} for submission ${e.offline_id}.`);const u=d.useMemo(()=>_e(s,{readonly:!0}),[s]),a=d.useMemo(()=>{const f=C.selectSubmissionAttachments(e.offline_id)(c.store.getState())??[],p={};for(const h of f){const g=c.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=p[h.field_identifier];y?y.push(g):p[h.field_identifier]=[g]}return{...e.values,...p}},[c.files,c.store,e.offline_id,e.values]);return n.jsx(Ce,{ref:i,schema:u,values:a,hideDescription:!t,hideTitle:!r})})),lt={favoriteIcon:"_favoriteIcon_1bixi_1",regularIcon:"_regularIcon_1bixi_9"},je="organization:",We="user:",At=d.memo(d.forwardRef((l,i)=>{const{maxResults:e=20,...t}=l,[r,s]=d.useState(""),[c,u]=d.useState(""),{sdk:a}=C.useSDK(),f=d.useMemo(()=>{const F={maxResults:e,searchTerm:r};return c&&(c.startsWith(je)?F.owner_organization=parseInt(c.slice(je.length)):c.startsWith(We)&&(F.owner_user=parseInt(c.slice(We.length)))),F},[r,e,c]),p=C.useAppSelector(C.selectFilteredUserForms(f))??[],h=C.useAppSelector(C.selectUserFormMapping),g=d.useCallback(F=>{F.favorite?a.userForms.unfavorite(F.offline_id).then():a.userForms.favorite(F.offline_id).then()},[a]),y=d.useMemo(()=>{const F=a.store.getState(),L={};for(const M of Object.values(h)){const b=C.selectOrganization(M.owner_organization||-1)(F);b&&(L[`${je}${b.id}`]=b.name);const P=C.selectUser(M.owner_user||-1)(F);P&&(L[`${We}${P.id}`]=P.username)}return Object.entries(L).map(([M,b])=>({itemContent:b,value:M}))},[h,a.store]),v=d.useCallback(F=>{s(F.currentTarget.value)},[]),S=(C.useAppSelector(C.selectNumberOfUserForms)||0)-p.length,I=p.length==e&&S>0?`Only the first ${e} results are shown (${S} hidden)`:S>0&&`${S} hidden forms`;return n.jsxs(o.Flex,{ref:i,direction:"column",gap:"2",children:[n.jsxs(o.Flex,{gap:"2",grow:"1",children:[n.jsx(o.Box,{grow:"1",asChild:!0,children:n.jsx(o.TextField.Root,{size:"3",children:n.jsx(o.TextField.Input,{placeholder:"Filter",value:r,onChange:v})})}),n.jsx(o.Select,{items:y,value:c,onValueChange:u,placeholder:"Owner",size:"large"})]}),p.length>0&&n.jsx(o.ButtonList.Root,{children:p.map(F=>n.jsx(Lt,{...t,form:F,handleToggleFavorite:()=>g(F)},F.offline_id))}),n.jsx(o.Box,{px:"3",children:n.jsx(o.Text,{size:"2",severity:"info",children:I})})]})})),Lt=l=>{var g;const{form:i,onSelectForm:e,isFavoriteEditable:t,handleToggleFavorite:r}=l,s=(g=C.useAppSelector(C.selectOrganization(i.owner_organization||-1)))==null?void 0:g.name,c=C.useAppSelector(C.selectUser(i.owner_user||-1)),u=C.useAppSelector(C.selectCurrentUser).id,a=!!c&&c.id===u,f=s??(a?"You":c==null?void 0:c.username)??"Unknown",p=d.useCallback(y=>{y.stopPropagation(),r()},[r]),h=n.jsx(o.ButtonList.Item,{onClick:()=>e(i),asChild:!0,children:n.jsxs(o.Flex,{justify:"between",gap:"2",py:"2",px:"3",...o.divButtonProps,children:[n.jsxs(o.Flex,{grow:"1",align:"center",gap:"2",children:[n.jsx(o.IconButton,{className:C.classNames(i.favorite?lt.favoriteIcon:lt.regularIcon),variant:"ghost",onClick:p,"aria-label":i.favorite?"Favorite form":"Standard form",disabled:!t,children:i.favorite?n.jsx(o.StarFilledIcon,{}):n.jsx(o.StarIcon,{})}),n.jsx(o.Text,{noWrap:!0,children:i.latestRevision.title}),i.latestRevision.description&&n.jsx(o.QuestionMarkCircledIcon,{})]}),f&&n.jsxs(o.Flex,{align:"center",gap:"2",children:[n.jsx(o.PersonIcon,{})," ",f]})]})});return i.latestRevision.description?n.jsx(o.Tooltip,{content:i.latestRevision.description,children:h},i.offline_id):h},at={submissionsContainer:"_submissionsContainer_9iirt_1",stopHorizontalOverflow:"_stopHorizontalOverflow_9iirt_6"},Pt=d.memo(function(i){var F;const{submission:e,onSubmissionClick:t,compact:r,labelType:s,rowDecorator:c}=i,u=C.useAppSelector(C.selectCurrentUser),a=C.useAppSelector(C.selectUser("created_by"in e?e.created_by:u.id)),f=He(e),p=C.isToday(f)?f.toLocaleTimeString([],{hour:"2-digit",minute:"2-digit"}):C.getLocalDateString(f),h=C.useAppSelector(C.selectFormRevision(e.form_revision));if(!h)throw new Error(`Could not find revision ${e.form_revision} for submission ${e.offline_id}.`);const g=(F=C.useAppSelector(C.selectLatestFormRevision(h.form)))==null?void 0:F.revision,y=C.useFileSrc({file:(a==null?void 0:a.profile.file)??null,fileSha1:(a==null?void 0:a.profile.file_sha1)??null}),v=(a==null?void 0:a.username.charAt(0).toUpperCase())??"?",w=h.revision===g,S=d.useCallback(()=>{t&&t({submission:e})},[e,t]),I=n.jsx(o.ButtonList.Item,{onClick:S,asChild:!0,children:n.jsxs(o.Flex,{grow:"1",width:"100%",p:"2",gap:"2",justify:"between",children:[n.jsxs(o.Flex,{gap:"2",align:"center",className:at.stopHorizontalOverflow,children:[n.jsx(o.Avatar,{src:y,size:"1",fallback:v}),n.jsx(o.Text,{size:"2",noWrap:!0,children:s==="creator"?(a||u).username:h.title})]}),n.jsxs(o.Flex,{gap:"2",align:"center",children:[!r&&(h.revision?n.jsx(o.Badge,{variant:"soft",severity:w?"primary":"info",children:r?h.revision.toString():`Revision #${h.revision}`}):!!g&&n.jsx(o.Badge,{children:"Original"})),n.jsx(o.Text,{size:"2",noWrap:!0,children:p})]})]})});return c?c(e,I):I}),He=l=>{const i="created_at"in l?l.created_at:l.submitted_at;return new Date(i)},Dt=d.memo(function(i){const{formId:e,submissions:t,compact:r=!1,className:s,after:c,variant:u="outline",...a}=i;if(!!e==!!t)throw new Error("Either formId or submissions must be provided, but not both.");const f=C.useAppSelector(t?()=>t:C.selectSubmissionsForForm(e)),p=d.useMemo(()=>f==null?void 0:f.sort((h,g)=>He(g).getTime()-He(h).getTime()),[f]);return n.jsx(o.ButtonList.Root,{className:C.classNames(at.submissionsContainer,s),size:"small",variant:u,before:!r&&n.jsxs(o.Text,{severity:"info",children:["There are ",((f==null?void 0:f.length)||0).toString()," submissions of this form."]}),after:c,children:p==null?void 0:p.map((h,g)=>n.jsx(Pt,{submission:h,compact:r,...a},g))})}),Vt=d.memo(function(i){const{name:e,render:t}=i,{submitForm:r}=D.useFormikContext(),[s,c,u]=D.useField(e);return d.useMemo(()=>{const a=f=>u.setValue(f,!1);return t({value:s.value,setValue:a,patchValue:r})},[r,u,s.value,t])}),$t=d.memo(d.forwardRef((l,i)=>{const{children:e,schema:t,values:r,onPatch:s,onError:c,...u}=l,a=d.useMemo(()=>Ne(t.fields,r),[t.fields,r]),f=d.useCallback(v=>{const w={};for(const S in v){const I=v[S];I!==a[S]&&I!==void 0&&(w[S]=I)}Fe(w)&&s(w)},[a,s]),p=d.useCallback(async v=>{const w=await st(t,v);return w&&c(w),w},[t,c]),h=D.useFormik({initialValues:a,onSubmit:f,validate:p,validateOnBlur:!1,validateOnChange:!1}),{errors:g,resetForm:y}=h;return d.useEffect(()=>{Fe(g)&&y({values:a,errors:{}})},[g,a,y]),n.jsx(D.FormikProvider,{value:h,children:n.jsx("form",{...u,ref:i,onSubmit:h.handleSubmit,children:e})})}));function Bt(l,i){if(l==null)return{};var e={},t=Object.keys(l),r,s;for(s=0;s<t.length;s++)r=t[s],!(i.indexOf(r)>=0)&&(e[r]=l[r]);return e}var _t=["color"],Ot=d.forwardRef(function(l,i){var e=l.color,t=e===void 0?"currentColor":e,r=Bt(l,_t);return d.createElement("svg",Object.assign({width:"15",height:"15",viewBox:"0 0 15 15",fill:"none",xmlns:"http://www.w3.org/2000/svg"},r,{ref:i}),d.createElement("path",{d:"M12.8536 2.85355C13.0488 2.65829 13.0488 2.34171 12.8536 2.14645C12.6583 1.95118 12.3417 1.95118 12.1464 2.14645L7.5 6.79289L2.85355 2.14645C2.65829 1.95118 2.34171 1.95118 2.14645 2.14645C1.95118 2.34171 1.95118 2.65829 2.14645 2.85355L6.79289 7.5L2.14645 12.1464C1.95118 12.3417 1.95118 12.6583 2.14645 12.8536C2.34171 13.0488 2.65829 13.0488 2.85355 12.8536L7.5 8.20711L12.1464 12.8536C12.3417 13.0488 12.6583 13.0488 12.8536 12.8536C13.0488 12.6583 13.0488 12.3417 12.8536 12.1464L8.20711 7.5L12.8536 2.85355Z",fill:t,fillRule:"evenodd",clipRule:"evenodd"}))});const dt={...$e,section:K},qt=d.memo(function(i){const{field:e,setFieldType:t}=i,r=e.fieldTypeName,s=e.fieldTypeDescription,c=e.Icon;return n.jsxs(o.Flex,{gap:"4",align:"center",children:[n.jsx(o.Button,{type:"button",variant:"surface",onClick:t,style:{width:"135px"},children:n.jsxs(o.Flex,{gap:"3",align:"center",grow:"1",children:[n.jsx(c,{}),r]})}),n.jsx(o.Text,{children:s})]})}),ct=[["string","text"],["select","multi-select","upload"],["boolean","date","number","multi-string"]],Nt=ct.length-1,jt=d.memo(function(i){const{setFieldType:e}=i;return n.jsx(o.Flex,{direction:"column",gap:"3",children:ct.map((t,r)=>n.jsxs(o.Flex,{direction:"column",gap:"3",children:[n.jsx(o.Flex,{direction:"column",gap:"2",children:t.map(s=>n.jsx(qt,{field:$e[s],setFieldType:()=>e(s)},s))}),r<Nt&&n.jsx(o.Separator,{size:"4"})]},r))})}),Wt=l=>i=>{if(!(!i||typeof i!="string")&&l.includes(i.trim()))return"This name is already taken."},Ht=(l,i)=>{const e=[new Z({label:"Label",required:!0,maxLength:200,identifier:"label",fieldValidators:[Wt(l)]}),new J({label:"Description",required:!1,maxLength:1e3,identifier:"description"})];return i==="section"?e:[...e,new G({label:"Required",description:null,required:!1,identifier:"required"})]},Ut=d.memo(function(i){const{fieldType:e,handleCancel:t,handleCreateField:r,defaultField:s,conditionalSourceFields:c}=i,u=dt[e],a=D.useFormikContext(),f=d.useMemo(()=>{const p=tt(a.values.fields).filter(g=>g!==(s==null?void 0:s.label));let h=Ht(p,e);if(u===K){if(c===void 0)throw new Error("Conditional source fields must be provided when changing sections.");h=h.concat(u.getFieldCreationSchema(c))}else{if(!(u.prototype instanceof $))throw new Error(`Field must be an instance of BaseField. Got ${u}.`);h=[...h,...u.getFieldCreationSchema()]}return{fields:h,meta:{readonly:!1},title:null}},[a.values.fields,e,u,s==null?void 0:s.label,c]);return n.jsx(Ce,{schema:f,values:s,onSubmit:r,cancelText:s?void 0:"Back",onCancel:t})}),fe=d.memo(function(i){const{parentPath:e,index:t,onComplete:r,initial:s,editing:c,conditionalSourceFields:u}=i,[a,f]=d.useState(),p=(s==null?void 0:s.type)??a,h=p?dt[p].fieldTypeName:void 0,{setFieldValue:g,values:y}=D.useFormikContext();if(c&&!s)throw new Error("Initial field must be provided if editing is true.");const v=!p&&!c&&!s,w=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.`,I=d.useCallback(()=>f(void 0),[]),F=d.useCallback(()=>{f(void 0),r()},[r]),L=d.useCallback(M=>{const{label:b}=M;if(!p)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 P=ce({type:p,...M,identifier:Re(M.identifier,b)}).serialize(),_=X(y,e);if(_===void 0)throw new Error("Parent path must point to an existing field.");let Y;if(!Array.isArray(_))throw new Error("Parent path must point to an array.");c?Y=vt(_,t,P):Y=ke(_,t,P),g(e,Y).then(),r()},[p,y,e,c,g,r,t]);return n.jsx(o.Card,{children:n.jsxs(o.Flex,{direction:"column",gap:"2",children:[n.jsxs(o.Flex,{justify:"between",children:[n.jsx(o.Text,{children:w}),n.jsx(o.IconButton,{"aria-label":"Close",onClick:F,type:"button",children:n.jsx(Ot,{})})]}),n.jsx(o.Text,{children:S}),v?n.jsx(jt,{setFieldType:f}):n.jsx(Ut,{conditionalSourceFields:u,handleCancel:I,handleCreateField:M=>L(M),fieldType:p,defaultField:s})]})})}),ut=({children:l})=>n.jsx(n.Fragment,{children:l}),ft=(l,i)=>({initial:l?i:"none",sm:l?"none":i}),pt=d.memo(function(i){const{remove:e,dragHandleProps:t,editProps:r,insertAfterProps:s,duplicateProps:c}=i,u=d.useMemo(()=>[{Wrapper:fe,wrapperProps:r,Icon:o.Pencil1Icon,text:"Edit"},{Icon:o.TrashIcon,buttonProps:{onClick:e},text:"Delete"},{Wrapper:fe,wrapperProps:c,Icon:o.CopyIcon,text:"Duplicate"},{Wrapper:fe,wrapperProps:s,Icon:o.PlusIcon,text:"Add after"},{Icon:a=>n.jsx("div",{...a,children:n.jsx(o.DragHandleDots2Icon,{})}),text:"Reorder",disableOnMobile:!0,buttonProps:{...t,asChild:!0}}],[t,c,r,s,e]);return n.jsxs(n.Fragment,{children:[n.jsx(o.Flex,{gap:"4",display:ft(!1,"flex"),children:u.map(a=>{const f=a.Wrapper??ut;return n.jsx(f,{...a.wrapperProps,children:n.jsx(o.IconButton,{type:"button",variant:"ghost","aria-label":a.text,...a.buttonProps,children:n.jsx(a.Icon,{})})},a.text)})}),n.jsx(o.Box,{display:ft(!0,"block"),children:n.jsx(o.DropdownMenu,{trigger:n.jsx(o.IconButton,{variant:"ghost","aria-label":"Actions menu",children:n.jsx(o.DotsVerticalIcon,{})}),closeOnSelect:!1,items:u.map(a=>{var p;if(a.disableOnMobile)return null;const f=a.Wrapper??ut;return{...a.buttonProps,onSelect:(p=a.buttonProps)==null?void 0:p.onClick,content:n.jsx(f,{...a.wrapperProps,children:n.jsxs(o.Flex,{gap:"2",align:"center",children:[n.jsx(a.Icon,{}),a.text]})})}}).filter(a=>a!==null)})})]})}),Ie="form-builder",Kt=d.memo(function(i){const{field:e,index:t,sectionIndex:r,mode:s,takenLabels:c,remove:u,onModeChange:a}=i,f=d.useMemo(()=>ce(e),[e]),p=ue(f,{formId:Ie,disabled:!0}),h=d.useCallback(w=>{if(w.label===null)throw new Error(`Expected a label for field ${w.identifier}`);return{...w,label:Pe(w.label,c),identifier:""}},[c]),g=d.useMemo(()=>(a("edit"),{index:t,parentPath:`fields.${r}.fields`,initial:e,editing:!0,onComplete:()=>a("display")}),[e,t,a,r]),y=d.useMemo(()=>({parentPath:`fields.${r}.fields`,index:t+1,initial:h(e),onComplete:()=>a("display")}),[h,e,t,a,r]),v=d.useMemo(()=>({parentPath:`fields.${r}.fields`,index:t+1,initial:void 0,onComplete:()=>a("display")}),[t,a,r]);return console.log(p),n.jsx(n.Fragment,{children:s==="display"?n.jsx(H.Draggable,{draggableId:e.identifier,index:t,children:w=>n.jsx(o.Card,{ref:w.innerRef,...w.draggableProps,...w.dragHandleProps,mb:"4",children:n.jsxs(o.Flex,{gap:"4",justify:"between",align:"center",children:[p,n.jsx(pt,{remove:u,editProps:g,duplicateProps:y,insertAfterProps:v,dragHandleProps:w.dragHandleProps})]})})}):null})}),Gt=d.memo(function(i){var be,Te,E,q,Q,Se,ze;const{field:e,index:t,dropState:r}=i,[s,c]=d.useState(Array(e.fields.length).fill("display")),u=(be=r[e.identifier])==null?void 0:be.disabled,{setFieldValue:a,values:f}=D.useFormikContext(),p=o.useAlertDialog(),h=tt(f.fields),g=d.useCallback((T,z)=>{for(const V of T){const A=z.indexOf(V);a(`fields.${A}.condition`,null).then(),a(`fields.${A}.conditional`,!1).then()}},[a]),y=d.useCallback(T=>{var A;const z=e.fields[T];if(!z)throw new Error("Could not find field to remove.");const V=[];for(const B of f.fields)((A=B.condition)==null?void 0:A.identifier)===z.identifier&&V.push(B);return{removing:z,affectedSections:V,action:()=>a(`fields.${t}.fields`,ye(e.fields,T))}},[e.fields,f.fields,a,t]),v=d.useCallback(T=>{const{affectedSections:z,action:V,removing:A}=y(T),B=()=>{V().then(),g(z,f.fields)};if(z.length>0){const Ue=z.map(Ee=>Ee.label).join(", ");return p({title:"Remove condition?",description:`${A.label} is being used as a condition, deleting it will remove the condition from the ${Ue} section(s).`,severity:"danger",actionText:"Remove",onAction:B})}B()},[y,g,f.fields,p]),w=d.useCallback(()=>{const z=e.fields.map((Me,ni)=>y(ni)).flatMap(Me=>Me.affectedSections),V=z.length?"Remove fields and conditions?":"Remove fields?",A=e.fields.length,B=z.map(Me=>Me.label).join(", "),Ue=z.length?`Deleting this section will remove the ${A} field(s) it contains and will remove the conditions from following sections: ${B}`:`Deleting this section will remove the ${A} field(s) it contains.`,Ee=ye(f.fields,t),gt=()=>a("fields",Ee);if(z.length>0)return p({title:V,description:Ue,severity:"danger",actionText:"Remove",onAction:()=>{gt().then(()=>{g(z,Ee)})}});gt().then()},[e.fields,f.fields,t,y,a,p,g]),S=d.useCallback(T=>{if(T.label===null)throw new Error(`Expected a label for field ${T.identifier}`);const z=Pe(T.label,h),V=T.fields.map(A=>{const B=Pe(A.label,h);return{...A,label:B,identifier:Re(void 0,B)}});return{...T,label:z,fields:V,identifier:""}},[h]),I=d.useCallback((T,z)=>{c(V=>{const A=[...V];return A[T]=z,A})},[]),F=d.useMemo(()=>({index:t,parentPath:"fields",initial:e,editing:!0,conditionalSourceFields:xe(f.fields,t),onComplete:()=>console.log("editsectionprops")}),[e,t,f.fields]),L=d.useMemo(()=>({index:t+1,parentPath:"fields",initial:ge(),conditionalSourceFields:xe(f.fields,t+1),onComplete:()=>console.log("insertsectionprops")}),[t,f.fields]),M=d.useMemo(()=>({parentPath:`fields.${t}.fields`,index:e.fields.length,initial:void 0,onComplete:()=>console.log("insertfieldatendofsectionprops")}),[e.fields.length,t]),b=d.useMemo(()=>({index:t+1,parentPath:"fields",initial:S(e),conditionalSourceFields:xe(f.fields,t+1),onComplete:()=>console.log("duplicatesectionprops")}),[S,e,t,f.fields]),P=d.useMemo(()=>{var T,z;return(z=et(f.fields,(T=e.condition)==null?void 0:T.identifier))==null?void 0:z.label},[(Te=e.condition)==null?void 0:Te.identifier,f.fields]),_=Array.isArray((E=e.condition)==null?void 0:E.value)?"contains all of":"equals";if(we((q=e.condition)==null?void 0:q.value))throw new Error("File values are not supported for conditions.");const Y=Array.isArray((Q=e.condition)==null?void 0:Q.value)?(Se=e.condition)==null?void 0:Se.value.map(T=>typeof T=="string"?T:T.label).join(", "):(ze=e.condition)==null?void 0:ze.value.toString();return n.jsx(H.Draggable,{draggableId:e.identifier,index:t,children:T=>n.jsx(o.Card,{ref:T.innerRef,...T.draggableProps,...T.dragHandleProps,mb:"4",children:n.jsxs(o.Flex,{gap:"3",justify:"between",align:"center",children:[n.jsxs(o.Flex,{direction:"column",gap:"2",grow:"1",children:[n.jsxs(o.Flex,{direction:"column",children:[n.jsx(o.Heading,{as:"h3",size:"3",children:e.label}),n.jsx(o.Text,{className:pe.description,children:e.description})]}),e.condition&&n.jsx(o.Text,{size:"1",children:n.jsxs(o.Em,{children:["Display only if ",n.jsx(o.Strong,{children:P})," ",_," ",n.jsx(o.Strong,{children:Y})]})}),n.jsx(H.Droppable,{droppableId:e.identifier,type:"SECTION",isDropDisabled:u,children:z=>n.jsxs(o.Flex,{ref:z.innerRef,...z.droppableProps,direction:"column",gap:"0",children:[e.fields.map((V,A)=>n.jsx(Kt,{field:V,index:A,sectionIndex:t,remove:()=>v(A),takenLabels:h,mode:s[A]??"display",onModeChange:B=>I(A,B)},V.identifier)),z.placeholder,n.jsx(fe,{...M,children:n.jsxs(o.Button,{type:"button",variant:"outline",children:[n.jsx(o.PlusIcon,{})," Add a field"]})})]})})]}),n.jsx(pt,{remove:w,insertAfterProps:L,dragHandleProps:T.dragHandleProps,editProps:F,duplicateProps:b})]})})})}),Yt=(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}},Qt=(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}}},ht=l=>{var e,t,r;const i={};for(let s=0;s<l.length;s++){const c=l[s];if(!c)throw new Error("Field is undefined.");const u=s>0?(e=i[l[s-1].identifier])==null?void 0:e.conditionFields:void 0,a=new Set(u);(t=c.condition)!=null&&t.identifier&&a.add(c.condition.identifier),i[c.identifier]={disabled:!1,conditionFields:a,conditionIndex:Qt(l,(r=c.condition)==null?void 0:r.identifier),index:s,label:c.label}}return i},mt=(l,i)=>{for(const[e,t]of Object.entries(l))if(t.identifier===i)return[t,e]},Xt=d.memo(function(){const{values:i,setFieldValue:e}=D.useFormikContext(),[t,r]=d.useReducer(Yt,i.fields,ht),{showInfo:s}=o.useToast();d.useEffect(()=>{r({type:"update",state:ht(i.fields)})},[r,i.fields]);const c=d.useCallback(f=>{f.type==="SECTION"&&r({type:"hold",fieldId:f.draggableId})},[]),u=d.useCallback(f=>{const{source:p,destination:h,type:g,reason:y,draggableId:v}=f;if(r({type:"release"}),!h||y==="CANCEL")return;if(g==="ROOT"){const L=t[v];if(!L)throw new Error("Could not find section context.");let M=typeof L.conditionIndex<"u"?Math.max(L.conditionIndex+1,h.index):h.index;for(const b of Object.values(t))b.conditionIndex===p.index&&(M=Math.min(M,b.index-1));return M!=h.index&&s({title:"Reordered sections",description:"Sections with conditions must be below the fields they reference."}),e("fields",Le(i.fields,p.index,M))}if(g!=="SECTION")throw new Error("Unexpected droppable type.");const[w,S]=mt(i.fields,p.droppableId)??[],[I,F]=mt(i.fields,h.droppableId)??[];if(!(w!=null&&w.fields)||!I)throw new Error("Could not find section with fields.");if(w.identifier===I.identifier)e(`fields.${S}.fields`,Le(w.fields,p.index,h.index)).then();else{const L=w.fields[p.index];if(!L)throw new Error("Could not find field to reorder.");e(`fields.${S}.fields`,ye(w.fields,p.index)).then(),e(`fields.${F}.fields`,ke(I.fields,h.index,L)).then()}},[i.fields,e,t,s]),a=d.useMemo(()=>({index:i.fields.length,parentPath:"fields",initial:ge(),conditionalSourceFields:xe(i.fields,i.fields.length),onComplete:()=>console.log("makefieldsectionprops")}),[i.fields]);return n.jsx(H.DragDropContext,{onDragStart:c,onDragEnd:u,children:n.jsx(H.Droppable,{droppableId:"droppable",type:"ROOT",children:f=>n.jsxs(o.Flex,{ref:f.innerRef,...f.droppableProps,direction:"column",gap:"0",children:[i.fields.map((p,h)=>n.jsx(Gt,{field:p,index:h,dropState:t},p.label)),f.placeholder,n.jsx(fe,{...a,children:n.jsxs(o.Button,{type:"button",variant:"outline",children:[n.jsx(o.PlusIcon,{})," Add a section"]})})]})})})}),Zt={title:"",description:"",fields:[]},Jt=new Z({label:"Title",minLength:0,maxLength:100,required:!0,identifier:"title"}),kt={formId:Ie,placeholder:"Give your form a title."},Rt=new J({label:"Description",minLength:0,maxLength:1e3,required:!1,identifier:"description"}),ei={formId:Ie,placeholder:"Explain the purpose of this form."},ti=()=>{alert("This is a form preview, your data will not be saved.")},ii=d.memo(d.forwardRef((l,i)=>{const{onCancel:e,onSave:t,revision:r}=l,s=r?"Edit form":"Create a new form",{heading:c=s}=l,u=d.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."),Fe(v))return v},[]),a=D.useFormik({initialValues:xt(r)??Zt,validate:u,onSubmit:y=>t(y),validateOnChange:!1,validateOnBlur:!1}),f=d.useMemo(()=>_e(a.values),[a.values]),p=ue(Jt,kt),h=ue(Rt,ei),g=d.useMemo(()=>typeof c=="object"?c:n.jsx(o.Heading,{children:c}),[c]);return n.jsx(o.Tabs.Root,{ref:i,defaultValue:"edit",children:n.jsxs(o.Flex,{direction:"column",gap:"2",children:[n.jsxs(o.Tabs.List,{children:[n.jsx(o.Tabs.Trigger,{value:"edit",children:"Edit"}),n.jsx(o.Tabs.Trigger,{value:"preview",children:"Preview"})]}),n.jsxs(o.Tabs.Content,{value:"edit",children:[g,n.jsxs(o.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(o.Flex,{asChild:!0,direction:"column",gap:"2",mt:"3",children:n.jsxs("form",{id:Ie,onSubmit:a.handleSubmit,children:[n.jsxs(D.FormikProvider,{value:a,children:[p,h,n.jsx(Xt,{}),n.jsx(o.Text,{severity:"danger",size:"1",children:typeof a.errors.fields=="string"&&a.errors.fields})]}),n.jsxs(o.Flex,{justify:"end",gap:"2",children:[n.jsx(o.Button,{type:"button",variant:"soft",onClick:e,children:"Cancel"}),n.jsx(o.Button,{type:"submit",disabled:!a.isValid,children:"Save"})]})]})})]}),n.jsx(o.Tabs.Content,{value:"preview",children:n.jsx(Ce,{schema:f,onSubmit:ti})})]})})}));x.BooleanField=G,x.BooleanInput=Ge,x.DateField=he,x.DateInput=Qe,x.FieldSection=K,x.FormBrowser=At,x.FormBuilder=ii,x.FormRenderer=Ce,x.FormSubmissionBrowser=Dt,x.FormSubmissionViewer=Mt,x.MultiSelectField=de,x.MultiSelectInput=rt,x.MultiStringField=le,x.MultiStringInput=it,x.NumberField=U,x.NumberInput=Ye,x.PatchField=Vt,x.PatchFormProvider=$t,x.SelectField=ae,x.SelectInput=Je,x.StringField=Z,x.StringInput=Xe,x.TextField=J,x.TextInput=Ze,x.deserialize=ce,x.deserializeField=Be,x.formRevisionToSchema=_e,x.isConditionMet=Oe,x.useFieldInput=ue,x.useFieldInputs=qe,x.valueIsFile=we,Object.defineProperty(x,Symbol.toStringTag,{value:"Module"})});
|
|
1
|
+
(function(global, factory) {
|
|
2
|
+
typeof exports === "object" && typeof module !== "undefined" ? factory(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"], factory) : (global = typeof globalThis !== "undefined" ? globalThis : global || self, factory(global.forms = {}, global.jsxRuntime, global.blocks, global.formik, global.React, global.dnd, global.core, global.get, global.set));
|
|
3
|
+
})(this, function(exports2, jsxRuntime, blocks, formik, React, dnd, core, get, set) {
|
|
4
|
+
"use strict";var __defProp = Object.defineProperty;
|
|
5
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
6
|
+
var __publicField = (obj, key, value) => {
|
|
7
|
+
__defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
8
|
+
return value;
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
class BaseFormElement {
|
|
12
|
+
constructor(options) {
|
|
13
|
+
__publicField(this, "type");
|
|
14
|
+
__publicField(this, "identifier");
|
|
15
|
+
__publicField(this, "description");
|
|
16
|
+
const { description: description2 = null, identifier, type } = options;
|
|
17
|
+
this.identifier = identifier;
|
|
18
|
+
this.description = description2;
|
|
19
|
+
this.type = type;
|
|
20
|
+
}
|
|
21
|
+
getId() {
|
|
22
|
+
return this.identifier;
|
|
23
|
+
}
|
|
24
|
+
static deserialize(_data) {
|
|
25
|
+
throw new Error(`${this.name} must implement deserialize.`);
|
|
26
|
+
}
|
|
27
|
+
_serialize() {
|
|
28
|
+
if (!this.identifier) {
|
|
29
|
+
throw new Error("Field identifier must be set before serializing.");
|
|
30
|
+
}
|
|
31
|
+
return {
|
|
32
|
+
type: this.type,
|
|
33
|
+
identifier: this.identifier,
|
|
34
|
+
description: this.description
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
class BaseField extends BaseFormElement {
|
|
39
|
+
constructor(options) {
|
|
40
|
+
const { label, required, fieldValidators = [], formValidators = [], ...base } = options;
|
|
41
|
+
super(base);
|
|
42
|
+
__publicField(this, "required");
|
|
43
|
+
__publicField(this, "formValidators");
|
|
44
|
+
__publicField(this, "fieldValidators");
|
|
45
|
+
__publicField(this, "label");
|
|
46
|
+
/**
|
|
47
|
+
* By default, validation doesn't execute on `onChange` events when editing fields
|
|
48
|
+
* until the field has been `touched`. This can be overridden by setting this to `false`
|
|
49
|
+
* if you want to validate on every `onChange` event. This is important for fields like booleans
|
|
50
|
+
* which don't have a `onBlur` event (which is used to set the `touched` state).
|
|
51
|
+
*/
|
|
52
|
+
__publicField(this, "onlyValidateAfterTouched", true);
|
|
53
|
+
this.label = label;
|
|
54
|
+
this.required = required;
|
|
55
|
+
this.fieldValidators = fieldValidators;
|
|
56
|
+
this.formValidators = formValidators;
|
|
57
|
+
}
|
|
58
|
+
static getFieldCreationSchema() {
|
|
59
|
+
return [];
|
|
60
|
+
}
|
|
61
|
+
isBlank(value) {
|
|
62
|
+
return value === null || value === void 0 || value === "";
|
|
63
|
+
}
|
|
64
|
+
getValueFromChangeEvent(event) {
|
|
65
|
+
return event.target.value;
|
|
66
|
+
}
|
|
67
|
+
getError(value, allValues) {
|
|
68
|
+
if (this.required && this.isBlank(value)) {
|
|
69
|
+
return "This field is required.";
|
|
70
|
+
}
|
|
71
|
+
for (const validator of this.getFieldValidators()) {
|
|
72
|
+
const error = validator(value);
|
|
73
|
+
if (error)
|
|
74
|
+
return error;
|
|
75
|
+
}
|
|
76
|
+
if (allValues) {
|
|
77
|
+
for (const validator of this.getFormValidators()) {
|
|
78
|
+
const error = validator(value, allValues);
|
|
79
|
+
if (error)
|
|
80
|
+
return error;
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
// TODO: We can probably combine _serialize and serialize.
|
|
85
|
+
_serialize() {
|
|
86
|
+
return {
|
|
87
|
+
...super._serialize(),
|
|
88
|
+
label: this.label,
|
|
89
|
+
required: this.required
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
getFieldValidators() {
|
|
93
|
+
return [...this.fieldValidators];
|
|
94
|
+
}
|
|
95
|
+
getFormValidators() {
|
|
96
|
+
return [...this.formValidators];
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
__publicField(BaseField, "fieldTypeName");
|
|
100
|
+
__publicField(BaseField, "fieldTypeDescription");
|
|
101
|
+
const description$1 = "_description_17zed_1";
|
|
102
|
+
const styles$3 = {
|
|
103
|
+
description: description$1
|
|
104
|
+
};
|
|
105
|
+
const InputWithLabel = (props) => {
|
|
106
|
+
const { label, children, severity, inputId, labelId, flexProps } = props;
|
|
107
|
+
return /* @__PURE__ */ jsxRuntime.jsx(blocks.Flex, { direction: "column", gap: "1", asChild: true, ...flexProps, children: /* @__PURE__ */ jsxRuntime.jsxs("label", { htmlFor: inputId, children: [
|
|
108
|
+
/* @__PURE__ */ jsxRuntime.jsx(blocks.Text, { severity, id: labelId, children: label }),
|
|
109
|
+
children
|
|
110
|
+
] }) });
|
|
111
|
+
};
|
|
112
|
+
const InputWithLabelAndHelpText = (props) => {
|
|
113
|
+
const { helpText, children, severity } = props;
|
|
114
|
+
return /* @__PURE__ */ jsxRuntime.jsxs(blocks.Flex, { direction: "column", gap: "1", children: [
|
|
115
|
+
children,
|
|
116
|
+
/* @__PURE__ */ jsxRuntime.jsx(blocks.Flex, { direction: "column", children: /* @__PURE__ */ jsxRuntime.jsx(blocks.Text, { size: "1", severity, className: styles$3.description, children: helpText }) })
|
|
117
|
+
] });
|
|
118
|
+
};
|
|
119
|
+
const useFormikInput = (props) => {
|
|
120
|
+
const { id, field, formId: formId2, ...rest } = props;
|
|
121
|
+
const [fieldProps, meta, helpers] = formik.useField(field.getId());
|
|
122
|
+
const { touched } = meta;
|
|
123
|
+
const helpText = meta.error ?? field.description;
|
|
124
|
+
const severity = meta.error ? "danger" : void 0;
|
|
125
|
+
const inputId = id ?? `${formId2}-${field.getId()}-input`;
|
|
126
|
+
const labelId = `${inputId}-label`;
|
|
127
|
+
const label = field.required ? `${field.label} *` : field.label;
|
|
128
|
+
const fieldPropsWithValidation = React.useMemo(() => {
|
|
129
|
+
const handleChange = (e) => {
|
|
130
|
+
const value = field.getValueFromChangeEvent(e);
|
|
131
|
+
helpers.setValue(value, false).then();
|
|
132
|
+
if (touched || !field.onlyValidateAfterTouched) {
|
|
133
|
+
helpers.setError(field.getError(value));
|
|
134
|
+
}
|
|
135
|
+
};
|
|
136
|
+
const handleBlur = (e) => {
|
|
137
|
+
helpers.setTouched(true, false).then();
|
|
138
|
+
helpers.setError(field.getError(field.getValueFromChangeEvent(e)));
|
|
139
|
+
};
|
|
140
|
+
return {
|
|
141
|
+
...fieldProps,
|
|
142
|
+
onChange: handleChange,
|
|
143
|
+
onBlur: handleBlur
|
|
144
|
+
};
|
|
145
|
+
}, [field, fieldProps, helpers, touched]);
|
|
146
|
+
return [
|
|
147
|
+
{
|
|
148
|
+
helpText,
|
|
149
|
+
severity,
|
|
150
|
+
inputId,
|
|
151
|
+
labelId,
|
|
152
|
+
label,
|
|
153
|
+
fieldProps: fieldPropsWithValidation,
|
|
154
|
+
helpers,
|
|
155
|
+
field
|
|
156
|
+
},
|
|
157
|
+
{ ...rest, "aria-labelledby": labelId }
|
|
158
|
+
];
|
|
159
|
+
};
|
|
160
|
+
const truthyValues = [true, "true"];
|
|
161
|
+
const BooleanInput = React.memo(function BooleanInput2(props) {
|
|
162
|
+
const [{ inputId, labelId, severity, helpText, label, fieldProps }, rest] = useFormikInput(props);
|
|
163
|
+
const color = blocks.useSeverityColor(severity);
|
|
164
|
+
const value = truthyValues.includes(fieldProps.value);
|
|
165
|
+
return /* @__PURE__ */ jsxRuntime.jsx(InputWithLabelAndHelpText, { helpText, severity, children: /* @__PURE__ */ jsxRuntime.jsx(
|
|
166
|
+
InputWithLabel,
|
|
167
|
+
{
|
|
168
|
+
severity,
|
|
169
|
+
inputId,
|
|
170
|
+
labelId,
|
|
171
|
+
label,
|
|
172
|
+
flexProps: { direction: "row-reverse", justify: "end", align: "center", gap: "2" },
|
|
173
|
+
children: /* @__PURE__ */ jsxRuntime.jsx(
|
|
174
|
+
blocks.Checkbox,
|
|
175
|
+
{
|
|
176
|
+
...rest,
|
|
177
|
+
...fieldProps,
|
|
178
|
+
id: inputId,
|
|
179
|
+
color,
|
|
180
|
+
value: value.toString(),
|
|
181
|
+
checked: value,
|
|
182
|
+
onCheckedChange: fieldProps.onChange,
|
|
183
|
+
onChange: void 0,
|
|
184
|
+
onBlur: void 0
|
|
185
|
+
}
|
|
186
|
+
)
|
|
187
|
+
}
|
|
188
|
+
) });
|
|
189
|
+
});
|
|
190
|
+
const _BooleanField = class _BooleanField extends BaseField {
|
|
191
|
+
constructor(options) {
|
|
192
|
+
super({ ...options, type: "boolean" });
|
|
193
|
+
__publicField(this, "onlyValidateAfterTouched", false);
|
|
194
|
+
}
|
|
195
|
+
// if a BooleanField is required, `false` is considered blank
|
|
196
|
+
isBlank(value) {
|
|
197
|
+
return this.required && !value;
|
|
198
|
+
}
|
|
199
|
+
getValueFromChangeEvent(event) {
|
|
200
|
+
if (typeof event === "boolean")
|
|
201
|
+
return event;
|
|
202
|
+
return event.target.checked;
|
|
203
|
+
}
|
|
204
|
+
serialize() {
|
|
205
|
+
return super._serialize();
|
|
206
|
+
}
|
|
207
|
+
static deserialize(data) {
|
|
208
|
+
if (data.type !== "boolean")
|
|
209
|
+
throw new Error("Type mismatch.");
|
|
210
|
+
return new _BooleanField(data);
|
|
211
|
+
}
|
|
212
|
+
getInput(props) {
|
|
213
|
+
return /* @__PURE__ */ jsxRuntime.jsx(BooleanInput, { ...props, field: this });
|
|
214
|
+
}
|
|
215
|
+
};
|
|
216
|
+
__publicField(_BooleanField, "fieldTypeName", "Checkbox");
|
|
217
|
+
__publicField(_BooleanField, "fieldTypeDescription", "Perfect for both optional and required yes/no questions.");
|
|
218
|
+
__publicField(_BooleanField, "Icon", blocks.CheckCircledIcon);
|
|
219
|
+
let BooleanField = _BooleanField;
|
|
220
|
+
const NumberInput$1 = React.memo(function NumberInput2(props) {
|
|
221
|
+
const [{ inputId, labelId, severity, helpText, label, fieldProps, field }, rest] = useFormikInput(props);
|
|
222
|
+
const color = blocks.useSeverityColor(severity);
|
|
223
|
+
return /* @__PURE__ */ jsxRuntime.jsx(InputWithLabelAndHelpText, { helpText, severity, children: /* @__PURE__ */ jsxRuntime.jsx(InputWithLabel, { severity, inputId, labelId, label, children: /* @__PURE__ */ jsxRuntime.jsx(
|
|
224
|
+
blocks.TextField.Input,
|
|
225
|
+
{
|
|
226
|
+
...rest,
|
|
227
|
+
...fieldProps,
|
|
228
|
+
type: "number",
|
|
229
|
+
id: inputId,
|
|
230
|
+
min: field.minimum,
|
|
231
|
+
max: field.maximum,
|
|
232
|
+
step: field.integers ? 1 : 0.1,
|
|
233
|
+
color
|
|
234
|
+
}
|
|
235
|
+
) }) });
|
|
236
|
+
});
|
|
237
|
+
const _NumberField = class _NumberField extends BaseField {
|
|
238
|
+
constructor(options) {
|
|
239
|
+
const {
|
|
240
|
+
minimum = Number.MIN_SAFE_INTEGER,
|
|
241
|
+
maximum = Number.MAX_SAFE_INTEGER,
|
|
242
|
+
integers = false,
|
|
243
|
+
...base
|
|
244
|
+
} = options;
|
|
245
|
+
super({ ...base, type: "number" });
|
|
246
|
+
__publicField(this, "minimum");
|
|
247
|
+
__publicField(this, "maximum");
|
|
248
|
+
__publicField(this, "integers");
|
|
249
|
+
this.minimum = minimum;
|
|
250
|
+
this.maximum = maximum;
|
|
251
|
+
this.integers = integers;
|
|
252
|
+
}
|
|
253
|
+
getValueFromChangeEvent(event) {
|
|
254
|
+
const number = Number.parseFloat(event.target.value);
|
|
255
|
+
if (Number.isNaN(number))
|
|
256
|
+
return "";
|
|
257
|
+
return number;
|
|
258
|
+
}
|
|
259
|
+
static getFieldCreationSchema() {
|
|
260
|
+
return [
|
|
261
|
+
new _NumberField({
|
|
262
|
+
label: "Minimum",
|
|
263
|
+
description: "Minimum value",
|
|
264
|
+
integers: true,
|
|
265
|
+
required: false,
|
|
266
|
+
identifier: "minimum",
|
|
267
|
+
formValidators: [this._validateMin]
|
|
268
|
+
}),
|
|
269
|
+
new _NumberField({
|
|
270
|
+
label: "Maximum",
|
|
271
|
+
description: "Maximum value",
|
|
272
|
+
integers: true,
|
|
273
|
+
required: false,
|
|
274
|
+
identifier: "maximum",
|
|
275
|
+
formValidators: [this._validateMax]
|
|
276
|
+
}),
|
|
277
|
+
new BooleanField({
|
|
278
|
+
label: "Integers",
|
|
279
|
+
description: "Whole numbers only",
|
|
280
|
+
required: false,
|
|
281
|
+
identifier: "integers"
|
|
282
|
+
})
|
|
283
|
+
];
|
|
284
|
+
}
|
|
285
|
+
getFieldValidators() {
|
|
286
|
+
const validators = super.getFieldValidators();
|
|
287
|
+
const min = this.minimum;
|
|
288
|
+
const max = this.maximum;
|
|
289
|
+
if (typeof min === "number") {
|
|
290
|
+
validators.push((value) => {
|
|
291
|
+
if (typeof value === "number" && value < min) {
|
|
292
|
+
return `Must be at least ${this.minimum}.`;
|
|
293
|
+
}
|
|
294
|
+
});
|
|
295
|
+
}
|
|
296
|
+
if (typeof max === "number") {
|
|
297
|
+
validators.push((value) => {
|
|
298
|
+
if (typeof value === "number" && value > max) {
|
|
299
|
+
return `Must be at most ${this.maximum}.`;
|
|
300
|
+
}
|
|
301
|
+
});
|
|
302
|
+
}
|
|
303
|
+
if (this.integers) {
|
|
304
|
+
validators.push((value) => {
|
|
305
|
+
if (typeof value === "number" && !Number.isInteger(value)) {
|
|
306
|
+
return "Must be a whole number.";
|
|
307
|
+
}
|
|
308
|
+
});
|
|
309
|
+
}
|
|
310
|
+
return validators;
|
|
311
|
+
}
|
|
312
|
+
serialize() {
|
|
313
|
+
return {
|
|
314
|
+
...super._serialize(),
|
|
315
|
+
minimum: this.minimum,
|
|
316
|
+
maximum: this.maximum,
|
|
317
|
+
integers: this.integers
|
|
318
|
+
};
|
|
319
|
+
}
|
|
320
|
+
static deserialize(data) {
|
|
321
|
+
if (data.type !== "number")
|
|
322
|
+
throw new Error("Type mismatch.");
|
|
323
|
+
return new _NumberField(data);
|
|
324
|
+
}
|
|
325
|
+
getInput(props) {
|
|
326
|
+
return /* @__PURE__ */ jsxRuntime.jsx(NumberInput$1, { field: this, ...props });
|
|
327
|
+
}
|
|
328
|
+
};
|
|
329
|
+
__publicField(_NumberField, "fieldTypeName", "Number");
|
|
330
|
+
__publicField(_NumberField, "fieldTypeDescription", "Allows specifying a number within a given range.");
|
|
331
|
+
__publicField(_NumberField, "Icon", blocks.FontFamilyIcon);
|
|
332
|
+
__publicField(_NumberField, "_validateMin", (value, allValues) => {
|
|
333
|
+
if (typeof allValues.maximum === "number" && typeof value === "number" && allValues.maximum < value) {
|
|
334
|
+
return "Minimum cannot be greater than minimum.";
|
|
335
|
+
}
|
|
336
|
+
return null;
|
|
337
|
+
});
|
|
338
|
+
__publicField(_NumberField, "_validateMax", (value, allValues) => {
|
|
339
|
+
if (typeof allValues.minimum === "number" && typeof value === "number" && allValues.minimum > value) {
|
|
340
|
+
return "Maximum cannot be less than minimum.";
|
|
341
|
+
}
|
|
342
|
+
return null;
|
|
343
|
+
});
|
|
344
|
+
let NumberField = _NumberField;
|
|
345
|
+
const DateInput = React.memo(function DateInput2(props) {
|
|
346
|
+
const [{ inputId, labelId, severity, helpText, label, fieldProps }, rest] = useFormikInput(props);
|
|
347
|
+
const color = blocks.useSeverityColor(severity);
|
|
348
|
+
const value = fieldProps.value ? fieldProps.value.split("T")[0] : "";
|
|
349
|
+
return /* @__PURE__ */ jsxRuntime.jsx(InputWithLabelAndHelpText, { helpText, severity, children: /* @__PURE__ */ jsxRuntime.jsx(InputWithLabel, { severity, inputId, labelId, label, children: /* @__PURE__ */ jsxRuntime.jsx(blocks.TextField.Input, { ...rest, ...fieldProps, type: "date", id: inputId, color, value }) }) });
|
|
350
|
+
});
|
|
351
|
+
const _DateField = class _DateField extends BaseField {
|
|
352
|
+
constructor(options) {
|
|
353
|
+
super({ ...options, type: "date" });
|
|
354
|
+
__publicField(this, "onlyValidateAfterTouched", false);
|
|
355
|
+
}
|
|
356
|
+
serialize() {
|
|
357
|
+
return super._serialize();
|
|
358
|
+
}
|
|
359
|
+
getValueFromChangeEvent(event) {
|
|
360
|
+
return new Date(event.target.value).toISOString();
|
|
361
|
+
}
|
|
362
|
+
static deserialize(data) {
|
|
363
|
+
if (data.type !== "date")
|
|
364
|
+
throw new Error("Type mismatch.");
|
|
365
|
+
return new _DateField(data);
|
|
366
|
+
}
|
|
367
|
+
getInput(props) {
|
|
368
|
+
return /* @__PURE__ */ jsxRuntime.jsx(DateInput, { field: this, ...props });
|
|
369
|
+
}
|
|
370
|
+
};
|
|
371
|
+
__publicField(_DateField, "fieldTypeName", "Date");
|
|
372
|
+
__publicField(_DateField, "fieldTypeDescription", "Allows specifying a date.");
|
|
373
|
+
__publicField(_DateField, "Icon", blocks.CalendarIcon);
|
|
374
|
+
let DateField = _DateField;
|
|
375
|
+
class StringOrTextField extends BaseField {
|
|
376
|
+
constructor(options) {
|
|
377
|
+
const { minLength, maxLength = 5e3, ...base } = options;
|
|
378
|
+
super(base);
|
|
379
|
+
__publicField(this, "minLength");
|
|
380
|
+
__publicField(this, "maxLength");
|
|
381
|
+
this.minLength = minLength ? Math.max(minLength, 0) : void 0;
|
|
382
|
+
this.maxLength = maxLength ? Math.max(maxLength, 0) : 5e3;
|
|
383
|
+
}
|
|
384
|
+
static getFieldCreationSchema() {
|
|
385
|
+
return [
|
|
386
|
+
// min, max
|
|
387
|
+
new NumberField({
|
|
388
|
+
label: "Minimum length",
|
|
389
|
+
description: "Minimum number of characters",
|
|
390
|
+
required: false,
|
|
391
|
+
identifier: "minimum_length",
|
|
392
|
+
minimum: 0,
|
|
393
|
+
maximum: 100,
|
|
394
|
+
formValidators: [this._validateMin],
|
|
395
|
+
integers: true
|
|
396
|
+
}),
|
|
397
|
+
new NumberField({
|
|
398
|
+
label: "Maximum length",
|
|
399
|
+
description: "Maximum number of characters",
|
|
400
|
+
required: false,
|
|
401
|
+
identifier: "maximum_length",
|
|
402
|
+
minimum: 1,
|
|
403
|
+
maximum: 5e3,
|
|
404
|
+
// TODO: depends on short vs long text
|
|
405
|
+
formValidators: [this._validateMax],
|
|
406
|
+
// TODO: default: 500 (see: "Short text fields can hold up to 500 characters on a single line.")
|
|
407
|
+
integers: true
|
|
408
|
+
})
|
|
409
|
+
];
|
|
410
|
+
}
|
|
411
|
+
getFieldValidators() {
|
|
412
|
+
const validators = super.getFieldValidators();
|
|
413
|
+
if (this.minLength) {
|
|
414
|
+
validators.push((value) => {
|
|
415
|
+
if (this.minLength && (!value || value.length < this.minLength)) {
|
|
416
|
+
if (!this.required && !value)
|
|
417
|
+
return null;
|
|
418
|
+
return `Minimum ${this.minLength} character(s).`;
|
|
419
|
+
}
|
|
420
|
+
});
|
|
421
|
+
}
|
|
422
|
+
if (this.maxLength) {
|
|
423
|
+
validators.push((value) => {
|
|
424
|
+
if (typeof value === "string" && this.maxLength && value.length > this.maxLength) {
|
|
425
|
+
return `Maximum ${this.maxLength} character(s).`;
|
|
426
|
+
}
|
|
427
|
+
});
|
|
428
|
+
}
|
|
429
|
+
return validators;
|
|
430
|
+
}
|
|
431
|
+
_serialize() {
|
|
432
|
+
if (!this.identifier) {
|
|
433
|
+
throw new Error("Field identifier must be set before serializing.");
|
|
434
|
+
}
|
|
435
|
+
return {
|
|
436
|
+
...super._serialize(),
|
|
437
|
+
minimum_length: this.minLength,
|
|
438
|
+
maximum_length: this.maxLength
|
|
439
|
+
};
|
|
440
|
+
}
|
|
441
|
+
}
|
|
442
|
+
/**
|
|
443
|
+
* This function validates that the value given for "minimum length" (when creating a new field) is less than or
|
|
444
|
+
* equal to the value given for "maximum length".
|
|
445
|
+
*/
|
|
446
|
+
__publicField(StringOrTextField, "_validateMin", (value, allValues) => {
|
|
447
|
+
if (typeof allValues.maximum_length === "number" && typeof value === "number" && allValues.maximum_length < value) {
|
|
448
|
+
return "Minimum cannot be greater than maximum.";
|
|
449
|
+
}
|
|
450
|
+
return null;
|
|
451
|
+
});
|
|
452
|
+
/**
|
|
453
|
+
* This function validates that the value given for "maximum length" (when creating a new field) is greater than or
|
|
454
|
+
* equal to the value given for "minimum length".
|
|
455
|
+
*/
|
|
456
|
+
__publicField(StringOrTextField, "_validateMax", (value, allValues) => {
|
|
457
|
+
if (typeof value !== "number")
|
|
458
|
+
return null;
|
|
459
|
+
const { minimum_length: minimumLength } = allValues;
|
|
460
|
+
if (typeof minimumLength !== "number") {
|
|
461
|
+
return null;
|
|
462
|
+
}
|
|
463
|
+
if (minimumLength > value) {
|
|
464
|
+
return "Maximum cannot be less than minimum.";
|
|
465
|
+
}
|
|
466
|
+
return null;
|
|
467
|
+
});
|
|
468
|
+
const StringInput = React.memo(function StringInput2(props) {
|
|
469
|
+
const [{ inputId, labelId, severity, helpText, label, fieldProps, field }, rest] = useFormikInput(props);
|
|
470
|
+
const color = blocks.useSeverityColor(severity);
|
|
471
|
+
return /* @__PURE__ */ jsxRuntime.jsx(InputWithLabelAndHelpText, { helpText, severity, children: /* @__PURE__ */ jsxRuntime.jsx(InputWithLabel, { severity, inputId, labelId, label, children: /* @__PURE__ */ jsxRuntime.jsx(blocks.TextField.Input, { ...rest, ...fieldProps, type: field.inputType, id: inputId, color }) }) });
|
|
472
|
+
});
|
|
473
|
+
const _StringField = class _StringField extends StringOrTextField {
|
|
474
|
+
constructor(options) {
|
|
475
|
+
const { inputType = "text", ...rest } = options;
|
|
476
|
+
const maxLength = options.maxLength ? Math.min(500, options.maxLength) : 500;
|
|
477
|
+
const minLength = options.minLength ? Math.min(options.minLength, maxLength) : void 0;
|
|
478
|
+
super({ ...rest, maxLength, minLength, type: "string" });
|
|
479
|
+
__publicField(this, "inputType");
|
|
480
|
+
this.inputType = inputType;
|
|
481
|
+
}
|
|
482
|
+
serialize() {
|
|
483
|
+
return { ...super._serialize(), input_type: this.inputType };
|
|
484
|
+
}
|
|
485
|
+
static deserialize(data) {
|
|
486
|
+
if (data.type !== "string")
|
|
487
|
+
throw new Error("Type mismatch.");
|
|
488
|
+
const { maximum_length, minimum_length, input_type, ...rest } = data;
|
|
489
|
+
return new _StringField({ ...rest, maxLength: maximum_length, minLength: minimum_length, inputType: input_type });
|
|
490
|
+
}
|
|
491
|
+
getInput(props) {
|
|
492
|
+
return /* @__PURE__ */ jsxRuntime.jsx(StringInput, { field: this, ...props });
|
|
493
|
+
}
|
|
494
|
+
};
|
|
495
|
+
__publicField(_StringField, "fieldTypeName", "Short Text");
|
|
496
|
+
__publicField(_StringField, "fieldTypeDescription", "Short text fields can hold up to 500 characters on a single line.");
|
|
497
|
+
__publicField(_StringField, "Icon", blocks.InputIcon);
|
|
498
|
+
let StringField = _StringField;
|
|
499
|
+
const TextInput = React.memo(function TextInput2(props) {
|
|
500
|
+
const [{ inputId, labelId, severity, helpText, label, fieldProps }, rest] = useFormikInput(props);
|
|
501
|
+
return /* @__PURE__ */ jsxRuntime.jsx(InputWithLabelAndHelpText, { helpText, severity, children: /* @__PURE__ */ jsxRuntime.jsx(InputWithLabel, { severity, inputId, labelId, label, children: /* @__PURE__ */ jsxRuntime.jsx(blocks.TextArea, { ...rest, ...fieldProps, resize: "vertical", id: inputId, severity }) }) });
|
|
502
|
+
});
|
|
503
|
+
const _TextField = class _TextField extends StringOrTextField {
|
|
504
|
+
constructor(options) {
|
|
505
|
+
const maxLength = options.maxLength ? Math.min(5e3, options.maxLength) : 5e3;
|
|
506
|
+
const minLength = options.minLength ? Math.min(options.minLength, maxLength) : void 0;
|
|
507
|
+
super({ ...options, maxLength, minLength, type: "text" });
|
|
508
|
+
}
|
|
509
|
+
serialize() {
|
|
510
|
+
return super._serialize();
|
|
511
|
+
}
|
|
512
|
+
static deserialize(data) {
|
|
513
|
+
if (data.type !== "text")
|
|
514
|
+
throw new Error("Type mismatch.");
|
|
515
|
+
const { maximum_length, minimum_length, ...rest } = data;
|
|
516
|
+
return new _TextField({ ...rest, maxLength: maximum_length, minLength: minimum_length });
|
|
517
|
+
}
|
|
518
|
+
getInput(props) {
|
|
519
|
+
return /* @__PURE__ */ jsxRuntime.jsx(TextInput, { field: this, ...props });
|
|
520
|
+
}
|
|
521
|
+
};
|
|
522
|
+
__publicField(_TextField, "fieldTypeName", "Paragraph");
|
|
523
|
+
__publicField(_TextField, "fieldTypeDescription", "Paragraph fields can hold up to 5000 characters and can have multiple lines.");
|
|
524
|
+
__publicField(_TextField, "Icon", blocks.RowsIcon);
|
|
525
|
+
let TextField = _TextField;
|
|
526
|
+
const SelectInput = React.memo(function SelectInput2(props) {
|
|
527
|
+
const [{ inputId, labelId, severity, helpText, label, fieldProps, field }, rest] = useFormikInput(props);
|
|
528
|
+
const { onChange, onBlur } = fieldProps;
|
|
529
|
+
const options = React.useMemo(
|
|
530
|
+
() => field.options.map((option) => ({ value: option.value, itemContent: option.label })),
|
|
531
|
+
[field.options]
|
|
532
|
+
);
|
|
533
|
+
const handleChange = React.useCallback(
|
|
534
|
+
(value) => {
|
|
535
|
+
onChange(value);
|
|
536
|
+
onBlur(value);
|
|
537
|
+
},
|
|
538
|
+
[onChange, onBlur]
|
|
539
|
+
);
|
|
540
|
+
return /* @__PURE__ */ jsxRuntime.jsx(InputWithLabelAndHelpText, { helpText, severity, children: /* @__PURE__ */ jsxRuntime.jsx(InputWithLabel, { severity, inputId, labelId, label, children: /* @__PURE__ */ jsxRuntime.jsx(
|
|
541
|
+
blocks.Select,
|
|
542
|
+
{
|
|
543
|
+
items: options,
|
|
544
|
+
...fieldProps,
|
|
545
|
+
onValueChange: handleChange,
|
|
546
|
+
placeholder: "Select one...",
|
|
547
|
+
id: inputId,
|
|
548
|
+
severity,
|
|
549
|
+
...rest
|
|
550
|
+
}
|
|
551
|
+
) }) });
|
|
552
|
+
});
|
|
553
|
+
const emptySection = (id = "", fields = []) => ({
|
|
554
|
+
type: "section",
|
|
555
|
+
fields,
|
|
556
|
+
identifier: id,
|
|
557
|
+
label: null,
|
|
558
|
+
condition: null,
|
|
559
|
+
conditional: false
|
|
560
|
+
});
|
|
561
|
+
const wrapRootFieldsWithFieldSection = (revision) => {
|
|
562
|
+
if (!revision)
|
|
563
|
+
return void 0;
|
|
564
|
+
const fields = revision.fields;
|
|
565
|
+
let pending = [];
|
|
566
|
+
const sections = [];
|
|
567
|
+
for (const field of fields) {
|
|
568
|
+
if (field.type === "section") {
|
|
569
|
+
if (pending.length > 0) {
|
|
570
|
+
sections.push(emptySection(`AUTO_section-${fields.indexOf(field)}`, pending));
|
|
571
|
+
pending = [];
|
|
572
|
+
}
|
|
573
|
+
sections.push(field);
|
|
574
|
+
} else {
|
|
575
|
+
pending.push(field);
|
|
576
|
+
}
|
|
577
|
+
}
|
|
578
|
+
if (pending.length > 0) {
|
|
579
|
+
sections.push(emptySection("AUTO_section-last", pending));
|
|
580
|
+
}
|
|
581
|
+
return { ...revision, fields: sections, description: revision.description ?? "" };
|
|
582
|
+
};
|
|
583
|
+
function reorder(list, source, destination) {
|
|
584
|
+
const result = Array.from(list);
|
|
585
|
+
const [removed] = result.splice(source, 1);
|
|
586
|
+
if (!removed)
|
|
587
|
+
throw new Error("Could not find field to reorder.");
|
|
588
|
+
result.splice(destination, 0, removed);
|
|
589
|
+
return result;
|
|
590
|
+
}
|
|
591
|
+
function replace(list, index, value) {
|
|
592
|
+
const result = Array.from(list);
|
|
593
|
+
result[index] = value;
|
|
594
|
+
return result;
|
|
595
|
+
}
|
|
596
|
+
function insert(list, index, value) {
|
|
597
|
+
const result = Array.from(list ?? []);
|
|
598
|
+
result.splice(index, 0, value);
|
|
599
|
+
return result;
|
|
600
|
+
}
|
|
601
|
+
function remove(list, index) {
|
|
602
|
+
const result = Array.from(list);
|
|
603
|
+
result.splice(index, 1);
|
|
604
|
+
return result;
|
|
605
|
+
}
|
|
606
|
+
const makeIdentifier = (existing, label) => {
|
|
607
|
+
if (typeof existing === "string" && existing.length > 0)
|
|
608
|
+
return existing;
|
|
609
|
+
const now = /* @__PURE__ */ new Date();
|
|
610
|
+
return `${core.slugify(label)}-${now.getTime()}`;
|
|
611
|
+
};
|
|
612
|
+
const findFieldByIdentifier = (fields, identifier) => {
|
|
613
|
+
if (!identifier)
|
|
614
|
+
return null;
|
|
615
|
+
for (const field of fields) {
|
|
616
|
+
if (field.type === "section") {
|
|
617
|
+
const result = findFieldByIdentifier(field.fields, identifier);
|
|
618
|
+
if (result)
|
|
619
|
+
return result;
|
|
620
|
+
} else if (field.identifier === identifier) {
|
|
621
|
+
return field;
|
|
622
|
+
}
|
|
623
|
+
}
|
|
624
|
+
return null;
|
|
625
|
+
};
|
|
626
|
+
const makeConditionalSourceFields = (sections, index) => {
|
|
627
|
+
return sections.filter((_, i) => i < index).flatMap((field) => field.fields);
|
|
628
|
+
};
|
|
629
|
+
const getTakenFieldLabels = (fields) => {
|
|
630
|
+
return fields.flatMap(
|
|
631
|
+
(field) => field.type === "section" ? [...field.fields.map((f) => f.label), field.label] : field.label
|
|
632
|
+
).filter((id) => id !== null);
|
|
633
|
+
};
|
|
634
|
+
const incrementFieldLabel = (label, takenLabels) => {
|
|
635
|
+
let count = 1;
|
|
636
|
+
let newLabel = `${label} (${count})`;
|
|
637
|
+
while (takenLabels.includes(newLabel)) {
|
|
638
|
+
newLabel = `${label} (${++count})`;
|
|
639
|
+
}
|
|
640
|
+
return newLabel;
|
|
641
|
+
};
|
|
642
|
+
const MultiStringInput = React.memo(function MultiStringInput2(props) {
|
|
643
|
+
const [{ inputId, labelId, severity, helpText, label, fieldProps }, rest] = useFormikInput(props);
|
|
644
|
+
const color = blocks.useSeverityColor(severity);
|
|
645
|
+
const value = React.useMemo(() => Array.isArray(fieldProps.value) ? fieldProps.value : [], [fieldProps.value]);
|
|
646
|
+
const { onChange, onBlur } = fieldProps;
|
|
647
|
+
const droppableId = `${inputId}-droppable`;
|
|
648
|
+
const { disabled } = rest;
|
|
649
|
+
const [intermediateValue, setIntermediateValue] = React.useState("");
|
|
650
|
+
const [internalError, setInternalError] = React.useState("");
|
|
651
|
+
const updatedHelpText = internalError || helpText;
|
|
652
|
+
const updatedColor = internalError ? "red" : color;
|
|
653
|
+
const setValueAndTouched = React.useCallback(
|
|
654
|
+
(newValue) => {
|
|
655
|
+
onChange(newValue);
|
|
656
|
+
onBlur(newValue);
|
|
657
|
+
},
|
|
658
|
+
[onChange, onBlur]
|
|
659
|
+
);
|
|
660
|
+
const handleChange = React.useCallback(
|
|
661
|
+
(e) => {
|
|
662
|
+
if (value.findIndex((option) => option.value === e.target.value.trim()) >= 0) {
|
|
663
|
+
setInternalError("All options must be unique");
|
|
664
|
+
} else if (!e.target.value) {
|
|
665
|
+
setInternalError("Option cannot be empty");
|
|
666
|
+
} else {
|
|
667
|
+
setInternalError("");
|
|
668
|
+
}
|
|
669
|
+
setIntermediateValue(e.target.value);
|
|
670
|
+
},
|
|
671
|
+
[setIntermediateValue, value]
|
|
672
|
+
);
|
|
673
|
+
const addOption = React.useCallback(() => {
|
|
674
|
+
if (internalError)
|
|
675
|
+
return;
|
|
676
|
+
if (!intermediateValue.trim()) {
|
|
677
|
+
return setInternalError("Option cannot be empty");
|
|
678
|
+
}
|
|
679
|
+
const trimmedValue = intermediateValue.trim();
|
|
680
|
+
setValueAndTouched([...value, { value: trimmedValue, label: trimmedValue }]);
|
|
681
|
+
setIntermediateValue("");
|
|
682
|
+
}, [intermediateValue, internalError, setValueAndTouched, value]);
|
|
683
|
+
const handleKeyDown = React.useCallback(
|
|
684
|
+
(e) => {
|
|
685
|
+
if (e.key === "Enter") {
|
|
686
|
+
e.preventDefault();
|
|
687
|
+
addOption();
|
|
688
|
+
}
|
|
689
|
+
},
|
|
690
|
+
[addOption]
|
|
691
|
+
);
|
|
692
|
+
const handleDeleteOption = React.useCallback(
|
|
693
|
+
(index) => {
|
|
694
|
+
setValueAndTouched(remove(value, index));
|
|
695
|
+
},
|
|
696
|
+
[value, setValueAndTouched]
|
|
697
|
+
);
|
|
698
|
+
const handleDragEnd = React.useCallback(
|
|
699
|
+
(result) => {
|
|
700
|
+
if (!result.destination)
|
|
701
|
+
return;
|
|
702
|
+
const sourceIndex = result.source.index;
|
|
703
|
+
const destinationIndex = result.destination.index;
|
|
704
|
+
setValueAndTouched(reorder(value, sourceIndex, destinationIndex));
|
|
705
|
+
},
|
|
706
|
+
[setValueAndTouched, value]
|
|
707
|
+
);
|
|
708
|
+
return /* @__PURE__ */ jsxRuntime.jsx(dnd.DragDropContext, { onDragEnd: handleDragEnd, children: /* @__PURE__ */ jsxRuntime.jsxs(blocks.Flex, { direction: "column", gap: "2", children: [
|
|
709
|
+
/* @__PURE__ */ jsxRuntime.jsx(InputWithLabelAndHelpText, { helpText: updatedHelpText, severity, children: /* @__PURE__ */ jsxRuntime.jsx(InputWithLabel, { severity, inputId, labelId, label, children: (!disabled || value.length === 0) && /* @__PURE__ */ jsxRuntime.jsxs(blocks.Flex, { gap: "2", children: [
|
|
710
|
+
/* @__PURE__ */ jsxRuntime.jsx(blocks.Box, { grow: "1", children: /* @__PURE__ */ jsxRuntime.jsx(
|
|
711
|
+
blocks.TextField.Input,
|
|
712
|
+
{
|
|
713
|
+
placeholder: "Press enter to add a new option",
|
|
714
|
+
...rest,
|
|
715
|
+
...fieldProps,
|
|
716
|
+
value: intermediateValue,
|
|
717
|
+
onChange: handleChange,
|
|
718
|
+
onKeyDown: handleKeyDown,
|
|
719
|
+
id: inputId,
|
|
720
|
+
color: updatedColor,
|
|
721
|
+
onBlur: void 0
|
|
722
|
+
}
|
|
723
|
+
) }),
|
|
724
|
+
/* @__PURE__ */ jsxRuntime.jsx(
|
|
725
|
+
blocks.IconButton,
|
|
726
|
+
{
|
|
727
|
+
type: "button",
|
|
728
|
+
"aria-label": "Add option",
|
|
729
|
+
disabled: !!internalError || disabled,
|
|
730
|
+
onClick: addOption,
|
|
731
|
+
children: /* @__PURE__ */ jsxRuntime.jsx(blocks.PlusIcon, {})
|
|
732
|
+
}
|
|
733
|
+
)
|
|
734
|
+
] }) }) }),
|
|
735
|
+
/* @__PURE__ */ jsxRuntime.jsx(dnd.Droppable, { droppableId, children: (droppableProvided) => /* @__PURE__ */ jsxRuntime.jsxs(blocks.Flex, { ...droppableProvided.droppableProps, ref: droppableProvided.innerRef, direction: "column", children: [
|
|
736
|
+
value.map((option, index) => /* @__PURE__ */ jsxRuntime.jsx(
|
|
737
|
+
dnd.Draggable,
|
|
738
|
+
{
|
|
739
|
+
draggableId: `${option.value}-draggable`,
|
|
740
|
+
index,
|
|
741
|
+
isDragDisabled: disabled,
|
|
742
|
+
children: ({ draggableProps, dragHandleProps, innerRef }) => /* @__PURE__ */ jsxRuntime.jsx(
|
|
743
|
+
blocks.Flex,
|
|
744
|
+
{
|
|
745
|
+
...dragHandleProps,
|
|
746
|
+
...draggableProps,
|
|
747
|
+
ref: innerRef,
|
|
748
|
+
gap: "2",
|
|
749
|
+
align: "center",
|
|
750
|
+
justify: "between",
|
|
751
|
+
mb: "1",
|
|
752
|
+
asChild: true,
|
|
753
|
+
children: /* @__PURE__ */ jsxRuntime.jsxs(blocks.Badge, { color: "gray", size: "2", children: [
|
|
754
|
+
/* @__PURE__ */ jsxRuntime.jsx("span", { children: option.label }),
|
|
755
|
+
/* @__PURE__ */ jsxRuntime.jsx(
|
|
756
|
+
blocks.IconButton,
|
|
757
|
+
{
|
|
758
|
+
size: "small",
|
|
759
|
+
variant: "ghost",
|
|
760
|
+
type: "button",
|
|
761
|
+
"aria-label": "Delete option",
|
|
762
|
+
severity: "info",
|
|
763
|
+
disabled,
|
|
764
|
+
onClick: () => handleDeleteOption(index),
|
|
765
|
+
children: /* @__PURE__ */ jsxRuntime.jsx(blocks.Cross1Icon, {})
|
|
766
|
+
}
|
|
767
|
+
)
|
|
768
|
+
] })
|
|
769
|
+
}
|
|
770
|
+
)
|
|
771
|
+
},
|
|
772
|
+
option.value
|
|
773
|
+
)),
|
|
774
|
+
droppableProvided.placeholder
|
|
775
|
+
] }) })
|
|
776
|
+
] }) });
|
|
777
|
+
});
|
|
778
|
+
const _MultiStringField = class _MultiStringField extends BaseField {
|
|
779
|
+
constructor(options) {
|
|
780
|
+
const { minimum_length, maximum_length, ...rest } = options;
|
|
781
|
+
super({ ...rest, type: "multi-string" });
|
|
782
|
+
__publicField(this, "minLength");
|
|
783
|
+
__publicField(this, "maxLength");
|
|
784
|
+
__publicField(this, "onlyValidateAfterTouched", false);
|
|
785
|
+
this.minLength = minimum_length ?? 0;
|
|
786
|
+
this.maxLength = maximum_length ?? Infinity;
|
|
787
|
+
}
|
|
788
|
+
getValueFromChangeEvent(event) {
|
|
789
|
+
if (Array.isArray(event))
|
|
790
|
+
return event;
|
|
791
|
+
throw new Error("Expected an array.");
|
|
792
|
+
}
|
|
793
|
+
getInput(props) {
|
|
794
|
+
return /* @__PURE__ */ jsxRuntime.jsx(MultiStringInput, { field: this, ...props });
|
|
795
|
+
}
|
|
796
|
+
serialize() {
|
|
797
|
+
return { ...super._serialize(), minimum_length: this.minLength, maximum_length: this.maxLength };
|
|
798
|
+
}
|
|
799
|
+
isBlank(value) {
|
|
800
|
+
return super.isBlank(value) || value.length === 0;
|
|
801
|
+
}
|
|
802
|
+
getFieldValidators() {
|
|
803
|
+
const validators = super.getFieldValidators();
|
|
804
|
+
validators.push((value) => {
|
|
805
|
+
if (Array.isArray(value) && value.length < this.minLength) {
|
|
806
|
+
return `Must have at least ${this.minLength} options.`;
|
|
807
|
+
}
|
|
808
|
+
});
|
|
809
|
+
validators.push((value) => {
|
|
810
|
+
if (Array.isArray(value) && value.length > this.maxLength) {
|
|
811
|
+
return `Must have at most ${this.maxLength} options.`;
|
|
812
|
+
}
|
|
813
|
+
});
|
|
814
|
+
return validators;
|
|
815
|
+
}
|
|
816
|
+
static deserialize(data) {
|
|
817
|
+
if (data.type !== "multi-string")
|
|
818
|
+
throw new Error("Type mismatch.");
|
|
819
|
+
return new _MultiStringField(data);
|
|
820
|
+
}
|
|
821
|
+
};
|
|
822
|
+
__publicField(_MultiStringField, "fieldTypeName", "Multi-string");
|
|
823
|
+
__publicField(_MultiStringField, "fieldTypeDescription", "Allows the user to provide multiple unique strings.");
|
|
824
|
+
__publicField(_MultiStringField, "Icon", blocks.ListBulletIcon);
|
|
825
|
+
let MultiStringField = _MultiStringField;
|
|
826
|
+
class BaseSelectField extends BaseField {
|
|
827
|
+
constructor(options) {
|
|
828
|
+
super(options);
|
|
829
|
+
__publicField(this, "options");
|
|
830
|
+
__publicField(this, "onlyValidateAfterTouched", false);
|
|
831
|
+
const encounteredIds = /* @__PURE__ */ new Set();
|
|
832
|
+
this.options = options.options.map((option) => {
|
|
833
|
+
if (typeof option === "string") {
|
|
834
|
+
option = { label: option, value: option };
|
|
835
|
+
}
|
|
836
|
+
encounteredIds.add(option.label);
|
|
837
|
+
return option;
|
|
838
|
+
});
|
|
839
|
+
if (encounteredIds.size !== options.options.length) {
|
|
840
|
+
console.error(
|
|
841
|
+
`${options.options.length - encounteredIds.size} duplicate identifiers found in options. This may cause unexpected behavior. Options:`,
|
|
842
|
+
options.options
|
|
843
|
+
);
|
|
844
|
+
}
|
|
845
|
+
}
|
|
846
|
+
_serialize() {
|
|
847
|
+
return {
|
|
848
|
+
...super._serialize(),
|
|
849
|
+
options: this.options
|
|
850
|
+
};
|
|
851
|
+
}
|
|
852
|
+
static getFieldCreationSchema() {
|
|
853
|
+
return [
|
|
854
|
+
new MultiStringField({
|
|
855
|
+
label: "Options",
|
|
856
|
+
description: "List possible options for the user to select from.",
|
|
857
|
+
required: true,
|
|
858
|
+
identifier: "options",
|
|
859
|
+
minimum_length: 2,
|
|
860
|
+
maximum_length: 20
|
|
861
|
+
})
|
|
862
|
+
];
|
|
863
|
+
}
|
|
864
|
+
}
|
|
865
|
+
const _SelectField = class _SelectField extends BaseSelectField {
|
|
866
|
+
constructor(options) {
|
|
867
|
+
super({ ...options, type: "select" });
|
|
868
|
+
}
|
|
869
|
+
getValueFromChangeEvent(event) {
|
|
870
|
+
if (typeof event === "string")
|
|
871
|
+
return event;
|
|
872
|
+
return event.target.value;
|
|
873
|
+
}
|
|
874
|
+
serialize() {
|
|
875
|
+
return super._serialize();
|
|
876
|
+
}
|
|
877
|
+
static deserialize(data) {
|
|
878
|
+
if (data.type !== "select")
|
|
879
|
+
throw new Error("Type mismatch.");
|
|
880
|
+
return new _SelectField(data);
|
|
881
|
+
}
|
|
882
|
+
getInput(props) {
|
|
883
|
+
return /* @__PURE__ */ jsxRuntime.jsx(SelectInput, { field: this, ...props });
|
|
884
|
+
}
|
|
885
|
+
};
|
|
886
|
+
__publicField(_SelectField, "fieldTypeName", "Dropdown");
|
|
887
|
+
__publicField(_SelectField, "fieldTypeDescription", "Allows the user to select a single option from a list of options.");
|
|
888
|
+
__publicField(_SelectField, "Icon", blocks.DropdownMenuIcon);
|
|
889
|
+
let SelectField = _SelectField;
|
|
890
|
+
const parseValueToArray = (value) => {
|
|
891
|
+
if (!value)
|
|
892
|
+
return [];
|
|
893
|
+
if (Array.isArray(value))
|
|
894
|
+
return value;
|
|
895
|
+
return [value];
|
|
896
|
+
};
|
|
897
|
+
const MultiSelectInput = React.memo(function MultiSelectInput2(props) {
|
|
898
|
+
const [{ inputId, labelId, severity, helpText, label, fieldProps, field }, rest] = useFormikInput(props);
|
|
899
|
+
const { onChange, onBlur } = fieldProps;
|
|
900
|
+
const value = React.useMemo(() => parseValueToArray(fieldProps.value), [fieldProps.value]);
|
|
901
|
+
const handleChange = React.useCallback(
|
|
902
|
+
(value2) => {
|
|
903
|
+
onChange(value2);
|
|
904
|
+
onBlur(value2);
|
|
905
|
+
},
|
|
906
|
+
[onChange, onBlur]
|
|
907
|
+
);
|
|
908
|
+
return /* @__PURE__ */ jsxRuntime.jsx(InputWithLabelAndHelpText, { helpText, severity, children: /* @__PURE__ */ jsxRuntime.jsx(InputWithLabel, { severity, inputId, labelId, label, children: /* @__PURE__ */ jsxRuntime.jsx(
|
|
909
|
+
blocks.MultiSelect,
|
|
910
|
+
{
|
|
911
|
+
value,
|
|
912
|
+
onValueChange: handleChange,
|
|
913
|
+
options: field.options,
|
|
914
|
+
name: fieldProps.name,
|
|
915
|
+
placeholder: "Select one or more...",
|
|
916
|
+
id: inputId,
|
|
917
|
+
severity,
|
|
918
|
+
...rest
|
|
919
|
+
}
|
|
920
|
+
) }) });
|
|
921
|
+
});
|
|
922
|
+
const _MultiSelectField = class _MultiSelectField extends BaseSelectField {
|
|
923
|
+
constructor(options) {
|
|
924
|
+
super({ ...options, type: "multi-select" });
|
|
925
|
+
}
|
|
926
|
+
getValueFromChangeEvent(event) {
|
|
927
|
+
if (Array.isArray(event))
|
|
928
|
+
return event;
|
|
929
|
+
throw new Error("Expected an array.");
|
|
930
|
+
}
|
|
931
|
+
isBlank(value) {
|
|
932
|
+
return super.isBlank(value) || value.length === 0;
|
|
933
|
+
}
|
|
934
|
+
serialize() {
|
|
935
|
+
return super._serialize();
|
|
936
|
+
}
|
|
937
|
+
static deserialize(data) {
|
|
938
|
+
if (data.type !== "multi-select")
|
|
939
|
+
throw new Error("Type mismatch.");
|
|
940
|
+
return new _MultiSelectField(data);
|
|
941
|
+
}
|
|
942
|
+
getInput(props) {
|
|
943
|
+
return /* @__PURE__ */ jsxRuntime.jsx(MultiSelectInput, { field: this, ...props });
|
|
944
|
+
}
|
|
945
|
+
};
|
|
946
|
+
__publicField(_MultiSelectField, "fieldTypeName", "Multi-select");
|
|
947
|
+
__publicField(_MultiSelectField, "fieldTypeDescription", "Allows the user to select a multiple options from a list of options.");
|
|
948
|
+
__publicField(_MultiSelectField, "Icon", blocks.CheckboxIcon);
|
|
949
|
+
let MultiSelectField = _MultiSelectField;
|
|
950
|
+
const FieldInputCloner = React.memo(function FieldInputCloner2({ field, ...props }) {
|
|
951
|
+
const [{ value: identifier }] = formik.useField(field.options.clonedFieldIdentifier);
|
|
952
|
+
const deserializedField = React.useMemo(() => {
|
|
953
|
+
const options = field.options.getFieldToClone(identifier);
|
|
954
|
+
if (!options)
|
|
955
|
+
return null;
|
|
956
|
+
return deserialize(options);
|
|
957
|
+
}, [field.options, identifier]);
|
|
958
|
+
return useFieldInput(deserializedField, props);
|
|
959
|
+
});
|
|
960
|
+
class CustomField extends BaseField {
|
|
961
|
+
constructor(options, Component) {
|
|
962
|
+
super({ ...options, type: "custom" });
|
|
963
|
+
__publicField(this, "Component");
|
|
964
|
+
// identifier of the field whose value is the label of the field to re-render
|
|
965
|
+
__publicField(this, "options");
|
|
966
|
+
this.options = options;
|
|
967
|
+
this.Component = Component;
|
|
968
|
+
}
|
|
969
|
+
serialize() {
|
|
970
|
+
throw new Error("Serializing only supported for public input types.");
|
|
971
|
+
}
|
|
972
|
+
getInput(props) {
|
|
973
|
+
const CustomInput = this.Component;
|
|
974
|
+
return /* @__PURE__ */ jsxRuntime.jsx(CustomInput, { field: this, ...props });
|
|
975
|
+
}
|
|
976
|
+
}
|
|
977
|
+
__publicField(CustomField, "fieldTypeName", "Custom");
|
|
978
|
+
__publicField(CustomField, "fieldTypeDescription", "Allows re-rendering of field already in the form");
|
|
979
|
+
class FieldInputClonerField extends CustomField {
|
|
980
|
+
constructor(options) {
|
|
981
|
+
super(options, FieldInputCloner);
|
|
982
|
+
}
|
|
983
|
+
}
|
|
984
|
+
const FieldSectionLayout = React.memo(function FieldSectionLayout2(props) {
|
|
985
|
+
const { field: section, ...rest } = props;
|
|
986
|
+
const { label, description: description2, fields, condition } = section;
|
|
987
|
+
const { values, setFieldValue } = formik.useFormikContext();
|
|
988
|
+
const conditionValue = (condition == null ? void 0 : condition.identifier) ? get(values, condition.identifier) : void 0;
|
|
989
|
+
const conditionMet = React.useMemo(() => isConditionMet(condition, conditionValue), [condition, conditionValue]);
|
|
990
|
+
React.useEffect(() => {
|
|
991
|
+
if (!conditionMet) {
|
|
992
|
+
for (const childField of fields) {
|
|
993
|
+
setFieldValue(childField.getId(), "").then();
|
|
994
|
+
}
|
|
995
|
+
}
|
|
996
|
+
}, [conditionMet, fields, setFieldValue]);
|
|
997
|
+
const inputs = useFieldInputs(fields, rest);
|
|
998
|
+
if (!conditionMet) {
|
|
999
|
+
return null;
|
|
1000
|
+
}
|
|
1001
|
+
if (!label) {
|
|
1002
|
+
return inputs;
|
|
1003
|
+
}
|
|
1004
|
+
return /* @__PURE__ */ jsxRuntime.jsx(blocks.Card, { children: /* @__PURE__ */ jsxRuntime.jsxs(blocks.Flex, { direction: "column", gap: "3", children: [
|
|
1005
|
+
/* @__PURE__ */ jsxRuntime.jsxs(blocks.Flex, { direction: "column", children: [
|
|
1006
|
+
/* @__PURE__ */ jsxRuntime.jsx(blocks.Heading, { as: "h3", size: "3", children: label }),
|
|
1007
|
+
/* @__PURE__ */ jsxRuntime.jsx(blocks.Text, { className: styles$3.description, children: description2 })
|
|
1008
|
+
] }),
|
|
1009
|
+
inputs
|
|
1010
|
+
] }) });
|
|
1011
|
+
});
|
|
1012
|
+
const _FieldSection = class _FieldSection extends BaseFormElement {
|
|
1013
|
+
constructor(options) {
|
|
1014
|
+
const { label = null, fields, condition = null, conditional, ...base } = options;
|
|
1015
|
+
super({ ...base, type: "section" });
|
|
1016
|
+
__publicField(this, "label");
|
|
1017
|
+
__publicField(this, "fields");
|
|
1018
|
+
__publicField(this, "condition");
|
|
1019
|
+
this.fields = fields;
|
|
1020
|
+
this.condition = condition;
|
|
1021
|
+
this.label = label;
|
|
1022
|
+
if (conditional === false) {
|
|
1023
|
+
this.condition = null;
|
|
1024
|
+
}
|
|
1025
|
+
}
|
|
1026
|
+
static getFieldCreationSchema(options) {
|
|
1027
|
+
if (options.length === 0)
|
|
1028
|
+
return [];
|
|
1029
|
+
return [
|
|
1030
|
+
new BooleanField({
|
|
1031
|
+
label: "Conditional",
|
|
1032
|
+
description: "Conditionally show or hide this section.",
|
|
1033
|
+
identifier: "conditional",
|
|
1034
|
+
required: false
|
|
1035
|
+
}),
|
|
1036
|
+
// Declare a section that will hold options for the condition (if any).
|
|
1037
|
+
new _FieldSection({
|
|
1038
|
+
label: "Conditional settings",
|
|
1039
|
+
identifier: "conditional-settings",
|
|
1040
|
+
// This section will only be rendered if the above "Conditional" field is checked.
|
|
1041
|
+
condition: {
|
|
1042
|
+
identifier: "conditional",
|
|
1043
|
+
value: true
|
|
1044
|
+
},
|
|
1045
|
+
// These are the options of the condition.
|
|
1046
|
+
fields: [
|
|
1047
|
+
// Declare a select field that will be used to select the field against which we will check the
|
|
1048
|
+
// condition. This must be selected before the next field is rendered.
|
|
1049
|
+
new SelectField({
|
|
1050
|
+
label: "Field",
|
|
1051
|
+
description: "The field to use for the condition.",
|
|
1052
|
+
// The options (for the field against which we will check the condition) are all the labels of
|
|
1053
|
+
// the fields in the previous section(s) (or fields declared before with no section) that
|
|
1054
|
+
// support conditions. We pass in both the label and the identifier of each supported field. The
|
|
1055
|
+
// identifier becomes the value of the option.
|
|
1056
|
+
options: options.map((option) => {
|
|
1057
|
+
if (!option.label)
|
|
1058
|
+
return null;
|
|
1059
|
+
if (option.type === "upload")
|
|
1060
|
+
return null;
|
|
1061
|
+
return {
|
|
1062
|
+
label: option.label,
|
|
1063
|
+
value: option.identifier
|
|
1064
|
+
};
|
|
1065
|
+
}).filter((option) => !!option),
|
|
1066
|
+
identifier: "condition.identifier",
|
|
1067
|
+
required: true
|
|
1068
|
+
}),
|
|
1069
|
+
// Declare a custom field that will be used to input a value for the condition. The value of the
|
|
1070
|
+
// conditional field selected in the previous step must be equal to the value the user inputs into
|
|
1071
|
+
// this field for the section to be rendered.
|
|
1072
|
+
new FieldInputClonerField({
|
|
1073
|
+
label: "Value",
|
|
1074
|
+
identifier: "condition.value",
|
|
1075
|
+
required: true,
|
|
1076
|
+
clonedFieldIdentifier: "condition.identifier",
|
|
1077
|
+
getFieldToClone(identifier) {
|
|
1078
|
+
if (!identifier) {
|
|
1079
|
+
return null;
|
|
1080
|
+
}
|
|
1081
|
+
const option = options.find((option2) => option2.identifier === identifier);
|
|
1082
|
+
if (!option) {
|
|
1083
|
+
console.error("Could not find field with identifier", identifier);
|
|
1084
|
+
return null;
|
|
1085
|
+
}
|
|
1086
|
+
return {
|
|
1087
|
+
...option,
|
|
1088
|
+
// Override some options to make it make sense in the context and to make it work with the framework.
|
|
1089
|
+
label: "Value",
|
|
1090
|
+
identifier: "condition.value",
|
|
1091
|
+
description: "The value to compare against.",
|
|
1092
|
+
required: option.type !== "boolean"
|
|
1093
|
+
};
|
|
1094
|
+
}
|
|
1095
|
+
})
|
|
1096
|
+
]
|
|
1097
|
+
})
|
|
1098
|
+
];
|
|
1099
|
+
}
|
|
1100
|
+
static deserialize(data) {
|
|
1101
|
+
var _a;
|
|
1102
|
+
if (data.type !== "section")
|
|
1103
|
+
throw new Error("Invalid type");
|
|
1104
|
+
const fields = ((_a = data.fields) == null ? void 0 : _a.map(deserializeField)) ?? [];
|
|
1105
|
+
return new _FieldSection({ ...data, fields });
|
|
1106
|
+
}
|
|
1107
|
+
conditional() {
|
|
1108
|
+
return this.condition !== null;
|
|
1109
|
+
}
|
|
1110
|
+
serialize() {
|
|
1111
|
+
return {
|
|
1112
|
+
...super._serialize(),
|
|
1113
|
+
label: this.label,
|
|
1114
|
+
condition: this.condition,
|
|
1115
|
+
conditional: this.conditional(),
|
|
1116
|
+
fields: this.fields.map((field) => field.serialize())
|
|
1117
|
+
};
|
|
1118
|
+
}
|
|
1119
|
+
getErrors(allValues) {
|
|
1120
|
+
const errors = {};
|
|
1121
|
+
for (const field of this.fields) {
|
|
1122
|
+
const id = field.getId();
|
|
1123
|
+
const error = field.getError(get(allValues, id), allValues);
|
|
1124
|
+
if (error) {
|
|
1125
|
+
set(errors, field.getId(), error);
|
|
1126
|
+
}
|
|
1127
|
+
}
|
|
1128
|
+
return errors;
|
|
1129
|
+
}
|
|
1130
|
+
getInput(props) {
|
|
1131
|
+
return /* @__PURE__ */ jsxRuntime.jsx(FieldSectionLayout, { field: this, ...props });
|
|
1132
|
+
}
|
|
1133
|
+
};
|
|
1134
|
+
__publicField(_FieldSection, "fieldTypeName", "Section");
|
|
1135
|
+
__publicField(_FieldSection, "fieldTypeDescription", "Sections can be useful for grouping fields together. They can also be conditionally shown or hidden.");
|
|
1136
|
+
let FieldSection = _FieldSection;
|
|
1137
|
+
const previewImage = "_previewImage_1ig84_1";
|
|
1138
|
+
const styles$2 = {
|
|
1139
|
+
previewImage
|
|
1140
|
+
};
|
|
1141
|
+
const convertBytesToLargestUnit = (bytes) => {
|
|
1142
|
+
const units = ["byte", "kilobyte", "megabyte"];
|
|
1143
|
+
let sizeInUnit = bytes;
|
|
1144
|
+
let unitIndex = 0;
|
|
1145
|
+
while (sizeInUnit > 1024 && unitIndex < units.length - 1) {
|
|
1146
|
+
sizeInUnit /= 1024;
|
|
1147
|
+
unitIndex++;
|
|
1148
|
+
}
|
|
1149
|
+
const formatter = new Intl.NumberFormat([], { maximumFractionDigits: 2, style: "unit", unit: units[unitIndex] });
|
|
1150
|
+
return formatter.format(sizeInUnit);
|
|
1151
|
+
};
|
|
1152
|
+
const NumberInput = React.memo(function NumberInput2(props) {
|
|
1153
|
+
var _a;
|
|
1154
|
+
const [{ inputId, labelId, severity, helpText, label, fieldProps, field }, rest] = useFormikInput(props);
|
|
1155
|
+
const { onChange } = fieldProps;
|
|
1156
|
+
const color = blocks.useSeverityColor(severity);
|
|
1157
|
+
const input = React.useRef(null);
|
|
1158
|
+
const { value } = fieldProps;
|
|
1159
|
+
const updatedHelpText = React.useMemo(() => {
|
|
1160
|
+
if (helpText)
|
|
1161
|
+
return helpText;
|
|
1162
|
+
if (field.maxFileSize) {
|
|
1163
|
+
const size = convertBytesToLargestUnit(field.maxFileSize);
|
|
1164
|
+
return `Maximum file size: ${size}`;
|
|
1165
|
+
}
|
|
1166
|
+
return null;
|
|
1167
|
+
}, [field.maxFileSize, helpText]);
|
|
1168
|
+
const handleClick = React.useCallback(() => {
|
|
1169
|
+
var _a2;
|
|
1170
|
+
(_a2 = input.current) == null ? void 0 : _a2.click();
|
|
1171
|
+
}, []);
|
|
1172
|
+
const handleRemove = React.useCallback(
|
|
1173
|
+
(index) => {
|
|
1174
|
+
const files = [...value];
|
|
1175
|
+
files.splice(index, 1);
|
|
1176
|
+
const event = { target: { files } };
|
|
1177
|
+
onChange(event);
|
|
1178
|
+
},
|
|
1179
|
+
[value, onChange]
|
|
1180
|
+
);
|
|
1181
|
+
const multipleButtonText = value ? "Select new files" : "Select files";
|
|
1182
|
+
const singleButtonText = value ? "Select new file" : "Select a file";
|
|
1183
|
+
const buttonText = field.maxFiles > 1 ? multipleButtonText : singleButtonText;
|
|
1184
|
+
return /* @__PURE__ */ jsxRuntime.jsxs(blocks.Flex, { direction: "column", gap: "2", children: [
|
|
1185
|
+
/* @__PURE__ */ jsxRuntime.jsx(InputWithLabelAndHelpText, { helpText: updatedHelpText, severity, children: /* @__PURE__ */ jsxRuntime.jsxs(InputWithLabel, { severity, inputId, labelId, label, children: [
|
|
1186
|
+
/* @__PURE__ */ jsxRuntime.jsx(blocks.Flex, { direction: "row", gap: "2", children: /* @__PURE__ */ jsxRuntime.jsx(blocks.Box, { width: "max-content", asChild: true, children: /* @__PURE__ */ jsxRuntime.jsxs(blocks.Button, { ...rest, onClick: handleClick, children: [
|
|
1187
|
+
/* @__PURE__ */ jsxRuntime.jsx(blocks.UploadIcon, {}),
|
|
1188
|
+
" ",
|
|
1189
|
+
buttonText
|
|
1190
|
+
] }) }) }),
|
|
1191
|
+
/* @__PURE__ */ jsxRuntime.jsx(
|
|
1192
|
+
"input",
|
|
1193
|
+
{
|
|
1194
|
+
...rest,
|
|
1195
|
+
type: "file",
|
|
1196
|
+
ref: input,
|
|
1197
|
+
id: inputId,
|
|
1198
|
+
accept: (_a = field.extensions) == null ? void 0 : _a.join(","),
|
|
1199
|
+
multiple: field.maxFiles > 1,
|
|
1200
|
+
color,
|
|
1201
|
+
style: { display: "none" },
|
|
1202
|
+
...fieldProps,
|
|
1203
|
+
value: ""
|
|
1204
|
+
}
|
|
1205
|
+
)
|
|
1206
|
+
] }) }),
|
|
1207
|
+
Array.isArray(value) && value.length > 0 && /* @__PURE__ */ jsxRuntime.jsx(blocks.Flex, { direction: "column", gap: "2", children: value.map((file, index) => /* @__PURE__ */ jsxRuntime.jsx(
|
|
1208
|
+
DisplayFile,
|
|
1209
|
+
{
|
|
1210
|
+
field,
|
|
1211
|
+
file,
|
|
1212
|
+
onRemove: () => handleRemove(index),
|
|
1213
|
+
disabled: rest.disabled
|
|
1214
|
+
},
|
|
1215
|
+
index
|
|
1216
|
+
)) })
|
|
1217
|
+
] });
|
|
1218
|
+
});
|
|
1219
|
+
const DisplayFile = React.memo(function DisplayFile2({ file, field, onRemove, disabled }) {
|
|
1220
|
+
const [resolvedFile, setResolvedFile] = React.useState(null);
|
|
1221
|
+
const error = React.useMemo(() => resolvedFile && field.getError([resolvedFile]), [field, resolvedFile]);
|
|
1222
|
+
const { url, name, size } = React.useMemo(() => {
|
|
1223
|
+
let url2 = null;
|
|
1224
|
+
let name2;
|
|
1225
|
+
let size2;
|
|
1226
|
+
if (resolvedFile == null ? void 0 : resolvedFile.type.startsWith("image/")) {
|
|
1227
|
+
url2 = URL.createObjectURL(resolvedFile);
|
|
1228
|
+
}
|
|
1229
|
+
if (resolvedFile) {
|
|
1230
|
+
name2 = resolvedFile.name;
|
|
1231
|
+
size2 = convertBytesToLargestUnit(resolvedFile.size);
|
|
1232
|
+
} else {
|
|
1233
|
+
name2 = "Downloading...";
|
|
1234
|
+
size2 = "...";
|
|
1235
|
+
}
|
|
1236
|
+
return { url: url2, name: name2, size: size2 };
|
|
1237
|
+
}, [resolvedFile]);
|
|
1238
|
+
React.useEffect(() => {
|
|
1239
|
+
if (file instanceof Promise) {
|
|
1240
|
+
file.then(setResolvedFile);
|
|
1241
|
+
} else {
|
|
1242
|
+
setResolvedFile(file);
|
|
1243
|
+
}
|
|
1244
|
+
}, [file]);
|
|
1245
|
+
return /* @__PURE__ */ jsxRuntime.jsx(blocks.Card, { children: /* @__PURE__ */ jsxRuntime.jsxs(blocks.Flex, { direction: { initial: "column", sm: "row" }, gap: "3", justify: "between", children: [
|
|
1246
|
+
/* @__PURE__ */ jsxRuntime.jsxs(blocks.Flex, { direction: "row", gap: "3", align: "center", grow: "1", shrink: "0", children: [
|
|
1247
|
+
/* @__PURE__ */ jsxRuntime.jsx(
|
|
1248
|
+
blocks.IconButton,
|
|
1249
|
+
{
|
|
1250
|
+
severity: "info",
|
|
1251
|
+
variant: "outline",
|
|
1252
|
+
"aria-label": `Remove ${name}`,
|
|
1253
|
+
disabled,
|
|
1254
|
+
onClick: onRemove,
|
|
1255
|
+
children: /* @__PURE__ */ jsxRuntime.jsx(blocks.Cross1Icon, {})
|
|
1256
|
+
}
|
|
1257
|
+
),
|
|
1258
|
+
/* @__PURE__ */ jsxRuntime.jsxs(blocks.Flex, { direction: "column", gap: "1", children: [
|
|
1259
|
+
/* @__PURE__ */ jsxRuntime.jsx(blocks.Text, { children: name }),
|
|
1260
|
+
/* @__PURE__ */ jsxRuntime.jsx(blocks.Text, { size: "1", children: size }),
|
|
1261
|
+
error && /* @__PURE__ */ jsxRuntime.jsx(blocks.Text, { size: "1", severity: "danger", children: error })
|
|
1262
|
+
] })
|
|
1263
|
+
] }),
|
|
1264
|
+
url && /* @__PURE__ */ jsxRuntime.jsx("img", { className: styles$2.previewImage, src: url, alt: name })
|
|
1265
|
+
] }) });
|
|
1266
|
+
});
|
|
1267
|
+
const largestSupportedSize = 50 * 1024 * 1024;
|
|
1268
|
+
const _UploadField = class _UploadField extends BaseField {
|
|
1269
|
+
constructor(options) {
|
|
1270
|
+
const { extensions, maximum_files, maximum_size, ...base } = options;
|
|
1271
|
+
super({ ...base, type: "upload" });
|
|
1272
|
+
__publicField(this, "extensions");
|
|
1273
|
+
__publicField(this, "maxFileSize");
|
|
1274
|
+
__publicField(this, "maxFiles");
|
|
1275
|
+
__publicField(this, "onlyValidateAfterTouched", false);
|
|
1276
|
+
this.maxFileSize = typeof maximum_size === "number" ? maximum_size : void 0;
|
|
1277
|
+
this.maxFiles = Math.max(typeof maximum_files === "number" ? maximum_files : 1, 1);
|
|
1278
|
+
this.extensions = extensions;
|
|
1279
|
+
}
|
|
1280
|
+
getValueFromChangeEvent(event) {
|
|
1281
|
+
return Array.from(event.target.files || []);
|
|
1282
|
+
}
|
|
1283
|
+
isBlank(value) {
|
|
1284
|
+
return super.isBlank(value) || value.length === 0;
|
|
1285
|
+
}
|
|
1286
|
+
static getFieldCreationSchema() {
|
|
1287
|
+
return [
|
|
1288
|
+
new NumberField({
|
|
1289
|
+
label: "How many files can be uploaded?",
|
|
1290
|
+
description: "By default, only one file can be uploaded.",
|
|
1291
|
+
required: false,
|
|
1292
|
+
minimum: 1,
|
|
1293
|
+
maximum: 10,
|
|
1294
|
+
identifier: "maximum_files"
|
|
1295
|
+
}),
|
|
1296
|
+
new NumberField({
|
|
1297
|
+
label: "What is the maximum size of each file?",
|
|
1298
|
+
description: "Maximum file size in bytes.",
|
|
1299
|
+
required: false,
|
|
1300
|
+
identifier: "maximum_size",
|
|
1301
|
+
minimum: 1,
|
|
1302
|
+
maximum: largestSupportedSize,
|
|
1303
|
+
integers: true
|
|
1304
|
+
}),
|
|
1305
|
+
new MultiSelectField({
|
|
1306
|
+
label: "Accepted file types",
|
|
1307
|
+
description: "Types of allowed files to upload. If left blank, all files will be accepted.",
|
|
1308
|
+
required: false,
|
|
1309
|
+
identifier: "extensions",
|
|
1310
|
+
options: [
|
|
1311
|
+
{
|
|
1312
|
+
value: "image/*",
|
|
1313
|
+
label: "Images"
|
|
1314
|
+
},
|
|
1315
|
+
{
|
|
1316
|
+
value: "audio/*",
|
|
1317
|
+
label: "Audio files"
|
|
1318
|
+
},
|
|
1319
|
+
{
|
|
1320
|
+
value: "video/*",
|
|
1321
|
+
label: "Videos"
|
|
1322
|
+
},
|
|
1323
|
+
{
|
|
1324
|
+
value: "text/*",
|
|
1325
|
+
label: "Text files"
|
|
1326
|
+
},
|
|
1327
|
+
{
|
|
1328
|
+
value: "application/*",
|
|
1329
|
+
label: "Application files (includes PDFs and Word documents)"
|
|
1330
|
+
}
|
|
1331
|
+
]
|
|
1332
|
+
})
|
|
1333
|
+
];
|
|
1334
|
+
}
|
|
1335
|
+
getFieldValidators() {
|
|
1336
|
+
const validators = super.getFieldValidators();
|
|
1337
|
+
const maxFileSize = this.maxFileSize ?? largestSupportedSize;
|
|
1338
|
+
const maxFiles = this.maxFiles ?? 1;
|
|
1339
|
+
validators.push((value) => {
|
|
1340
|
+
if (value && value.some((file) => file.size > maxFileSize)) {
|
|
1341
|
+
return `Files must be at most ${convertBytesToLargestUnit(maxFileSize)}.`;
|
|
1342
|
+
}
|
|
1343
|
+
});
|
|
1344
|
+
validators.push((value) => {
|
|
1345
|
+
if (value && value.length > maxFiles) {
|
|
1346
|
+
return `You can only upload ${maxFiles} files.`;
|
|
1347
|
+
}
|
|
1348
|
+
});
|
|
1349
|
+
return validators;
|
|
1350
|
+
}
|
|
1351
|
+
serialize() {
|
|
1352
|
+
return {
|
|
1353
|
+
...super._serialize(),
|
|
1354
|
+
extensions: this.extensions,
|
|
1355
|
+
maximum_size: this.maxFileSize,
|
|
1356
|
+
maximum_files: this.maxFiles
|
|
1357
|
+
};
|
|
1358
|
+
}
|
|
1359
|
+
static deserialize(data) {
|
|
1360
|
+
if (data.type !== "upload")
|
|
1361
|
+
throw new Error("Type mismatch.");
|
|
1362
|
+
return new _UploadField(data);
|
|
1363
|
+
}
|
|
1364
|
+
getInput(props) {
|
|
1365
|
+
return /* @__PURE__ */ jsxRuntime.jsx(NumberInput, { field: this, ...props });
|
|
1366
|
+
}
|
|
1367
|
+
};
|
|
1368
|
+
__publicField(_UploadField, "fieldTypeName", "Upload");
|
|
1369
|
+
__publicField(_UploadField, "fieldTypeDescription", "Allows a file to be uploaded.");
|
|
1370
|
+
__publicField(_UploadField, "Icon", blocks.UploadIcon);
|
|
1371
|
+
let UploadField = _UploadField;
|
|
1372
|
+
const FieldTypeToClsMapping = {
|
|
1373
|
+
date: DateField,
|
|
1374
|
+
number: NumberField,
|
|
1375
|
+
boolean: BooleanField,
|
|
1376
|
+
select: SelectField,
|
|
1377
|
+
string: StringField,
|
|
1378
|
+
text: TextField,
|
|
1379
|
+
custom: CustomField,
|
|
1380
|
+
upload: UploadField,
|
|
1381
|
+
// TODO: Underscore
|
|
1382
|
+
"multi-string": MultiStringField,
|
|
1383
|
+
"multi-select": MultiSelectField
|
|
1384
|
+
};
|
|
1385
|
+
const deserializeField = (serializedField) => {
|
|
1386
|
+
const fieldType = serializedField.type;
|
|
1387
|
+
const fieldCls = FieldTypeToClsMapping[fieldType];
|
|
1388
|
+
return fieldCls.deserialize(serializedField);
|
|
1389
|
+
};
|
|
1390
|
+
const deserialize = (serialized) => {
|
|
1391
|
+
if (serialized.type === "section") {
|
|
1392
|
+
return FieldSection.deserialize(serialized);
|
|
1393
|
+
}
|
|
1394
|
+
return deserializeField(serialized);
|
|
1395
|
+
};
|
|
1396
|
+
function formRevisionToSchema(formRevision, meta = {}) {
|
|
1397
|
+
const { readonly = false } = meta;
|
|
1398
|
+
return {
|
|
1399
|
+
title: formRevision.title,
|
|
1400
|
+
description: formRevision.description,
|
|
1401
|
+
fields: formRevision.fields.map((serializedField) => deserialize(serializedField)),
|
|
1402
|
+
meta: { readonly }
|
|
1403
|
+
};
|
|
1404
|
+
}
|
|
1405
|
+
function valueIsFile(v) {
|
|
1406
|
+
if (Array.isArray(v) && v.some((v2) => v2 instanceof File || v2 instanceof Promise))
|
|
1407
|
+
return true;
|
|
1408
|
+
return false;
|
|
1409
|
+
}
|
|
1410
|
+
function isConditionMet(condition, value) {
|
|
1411
|
+
if (!condition)
|
|
1412
|
+
return true;
|
|
1413
|
+
if (valueIsFile(value) || valueIsFile(condition.value))
|
|
1414
|
+
throw new Error("Conditions do not support file uploads");
|
|
1415
|
+
const valueAsPrimitive = Array.isArray(value) ? value.map((v) => typeof v === "string" ? v : v.value) : value;
|
|
1416
|
+
const valueToCompare = Array.isArray(condition.value) ? condition.value.map((v) => typeof v === "string" ? v : v.value) : condition.value;
|
|
1417
|
+
if (Array.isArray(valueToCompare) && Array.isArray(valueAsPrimitive)) {
|
|
1418
|
+
for (const v of valueToCompare) {
|
|
1419
|
+
if (!valueAsPrimitive.includes(v))
|
|
1420
|
+
return false;
|
|
1421
|
+
}
|
|
1422
|
+
return true;
|
|
1423
|
+
}
|
|
1424
|
+
return valueToCompare === value;
|
|
1425
|
+
}
|
|
1426
|
+
const useFieldInput = (field, props) => {
|
|
1427
|
+
return React.useMemo(() => {
|
|
1428
|
+
if (!props || !field)
|
|
1429
|
+
return null;
|
|
1430
|
+
return field.getInput(props);
|
|
1431
|
+
}, [field, props]);
|
|
1432
|
+
};
|
|
1433
|
+
const useFieldInputs = (fields, props) => {
|
|
1434
|
+
const inputs = React.useMemo(() => {
|
|
1435
|
+
return fields.map((field) => {
|
|
1436
|
+
return /* @__PURE__ */ jsxRuntime.jsx("div", { children: field.getInput(props) }, field.getId());
|
|
1437
|
+
});
|
|
1438
|
+
}, [fields, props]);
|
|
1439
|
+
return /* @__PURE__ */ jsxRuntime.jsx(blocks.Flex, { direction: "column", gap: "2", children: inputs });
|
|
1440
|
+
};
|
|
1441
|
+
const hasKeys = (errors) => {
|
|
1442
|
+
return Object.keys(errors).length > 0;
|
|
1443
|
+
};
|
|
1444
|
+
const validateForm = async (schema, form) => {
|
|
1445
|
+
const errors = {};
|
|
1446
|
+
for (const field of schema.fields) {
|
|
1447
|
+
if (field instanceof FieldSection) {
|
|
1448
|
+
if (field.condition) {
|
|
1449
|
+
const { identifier } = field.condition;
|
|
1450
|
+
if (!isConditionMet(field.condition, get(form, identifier))) {
|
|
1451
|
+
continue;
|
|
1452
|
+
}
|
|
1453
|
+
}
|
|
1454
|
+
Object.assign(errors, field.getErrors(form));
|
|
1455
|
+
} else {
|
|
1456
|
+
if (!(field instanceof BaseField)) {
|
|
1457
|
+
throw new Error("Invalid field type");
|
|
1458
|
+
}
|
|
1459
|
+
const id = field.getId();
|
|
1460
|
+
const error = field.getError(get(form, id), form);
|
|
1461
|
+
if (error)
|
|
1462
|
+
set(errors, id, error);
|
|
1463
|
+
}
|
|
1464
|
+
}
|
|
1465
|
+
if (hasKeys(errors))
|
|
1466
|
+
return errors;
|
|
1467
|
+
};
|
|
1468
|
+
const uncontrolledValues = [null, void 0];
|
|
1469
|
+
const initialFormValues = (fields, values) => {
|
|
1470
|
+
return fields.reduce((acc, field) => {
|
|
1471
|
+
if (field instanceof FieldSection) {
|
|
1472
|
+
return { ...acc, ...initialFormValues(field.fields, values) };
|
|
1473
|
+
}
|
|
1474
|
+
if (uncontrolledValues.includes(get(acc, field.getId()))) {
|
|
1475
|
+
set(acc, field.getId(), "");
|
|
1476
|
+
}
|
|
1477
|
+
return acc;
|
|
1478
|
+
}, values);
|
|
1479
|
+
};
|
|
1480
|
+
const defaultHandleSubmit = () => {
|
|
1481
|
+
throw new Error("onSubmit must be provided if form is not readonly.");
|
|
1482
|
+
};
|
|
1483
|
+
const FormRenderer = React.memo(
|
|
1484
|
+
React.forwardRef((props, ref) => {
|
|
1485
|
+
const {
|
|
1486
|
+
schema,
|
|
1487
|
+
values = {},
|
|
1488
|
+
onSubmit = defaultHandleSubmit,
|
|
1489
|
+
submitText = "Submit",
|
|
1490
|
+
cancelText,
|
|
1491
|
+
onCancel,
|
|
1492
|
+
onDirty,
|
|
1493
|
+
// if the title isn't provided, hide it by default
|
|
1494
|
+
hideTitle = !schema.title,
|
|
1495
|
+
hideDescription,
|
|
1496
|
+
className
|
|
1497
|
+
} = props;
|
|
1498
|
+
const { readonly } = schema.meta;
|
|
1499
|
+
const formId2 = React.useMemo(() => crypto.randomUUID(), []);
|
|
1500
|
+
const formik$1 = formik.useFormik({
|
|
1501
|
+
initialValues: initialFormValues(schema.fields, values),
|
|
1502
|
+
onSubmit,
|
|
1503
|
+
validate: (form) => validateForm(schema, form),
|
|
1504
|
+
// only validate the entire form on submit
|
|
1505
|
+
validateOnBlur: false,
|
|
1506
|
+
validateOnChange: false
|
|
1507
|
+
});
|
|
1508
|
+
const { dirty } = formik$1;
|
|
1509
|
+
const Title = React.useMemo(
|
|
1510
|
+
() => typeof schema.title === "string" ? /* @__PURE__ */ jsxRuntime.jsx(blocks.Heading, { children: schema.title }) : schema.title,
|
|
1511
|
+
[schema.title]
|
|
1512
|
+
);
|
|
1513
|
+
const Description = React.useMemo(
|
|
1514
|
+
() => typeof schema.description === "string" ? /* @__PURE__ */ jsxRuntime.jsx(blocks.Text, { className: styles$3.description, children: schema.description }) : schema.description,
|
|
1515
|
+
[schema.description]
|
|
1516
|
+
);
|
|
1517
|
+
const inputs = useFieldInputs(schema.fields, { formId: formId2, disabled: readonly });
|
|
1518
|
+
React.useEffect(() => {
|
|
1519
|
+
if (dirty && onDirty)
|
|
1520
|
+
onDirty();
|
|
1521
|
+
}, [dirty, onDirty]);
|
|
1522
|
+
return /* @__PURE__ */ jsxRuntime.jsx(formik.FormikProvider, { value: formik$1, children: /* @__PURE__ */ jsxRuntime.jsx(blocks.Flex, { ref, direction: "column", gap: "2", className, asChild: true, children: /* @__PURE__ */ jsxRuntime.jsxs("form", { id: formId2, onSubmit: formik$1.handleSubmit, children: [
|
|
1523
|
+
!hideTitle && /* @__PURE__ */ jsxRuntime.jsx(blocks.Card, { children: /* @__PURE__ */ jsxRuntime.jsxs(blocks.Flex, { direction: "column", gap: "1", children: [
|
|
1524
|
+
Title,
|
|
1525
|
+
!hideDescription && Description
|
|
1526
|
+
] }) }),
|
|
1527
|
+
inputs,
|
|
1528
|
+
!readonly && /* @__PURE__ */ jsxRuntime.jsxs(blocks.Flex, { justify: "end", gap: "2", children: [
|
|
1529
|
+
cancelText && /* @__PURE__ */ jsxRuntime.jsx(blocks.Button, { type: "button", variant: "soft", onClick: onCancel, children: cancelText }),
|
|
1530
|
+
/* @__PURE__ */ jsxRuntime.jsx(blocks.Button, { type: "submit", disabled: !formik$1.isValid, children: submitText })
|
|
1531
|
+
] })
|
|
1532
|
+
] }) }) });
|
|
1533
|
+
})
|
|
1534
|
+
);
|
|
1535
|
+
const FormSubmissionViewer = React.memo(
|
|
1536
|
+
React.forwardRef((props, ref) => {
|
|
1537
|
+
const { submission, showFormDescription = false, showFormTitle = true } = props;
|
|
1538
|
+
const revision = core.useAppSelector(core.selectFormRevision(submission.form_revision));
|
|
1539
|
+
const { sdk } = core.useSDK();
|
|
1540
|
+
if (!revision) {
|
|
1541
|
+
throw new Error(
|
|
1542
|
+
`Could not find revision ${submission.form_revision} for submission ${submission.offline_id}.`
|
|
1543
|
+
);
|
|
1544
|
+
}
|
|
1545
|
+
const schema = React.useMemo(() => {
|
|
1546
|
+
return formRevisionToSchema(revision, { readonly: true });
|
|
1547
|
+
}, [revision]);
|
|
1548
|
+
const submissionValuesWithAttachments = React.useMemo(() => {
|
|
1549
|
+
const attachments = core.selectSubmissionAttachments(submission.offline_id)(sdk.store.getState()) ?? [];
|
|
1550
|
+
const downloadedAttachments = {};
|
|
1551
|
+
for (const attachment of attachments) {
|
|
1552
|
+
const promise = sdk.files.fetchFileFromUrl(attachment.file, attachment.file_sha1, attachment.file_name).then((response) => {
|
|
1553
|
+
if (!response.success)
|
|
1554
|
+
throw new Error(`Failed to download attachment ${attachment.file_name}.`);
|
|
1555
|
+
return response.body;
|
|
1556
|
+
});
|
|
1557
|
+
const fieldAttachments = downloadedAttachments[attachment.field_identifier];
|
|
1558
|
+
if (fieldAttachments) {
|
|
1559
|
+
fieldAttachments.push(promise);
|
|
1560
|
+
} else {
|
|
1561
|
+
downloadedAttachments[attachment.field_identifier] = [promise];
|
|
1562
|
+
}
|
|
1563
|
+
}
|
|
1564
|
+
return { ...submission.values, ...downloadedAttachments };
|
|
1565
|
+
}, [sdk.files, sdk.store, submission.offline_id, submission.values]);
|
|
1566
|
+
return /* @__PURE__ */ jsxRuntime.jsx(
|
|
1567
|
+
FormRenderer,
|
|
1568
|
+
{
|
|
1569
|
+
ref,
|
|
1570
|
+
schema,
|
|
1571
|
+
values: submissionValuesWithAttachments,
|
|
1572
|
+
hideDescription: !showFormDescription,
|
|
1573
|
+
hideTitle: !showFormTitle
|
|
1574
|
+
}
|
|
1575
|
+
);
|
|
1576
|
+
})
|
|
1577
|
+
);
|
|
1578
|
+
const favoriteIcon = "_favoriteIcon_1bixi_1";
|
|
1579
|
+
const regularIcon = "_regularIcon_1bixi_9";
|
|
1580
|
+
const styles$1 = {
|
|
1581
|
+
favoriteIcon,
|
|
1582
|
+
regularIcon
|
|
1583
|
+
};
|
|
1584
|
+
const orgOptionPrefix = "organization:";
|
|
1585
|
+
const userOptionPrefix = "user:";
|
|
1586
|
+
const FormBrowser = React.memo(
|
|
1587
|
+
React.forwardRef((props, ref) => {
|
|
1588
|
+
const { maxResults = 20, ...entryProps } = props;
|
|
1589
|
+
const [filter, setFilter] = React.useState("");
|
|
1590
|
+
const [ownerFilter, setOwnerFilter] = React.useState("");
|
|
1591
|
+
const { sdk } = core.useSDK();
|
|
1592
|
+
const ownerFilterOptions = React.useMemo(() => {
|
|
1593
|
+
const ret = { maxResults, searchTerm: filter };
|
|
1594
|
+
if (ownerFilter) {
|
|
1595
|
+
if (ownerFilter.startsWith(orgOptionPrefix)) {
|
|
1596
|
+
ret.owner_organization = parseInt(ownerFilter.slice(orgOptionPrefix.length));
|
|
1597
|
+
} else if (ownerFilter.startsWith(userOptionPrefix)) {
|
|
1598
|
+
ret.owner_user = parseInt(ownerFilter.slice(userOptionPrefix.length));
|
|
1599
|
+
}
|
|
1600
|
+
}
|
|
1601
|
+
return ret;
|
|
1602
|
+
}, [filter, maxResults, ownerFilter]);
|
|
1603
|
+
const userForms = core.useAppSelector(core.selectFilteredUserForms(ownerFilterOptions)) ?? [];
|
|
1604
|
+
const userFormMapping = core.useAppSelector(core.selectUserFormMapping);
|
|
1605
|
+
const handleToggleFavorite = React.useCallback(
|
|
1606
|
+
(form) => {
|
|
1607
|
+
if (form.favorite) {
|
|
1608
|
+
sdk.userForms.unfavorite(form.offline_id).then();
|
|
1609
|
+
} else {
|
|
1610
|
+
sdk.userForms.favorite(form.offline_id).then();
|
|
1611
|
+
}
|
|
1612
|
+
},
|
|
1613
|
+
[sdk]
|
|
1614
|
+
);
|
|
1615
|
+
const options = React.useMemo(() => {
|
|
1616
|
+
const state = sdk.store.getState();
|
|
1617
|
+
const accumulator = {};
|
|
1618
|
+
for (const form of Object.values(userFormMapping)) {
|
|
1619
|
+
const organization = core.selectOrganization(form.owner_organization || -1)(state);
|
|
1620
|
+
if (organization) {
|
|
1621
|
+
accumulator[`${orgOptionPrefix}${organization.id}`] = organization.name;
|
|
1622
|
+
}
|
|
1623
|
+
const user = core.selectUser(form.owner_user || -1)(state);
|
|
1624
|
+
if (user) {
|
|
1625
|
+
accumulator[`${userOptionPrefix}${user.id}`] = user.username;
|
|
1626
|
+
}
|
|
1627
|
+
}
|
|
1628
|
+
return Object.entries(accumulator).map(([value, label]) => ({ itemContent: label, value }));
|
|
1629
|
+
}, [userFormMapping, sdk.store]);
|
|
1630
|
+
const handleChange = React.useCallback((e) => {
|
|
1631
|
+
setFilter(e.currentTarget.value);
|
|
1632
|
+
}, []);
|
|
1633
|
+
const numberOfForms = core.useAppSelector(core.selectNumberOfUserForms) || 0;
|
|
1634
|
+
const numberOfHiddenForms = numberOfForms - userForms.length;
|
|
1635
|
+
const overflowMessage = userForms.length == maxResults && numberOfHiddenForms > 0 ? `Only the first ${maxResults} results are shown (${numberOfHiddenForms} hidden)` : numberOfHiddenForms > 0 && `${numberOfHiddenForms} hidden forms`;
|
|
1636
|
+
return /* @__PURE__ */ jsxRuntime.jsxs(blocks.Flex, { ref, direction: "column", gap: "2", children: [
|
|
1637
|
+
/* @__PURE__ */ jsxRuntime.jsxs(blocks.Flex, { gap: "2", grow: "1", children: [
|
|
1638
|
+
/* @__PURE__ */ jsxRuntime.jsx(blocks.Box, { grow: "1", asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(blocks.TextField.Root, { size: "3", children: /* @__PURE__ */ jsxRuntime.jsx(blocks.TextField.Input, { placeholder: "Filter", value: filter, onChange: handleChange }) }) }),
|
|
1639
|
+
/* @__PURE__ */ jsxRuntime.jsx(
|
|
1640
|
+
blocks.Select,
|
|
1641
|
+
{
|
|
1642
|
+
items: options,
|
|
1643
|
+
value: ownerFilter,
|
|
1644
|
+
onValueChange: setOwnerFilter,
|
|
1645
|
+
placeholder: "Owner",
|
|
1646
|
+
size: "large"
|
|
1647
|
+
}
|
|
1648
|
+
)
|
|
1649
|
+
] }),
|
|
1650
|
+
userForms.length > 0 && /* @__PURE__ */ jsxRuntime.jsx(blocks.ButtonList.Root, { children: userForms.map((form) => /* @__PURE__ */ jsxRuntime.jsx(
|
|
1651
|
+
FormBrowserEntry,
|
|
1652
|
+
{
|
|
1653
|
+
...entryProps,
|
|
1654
|
+
form,
|
|
1655
|
+
handleToggleFavorite: () => handleToggleFavorite(form)
|
|
1656
|
+
},
|
|
1657
|
+
form.offline_id
|
|
1658
|
+
)) }),
|
|
1659
|
+
/* @__PURE__ */ jsxRuntime.jsx(blocks.Box, { px: "3", children: /* @__PURE__ */ jsxRuntime.jsx(blocks.Text, { size: "2", severity: "info", children: overflowMessage }) })
|
|
1660
|
+
] });
|
|
1661
|
+
})
|
|
1662
|
+
);
|
|
1663
|
+
const FormBrowserEntry = (props) => {
|
|
1664
|
+
var _a;
|
|
1665
|
+
const { form, onSelectForm, isFavoriteEditable, handleToggleFavorite } = props;
|
|
1666
|
+
const ownerOrganization = (_a = core.useAppSelector(core.selectOrganization(form.owner_organization || -1))) == null ? void 0 : _a.name;
|
|
1667
|
+
const ownerUser = core.useAppSelector(core.selectUser(form.owner_user || -1));
|
|
1668
|
+
const currentUserId = core.useAppSelector(core.selectCurrentUser).id;
|
|
1669
|
+
const ownedByCurrentUser = !!ownerUser && ownerUser.id === currentUserId;
|
|
1670
|
+
const owner = ownerOrganization ?? (ownedByCurrentUser ? "You" : ownerUser == null ? void 0 : ownerUser.username) ?? "Unknown";
|
|
1671
|
+
const handleFavoriteClick = React.useCallback(
|
|
1672
|
+
(e) => {
|
|
1673
|
+
e.stopPropagation();
|
|
1674
|
+
handleToggleFavorite();
|
|
1675
|
+
},
|
|
1676
|
+
[handleToggleFavorite]
|
|
1677
|
+
);
|
|
1678
|
+
const ret = /* @__PURE__ */ jsxRuntime.jsx(blocks.ButtonList.Item, { onClick: () => onSelectForm(form), asChild: true, children: /* @__PURE__ */ jsxRuntime.jsxs(blocks.Flex, { justify: "between", gap: "2", py: "2", px: "3", ...blocks.divButtonProps, children: [
|
|
1679
|
+
/* @__PURE__ */ jsxRuntime.jsxs(blocks.Flex, { grow: "1", align: "center", gap: "2", children: [
|
|
1680
|
+
/* @__PURE__ */ jsxRuntime.jsx(
|
|
1681
|
+
blocks.IconButton,
|
|
1682
|
+
{
|
|
1683
|
+
className: core.classNames(form.favorite ? styles$1.favoriteIcon : styles$1.regularIcon),
|
|
1684
|
+
variant: "ghost",
|
|
1685
|
+
onClick: handleFavoriteClick,
|
|
1686
|
+
"aria-label": form.favorite ? "Favorite form" : "Standard form",
|
|
1687
|
+
disabled: !isFavoriteEditable,
|
|
1688
|
+
children: form.favorite ? /* @__PURE__ */ jsxRuntime.jsx(blocks.StarFilledIcon, {}) : /* @__PURE__ */ jsxRuntime.jsx(blocks.StarIcon, {})
|
|
1689
|
+
}
|
|
1690
|
+
),
|
|
1691
|
+
/* @__PURE__ */ jsxRuntime.jsx(blocks.Text, { noWrap: true, children: form.latestRevision.title }),
|
|
1692
|
+
form.latestRevision.description && /* @__PURE__ */ jsxRuntime.jsx(blocks.QuestionMarkCircledIcon, {})
|
|
1693
|
+
] }),
|
|
1694
|
+
owner && /* @__PURE__ */ jsxRuntime.jsxs(blocks.Flex, { align: "center", gap: "2", children: [
|
|
1695
|
+
/* @__PURE__ */ jsxRuntime.jsx(blocks.PersonIcon, {}),
|
|
1696
|
+
" ",
|
|
1697
|
+
owner
|
|
1698
|
+
] })
|
|
1699
|
+
] }) });
|
|
1700
|
+
if (form.latestRevision.description) {
|
|
1701
|
+
return /* @__PURE__ */ jsxRuntime.jsx(blocks.Tooltip, { content: form.latestRevision.description, children: ret }, form.offline_id);
|
|
1702
|
+
}
|
|
1703
|
+
return ret;
|
|
1704
|
+
};
|
|
1705
|
+
const submissionsContainer = "_submissionsContainer_9iirt_1";
|
|
1706
|
+
const stopHorizontalOverflow = "_stopHorizontalOverflow_9iirt_6";
|
|
1707
|
+
const styles = {
|
|
1708
|
+
submissionsContainer,
|
|
1709
|
+
stopHorizontalOverflow
|
|
1710
|
+
};
|
|
1711
|
+
const FormSubmissionBrowserEntry = React.memo(function FormSubmissionBrowserEntry2(props) {
|
|
1712
|
+
var _a;
|
|
1713
|
+
const { submission, onSubmissionClick, compact, labelType, rowDecorator } = props;
|
|
1714
|
+
const currentUser = core.useAppSelector(core.selectCurrentUser);
|
|
1715
|
+
const createdBy = core.useAppSelector(core.selectUser("created_by" in submission ? submission.created_by : currentUser.id));
|
|
1716
|
+
const dateToUse = getCreatedAtOrSubmittedAtDate(submission);
|
|
1717
|
+
const formattedDateTime = core.isToday(dateToUse) ? dateToUse.toLocaleTimeString([], {
|
|
1718
|
+
hour: "2-digit",
|
|
1719
|
+
minute: "2-digit"
|
|
1720
|
+
}) : core.getLocalDateString(dateToUse);
|
|
1721
|
+
const revision = core.useAppSelector(core.selectFormRevision(submission.form_revision));
|
|
1722
|
+
if (!revision) {
|
|
1723
|
+
throw new Error(`Could not find revision ${submission.form_revision} for submission ${submission.offline_id}.`);
|
|
1724
|
+
}
|
|
1725
|
+
const latestRevisionNumber = (_a = core.useAppSelector(core.selectLatestFormRevision(revision.form))) == null ? void 0 : _a.revision;
|
|
1726
|
+
const creatorProfileSrc = core.useFileSrc({
|
|
1727
|
+
file: (createdBy == null ? void 0 : createdBy.profile.file) ?? null,
|
|
1728
|
+
fileSha1: (createdBy == null ? void 0 : createdBy.profile.file_sha1) ?? null
|
|
1729
|
+
});
|
|
1730
|
+
const creatorProfileFallback = (createdBy == null ? void 0 : createdBy.username.charAt(0).toUpperCase()) ?? "?";
|
|
1731
|
+
const isLatestRevision = revision.revision === latestRevisionNumber;
|
|
1732
|
+
const handleClick = React.useCallback(() => {
|
|
1733
|
+
if (onSubmissionClick) {
|
|
1734
|
+
onSubmissionClick({ submission });
|
|
1735
|
+
}
|
|
1736
|
+
}, [submission, onSubmissionClick]);
|
|
1737
|
+
const row = /* @__PURE__ */ jsxRuntime.jsx(blocks.ButtonList.Item, { onClick: handleClick, asChild: true, children: /* @__PURE__ */ jsxRuntime.jsxs(blocks.Flex, { grow: "1", width: "100%", p: "2", gap: "2", justify: "between", children: [
|
|
1738
|
+
/* @__PURE__ */ jsxRuntime.jsxs(blocks.Flex, { gap: "2", align: "center", className: styles.stopHorizontalOverflow, children: [
|
|
1739
|
+
/* @__PURE__ */ jsxRuntime.jsx(blocks.Avatar, { src: creatorProfileSrc, size: "1", fallback: creatorProfileFallback }),
|
|
1740
|
+
/* @__PURE__ */ jsxRuntime.jsx(blocks.Text, { size: "2", noWrap: true, children: labelType === "creator" ? (createdBy || currentUser).username : revision.title })
|
|
1741
|
+
] }),
|
|
1742
|
+
/* @__PURE__ */ jsxRuntime.jsxs(blocks.Flex, { gap: "2", align: "center", children: [
|
|
1743
|
+
!compact && (revision.revision ? /* @__PURE__ */ jsxRuntime.jsx(blocks.Badge, { variant: "soft", severity: isLatestRevision ? "primary" : "info", children: compact ? revision.revision.toString() : `Revision #${revision.revision}` }) : !!latestRevisionNumber && /* @__PURE__ */ jsxRuntime.jsx(blocks.Badge, { children: "Original" })),
|
|
1744
|
+
/* @__PURE__ */ jsxRuntime.jsx(blocks.Text, { size: "2", noWrap: true, children: formattedDateTime })
|
|
1745
|
+
] })
|
|
1746
|
+
] }) });
|
|
1747
|
+
if (rowDecorator) {
|
|
1748
|
+
return rowDecorator(submission, row);
|
|
1749
|
+
}
|
|
1750
|
+
return row;
|
|
1751
|
+
});
|
|
1752
|
+
const getCreatedAtOrSubmittedAtDate = (submission) => {
|
|
1753
|
+
const date = "created_at" in submission ? submission.created_at : submission.submitted_at;
|
|
1754
|
+
return new Date(date);
|
|
1755
|
+
};
|
|
1756
|
+
const FormSubmissionBrowser = React.memo(function FormSubmissionBrowser2(props) {
|
|
1757
|
+
const {
|
|
1758
|
+
formId: formId2,
|
|
1759
|
+
submissions: propSubmissions,
|
|
1760
|
+
compact = false,
|
|
1761
|
+
className,
|
|
1762
|
+
after,
|
|
1763
|
+
variant = "outline",
|
|
1764
|
+
...submissionEntryProps
|
|
1765
|
+
} = props;
|
|
1766
|
+
if (!!formId2 === !!propSubmissions) {
|
|
1767
|
+
throw new Error("Either formId or submissions must be provided, but not both.");
|
|
1768
|
+
}
|
|
1769
|
+
const submissions = core.useAppSelector(
|
|
1770
|
+
propSubmissions ? () => propSubmissions : core.selectSubmissionsForForm(formId2)
|
|
1771
|
+
);
|
|
1772
|
+
const sortedSubmissions = React.useMemo(
|
|
1773
|
+
() => submissions == null ? void 0 : submissions.sort((a, b) => {
|
|
1774
|
+
return getCreatedAtOrSubmittedAtDate(b).getTime() - getCreatedAtOrSubmittedAtDate(a).getTime();
|
|
1775
|
+
}),
|
|
1776
|
+
[submissions]
|
|
1777
|
+
);
|
|
1778
|
+
return /* @__PURE__ */ jsxRuntime.jsx(
|
|
1779
|
+
blocks.ButtonList.Root,
|
|
1780
|
+
{
|
|
1781
|
+
className: core.classNames(styles.submissionsContainer, className),
|
|
1782
|
+
size: "small",
|
|
1783
|
+
variant,
|
|
1784
|
+
before: !compact && /* @__PURE__ */ jsxRuntime.jsxs(blocks.Text, { severity: "info", children: [
|
|
1785
|
+
"There are ",
|
|
1786
|
+
((submissions == null ? void 0 : submissions.length) || 0).toString(),
|
|
1787
|
+
" submissions of this form."
|
|
1788
|
+
] }),
|
|
1789
|
+
after,
|
|
1790
|
+
children: sortedSubmissions == null ? void 0 : sortedSubmissions.map((submission, index) => {
|
|
1791
|
+
return /* @__PURE__ */ jsxRuntime.jsx(
|
|
1792
|
+
FormSubmissionBrowserEntry,
|
|
1793
|
+
{
|
|
1794
|
+
submission,
|
|
1795
|
+
compact,
|
|
1796
|
+
...submissionEntryProps
|
|
1797
|
+
},
|
|
1798
|
+
index
|
|
1799
|
+
);
|
|
1800
|
+
})
|
|
1801
|
+
}
|
|
1802
|
+
);
|
|
1803
|
+
});
|
|
1804
|
+
const PatchField = React.memo(function PatchField2(props) {
|
|
1805
|
+
const { name, render } = props;
|
|
1806
|
+
const { submitForm } = formik.useFormikContext();
|
|
1807
|
+
const [fieldProps, _meta, helpers] = formik.useField(name);
|
|
1808
|
+
return React.useMemo(() => {
|
|
1809
|
+
const setValue = (value) => helpers.setValue(value, false);
|
|
1810
|
+
return render({
|
|
1811
|
+
value: fieldProps.value,
|
|
1812
|
+
setValue,
|
|
1813
|
+
patchValue: submitForm
|
|
1814
|
+
});
|
|
1815
|
+
}, [submitForm, helpers, fieldProps.value, render]);
|
|
1816
|
+
});
|
|
1817
|
+
const PatchFormProvider = React.memo(
|
|
1818
|
+
React.forwardRef((props, ref) => {
|
|
1819
|
+
const { children, schema, values, onPatch, onError, ...rest } = props;
|
|
1820
|
+
const initialValues2 = React.useMemo(() => initialFormValues(schema.fields, values), [schema.fields, values]);
|
|
1821
|
+
const handlePatch = React.useCallback(
|
|
1822
|
+
(values2) => {
|
|
1823
|
+
const diff = {};
|
|
1824
|
+
for (const key in values2) {
|
|
1825
|
+
const value = values2[key];
|
|
1826
|
+
if (value !== initialValues2[key] && value !== void 0) {
|
|
1827
|
+
diff[key] = value;
|
|
1828
|
+
}
|
|
1829
|
+
}
|
|
1830
|
+
if (!hasKeys(diff))
|
|
1831
|
+
return;
|
|
1832
|
+
onPatch(diff);
|
|
1833
|
+
},
|
|
1834
|
+
[initialValues2, onPatch]
|
|
1835
|
+
);
|
|
1836
|
+
const validate = React.useCallback(
|
|
1837
|
+
async (form) => {
|
|
1838
|
+
const error = await validateForm(schema, form);
|
|
1839
|
+
if (error) {
|
|
1840
|
+
onError(error);
|
|
1841
|
+
}
|
|
1842
|
+
return error;
|
|
1843
|
+
},
|
|
1844
|
+
[schema, onError]
|
|
1845
|
+
);
|
|
1846
|
+
const formik$1 = formik.useFormik({
|
|
1847
|
+
initialValues: initialValues2,
|
|
1848
|
+
onSubmit: handlePatch,
|
|
1849
|
+
validate,
|
|
1850
|
+
// only validate the entire form on submit
|
|
1851
|
+
validateOnBlur: false,
|
|
1852
|
+
validateOnChange: false
|
|
1853
|
+
});
|
|
1854
|
+
const { errors, resetForm } = formik$1;
|
|
1855
|
+
React.useEffect(() => {
|
|
1856
|
+
if (hasKeys(errors)) {
|
|
1857
|
+
resetForm({ values: initialValues2, errors: {} });
|
|
1858
|
+
}
|
|
1859
|
+
}, [errors, initialValues2, resetForm]);
|
|
1860
|
+
return /* @__PURE__ */ jsxRuntime.jsx(formik.FormikProvider, { value: formik$1, children: /* @__PURE__ */ jsxRuntime.jsx("form", { ...rest, ref, onSubmit: formik$1.handleSubmit, children }) });
|
|
1861
|
+
})
|
|
1862
|
+
);
|
|
1863
|
+
const CompleteFieldTypeToClsMapping = {
|
|
1864
|
+
...FieldTypeToClsMapping,
|
|
1865
|
+
section: FieldSection
|
|
1866
|
+
};
|
|
1867
|
+
const FieldToChoose = React.memo(function FieldToChoose2(props) {
|
|
1868
|
+
const { field, setFieldType } = props;
|
|
1869
|
+
const typeName = field.fieldTypeName;
|
|
1870
|
+
const description2 = field.fieldTypeDescription;
|
|
1871
|
+
const Icon = field.Icon;
|
|
1872
|
+
return /* @__PURE__ */ jsxRuntime.jsxs(blocks.Flex, { gap: "4", align: "center", children: [
|
|
1873
|
+
/* @__PURE__ */ jsxRuntime.jsx(blocks.Button, { type: "button", variant: "surface", onClick: setFieldType, style: { width: "135px" }, children: /* @__PURE__ */ jsxRuntime.jsxs(blocks.Flex, { gap: "3", align: "center", grow: "1", children: [
|
|
1874
|
+
/* @__PURE__ */ jsxRuntime.jsx(Icon, {}),
|
|
1875
|
+
typeName
|
|
1876
|
+
] }) }),
|
|
1877
|
+
/* @__PURE__ */ jsxRuntime.jsx(blocks.Text, { children: description2 })
|
|
1878
|
+
] });
|
|
1879
|
+
});
|
|
1880
|
+
const fieldsToChoose = [
|
|
1881
|
+
["string", "text"],
|
|
1882
|
+
["select", "multi-select", "upload"],
|
|
1883
|
+
["boolean", "date", "number", "multi-string"]
|
|
1884
|
+
];
|
|
1885
|
+
const indexOfLastFieldGroup = fieldsToChoose.length - 1;
|
|
1886
|
+
const ChooseFieldToAdd = React.memo(function ChooseFieldToAdd2(props) {
|
|
1887
|
+
const { setFieldType } = props;
|
|
1888
|
+
return /* @__PURE__ */ jsxRuntime.jsx(blocks.Flex, { direction: "column", gap: "3", children: fieldsToChoose.map((fieldGroup, index) => /* @__PURE__ */ jsxRuntime.jsxs(blocks.Flex, { direction: "column", gap: "3", children: [
|
|
1889
|
+
/* @__PURE__ */ jsxRuntime.jsx(blocks.Flex, { direction: "column", gap: "2", children: fieldGroup.map((identifier) => /* @__PURE__ */ jsxRuntime.jsx(
|
|
1890
|
+
FieldToChoose,
|
|
1891
|
+
{
|
|
1892
|
+
field: FieldTypeToClsMapping[identifier],
|
|
1893
|
+
setFieldType: () => setFieldType(identifier)
|
|
1894
|
+
},
|
|
1895
|
+
identifier
|
|
1896
|
+
)) }),
|
|
1897
|
+
index < indexOfLastFieldGroup && /* @__PURE__ */ jsxRuntime.jsx(blocks.Separator, { size: "4" })
|
|
1898
|
+
] }, index)) });
|
|
1899
|
+
});
|
|
1900
|
+
const validateNewFieldName = (takenLabels) => {
|
|
1901
|
+
return (value) => {
|
|
1902
|
+
if (!value || typeof value !== "string")
|
|
1903
|
+
return;
|
|
1904
|
+
if (takenLabels.includes(value.trim())) {
|
|
1905
|
+
return "This name is already taken.";
|
|
1906
|
+
}
|
|
1907
|
+
};
|
|
1908
|
+
};
|
|
1909
|
+
const commonFields = (takenLabels, type) => {
|
|
1910
|
+
const base = [
|
|
1911
|
+
new StringField({
|
|
1912
|
+
label: "Label",
|
|
1913
|
+
required: true,
|
|
1914
|
+
maxLength: 200,
|
|
1915
|
+
identifier: "label",
|
|
1916
|
+
fieldValidators: [validateNewFieldName(takenLabels)]
|
|
1917
|
+
}),
|
|
1918
|
+
new TextField({
|
|
1919
|
+
label: "Description",
|
|
1920
|
+
required: false,
|
|
1921
|
+
maxLength: 1e3,
|
|
1922
|
+
identifier: "description"
|
|
1923
|
+
})
|
|
1924
|
+
];
|
|
1925
|
+
if (type === "section")
|
|
1926
|
+
return base;
|
|
1927
|
+
return [
|
|
1928
|
+
...base,
|
|
1929
|
+
new BooleanField({ label: "Required", description: null, required: false, identifier: "required" })
|
|
1930
|
+
];
|
|
1931
|
+
};
|
|
1932
|
+
const FieldOptionsForm = React.memo(function FieldOptionsForm2(props) {
|
|
1933
|
+
const { fieldType, handleCancel, handleCreateField, defaultField, conditionalSourceFields } = props;
|
|
1934
|
+
const fieldCls = CompleteFieldTypeToClsMapping[fieldType];
|
|
1935
|
+
const formik$1 = formik.useFormikContext();
|
|
1936
|
+
const schema = React.useMemo(() => {
|
|
1937
|
+
const takenFieldLabels = getTakenFieldLabels(formik$1.values.fields).filter((id) => id !== (defaultField == null ? void 0 : defaultField.label));
|
|
1938
|
+
let fields = commonFields(takenFieldLabels, fieldType);
|
|
1939
|
+
if (fieldCls === FieldSection) {
|
|
1940
|
+
if (conditionalSourceFields === void 0) {
|
|
1941
|
+
throw new Error("Conditional source fields must be provided when changing sections.");
|
|
1942
|
+
}
|
|
1943
|
+
fields = fields.concat(fieldCls.getFieldCreationSchema(conditionalSourceFields));
|
|
1944
|
+
} else {
|
|
1945
|
+
if (!(fieldCls.prototype instanceof BaseField)) {
|
|
1946
|
+
throw new Error(`Field must be an instance of BaseField. Got ${fieldCls}.`);
|
|
1947
|
+
}
|
|
1948
|
+
fields = [...fields, ...fieldCls.getFieldCreationSchema()];
|
|
1949
|
+
}
|
|
1950
|
+
return {
|
|
1951
|
+
fields,
|
|
1952
|
+
meta: { readonly: false },
|
|
1953
|
+
// using the dialog title as the form title
|
|
1954
|
+
title: null
|
|
1955
|
+
};
|
|
1956
|
+
}, [formik$1.values.fields, fieldType, fieldCls, defaultField == null ? void 0 : defaultField.label, conditionalSourceFields]);
|
|
1957
|
+
return /* @__PURE__ */ jsxRuntime.jsx(
|
|
1958
|
+
FormRenderer,
|
|
1959
|
+
{
|
|
1960
|
+
schema,
|
|
1961
|
+
values: defaultField,
|
|
1962
|
+
onSubmit: handleCreateField,
|
|
1963
|
+
cancelText: defaultField ? void 0 : "Back",
|
|
1964
|
+
onCancel: handleCancel
|
|
1965
|
+
}
|
|
1966
|
+
);
|
|
1967
|
+
});
|
|
1968
|
+
const FieldBuilder = React.memo(function FieldBuilder2(props) {
|
|
1969
|
+
const { parentPath, index, children, initial, editing, conditionalSourceFields } = props;
|
|
1970
|
+
const [fieldType, setFieldType] = React.useState();
|
|
1971
|
+
const type = (initial == null ? void 0 : initial.type) ?? fieldType;
|
|
1972
|
+
const typeName = type ? CompleteFieldTypeToClsMapping[type].fieldTypeName : void 0;
|
|
1973
|
+
const { setFieldValue, values } = formik.useFormikContext();
|
|
1974
|
+
if (editing && !initial)
|
|
1975
|
+
throw new Error("Initial field must be provided if editing is true.");
|
|
1976
|
+
const showChooseField = !type && !editing && !initial;
|
|
1977
|
+
const title2 = showChooseField ? "Choose a field type" : `${typeName} settings`;
|
|
1978
|
+
const description2 = showChooseField ? "Select a field type to add to this section." : (typeName == null ? void 0 : typeName.toLowerCase()) === "section" ? "Customize your section" : `Customize your ${typeName == null ? void 0 : typeName.toLowerCase()} field.`;
|
|
1979
|
+
const handleCancel = React.useCallback(() => setFieldType(void 0), []);
|
|
1980
|
+
const handleCloseInterrupt = React.useCallback((confirmClose) => {
|
|
1981
|
+
setFieldType(void 0);
|
|
1982
|
+
confirmClose();
|
|
1983
|
+
}, []);
|
|
1984
|
+
const handleCreateField = React.useCallback(
|
|
1985
|
+
(form, closeDialog) => {
|
|
1986
|
+
const { label } = form;
|
|
1987
|
+
if (!type)
|
|
1988
|
+
throw new Error("Field type must be selected before creating a field.");
|
|
1989
|
+
if (!label || typeof label !== "string")
|
|
1990
|
+
throw new Error("Label must be provided before creating a field.");
|
|
1991
|
+
const field = deserialize({
|
|
1992
|
+
type,
|
|
1993
|
+
...form,
|
|
1994
|
+
identifier: makeIdentifier(form.identifier, label)
|
|
1995
|
+
}).serialize();
|
|
1996
|
+
const parent = get(values, parentPath);
|
|
1997
|
+
if (parent === void 0) {
|
|
1998
|
+
throw new Error("Parent path must point to an existing field.");
|
|
1999
|
+
}
|
|
2000
|
+
let newFields;
|
|
2001
|
+
if (!Array.isArray(parent))
|
|
2002
|
+
throw new Error("Parent path must point to an array.");
|
|
2003
|
+
if (editing) {
|
|
2004
|
+
newFields = replace(parent, index, field);
|
|
2005
|
+
} else {
|
|
2006
|
+
newFields = insert(parent, index, field);
|
|
2007
|
+
}
|
|
2008
|
+
setFieldValue(parentPath, newFields).then();
|
|
2009
|
+
closeDialog();
|
|
2010
|
+
},
|
|
2011
|
+
[editing, type, values, parentPath, setFieldValue, index]
|
|
2012
|
+
);
|
|
2013
|
+
const dialogContent = React.useCallback(
|
|
2014
|
+
(closeDialog) => {
|
|
2015
|
+
if (showChooseField) {
|
|
2016
|
+
return /* @__PURE__ */ jsxRuntime.jsx(ChooseFieldToAdd, { setFieldType });
|
|
2017
|
+
}
|
|
2018
|
+
return /* @__PURE__ */ jsxRuntime.jsx(
|
|
2019
|
+
FieldOptionsForm,
|
|
2020
|
+
{
|
|
2021
|
+
conditionalSourceFields,
|
|
2022
|
+
handleCancel,
|
|
2023
|
+
handleCreateField: (form) => handleCreateField(form, closeDialog),
|
|
2024
|
+
fieldType: type,
|
|
2025
|
+
defaultField: initial
|
|
2026
|
+
}
|
|
2027
|
+
);
|
|
2028
|
+
},
|
|
2029
|
+
[conditionalSourceFields, handleCancel, handleCreateField, initial, showChooseField, type]
|
|
2030
|
+
);
|
|
2031
|
+
return /* @__PURE__ */ jsxRuntime.jsx(blocks.Dialog, { onCloseInterrupt: handleCloseInterrupt, title: title2, description: description2, content: dialogContent, children });
|
|
2032
|
+
});
|
|
2033
|
+
const DefaultWrapper = ({ children }) => /* @__PURE__ */ jsxRuntime.jsx(jsxRuntime.Fragment, { children });
|
|
2034
|
+
const forMobile = (mobile, display) => ({
|
|
2035
|
+
initial: mobile ? display : "none",
|
|
2036
|
+
sm: mobile ? "none" : display
|
|
2037
|
+
});
|
|
2038
|
+
const FieldActions = React.memo(function FieldActions2(props) {
|
|
2039
|
+
const { remove: remove2, dragHandleProps, editProps, insertAfterProps, duplicateProps } = props;
|
|
2040
|
+
const actions = React.useMemo(
|
|
2041
|
+
() => [
|
|
2042
|
+
{
|
|
2043
|
+
Wrapper: FieldBuilder,
|
|
2044
|
+
wrapperProps: editProps,
|
|
2045
|
+
Icon: blocks.Pencil1Icon,
|
|
2046
|
+
text: "Edit"
|
|
2047
|
+
},
|
|
2048
|
+
{
|
|
2049
|
+
Icon: blocks.TrashIcon,
|
|
2050
|
+
buttonProps: {
|
|
2051
|
+
onClick: remove2
|
|
2052
|
+
},
|
|
2053
|
+
text: "Delete"
|
|
2054
|
+
},
|
|
2055
|
+
{
|
|
2056
|
+
Wrapper: FieldBuilder,
|
|
2057
|
+
wrapperProps: duplicateProps,
|
|
2058
|
+
Icon: blocks.CopyIcon,
|
|
2059
|
+
text: "Duplicate"
|
|
2060
|
+
},
|
|
2061
|
+
{
|
|
2062
|
+
Wrapper: FieldBuilder,
|
|
2063
|
+
wrapperProps: insertAfterProps,
|
|
2064
|
+
Icon: blocks.PlusIcon,
|
|
2065
|
+
text: "Add after"
|
|
2066
|
+
},
|
|
2067
|
+
{
|
|
2068
|
+
// Wrapping icon in a div so that the asChild turns the button into a div
|
|
2069
|
+
// so that the drag handle props are not applied to the icon
|
|
2070
|
+
// Note: b/c the <button> does not handle the space-press event correctly
|
|
2071
|
+
Icon: (props2) => /* @__PURE__ */ jsxRuntime.jsx("div", { ...props2, children: /* @__PURE__ */ jsxRuntime.jsx(blocks.DragHandleDots2Icon, {}) }),
|
|
2072
|
+
text: "Reorder",
|
|
2073
|
+
disableOnMobile: true,
|
|
2074
|
+
buttonProps: { ...dragHandleProps, asChild: true }
|
|
2075
|
+
}
|
|
2076
|
+
],
|
|
2077
|
+
[dragHandleProps, duplicateProps, editProps, insertAfterProps, remove2]
|
|
2078
|
+
);
|
|
2079
|
+
return /* @__PURE__ */ jsxRuntime.jsxs(jsxRuntime.Fragment, { children: [
|
|
2080
|
+
/* @__PURE__ */ jsxRuntime.jsx(blocks.Flex, { gap: "4", display: forMobile(false, "flex"), children: actions.map((Action) => {
|
|
2081
|
+
const Wrapper = Action.Wrapper ?? DefaultWrapper;
|
|
2082
|
+
return /* @__PURE__ */ jsxRuntime.jsx(Wrapper, { ...Action.wrapperProps, children: /* @__PURE__ */ jsxRuntime.jsx(blocks.IconButton, { type: "button", variant: "ghost", "aria-label": Action.text, ...Action.buttonProps, children: /* @__PURE__ */ jsxRuntime.jsx(Action.Icon, {}) }) }, Action.text);
|
|
2083
|
+
}) }),
|
|
2084
|
+
/* @__PURE__ */ jsxRuntime.jsx(blocks.Box, { display: forMobile(true, "block"), children: /* @__PURE__ */ jsxRuntime.jsx(
|
|
2085
|
+
blocks.DropdownMenu,
|
|
2086
|
+
{
|
|
2087
|
+
trigger: /* @__PURE__ */ jsxRuntime.jsx(blocks.IconButton, { variant: "ghost", "aria-label": "Actions menu", children: /* @__PURE__ */ jsxRuntime.jsx(blocks.DotsVerticalIcon, {}) }),
|
|
2088
|
+
closeOnSelect: false,
|
|
2089
|
+
items: actions.map((Action) => {
|
|
2090
|
+
var _a;
|
|
2091
|
+
if (Action.disableOnMobile)
|
|
2092
|
+
return null;
|
|
2093
|
+
const Wrapper = Action.Wrapper ?? DefaultWrapper;
|
|
2094
|
+
return {
|
|
2095
|
+
...Action.buttonProps,
|
|
2096
|
+
onSelect: (_a = Action.buttonProps) == null ? void 0 : _a.onClick,
|
|
2097
|
+
content: /* @__PURE__ */ jsxRuntime.jsx(Wrapper, { ...Action.wrapperProps, children: /* @__PURE__ */ jsxRuntime.jsxs(blocks.Flex, { gap: "2", align: "center", children: [
|
|
2098
|
+
/* @__PURE__ */ jsxRuntime.jsx(Action.Icon, {}),
|
|
2099
|
+
Action.text
|
|
2100
|
+
] }) })
|
|
2101
|
+
};
|
|
2102
|
+
}).filter((x) => x !== null)
|
|
2103
|
+
}
|
|
2104
|
+
) })
|
|
2105
|
+
] });
|
|
2106
|
+
});
|
|
2107
|
+
const formId = "form-builder";
|
|
2108
|
+
const FieldWithActions = React.memo(function FieldWithActions2(props) {
|
|
2109
|
+
const { field, index, sectionIndex, takenLabels, remove: remove2 } = props;
|
|
2110
|
+
const deserializedField = React.useMemo(() => deserialize(field), [field]);
|
|
2111
|
+
const input = useFieldInput(deserializedField, { formId, disabled: true });
|
|
2112
|
+
const duplicateField = React.useCallback(
|
|
2113
|
+
(field2) => {
|
|
2114
|
+
if (field2.label === null) {
|
|
2115
|
+
throw new Error(`Expected a label for field ${field2.identifier}`);
|
|
2116
|
+
}
|
|
2117
|
+
return { ...field2, label: incrementFieldLabel(field2.label, takenLabels), identifier: "" };
|
|
2118
|
+
},
|
|
2119
|
+
[takenLabels]
|
|
2120
|
+
);
|
|
2121
|
+
const editFieldProps = React.useMemo(
|
|
2122
|
+
() => ({
|
|
2123
|
+
index,
|
|
2124
|
+
parentPath: `fields.${sectionIndex}.fields`,
|
|
2125
|
+
initial: field,
|
|
2126
|
+
editing: true
|
|
2127
|
+
}),
|
|
2128
|
+
[field, index, sectionIndex]
|
|
2129
|
+
);
|
|
2130
|
+
const duplicateFieldProps = React.useMemo(
|
|
2131
|
+
() => ({
|
|
2132
|
+
parentPath: `fields.${sectionIndex}.fields`,
|
|
2133
|
+
index: index + 1,
|
|
2134
|
+
initial: duplicateField(field)
|
|
2135
|
+
}),
|
|
2136
|
+
[duplicateField, field, index, sectionIndex]
|
|
2137
|
+
);
|
|
2138
|
+
const insertAfterProps = React.useMemo(
|
|
2139
|
+
() => ({
|
|
2140
|
+
parentPath: `fields.${sectionIndex}.fields`,
|
|
2141
|
+
index: index + 1,
|
|
2142
|
+
initial: void 0
|
|
2143
|
+
}),
|
|
2144
|
+
[index, sectionIndex]
|
|
2145
|
+
);
|
|
2146
|
+
return /* @__PURE__ */ jsxRuntime.jsx(dnd.Draggable, { draggableId: field.identifier, index, children: (draggableProvided) => /* @__PURE__ */ jsxRuntime.jsx(
|
|
2147
|
+
blocks.Card,
|
|
2148
|
+
{
|
|
2149
|
+
ref: draggableProvided.innerRef,
|
|
2150
|
+
...draggableProvided.draggableProps,
|
|
2151
|
+
...draggableProvided.dragHandleProps,
|
|
2152
|
+
mb: "4",
|
|
2153
|
+
children: /* @__PURE__ */ jsxRuntime.jsxs(blocks.Flex, { gap: "4", justify: "between", align: "center", children: [
|
|
2154
|
+
input,
|
|
2155
|
+
/* @__PURE__ */ jsxRuntime.jsx(
|
|
2156
|
+
FieldActions,
|
|
2157
|
+
{
|
|
2158
|
+
remove: remove2,
|
|
2159
|
+
editProps: editFieldProps,
|
|
2160
|
+
duplicateProps: duplicateFieldProps,
|
|
2161
|
+
insertAfterProps,
|
|
2162
|
+
dragHandleProps: draggableProvided.dragHandleProps
|
|
2163
|
+
}
|
|
2164
|
+
)
|
|
2165
|
+
] })
|
|
2166
|
+
}
|
|
2167
|
+
) });
|
|
2168
|
+
});
|
|
2169
|
+
const FieldSectionWithActions = React.memo(function FieldSectionWithActions2(props) {
|
|
2170
|
+
var _a, _b, _c, _d, _e, _f, _g;
|
|
2171
|
+
const { field, index: sectionIndex, dropState } = props;
|
|
2172
|
+
const isDropDisabled = (_a = dropState[field.identifier]) == null ? void 0 : _a.disabled;
|
|
2173
|
+
const { setFieldValue, values } = formik.useFormikContext();
|
|
2174
|
+
const alertDialog = blocks.useAlertDialog();
|
|
2175
|
+
const takenFieldLabels = getTakenFieldLabels(values.fields);
|
|
2176
|
+
const removeSectionConditions = React.useCallback(
|
|
2177
|
+
(sectionsToUpdate, allSections) => {
|
|
2178
|
+
for (const section of sectionsToUpdate) {
|
|
2179
|
+
const sectionIndex2 = allSections.indexOf(section);
|
|
2180
|
+
setFieldValue(`fields.${sectionIndex2}.condition`, null).then();
|
|
2181
|
+
setFieldValue(`fields.${sectionIndex2}.conditional`, false).then();
|
|
2182
|
+
}
|
|
2183
|
+
},
|
|
2184
|
+
[setFieldValue]
|
|
2185
|
+
);
|
|
2186
|
+
const makeRemoveFieldAction = React.useCallback(
|
|
2187
|
+
(fieldIndex) => {
|
|
2188
|
+
var _a2;
|
|
2189
|
+
const removing = field.fields[fieldIndex];
|
|
2190
|
+
if (!removing)
|
|
2191
|
+
throw new Error("Could not find field to remove.");
|
|
2192
|
+
const sectionsWithMatchingCondition = [];
|
|
2193
|
+
for (const section of values.fields) {
|
|
2194
|
+
if (((_a2 = section.condition) == null ? void 0 : _a2.identifier) === removing.identifier) {
|
|
2195
|
+
sectionsWithMatchingCondition.push(section);
|
|
2196
|
+
}
|
|
2197
|
+
}
|
|
2198
|
+
return {
|
|
2199
|
+
removing,
|
|
2200
|
+
affectedSections: sectionsWithMatchingCondition,
|
|
2201
|
+
action: () => setFieldValue(`fields.${sectionIndex}.fields`, remove(field.fields, fieldIndex))
|
|
2202
|
+
};
|
|
2203
|
+
},
|
|
2204
|
+
[field.fields, values.fields, setFieldValue, sectionIndex]
|
|
2205
|
+
);
|
|
2206
|
+
const removeField = React.useCallback(
|
|
2207
|
+
(i) => {
|
|
2208
|
+
const { affectedSections, action, removing } = makeRemoveFieldAction(i);
|
|
2209
|
+
const cmd = () => {
|
|
2210
|
+
action().then();
|
|
2211
|
+
removeSectionConditions(affectedSections, values.fields);
|
|
2212
|
+
};
|
|
2213
|
+
if (affectedSections.length > 0) {
|
|
2214
|
+
const labels = affectedSections.map((section) => section.label).join(", ");
|
|
2215
|
+
return alertDialog({
|
|
2216
|
+
title: "Remove condition?",
|
|
2217
|
+
description: `${removing.label} is being used as a condition, deleting it will remove the condition from the ${labels} section(s).`,
|
|
2218
|
+
severity: "danger",
|
|
2219
|
+
actionText: "Remove",
|
|
2220
|
+
onAction: cmd
|
|
2221
|
+
});
|
|
2222
|
+
}
|
|
2223
|
+
cmd();
|
|
2224
|
+
},
|
|
2225
|
+
[makeRemoveFieldAction, removeSectionConditions, values.fields, alertDialog]
|
|
2226
|
+
);
|
|
2227
|
+
const removeSection = React.useCallback(() => {
|
|
2228
|
+
const fieldSideEffects = field.fields.map((_, i) => makeRemoveFieldAction(i));
|
|
2229
|
+
const affectedSections = fieldSideEffects.flatMap((sideEffect) => sideEffect.affectedSections);
|
|
2230
|
+
const title2 = affectedSections.length ? "Remove fields and conditions?" : "Remove fields?";
|
|
2231
|
+
const numFields = field.fields.length;
|
|
2232
|
+
const sectionLabels = affectedSections.map((section) => section.label).join(", ");
|
|
2233
|
+
const description2 = affectedSections.length ? `Deleting this section will remove the ${numFields} field(s) it contains and will remove the conditions from following sections: ${sectionLabels}` : `Deleting this section will remove the ${numFields} field(s) it contains.`;
|
|
2234
|
+
const updatedSections = remove(values.fields, sectionIndex);
|
|
2235
|
+
const cmd = () => setFieldValue("fields", updatedSections);
|
|
2236
|
+
if (affectedSections.length > 0) {
|
|
2237
|
+
return alertDialog({
|
|
2238
|
+
title: title2,
|
|
2239
|
+
description: description2,
|
|
2240
|
+
severity: "danger",
|
|
2241
|
+
actionText: "Remove",
|
|
2242
|
+
onAction: () => {
|
|
2243
|
+
cmd().then(() => {
|
|
2244
|
+
removeSectionConditions(affectedSections, updatedSections);
|
|
2245
|
+
});
|
|
2246
|
+
}
|
|
2247
|
+
});
|
|
2248
|
+
}
|
|
2249
|
+
cmd().then();
|
|
2250
|
+
}, [
|
|
2251
|
+
field.fields,
|
|
2252
|
+
values.fields,
|
|
2253
|
+
sectionIndex,
|
|
2254
|
+
makeRemoveFieldAction,
|
|
2255
|
+
setFieldValue,
|
|
2256
|
+
alertDialog,
|
|
2257
|
+
removeSectionConditions
|
|
2258
|
+
]);
|
|
2259
|
+
const duplicateSection = React.useCallback(
|
|
2260
|
+
(field2) => {
|
|
2261
|
+
if (field2.label === null) {
|
|
2262
|
+
throw new Error(`Expected a label for field ${field2.identifier}`);
|
|
2263
|
+
}
|
|
2264
|
+
const newSectionLabel = incrementFieldLabel(field2.label, takenFieldLabels);
|
|
2265
|
+
const newFields = field2.fields.map((f) => {
|
|
2266
|
+
const newLabel = incrementFieldLabel(f.label, takenFieldLabels);
|
|
2267
|
+
return {
|
|
2268
|
+
...f,
|
|
2269
|
+
label: newLabel,
|
|
2270
|
+
identifier: makeIdentifier(void 0, newLabel)
|
|
2271
|
+
};
|
|
2272
|
+
});
|
|
2273
|
+
return { ...field2, label: newSectionLabel, fields: newFields, identifier: "" };
|
|
2274
|
+
},
|
|
2275
|
+
[takenFieldLabels]
|
|
2276
|
+
);
|
|
2277
|
+
const editSectionProps = React.useMemo(
|
|
2278
|
+
() => ({
|
|
2279
|
+
index: sectionIndex,
|
|
2280
|
+
parentPath: "fields",
|
|
2281
|
+
initial: field,
|
|
2282
|
+
editing: true,
|
|
2283
|
+
conditionalSourceFields: makeConditionalSourceFields(values.fields, sectionIndex)
|
|
2284
|
+
}),
|
|
2285
|
+
[field, sectionIndex, values.fields]
|
|
2286
|
+
);
|
|
2287
|
+
const insertSectionProps = React.useMemo(
|
|
2288
|
+
() => ({
|
|
2289
|
+
index: sectionIndex + 1,
|
|
2290
|
+
parentPath: "fields",
|
|
2291
|
+
initial: emptySection(),
|
|
2292
|
+
conditionalSourceFields: makeConditionalSourceFields(values.fields, sectionIndex + 1)
|
|
2293
|
+
}),
|
|
2294
|
+
[sectionIndex, values.fields]
|
|
2295
|
+
);
|
|
2296
|
+
const insertFieldAtEndOfSection = React.useMemo(
|
|
2297
|
+
() => ({
|
|
2298
|
+
parentPath: `fields.${sectionIndex}.fields`,
|
|
2299
|
+
index: field.fields.length,
|
|
2300
|
+
initial: void 0
|
|
2301
|
+
}),
|
|
2302
|
+
[field.fields.length, sectionIndex]
|
|
2303
|
+
);
|
|
2304
|
+
const duplicateSectionProps = React.useMemo(
|
|
2305
|
+
() => ({
|
|
2306
|
+
index: sectionIndex + 1,
|
|
2307
|
+
parentPath: "fields",
|
|
2308
|
+
initial: duplicateSection(field),
|
|
2309
|
+
conditionalSourceFields: makeConditionalSourceFields(values.fields, sectionIndex + 1)
|
|
2310
|
+
}),
|
|
2311
|
+
[duplicateSection, field, sectionIndex, values.fields]
|
|
2312
|
+
);
|
|
2313
|
+
const conditionLabel = React.useMemo(
|
|
2314
|
+
() => {
|
|
2315
|
+
var _a2, _b2;
|
|
2316
|
+
return (_b2 = findFieldByIdentifier(values.fields, (_a2 = field.condition) == null ? void 0 : _a2.identifier)) == null ? void 0 : _b2.label;
|
|
2317
|
+
},
|
|
2318
|
+
[(_b = field.condition) == null ? void 0 : _b.identifier, values.fields]
|
|
2319
|
+
);
|
|
2320
|
+
const conditionComparison = Array.isArray((_c = field.condition) == null ? void 0 : _c.value) ? "contains all of" : "equals";
|
|
2321
|
+
if (valueIsFile((_d = field.condition) == null ? void 0 : _d.value))
|
|
2322
|
+
throw new Error("File values are not supported for conditions.");
|
|
2323
|
+
const conditionValue = Array.isArray((_e = field.condition) == null ? void 0 : _e.value) ? (_f = field.condition) == null ? void 0 : _f.value.map((v) => typeof v === "string" ? v : v.label).join(", ") : (_g = field.condition) == null ? void 0 : _g.value.toString();
|
|
2324
|
+
return /* @__PURE__ */ jsxRuntime.jsx(dnd.Draggable, { draggableId: field.identifier, index: sectionIndex, children: (draggableProvided) => /* @__PURE__ */ jsxRuntime.jsx(
|
|
2325
|
+
blocks.Card,
|
|
2326
|
+
{
|
|
2327
|
+
ref: draggableProvided.innerRef,
|
|
2328
|
+
...draggableProvided.draggableProps,
|
|
2329
|
+
...draggableProvided.dragHandleProps,
|
|
2330
|
+
mb: "4",
|
|
2331
|
+
children: /* @__PURE__ */ jsxRuntime.jsxs(blocks.Flex, { gap: "3", justify: "between", align: "center", children: [
|
|
2332
|
+
/* @__PURE__ */ jsxRuntime.jsxs(blocks.Flex, { direction: "column", gap: "2", grow: "1", children: [
|
|
2333
|
+
/* @__PURE__ */ jsxRuntime.jsxs(blocks.Flex, { direction: "column", children: [
|
|
2334
|
+
/* @__PURE__ */ jsxRuntime.jsx(blocks.Heading, { as: "h3", size: "3", children: field.label }),
|
|
2335
|
+
/* @__PURE__ */ jsxRuntime.jsx(blocks.Text, { className: styles$3.description, children: field.description })
|
|
2336
|
+
] }),
|
|
2337
|
+
field.condition && /* @__PURE__ */ jsxRuntime.jsx(blocks.Text, { size: "1", children: /* @__PURE__ */ jsxRuntime.jsxs(blocks.Em, { children: [
|
|
2338
|
+
"Display only if ",
|
|
2339
|
+
/* @__PURE__ */ jsxRuntime.jsx(blocks.Strong, { children: conditionLabel }),
|
|
2340
|
+
" ",
|
|
2341
|
+
conditionComparison,
|
|
2342
|
+
" ",
|
|
2343
|
+
/* @__PURE__ */ jsxRuntime.jsx(blocks.Strong, { children: conditionValue })
|
|
2344
|
+
] }) }),
|
|
2345
|
+
/* @__PURE__ */ jsxRuntime.jsx(dnd.Droppable, { droppableId: field.identifier, type: "SECTION", isDropDisabled, children: (droppableProvided) => /* @__PURE__ */ jsxRuntime.jsxs(
|
|
2346
|
+
blocks.Flex,
|
|
2347
|
+
{
|
|
2348
|
+
ref: droppableProvided.innerRef,
|
|
2349
|
+
...droppableProvided.droppableProps,
|
|
2350
|
+
direction: "column",
|
|
2351
|
+
gap: "0",
|
|
2352
|
+
children: [
|
|
2353
|
+
field.fields.map((child, i) => /* @__PURE__ */ jsxRuntime.jsx(
|
|
2354
|
+
FieldWithActions,
|
|
2355
|
+
{
|
|
2356
|
+
field: child,
|
|
2357
|
+
index: i,
|
|
2358
|
+
sectionIndex,
|
|
2359
|
+
remove: () => removeField(i),
|
|
2360
|
+
takenLabels: takenFieldLabels
|
|
2361
|
+
},
|
|
2362
|
+
child.identifier
|
|
2363
|
+
)),
|
|
2364
|
+
droppableProvided.placeholder,
|
|
2365
|
+
/* @__PURE__ */ jsxRuntime.jsx(FieldBuilder, { ...insertFieldAtEndOfSection, children: /* @__PURE__ */ jsxRuntime.jsxs(blocks.Button, { type: "button", variant: "outline", children: [
|
|
2366
|
+
/* @__PURE__ */ jsxRuntime.jsx(blocks.PlusIcon, {}),
|
|
2367
|
+
" Add a field"
|
|
2368
|
+
] }) })
|
|
2369
|
+
]
|
|
2370
|
+
}
|
|
2371
|
+
) })
|
|
2372
|
+
] }),
|
|
2373
|
+
/* @__PURE__ */ jsxRuntime.jsx(
|
|
2374
|
+
FieldActions,
|
|
2375
|
+
{
|
|
2376
|
+
remove: removeSection,
|
|
2377
|
+
insertAfterProps: insertSectionProps,
|
|
2378
|
+
dragHandleProps: draggableProvided.dragHandleProps,
|
|
2379
|
+
editProps: editSectionProps,
|
|
2380
|
+
duplicateProps: duplicateSectionProps
|
|
2381
|
+
}
|
|
2382
|
+
)
|
|
2383
|
+
] })
|
|
2384
|
+
}
|
|
2385
|
+
) });
|
|
2386
|
+
});
|
|
2387
|
+
const reducer = (state, action) => {
|
|
2388
|
+
var _a;
|
|
2389
|
+
const next = { ...state };
|
|
2390
|
+
switch (action.type) {
|
|
2391
|
+
case "release":
|
|
2392
|
+
for (const sectionId in next) {
|
|
2393
|
+
next[sectionId].disabled = false;
|
|
2394
|
+
}
|
|
2395
|
+
return next;
|
|
2396
|
+
case "hold":
|
|
2397
|
+
for (const sectionId in next) {
|
|
2398
|
+
if ((_a = next[sectionId]) == null ? void 0 : _a.conditionFields.has(action.fieldId)) {
|
|
2399
|
+
next[sectionId].disabled = true;
|
|
2400
|
+
}
|
|
2401
|
+
}
|
|
2402
|
+
return next;
|
|
2403
|
+
case "update":
|
|
2404
|
+
return action.state;
|
|
2405
|
+
}
|
|
2406
|
+
};
|
|
2407
|
+
const getConditionIndex = (fields, identifier) => {
|
|
2408
|
+
if (!identifier)
|
|
2409
|
+
return void 0;
|
|
2410
|
+
for (let i = 0; i < fields.length; i++) {
|
|
2411
|
+
const section = fields[i];
|
|
2412
|
+
if (!section)
|
|
2413
|
+
continue;
|
|
2414
|
+
for (const field of section.fields) {
|
|
2415
|
+
if (field.identifier === identifier)
|
|
2416
|
+
return i;
|
|
2417
|
+
}
|
|
2418
|
+
}
|
|
2419
|
+
};
|
|
2420
|
+
const initializer = (fields) => {
|
|
2421
|
+
var _a, _b, _c;
|
|
2422
|
+
const acc = {};
|
|
2423
|
+
for (let index = 0; index < fields.length; index++) {
|
|
2424
|
+
const field = fields[index];
|
|
2425
|
+
if (!field)
|
|
2426
|
+
throw new Error("Field is undefined.");
|
|
2427
|
+
const previousConditionFields = index > 0 ? (_a = acc[fields[index - 1].identifier]) == null ? void 0 : _a.conditionFields : void 0;
|
|
2428
|
+
const disabledFields = new Set(previousConditionFields);
|
|
2429
|
+
if ((_b = field.condition) == null ? void 0 : _b.identifier) {
|
|
2430
|
+
disabledFields.add(field.condition.identifier);
|
|
2431
|
+
}
|
|
2432
|
+
acc[field.identifier] = {
|
|
2433
|
+
disabled: false,
|
|
2434
|
+
conditionFields: disabledFields,
|
|
2435
|
+
conditionIndex: getConditionIndex(fields, (_c = field.condition) == null ? void 0 : _c.identifier),
|
|
2436
|
+
index,
|
|
2437
|
+
label: field.label
|
|
2438
|
+
};
|
|
2439
|
+
}
|
|
2440
|
+
return acc;
|
|
2441
|
+
};
|
|
2442
|
+
const findSection = (fields, sectionId) => {
|
|
2443
|
+
for (const [i, section] of Object.entries(fields)) {
|
|
2444
|
+
if (section.identifier === sectionId)
|
|
2445
|
+
return [section, i];
|
|
2446
|
+
}
|
|
2447
|
+
};
|
|
2448
|
+
const FieldsEditor = React.memo(function FieldsEditor2() {
|
|
2449
|
+
const { values, setFieldValue } = formik.useFormikContext();
|
|
2450
|
+
const [dropState, dispatch] = React.useReducer(reducer, values.fields, initializer);
|
|
2451
|
+
const { showInfo } = blocks.useToast();
|
|
2452
|
+
React.useEffect(() => {
|
|
2453
|
+
dispatch({ type: "update", state: initializer(values.fields) });
|
|
2454
|
+
}, [dispatch, values.fields]);
|
|
2455
|
+
const handleDragStart = React.useCallback((start) => {
|
|
2456
|
+
if (start.type === "SECTION") {
|
|
2457
|
+
dispatch({ type: "hold", fieldId: start.draggableId });
|
|
2458
|
+
}
|
|
2459
|
+
}, []);
|
|
2460
|
+
const handleDragEnd = React.useCallback(
|
|
2461
|
+
(result) => {
|
|
2462
|
+
const { source, destination, type, reason, draggableId } = result;
|
|
2463
|
+
dispatch({ type: "release" });
|
|
2464
|
+
if (!destination || reason === "CANCEL")
|
|
2465
|
+
return;
|
|
2466
|
+
if (type === "ROOT") {
|
|
2467
|
+
const state = dropState[draggableId];
|
|
2468
|
+
if (!state)
|
|
2469
|
+
throw new Error("Could not find section context.");
|
|
2470
|
+
let dest = typeof state.conditionIndex !== "undefined" ? (
|
|
2471
|
+
// cannot move a section with a condition before the condition's field
|
|
2472
|
+
Math.max(state.conditionIndex + 1, destination.index)
|
|
2473
|
+
) : destination.index;
|
|
2474
|
+
for (const section of Object.values(dropState)) {
|
|
2475
|
+
if (section.conditionIndex === source.index) {
|
|
2476
|
+
dest = Math.min(dest, section.index - 1);
|
|
2477
|
+
}
|
|
2478
|
+
}
|
|
2479
|
+
if (dest != destination.index) {
|
|
2480
|
+
showInfo({
|
|
2481
|
+
title: "Reordered sections",
|
|
2482
|
+
description: "Sections with conditions must be below the fields they reference."
|
|
2483
|
+
});
|
|
2484
|
+
}
|
|
2485
|
+
return setFieldValue("fields", reorder(values.fields, source.index, dest));
|
|
2486
|
+
}
|
|
2487
|
+
if (type !== "SECTION")
|
|
2488
|
+
throw new Error("Unexpected droppable type.");
|
|
2489
|
+
const [sourceSection, srcIndex] = findSection(values.fields, source.droppableId) ?? [];
|
|
2490
|
+
const [destinationSection, destIndex] = findSection(values.fields, destination.droppableId) ?? [];
|
|
2491
|
+
if (!(sourceSection == null ? void 0 : sourceSection.fields) || !destinationSection)
|
|
2492
|
+
throw new Error("Could not find section with fields.");
|
|
2493
|
+
if (sourceSection.identifier === destinationSection.identifier) {
|
|
2494
|
+
setFieldValue(
|
|
2495
|
+
`fields.${srcIndex}.fields`,
|
|
2496
|
+
reorder(sourceSection.fields, source.index, destination.index)
|
|
2497
|
+
).then();
|
|
2498
|
+
} else {
|
|
2499
|
+
const removed = sourceSection.fields[source.index];
|
|
2500
|
+
if (!removed)
|
|
2501
|
+
throw new Error("Could not find field to reorder.");
|
|
2502
|
+
setFieldValue(`fields.${srcIndex}.fields`, remove(sourceSection.fields, source.index)).then();
|
|
2503
|
+
setFieldValue(
|
|
2504
|
+
`fields.${destIndex}.fields`,
|
|
2505
|
+
insert(destinationSection.fields, destination.index, removed)
|
|
2506
|
+
).then();
|
|
2507
|
+
}
|
|
2508
|
+
},
|
|
2509
|
+
[values.fields, setFieldValue, dropState, showInfo]
|
|
2510
|
+
);
|
|
2511
|
+
const makeFieldSectionProps = React.useMemo(
|
|
2512
|
+
() => ({
|
|
2513
|
+
index: values.fields.length,
|
|
2514
|
+
parentPath: "fields",
|
|
2515
|
+
initial: emptySection(),
|
|
2516
|
+
conditionalSourceFields: makeConditionalSourceFields(values.fields, values.fields.length)
|
|
2517
|
+
}),
|
|
2518
|
+
[values.fields]
|
|
2519
|
+
);
|
|
2520
|
+
return /* @__PURE__ */ jsxRuntime.jsx(dnd.DragDropContext, { onDragStart: handleDragStart, onDragEnd: handleDragEnd, children: /* @__PURE__ */ jsxRuntime.jsx(dnd.Droppable, { droppableId: "droppable", type: "ROOT", children: (droppableProvided) => /* @__PURE__ */ jsxRuntime.jsxs(
|
|
2521
|
+
blocks.Flex,
|
|
2522
|
+
{
|
|
2523
|
+
ref: droppableProvided.innerRef,
|
|
2524
|
+
...droppableProvided.droppableProps,
|
|
2525
|
+
direction: "column",
|
|
2526
|
+
gap: "0",
|
|
2527
|
+
children: [
|
|
2528
|
+
values.fields.map((field, index) => /* @__PURE__ */ jsxRuntime.jsx(
|
|
2529
|
+
FieldSectionWithActions,
|
|
2530
|
+
{
|
|
2531
|
+
field,
|
|
2532
|
+
index,
|
|
2533
|
+
dropState
|
|
2534
|
+
},
|
|
2535
|
+
field.label
|
|
2536
|
+
)),
|
|
2537
|
+
droppableProvided.placeholder,
|
|
2538
|
+
/* @__PURE__ */ jsxRuntime.jsx(FieldBuilder, { ...makeFieldSectionProps, children: /* @__PURE__ */ jsxRuntime.jsxs(blocks.Button, { type: "button", variant: "outline", children: [
|
|
2539
|
+
/* @__PURE__ */ jsxRuntime.jsx(blocks.PlusIcon, {}),
|
|
2540
|
+
" Add a section"
|
|
2541
|
+
] }) })
|
|
2542
|
+
]
|
|
2543
|
+
}
|
|
2544
|
+
) }) });
|
|
2545
|
+
});
|
|
2546
|
+
const initialValues = {
|
|
2547
|
+
title: "",
|
|
2548
|
+
description: "",
|
|
2549
|
+
fields: []
|
|
2550
|
+
};
|
|
2551
|
+
const title = new StringField({
|
|
2552
|
+
label: "Title",
|
|
2553
|
+
minLength: 0,
|
|
2554
|
+
maxLength: 100,
|
|
2555
|
+
required: true,
|
|
2556
|
+
identifier: "title"
|
|
2557
|
+
});
|
|
2558
|
+
const titleProps = { formId, placeholder: "Give your form a title." };
|
|
2559
|
+
const description = new TextField({
|
|
2560
|
+
label: "Description",
|
|
2561
|
+
minLength: 0,
|
|
2562
|
+
maxLength: 1e3,
|
|
2563
|
+
required: false,
|
|
2564
|
+
identifier: "description"
|
|
2565
|
+
});
|
|
2566
|
+
const descriptionProps = { formId, placeholder: "Explain the purpose of this form." };
|
|
2567
|
+
const previewSubmit = () => {
|
|
2568
|
+
alert("This is a form preview, your data will not be saved.");
|
|
2569
|
+
};
|
|
2570
|
+
const FormBuilder = React.memo(
|
|
2571
|
+
React.forwardRef((props, ref) => {
|
|
2572
|
+
const { onCancel, onSave, revision } = props;
|
|
2573
|
+
const defaultHeading = revision ? "Edit form" : "Create a new form";
|
|
2574
|
+
const { heading = defaultHeading } = props;
|
|
2575
|
+
const validate = React.useCallback((form) => {
|
|
2576
|
+
const errors = {};
|
|
2577
|
+
if (!form.title) {
|
|
2578
|
+
errors.title = "Title is required.";
|
|
2579
|
+
}
|
|
2580
|
+
if (!form.fields || form.fields.length === 0) {
|
|
2581
|
+
errors.fields = "At least one field is required.";
|
|
2582
|
+
}
|
|
2583
|
+
if (hasKeys(errors)) {
|
|
2584
|
+
return errors;
|
|
2585
|
+
}
|
|
2586
|
+
}, []);
|
|
2587
|
+
const formik$1 = formik.useFormik({
|
|
2588
|
+
initialValues: wrapRootFieldsWithFieldSection(revision) ?? initialValues,
|
|
2589
|
+
validate,
|
|
2590
|
+
onSubmit: (form) => onSave(form),
|
|
2591
|
+
// only validate the entire for on submit
|
|
2592
|
+
validateOnChange: false,
|
|
2593
|
+
validateOnBlur: false
|
|
2594
|
+
});
|
|
2595
|
+
const previewSchema = React.useMemo(() => formRevisionToSchema(formik$1.values), [formik$1.values]);
|
|
2596
|
+
const titleInput = useFieldInput(title, titleProps);
|
|
2597
|
+
const descriptionInput = useFieldInput(description, descriptionProps);
|
|
2598
|
+
const FormBuilderHeading = React.useMemo(
|
|
2599
|
+
() => typeof heading === "object" ? heading : /* @__PURE__ */ jsxRuntime.jsx(blocks.Heading, { children: heading }),
|
|
2600
|
+
[heading]
|
|
2601
|
+
);
|
|
2602
|
+
return /* @__PURE__ */ jsxRuntime.jsx(blocks.Tabs.Root, { ref, defaultValue: "edit", children: /* @__PURE__ */ jsxRuntime.jsxs(blocks.Flex, { direction: "column", gap: "2", children: [
|
|
2603
|
+
/* @__PURE__ */ jsxRuntime.jsxs(blocks.Tabs.List, { children: [
|
|
2604
|
+
/* @__PURE__ */ jsxRuntime.jsx(blocks.Tabs.Trigger, { value: "edit", children: "Edit" }),
|
|
2605
|
+
/* @__PURE__ */ jsxRuntime.jsx(blocks.Tabs.Trigger, { value: "preview", children: "Preview" })
|
|
2606
|
+
] }),
|
|
2607
|
+
/* @__PURE__ */ jsxRuntime.jsxs(blocks.Tabs.Content, { value: "edit", children: [
|
|
2608
|
+
FormBuilderHeading,
|
|
2609
|
+
/* @__PURE__ */ jsxRuntime.jsxs(blocks.Text, { children: [
|
|
2610
|
+
"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",
|
|
2611
|
+
" ",
|
|
2612
|
+
/* @__PURE__ */ jsxRuntime.jsx("em", { children: "Preview" }),
|
|
2613
|
+
" tab, but",
|
|
2614
|
+
" ",
|
|
2615
|
+
/* @__PURE__ */ jsxRuntime.jsx("strong", { children: "field values entered on this page will not be saved." })
|
|
2616
|
+
] }),
|
|
2617
|
+
/* @__PURE__ */ jsxRuntime.jsx(blocks.Flex, { asChild: true, direction: "column", gap: "2", mt: "3", children: /* @__PURE__ */ jsxRuntime.jsxs("form", { id: formId, onSubmit: formik$1.handleSubmit, children: [
|
|
2618
|
+
/* @__PURE__ */ jsxRuntime.jsxs(formik.FormikProvider, { value: formik$1, children: [
|
|
2619
|
+
titleInput,
|
|
2620
|
+
descriptionInput,
|
|
2621
|
+
/* @__PURE__ */ jsxRuntime.jsx(FieldsEditor, {}),
|
|
2622
|
+
/* @__PURE__ */ jsxRuntime.jsx(blocks.Text, { severity: "danger", size: "1", children: typeof formik$1.errors.fields === "string" && formik$1.errors.fields })
|
|
2623
|
+
] }),
|
|
2624
|
+
/* @__PURE__ */ jsxRuntime.jsxs(blocks.Flex, { justify: "end", gap: "2", children: [
|
|
2625
|
+
/* @__PURE__ */ jsxRuntime.jsx(blocks.Button, { type: "button", variant: "soft", onClick: onCancel, children: "Cancel" }),
|
|
2626
|
+
/* @__PURE__ */ jsxRuntime.jsx(blocks.Button, { type: "submit", disabled: !formik$1.isValid, children: "Save" })
|
|
2627
|
+
] })
|
|
2628
|
+
] }) })
|
|
2629
|
+
] }),
|
|
2630
|
+
/* @__PURE__ */ jsxRuntime.jsx(blocks.Tabs.Content, { value: "preview", children: /* @__PURE__ */ jsxRuntime.jsx(FormRenderer, { schema: previewSchema, onSubmit: previewSubmit }) })
|
|
2631
|
+
] }) });
|
|
2632
|
+
})
|
|
2633
|
+
);
|
|
2634
|
+
exports2.BooleanField = BooleanField;
|
|
2635
|
+
exports2.BooleanInput = BooleanInput;
|
|
2636
|
+
exports2.DateField = DateField;
|
|
2637
|
+
exports2.DateInput = DateInput;
|
|
2638
|
+
exports2.FieldSection = FieldSection;
|
|
2639
|
+
exports2.FormBrowser = FormBrowser;
|
|
2640
|
+
exports2.FormBuilder = FormBuilder;
|
|
2641
|
+
exports2.FormRenderer = FormRenderer;
|
|
2642
|
+
exports2.FormSubmissionBrowser = FormSubmissionBrowser;
|
|
2643
|
+
exports2.FormSubmissionViewer = FormSubmissionViewer;
|
|
2644
|
+
exports2.MultiSelectField = MultiSelectField;
|
|
2645
|
+
exports2.MultiSelectInput = MultiSelectInput;
|
|
2646
|
+
exports2.MultiStringField = MultiStringField;
|
|
2647
|
+
exports2.MultiStringInput = MultiStringInput;
|
|
2648
|
+
exports2.NumberField = NumberField;
|
|
2649
|
+
exports2.NumberInput = NumberInput$1;
|
|
2650
|
+
exports2.PatchField = PatchField;
|
|
2651
|
+
exports2.PatchFormProvider = PatchFormProvider;
|
|
2652
|
+
exports2.SelectField = SelectField;
|
|
2653
|
+
exports2.SelectInput = SelectInput;
|
|
2654
|
+
exports2.StringField = StringField;
|
|
2655
|
+
exports2.StringInput = StringInput;
|
|
2656
|
+
exports2.TextField = TextField;
|
|
2657
|
+
exports2.TextInput = TextInput;
|
|
2658
|
+
exports2.deserialize = deserialize;
|
|
2659
|
+
exports2.deserializeField = deserializeField;
|
|
2660
|
+
exports2.formRevisionToSchema = formRevisionToSchema;
|
|
2661
|
+
exports2.isConditionMet = isConditionMet;
|
|
2662
|
+
exports2.useFieldInput = useFieldInput;
|
|
2663
|
+
exports2.useFieldInputs = useFieldInputs;
|
|
2664
|
+
exports2.valueIsFile = valueIsFile;
|
|
2665
|
+
Object.defineProperty(exports2, Symbol.toStringTag, { value: "Module" });
|
|
2666
|
+
});
|
|
2667
|
+
//# sourceMappingURL=forms.umd.cjs.map
|