form-builder-pro 1.0.14 → 1.0.15

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.js CHANGED
@@ -7719,102 +7719,105 @@ var FormBuilder = class {
7719
7719
  wrapper.appendChild(this.renderToolbar(state));
7720
7720
  const main = createElement("div", { className: "flex flex-col md:flex-row flex-1 overflow-hidden" });
7721
7721
  if (state.isPreviewMode) {
7722
- const masterTypes = state.masterTypes;
7722
+ const masterTypes = state.masterTypes || [];
7723
7723
  const dropdownOptionsMap = state.dropdownOptionsMap;
7724
- if (masterTypes && masterTypes.length > 0 && state.schema.sections) {
7725
- const convertIndexesToOptions = (indexes) => {
7726
- if (!indexes || !Array.isArray(indexes) || indexes.length === 0) {
7727
- return [];
7728
- }
7729
- return indexes.map((item, index2) => {
7730
- if (typeof item === "string") {
7731
- return { label: item, value: item };
7732
- }
7733
- if (typeof item === "object" && item !== null) {
7734
- const label = item.label || item.name || item.displayName || item.text || `Option ${index2 + 1}`;
7735
- const value = item.value || item.id || item.name || String(index2);
7736
- return { label, value };
7724
+ if (state.schema.sections) {
7725
+ let previewSchema = state.schema;
7726
+ if (masterTypes && masterTypes.length > 0) {
7727
+ const convertIndexesToOptions = (indexes) => {
7728
+ if (!indexes || !Array.isArray(indexes) || indexes.length === 0) {
7729
+ return [];
7737
7730
  }
7738
- return { label: String(item), value: String(item) };
7739
- });
7740
- };
7741
- const areDefaultOptions = (options) => {
7742
- if (!options || options.length === 0)
7743
- return true;
7744
- return options.every(
7745
- (opt, idx) => opt.label === `Option ${idx + 1}` && (opt.value === `opt${idx + 1}` || opt.value === `Option ${idx + 1}`)
7746
- );
7747
- };
7748
- const fieldsNeedingApiCall = [];
7749
- const previewSchema = {
7750
- ...state.schema,
7751
- sections: state.schema.sections.map((section) => ({
7752
- ...section,
7753
- fields: section.fields.map((field) => {
7754
- if (field.type === "select") {
7755
- let masterType;
7756
- if (field.masterTypeName) {
7757
- masterType = masterTypes.find(
7758
- (mt) => mt.active === true && mt.enumName === field.masterTypeName
7759
- );
7760
- if (masterType && masterType.enumName) {
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
- });
7731
+ return indexes.map((item, index2) => {
7732
+ if (typeof item === "string") {
7733
+ return { label: item, value: item };
7734
+ }
7735
+ if (typeof item === "object" && item !== null) {
7736
+ const label = item.label || item.name || item.displayName || item.text || `Option ${index2 + 1}`;
7737
+ const value = item.value || item.id || item.name || String(index2);
7738
+ return { label, value };
7739
+ }
7740
+ return { label: String(item), value: String(item) };
7741
+ });
7742
+ };
7743
+ const areDefaultOptions = (options) => {
7744
+ if (!options || options.length === 0)
7745
+ return true;
7746
+ return options.every(
7747
+ (opt, idx) => opt.label === `Option ${idx + 1}` && (opt.value === `opt${idx + 1}` || opt.value === `Option ${idx + 1}`)
7748
+ );
7749
+ };
7750
+ const fieldsNeedingApiCall = [];
7751
+ previewSchema = {
7752
+ ...state.schema,
7753
+ sections: state.schema.sections.map((section) => ({
7754
+ ...section,
7755
+ fields: section.fields.map((field) => {
7756
+ if (field.type === "select") {
7757
+ let masterType;
7758
+ if (field.masterTypeName) {
7759
+ masterType = masterTypes.find(
7760
+ (mt) => mt.active === true && mt.enumName === field.masterTypeName
7761
+ );
7762
+ if (masterType && masterType.enumName) {
7763
+ const hasOptionsInMap = dropdownOptionsMap && dropdownOptionsMap[masterType.enumName] && dropdownOptionsMap[masterType.enumName].length > 0;
7764
+ const hasOptionsInIndexes = masterType.indexes && masterType.indexes.length > 0;
7765
+ const alreadyRequested = this.requestedMasterTypesCache.has(masterType.enumName);
7766
+ if (!hasOptionsInMap && !hasOptionsInIndexes && !alreadyRequested) {
7767
+ this.requestedMasterTypesCache.add(masterType.enumName);
7768
+ fieldsNeedingApiCall.push({
7769
+ fieldId: field.id,
7770
+ groupEnumName: masterType.enumName
7771
+ });
7772
+ }
7770
7773
  }
7774
+ } else if (field.groupName) {
7775
+ masterType = masterTypes.find(
7776
+ (mt) => mt.active === true && (mt.id === field.groupName?.id || mt.name === field.groupName?.name)
7777
+ );
7771
7778
  }
7772
- } else if (field.groupName) {
7773
- masterType = masterTypes.find(
7774
- (mt) => mt.active === true && (mt.id === field.groupName?.id || mt.name === field.groupName?.name)
7775
- );
7776
- }
7777
- if (masterType) {
7778
- let options = [];
7779
- if (masterType.enumName && dropdownOptionsMap && dropdownOptionsMap[masterType.enumName]) {
7780
- options = dropdownOptionsMap[masterType.enumName];
7781
- } else if (masterType.indexes && masterType.indexes.length > 0) {
7782
- options = convertIndexesToOptions(masterType.indexes);
7783
- }
7784
- if (options.length > 0) {
7785
- if (field.masterTypeName || !field.options || field.options.length === 0 || areDefaultOptions(field.options)) {
7786
- return { ...field, options };
7779
+ if (masterType) {
7780
+ let options = [];
7781
+ if (masterType.enumName && dropdownOptionsMap && dropdownOptionsMap[masterType.enumName]) {
7782
+ options = dropdownOptionsMap[masterType.enumName];
7783
+ } else if (masterType.indexes && masterType.indexes.length > 0) {
7784
+ options = convertIndexesToOptions(masterType.indexes);
7785
+ }
7786
+ if (options.length > 0) {
7787
+ if (field.masterTypeName || !field.options || field.options.length === 0 || areDefaultOptions(field.options)) {
7788
+ return { ...field, options };
7789
+ }
7787
7790
  }
7788
7791
  }
7789
7792
  }
7790
- }
7791
- return field;
7792
- })
7793
- }))
7794
- };
7795
- if (fieldsNeedingApiCall.length > 0 && this.options.onGroupSelectionChange && !this.isTriggeringApiCalls) {
7796
- this.isTriggeringApiCalls = true;
7797
- setTimeout(() => {
7798
- const uniqueApiCalls = /* @__PURE__ */ new Map();
7799
- fieldsNeedingApiCall.forEach(({ fieldId, groupEnumName }) => {
7800
- if (!uniqueApiCalls.has(groupEnumName)) {
7801
- uniqueApiCalls.set(groupEnumName, []);
7802
- }
7803
- uniqueApiCalls.get(groupEnumName).push(fieldId);
7804
- });
7805
- uniqueApiCalls.forEach((fieldIds, groupEnumName) => {
7806
- if (process.env.NODE_ENV === "development") {
7807
- console.log(`[FormBuilder] Preview: Triggering API call for masterTypeName "${groupEnumName}" (affects ${fieldIds.length} field(s))`);
7808
- }
7809
- this.options.onGroupSelectionChange({
7810
- fieldId: fieldIds[0],
7811
- groupEnumName
7812
- });
7813
- });
7793
+ return field;
7794
+ })
7795
+ }))
7796
+ };
7797
+ if (fieldsNeedingApiCall.length > 0 && this.options.onGroupSelectionChange && !this.isTriggeringApiCalls) {
7798
+ this.isTriggeringApiCalls = true;
7814
7799
  setTimeout(() => {
7815
- this.isTriggeringApiCalls = false;
7816
- }, 100);
7817
- }, 0);
7800
+ const uniqueApiCalls = /* @__PURE__ */ new Map();
7801
+ fieldsNeedingApiCall.forEach(({ fieldId, groupEnumName }) => {
7802
+ if (!uniqueApiCalls.has(groupEnumName)) {
7803
+ uniqueApiCalls.set(groupEnumName, []);
7804
+ }
7805
+ uniqueApiCalls.get(groupEnumName).push(fieldId);
7806
+ });
7807
+ uniqueApiCalls.forEach((fieldIds, groupEnumName) => {
7808
+ if (process.env.NODE_ENV === "development") {
7809
+ console.log(`[FormBuilder] Preview: Triggering API call for masterTypeName "${groupEnumName}" (affects ${fieldIds.length} field(s))`);
7810
+ }
7811
+ this.options.onGroupSelectionChange({
7812
+ fieldId: fieldIds[0],
7813
+ groupEnumName
7814
+ });
7815
+ });
7816
+ setTimeout(() => {
7817
+ this.isTriggeringApiCalls = false;
7818
+ }, 100);
7819
+ }, 0);
7820
+ }
7818
7821
  }
7819
7822
  const previewContainer = createElement("div", { className: "flex-1 p-8 overflow-y-auto bg-white dark:bg-gray-900 flex justify-center" });
7820
7823
  const inner = createElement("div", { className: "w-full max-w-3xl" });
@@ -7839,21 +7842,12 @@ var FormBuilder = class {
7839
7842
  } else {
7840
7843
  const previewContainer = createElement("div", { className: "flex-1 p-8 overflow-y-auto bg-white dark:bg-gray-900 flex justify-center" });
7841
7844
  const inner = createElement("div", { className: "w-full max-w-3xl" });
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
- }
7845
+ if (!this.previewRenderer) {
7847
7846
  this.previewRenderer = new FormRenderer(inner, state.schema, (data) => alert(JSON.stringify(data, null, 2)), this.options.onDropdownValueChange);
7848
7847
  this.lastPreviewSchema = state.schema;
7849
7848
  } 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
- }
7849
+ this.previewRenderer.setSchema(state.schema);
7850
+ this.lastPreviewSchema = state.schema;
7857
7851
  }
7858
7852
  previewContainer.appendChild(inner);
7859
7853
  main.appendChild(previewContainer);