form-builder-pro 1.0.13 → 1.0.14

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/index.d.mts CHANGED
@@ -186,6 +186,11 @@ declare class FormBuilder {
186
186
  private container;
187
187
  private unsubscribe;
188
188
  private options;
189
+ private requestedMasterTypesCache;
190
+ private isTriggeringApiCalls;
191
+ private previewRenderer;
192
+ private lastPreviewSchema;
193
+ private wasInPreviewMode;
189
194
  constructor(container: HTMLElement, options?: FormBuilderOptions);
190
195
  loadForm(json: FormSchema): void;
191
196
  cloneForm(json: FormSchema): void;
package/dist/index.d.ts CHANGED
@@ -186,6 +186,11 @@ declare class FormBuilder {
186
186
  private container;
187
187
  private unsubscribe;
188
188
  private options;
189
+ private requestedMasterTypesCache;
190
+ private isTriggeringApiCalls;
191
+ private previewRenderer;
192
+ private lastPreviewSchema;
193
+ private wasInPreviewMode;
189
194
  constructor(container: HTMLElement, options?: FormBuilderOptions);
190
195
  loadForm(json: FormSchema): void;
191
196
  cloneForm(json: FormSchema): void;
package/dist/index.js CHANGED
@@ -7566,10 +7566,20 @@ var SectionList = class {
7566
7566
 
7567
7567
  // src/builder/FormBuilder.ts
7568
7568
  var FormBuilder = class {
7569
+ // Track previous preview state to detect mode changes
7569
7570
  constructor(container, options = {}) {
7570
7571
  __publicField(this, "container");
7571
7572
  __publicField(this, "unsubscribe");
7572
7573
  __publicField(this, "options");
7574
+ __publicField(this, "requestedMasterTypesCache", /* @__PURE__ */ new Set());
7575
+ // Cache to track requested master types
7576
+ __publicField(this, "isTriggeringApiCalls", false);
7577
+ // Flag to prevent concurrent API triggers
7578
+ __publicField(this, "previewRenderer", null);
7579
+ // Store FormRenderer instance to preserve selections
7580
+ __publicField(this, "lastPreviewSchema", null);
7581
+ // Track last preview schema to detect changes
7582
+ __publicField(this, "wasInPreviewMode", false);
7573
7583
  __publicField(this, "activeTab", "fields");
7574
7584
  if (!container) {
7575
7585
  throw new Error("Builder container not found. Please ensure the container element exists before initializing FormBuilder.");
@@ -7650,6 +7660,9 @@ var FormBuilder = class {
7650
7660
  }
7651
7661
  updateDropdownOptionsMap(dropdownOptionsMap) {
7652
7662
  formStore.getState().setDropdownOptionsMap(dropdownOptionsMap);
7663
+ Object.keys(dropdownOptionsMap).forEach((groupEnumName) => {
7664
+ if (dropdownOptionsMap[groupEnumName] && dropdownOptionsMap[groupEnumName].length > 0) ;
7665
+ });
7653
7666
  this.render();
7654
7667
  }
7655
7668
  updateMasterTypeGroups(masterTypeGroups) {
@@ -7745,10 +7758,16 @@ var FormBuilder = class {
7745
7758
  (mt) => mt.active === true && mt.enumName === field.masterTypeName
7746
7759
  );
7747
7760
  if (masterType && masterType.enumName) {
7748
- fieldsNeedingApiCall.push({
7749
- fieldId: field.id,
7750
- groupEnumName: masterType.enumName
7751
- });
7761
+ const hasOptionsInMap = dropdownOptionsMap && dropdownOptionsMap[masterType.enumName] && dropdownOptionsMap[masterType.enumName].length > 0;
7762
+ const hasOptionsInIndexes = masterType.indexes && masterType.indexes.length > 0;
7763
+ const alreadyRequested = this.requestedMasterTypesCache.has(masterType.enumName);
7764
+ if (!hasOptionsInMap && !hasOptionsInIndexes && !alreadyRequested) {
7765
+ this.requestedMasterTypesCache.add(masterType.enumName);
7766
+ fieldsNeedingApiCall.push({
7767
+ fieldId: field.id,
7768
+ groupEnumName: masterType.enumName
7769
+ });
7770
+ }
7752
7771
  }
7753
7772
  } else if (field.groupName) {
7754
7773
  masterType = masterTypes.find(
@@ -7773,7 +7792,8 @@ var FormBuilder = class {
7773
7792
  })
7774
7793
  }))
7775
7794
  };
7776
- if (fieldsNeedingApiCall.length > 0 && this.options.onGroupSelectionChange) {
7795
+ if (fieldsNeedingApiCall.length > 0 && this.options.onGroupSelectionChange && !this.isTriggeringApiCalls) {
7796
+ this.isTriggeringApiCalls = true;
7777
7797
  setTimeout(() => {
7778
7798
  const uniqueApiCalls = /* @__PURE__ */ new Map();
7779
7799
  fieldsNeedingApiCall.forEach(({ fieldId, groupEnumName }) => {
@@ -7791,21 +7811,59 @@ var FormBuilder = class {
7791
7811
  groupEnumName
7792
7812
  });
7793
7813
  });
7814
+ setTimeout(() => {
7815
+ this.isTriggeringApiCalls = false;
7816
+ }, 100);
7794
7817
  }, 0);
7795
7818
  }
7796
7819
  const previewContainer = createElement("div", { className: "flex-1 p-8 overflow-y-auto bg-white dark:bg-gray-900 flex justify-center" });
7797
7820
  const inner = createElement("div", { className: "w-full max-w-3xl" });
7798
- new FormRenderer(inner, previewSchema, (data) => alert(JSON.stringify(data, null, 2)), this.options.onDropdownValueChange);
7821
+ const structureChanged = !this.lastPreviewSchema || this.lastPreviewSchema.id !== previewSchema.id || this.lastPreviewSchema.sections.length !== previewSchema.sections.length || JSON.stringify(this.lastPreviewSchema.sections.map((s) => ({ id: s.id, fields: s.fields.map((f) => ({ id: f.id, type: f.type })) }))) !== JSON.stringify(previewSchema.sections.map((s) => ({ id: s.id, fields: s.fields.map((f) => ({ id: f.id, type: f.type })) })));
7822
+ if (!this.previewRenderer || structureChanged) {
7823
+ if (this.previewRenderer) {
7824
+ inner.innerHTML = "";
7825
+ }
7826
+ this.previewRenderer = new FormRenderer(inner, previewSchema, (data) => alert(JSON.stringify(data, null, 2)), this.options.onDropdownValueChange);
7827
+ this.lastPreviewSchema = previewSchema;
7828
+ } else {
7829
+ if (this.lastPreviewSchema) {
7830
+ const optionsChanged = JSON.stringify(this.lastPreviewSchema.sections.flatMap((s) => s.fields.map((f) => f.options))) !== JSON.stringify(previewSchema.sections.flatMap((s) => s.fields.map((f) => f.options)));
7831
+ if (optionsChanged && this.previewRenderer) {
7832
+ this.previewRenderer.setSchema(previewSchema);
7833
+ this.lastPreviewSchema = previewSchema;
7834
+ }
7835
+ }
7836
+ }
7799
7837
  previewContainer.appendChild(inner);
7800
7838
  main.appendChild(previewContainer);
7801
7839
  } else {
7802
7840
  const previewContainer = createElement("div", { className: "flex-1 p-8 overflow-y-auto bg-white dark:bg-gray-900 flex justify-center" });
7803
7841
  const inner = createElement("div", { className: "w-full max-w-3xl" });
7804
- new FormRenderer(inner, state.schema, (data) => alert(JSON.stringify(data, null, 2)), this.options.onDropdownValueChange);
7842
+ const structureChanged = !this.lastPreviewSchema || this.lastPreviewSchema.id !== state.schema.id || this.lastPreviewSchema.sections.length !== state.schema.sections.length || JSON.stringify(this.lastPreviewSchema.sections.map((s) => ({ id: s.id, fields: s.fields.map((f) => ({ id: f.id, type: f.type })) }))) !== JSON.stringify(state.schema.sections.map((s) => ({ id: s.id, fields: s.fields.map((f) => ({ id: f.id, type: f.type })) })));
7843
+ if (!this.previewRenderer || structureChanged) {
7844
+ if (this.previewRenderer) {
7845
+ inner.innerHTML = "";
7846
+ }
7847
+ this.previewRenderer = new FormRenderer(inner, state.schema, (data) => alert(JSON.stringify(data, null, 2)), this.options.onDropdownValueChange);
7848
+ this.lastPreviewSchema = state.schema;
7849
+ } else {
7850
+ if (this.lastPreviewSchema) {
7851
+ const optionsChanged = JSON.stringify(this.lastPreviewSchema.sections.flatMap((s) => s.fields.map((f) => f.options))) !== JSON.stringify(state.schema.sections.flatMap((s) => s.fields.map((f) => f.options)));
7852
+ if (optionsChanged && this.previewRenderer) {
7853
+ this.previewRenderer.setSchema(state.schema);
7854
+ this.lastPreviewSchema = state.schema;
7855
+ }
7856
+ }
7857
+ }
7805
7858
  previewContainer.appendChild(inner);
7806
7859
  main.appendChild(previewContainer);
7807
7860
  }
7808
7861
  } else {
7862
+ if (this.wasInPreviewMode) {
7863
+ this.previewRenderer = null;
7864
+ this.lastPreviewSchema = null;
7865
+ }
7866
+ this.wasInPreviewMode = false;
7809
7867
  const toolboxWrapper = createElement("div", { className: "form-builder-toolbox-wrapper w-full md:w-80 bg-white dark:bg-gray-900 border-r md:border-r border-b md:border-b-0 border-gray-200 dark:border-gray-800" });
7810
7868
  toolboxWrapper.appendChild(this.renderToolbox());
7811
7869
  main.appendChild(toolboxWrapper);