form-builder-pro 1.0.8 → 1.0.9

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
@@ -231,6 +231,15 @@ declare class FormRenderer {
231
231
  private render;
232
232
  }
233
233
 
234
+ /**
235
+ * Cleans a form schema by removing invalid properties and normalizing field types
236
+ * - Converts "decimal" type to "number"
237
+ * - Removes invalid properties like "masterTypeName"
238
+ * - Removes empty options arrays from non-select/radio fields
239
+ * @param schema
240
+ * @returns Cleaned schema
241
+ */
242
+ declare const cleanFormSchema: (schema: any) => FormSchema;
234
243
  /**
235
244
  * Transforms Form Builder JSON to Platform JSON
236
245
  * @param builderSchema
@@ -248,4 +257,4 @@ declare const initFormBuilder: (options: FormBuilderOptions & {
248
257
  containerId: string;
249
258
  }) => FormBuilder;
250
259
 
251
- export { AsyncOptionSource, FieldType, FieldWidth, FormBuilder, FormBuilderOptions, FormField, FormRenderer, FormSchema, FormSchemaValidation, FormSection, MasterType, ValidationRule, builderToPlatform, formStore, initFormBuilder, platformToBuilder };
260
+ export { AsyncOptionSource, FieldType, FieldWidth, FormBuilder, FormBuilderOptions, FormField, FormRenderer, FormSchema, FormSchemaValidation, FormSection, MasterType, ValidationRule, builderToPlatform, cleanFormSchema, formStore, initFormBuilder, platformToBuilder };
package/dist/index.d.ts CHANGED
@@ -231,6 +231,15 @@ declare class FormRenderer {
231
231
  private render;
232
232
  }
233
233
 
234
+ /**
235
+ * Cleans a form schema by removing invalid properties and normalizing field types
236
+ * - Converts "decimal" type to "number"
237
+ * - Removes invalid properties like "masterTypeName"
238
+ * - Removes empty options arrays from non-select/radio fields
239
+ * @param schema
240
+ * @returns Cleaned schema
241
+ */
242
+ declare const cleanFormSchema: (schema: any) => FormSchema;
234
243
  /**
235
244
  * Transforms Form Builder JSON to Platform JSON
236
245
  * @param builderSchema
@@ -248,4 +257,4 @@ declare const initFormBuilder: (options: FormBuilderOptions & {
248
257
  containerId: string;
249
258
  }) => FormBuilder;
250
259
 
251
- export { AsyncOptionSource, FieldType, FieldWidth, FormBuilder, FormBuilderOptions, FormField, FormRenderer, FormSchema, FormSchemaValidation, FormSection, MasterType, ValidationRule, builderToPlatform, formStore, initFormBuilder, platformToBuilder };
260
+ export { AsyncOptionSource, FieldType, FieldWidth, FormBuilder, FormBuilderOptions, FormField, FormRenderer, FormSchema, FormSchemaValidation, FormSection, MasterType, ValidationRule, builderToPlatform, cleanFormSchema, formStore, initFormBuilder, platformToBuilder };
package/dist/index.js CHANGED
@@ -4152,6 +4152,64 @@ var cloneField = (field) => {
4152
4152
  };
4153
4153
  };
4154
4154
 
4155
+ // src/utils/mapper.ts
4156
+ var cleanFormSchema = (schema) => {
4157
+ const cleanField = (field) => {
4158
+ const cleaned = {
4159
+ id: field.id,
4160
+ type: field.type === "decimal" ? "number" : field.type,
4161
+ // Convert decimal to number
4162
+ label: field.label,
4163
+ width: field.width
4164
+ };
4165
+ if (field.placeholder !== void 0)
4166
+ cleaned.placeholder = field.placeholder;
4167
+ if (field.description !== void 0)
4168
+ cleaned.description = field.description;
4169
+ if (field.required !== void 0)
4170
+ cleaned.required = field.required;
4171
+ if (field.defaultValue !== void 0)
4172
+ cleaned.defaultValue = field.defaultValue;
4173
+ if (field.validation !== void 0)
4174
+ cleaned.validation = field.validation;
4175
+ if (field.hidden !== void 0)
4176
+ cleaned.hidden = field.hidden;
4177
+ if (field.position !== void 0)
4178
+ cleaned.position = field.position;
4179
+ if (field.enabled !== void 0)
4180
+ cleaned.enabled = field.enabled;
4181
+ if (field.visible !== void 0)
4182
+ cleaned.visible = field.visible;
4183
+ if (field.optionsSource !== void 0)
4184
+ cleaned.optionsSource = field.optionsSource;
4185
+ if ((field.type === "select" || field.type === "radio") && field.options) {
4186
+ cleaned.options = field.options;
4187
+ }
4188
+ if (field.type === "select" && field.groupName) {
4189
+ cleaned.groupName = field.groupName;
4190
+ }
4191
+ return cleaned;
4192
+ };
4193
+ return {
4194
+ id: schema.id,
4195
+ title: schema.title,
4196
+ formName: schema.formName,
4197
+ sections: schema.sections.map((section) => ({
4198
+ id: section.id,
4199
+ title: section.title,
4200
+ fields: section.fields.map(cleanField),
4201
+ isExpanded: section.isExpanded,
4202
+ columns: section.columns
4203
+ }))
4204
+ };
4205
+ };
4206
+ var builderToPlatform = (builderSchema) => {
4207
+ return builderSchema;
4208
+ };
4209
+ var platformToBuilder = (platformSchema) => {
4210
+ return cleanFormSchema(platformSchema);
4211
+ };
4212
+
4155
4213
  // src/core/useFormStore.ts
4156
4214
  var INITIAL_SCHEMA = {
4157
4215
  id: "form_1",
@@ -4170,6 +4228,7 @@ var formStore = createStore((set, get) => ({
4170
4228
  masterTypes: [],
4171
4229
  dropdownOptionsMap: {},
4172
4230
  setSchema: (schema) => {
4231
+ const cleanedSchema = cleanFormSchema(schema);
4173
4232
  const convertIndexesToOptions = (indexes) => {
4174
4233
  if (!indexes || !Array.isArray(indexes) || indexes.length === 0) {
4175
4234
  return [];
@@ -4194,8 +4253,8 @@ var formStore = createStore((set, get) => ({
4194
4253
  );
4195
4254
  };
4196
4255
  const state = get();
4197
- if (state.masterTypes && state.masterTypes.length > 0 && schema.sections) {
4198
- const updatedSections = schema.sections.map((section) => ({
4256
+ if (state.masterTypes && state.masterTypes.length > 0 && cleanedSchema.sections) {
4257
+ const updatedSections = cleanedSchema.sections.map((section) => ({
4199
4258
  ...section,
4200
4259
  fields: section.fields.map((field) => {
4201
4260
  if (field.type === "select" && field.groupName) {
@@ -4228,9 +4287,9 @@ var formStore = createStore((set, get) => ({
4228
4287
  return field;
4229
4288
  })
4230
4289
  }));
4231
- set({ schema: { ...schema, sections: updatedSections } });
4290
+ set({ schema: { ...cleanedSchema, sections: updatedSections } });
4232
4291
  } else {
4233
- set({ schema });
4292
+ set({ schema: cleanedSchema });
4234
4293
  }
4235
4294
  },
4236
4295
  togglePreview: () => {
@@ -7994,8 +8053,6 @@ var FormBuilder = class {
7994
8053
  }
7995
8054
  }
7996
8055
  }
7997
- const validationHeader = createElement("h3", { className: "text-xs font-semibold text-gray-500 uppercase tracking-wider mb-3 mt-6", text: "Validation Rules" });
7998
- body.appendChild(validationHeader);
7999
8056
  const validations = selectedField.validation || [];
8000
8057
  const updateValidation = (rule) => {
8001
8058
  const newValidations = validations.filter((v) => v.type !== rule.type);
@@ -8005,6 +8062,7 @@ var FormBuilder = class {
8005
8062
  formStore.getState().updateField(selectedField.id, { validation: newValidations });
8006
8063
  };
8007
8064
  const getRuleValue = (type) => validations.find((v) => v.type === type)?.value || "";
8065
+ const validationElements = [];
8008
8066
  if (["text", "textarea", "email", "password"].includes(selectedField.type)) {
8009
8067
  const minLenGroup = createElement("div", { className: "mb-3" });
8010
8068
  minLenGroup.appendChild(createElement("label", { className: "block text-sm font-medium text-gray-700 dark:text-gray-300 mb-1", text: "Min Length" }));
@@ -8015,7 +8073,7 @@ var FormBuilder = class {
8015
8073
  placeholder: "e.g. 3",
8016
8074
  onchange: (e) => updateValidation({ type: "minLength", value: parseInt(e.target.value) })
8017
8075
  }));
8018
- body.appendChild(minLenGroup);
8076
+ validationElements.push(minLenGroup);
8019
8077
  const maxLenGroup = createElement("div", { className: "mb-3" });
8020
8078
  maxLenGroup.appendChild(createElement("label", { className: "block text-sm font-medium text-gray-700 dark:text-gray-300 mb-1", text: "Max Length" }));
8021
8079
  maxLenGroup.appendChild(createElement("input", {
@@ -8025,7 +8083,7 @@ var FormBuilder = class {
8025
8083
  placeholder: "e.g. 100",
8026
8084
  onchange: (e) => updateValidation({ type: "maxLength", value: parseInt(e.target.value) })
8027
8085
  }));
8028
- body.appendChild(maxLenGroup);
8086
+ validationElements.push(maxLenGroup);
8029
8087
  const regexGroup = createElement("div", { className: "mb-3" });
8030
8088
  regexGroup.appendChild(createElement("label", { className: "block text-sm font-medium text-gray-700 dark:text-gray-300 mb-1", text: "Regex Pattern" }));
8031
8089
  regexGroup.appendChild(createElement("input", {
@@ -8043,7 +8101,7 @@ var FormBuilder = class {
8043
8101
  formStore.getState().updateField(selectedField.id, { validation: newValidations });
8044
8102
  }
8045
8103
  }));
8046
- body.appendChild(regexGroup);
8104
+ validationElements.push(regexGroup);
8047
8105
  }
8048
8106
  if (selectedField.type === "number") {
8049
8107
  const minValGroup = createElement("div", { className: "mb-3" });
@@ -8054,7 +8112,7 @@ var FormBuilder = class {
8054
8112
  value: getRuleValue("min"),
8055
8113
  onchange: (e) => updateValidation({ type: "min", value: parseInt(e.target.value) })
8056
8114
  }));
8057
- body.appendChild(minValGroup);
8115
+ validationElements.push(minValGroup);
8058
8116
  const maxValGroup = createElement("div", { className: "mb-3" });
8059
8117
  maxValGroup.appendChild(createElement("label", { className: "block text-sm font-medium text-gray-700 dark:text-gray-300 mb-1", text: "Max Value" }));
8060
8118
  maxValGroup.appendChild(createElement("input", {
@@ -8063,59 +8121,12 @@ var FormBuilder = class {
8063
8121
  value: getRuleValue("max"),
8064
8122
  onchange: (e) => updateValidation({ type: "max", value: parseInt(e.target.value) })
8065
8123
  }));
8066
- body.appendChild(maxValGroup);
8067
- }
8068
- if (["select", "radio"].includes(selectedField.type)) {
8069
- const optionsHeader = createElement("h3", { className: "text-xs font-semibold text-gray-500 uppercase tracking-wider mb-3 mt-6", text: "Options Source" });
8070
- body.appendChild(optionsHeader);
8071
- const sourceGroup = createElement("div", { className: "mb-4" });
8072
- sourceGroup.appendChild(createElement("label", { className: "block text-sm font-medium text-gray-700 dark:text-gray-300 mb-1", text: "Source Type" }));
8073
- const sourceSelect = createElement("select", {
8074
- className: "w-full px-3 py-2 border border-gray-300 dark:border-gray-700 rounded-md bg-transparent",
8075
- onchange: (e) => {
8076
- const isAsync2 = e.target.value === "api";
8077
- if (isAsync2) {
8078
- formStore.getState().updateField(selectedField.id, {
8079
- optionsSource: { api: "https://", method: "GET", labelKey: "name", valueKey: "id" }
8080
- });
8081
- } else {
8082
- formStore.getState().updateField(selectedField.id, { optionsSource: void 0 });
8083
- }
8084
- this.render();
8085
- }
8086
- });
8087
- sourceSelect.appendChild(createElement("option", { value: "static", text: "Static Options", selected: !selectedField.optionsSource }));
8088
- sourceSelect.appendChild(createElement("option", { value: "api", text: "API Endpoint", selected: !!selectedField.optionsSource }));
8089
- sourceGroup.appendChild(sourceSelect);
8090
- body.appendChild(sourceGroup);
8091
- if (selectedField.optionsSource) {
8092
- const apiGroup = createElement("div", { className: "mb-3" });
8093
- apiGroup.appendChild(createElement("label", { className: "block text-sm font-medium text-gray-700 dark:text-gray-300 mb-1", text: "API URL" }));
8094
- apiGroup.appendChild(createElement("input", {
8095
- className: "w-full px-3 py-2 border border-gray-300 dark:border-gray-700 rounded-md bg-transparent",
8096
- value: selectedField.optionsSource.api,
8097
- oninput: (e) => formStore.getState().updateField(selectedField.id, { optionsSource: { ...selectedField.optionsSource, api: e.target.value } })
8098
- }));
8099
- body.appendChild(apiGroup);
8100
- const keysRow = createElement("div", { className: "flex gap-2 mb-3" });
8101
- const labelKeyGroup = createElement("div", { className: "flex-1" });
8102
- labelKeyGroup.appendChild(createElement("label", { className: "block text-xs font-medium text-gray-500 mb-1", text: "Label Key" }));
8103
- labelKeyGroup.appendChild(createElement("input", {
8104
- className: "w-full px-2 py-1 border border-gray-300 dark:border-gray-700 rounded-md bg-transparent text-sm",
8105
- value: selectedField.optionsSource.labelKey,
8106
- oninput: (e) => formStore.getState().updateField(selectedField.id, { optionsSource: { ...selectedField.optionsSource, labelKey: e.target.value } })
8107
- }));
8108
- const valueKeyGroup = createElement("div", { className: "flex-1" });
8109
- valueKeyGroup.appendChild(createElement("label", { className: "block text-xs font-medium text-gray-500 mb-1", text: "Value Key" }));
8110
- valueKeyGroup.appendChild(createElement("input", {
8111
- className: "w-full px-2 py-1 border border-gray-300 dark:border-gray-700 rounded-md bg-transparent text-sm",
8112
- value: selectedField.optionsSource.valueKey,
8113
- oninput: (e) => formStore.getState().updateField(selectedField.id, { optionsSource: { ...selectedField.optionsSource, valueKey: e.target.value } })
8114
- }));
8115
- keysRow.appendChild(labelKeyGroup);
8116
- keysRow.appendChild(valueKeyGroup);
8117
- body.appendChild(keysRow);
8118
- }
8124
+ validationElements.push(maxValGroup);
8125
+ }
8126
+ if (validationElements.length > 0) {
8127
+ const validationHeader = createElement("h3", { className: "text-xs font-semibold text-gray-500 uppercase tracking-wider mb-3 mt-6", text: "Validation Rules" });
8128
+ body.appendChild(validationHeader);
8129
+ validationElements.forEach((el) => body.appendChild(el));
8119
8130
  }
8120
8131
  panel.appendChild(body);
8121
8132
  return panel;
@@ -8151,14 +8162,6 @@ var FormBuilder = class {
8151
8162
  }
8152
8163
  };
8153
8164
 
8154
- // src/utils/mapper.ts
8155
- var builderToPlatform = (builderSchema) => {
8156
- return builderSchema;
8157
- };
8158
- var platformToBuilder = (platformSchema) => {
8159
- return platformSchema;
8160
- };
8161
-
8162
8165
  // src/index.ts
8163
8166
  var initFormBuilder = (options) => {
8164
8167
  const container = document.getElementById(options.containerId);
@@ -8182,6 +8185,7 @@ exports.FormBuilder = FormBuilder;
8182
8185
  exports.FormRenderer = FormRenderer;
8183
8186
  exports.FormSchemaValidation = FormSchemaValidation;
8184
8187
  exports.builderToPlatform = builderToPlatform;
8188
+ exports.cleanFormSchema = cleanFormSchema;
8185
8189
  exports.formStore = formStore;
8186
8190
  exports.initFormBuilder = initFormBuilder;
8187
8191
  exports.platformToBuilder = platformToBuilder;