form-builder-pro 1.0.4 → 1.0.5

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
@@ -4168,12 +4168,88 @@ var formStore = createStore((set, get) => ({
4168
4168
  existingForms: [],
4169
4169
  templates: [],
4170
4170
  masterTypes: [],
4171
- setSchema: (schema) => set({ schema }),
4171
+ setSchema: (schema) => {
4172
+ const convertIndexesToOptions = (indexes) => {
4173
+ if (!indexes || !Array.isArray(indexes) || indexes.length === 0) {
4174
+ return [];
4175
+ }
4176
+ return indexes.map((item, index2) => {
4177
+ if (typeof item === "string") {
4178
+ return { label: item, value: item };
4179
+ }
4180
+ if (typeof item === "object" && item !== null) {
4181
+ const label = item.label || item.name || item.displayName || item.text || `Option ${index2 + 1}`;
4182
+ const value = item.value || item.id || item.name || String(index2);
4183
+ return { label, value };
4184
+ }
4185
+ return { label: String(item), value: String(item) };
4186
+ });
4187
+ };
4188
+ const state = get();
4189
+ if (state.masterTypes && state.masterTypes.length > 0 && schema.sections) {
4190
+ const updatedSections = schema.sections.map((section) => ({
4191
+ ...section,
4192
+ fields: section.fields.map((field) => {
4193
+ if (field.type === "select" && field.groupName && (!field.options || field.options.length === 0)) {
4194
+ const masterType = state.masterTypes.find(
4195
+ (mt) => mt.active === true && (mt.id === field.groupName?.id || mt.name === field.groupName?.name)
4196
+ );
4197
+ if (masterType && masterType.indexes && masterType.indexes.length > 0) {
4198
+ const options = convertIndexesToOptions(masterType.indexes);
4199
+ return { ...field, options };
4200
+ }
4201
+ }
4202
+ return field;
4203
+ })
4204
+ }));
4205
+ set({ schema: { ...schema, sections: updatedSections } });
4206
+ } else {
4207
+ set({ schema });
4208
+ }
4209
+ },
4172
4210
  togglePreview: () => set((state) => ({ isPreviewMode: !state.isPreviewMode })),
4173
4211
  // New Actions
4174
4212
  setExistingForms: (forms) => set({ existingForms: forms }),
4175
4213
  setTemplates: (templates) => set({ templates }),
4176
- setMasterTypes: (masterTypes) => set({ masterTypes }),
4214
+ setMasterTypes: (masterTypes) => {
4215
+ set({ masterTypes });
4216
+ const state = get();
4217
+ if (state.schema && state.schema.sections) {
4218
+ const updatedSections = state.schema.sections.map((section) => ({
4219
+ ...section,
4220
+ fields: section.fields.map((field) => {
4221
+ if (field.type === "select" && field.groupName && (!field.options || field.options.length === 0)) {
4222
+ const masterType = masterTypes.find(
4223
+ (mt) => mt.active === true && (mt.id === field.groupName?.id || mt.name === field.groupName?.name)
4224
+ );
4225
+ if (masterType && masterType.indexes && masterType.indexes.length > 0) {
4226
+ const options = masterType.indexes.map((item, index2) => {
4227
+ if (typeof item === "string") {
4228
+ return { label: item, value: item };
4229
+ }
4230
+ if (typeof item === "object" && item !== null) {
4231
+ const label = item.label || item.name || item.displayName || item.text || `Option ${index2 + 1}`;
4232
+ const value = item.value || item.id || item.name || String(index2);
4233
+ return { label, value };
4234
+ }
4235
+ return { label: String(item), value: String(item) };
4236
+ });
4237
+ return { ...field, options };
4238
+ }
4239
+ }
4240
+ return field;
4241
+ })
4242
+ }));
4243
+ const hasChanges = updatedSections.some(
4244
+ (section, idx) => section.fields.some(
4245
+ (field, fieldIdx) => field !== state.schema.sections[idx]?.fields[fieldIdx]
4246
+ )
4247
+ );
4248
+ if (hasChanges) {
4249
+ set({ schema: { ...state.schema, sections: updatedSections } });
4250
+ }
4251
+ }
4252
+ },
4177
4253
  loadForm: (formId) => {
4178
4254
  const { existingForms, history, historyIndex } = get();
4179
4255
  const found = existingForms.find((f) => f.id === formId);
@@ -7615,6 +7691,22 @@ var FormBuilder = class {
7615
7691
  if (selectedField.type === "select") {
7616
7692
  const masterTypes = formStore.getState().masterTypes;
7617
7693
  const activeMasterTypes = masterTypes.filter((mt) => mt.active === true);
7694
+ const convertIndexesToOptions = (indexes) => {
7695
+ if (!indexes || !Array.isArray(indexes) || indexes.length === 0) {
7696
+ return [];
7697
+ }
7698
+ return indexes.map((item, index2) => {
7699
+ if (typeof item === "string") {
7700
+ return { label: item, value: item };
7701
+ }
7702
+ if (typeof item === "object" && item !== null) {
7703
+ const label = item.label || item.name || item.displayName || item.text || `Option ${index2 + 1}`;
7704
+ const value = item.value || item.id || item.name || String(index2);
7705
+ return { label, value };
7706
+ }
7707
+ return { label: String(item), value: String(item) };
7708
+ });
7709
+ };
7618
7710
  if (activeMasterTypes.length > 0) {
7619
7711
  const groupNameGroup = createElement("div", { className: "mb-4" });
7620
7712
  groupNameGroup.appendChild(createElement("label", { className: "block text-sm font-medium text-gray-700 dark:text-gray-300 mb-1", text: "Group Name" }));
@@ -7625,15 +7717,21 @@ var FormBuilder = class {
7625
7717
  if (selectedValue) {
7626
7718
  const selectedMasterType = activeMasterTypes.find((mt) => mt.id === selectedValue || mt.name === selectedValue);
7627
7719
  if (selectedMasterType) {
7720
+ const options = convertIndexesToOptions(selectedMasterType.indexes || []);
7628
7721
  formStore.getState().updateField(selectedField.id, {
7629
7722
  groupName: {
7630
7723
  id: selectedMasterType.id,
7631
7724
  name: selectedMasterType.name
7632
- }
7725
+ },
7726
+ options: options.length > 0 ? options : void 0
7633
7727
  });
7634
7728
  }
7635
7729
  } else {
7636
- formStore.getState().updateField(selectedField.id, { groupName: void 0 });
7730
+ formStore.getState().updateField(selectedField.id, {
7731
+ groupName: void 0,
7732
+ options: void 0
7733
+ // Clear options when groupName is cleared
7734
+ });
7637
7735
  }
7638
7736
  }
7639
7737
  });
@@ -7652,6 +7750,17 @@ var FormBuilder = class {
7652
7750
  });
7653
7751
  groupNameGroup.appendChild(groupNameSelect);
7654
7752
  body.appendChild(groupNameGroup);
7753
+ if (selectedField.groupName && (!selectedField.options || selectedField.options.length === 0)) {
7754
+ const currentMasterType = activeMasterTypes.find(
7755
+ (mt) => mt.id === selectedField.groupName?.id || mt.name === selectedField.groupName?.name
7756
+ );
7757
+ if (currentMasterType && currentMasterType.indexes && currentMasterType.indexes.length > 0) {
7758
+ const options = convertIndexesToOptions(currentMasterType.indexes);
7759
+ if (options.length > 0) {
7760
+ formStore.getState().updateField(selectedField.id, { options });
7761
+ }
7762
+ }
7763
+ }
7655
7764
  }
7656
7765
  }
7657
7766
  const validationHeader = createElement("h3", { className: "text-xs font-semibold text-gray-500 uppercase tracking-wider mb-3 mt-6", text: "Validation Rules" });