@overmap-ai/core 1.0.38-component-fields.1 → 1.0.38-component-fields.3

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.
@@ -4,7 +4,6 @@ interface FieldWithActionsProps {
4
4
  field: ISerializedField;
5
5
  index: number;
6
6
  sectionIndex: number;
7
- takenLabels: string[];
8
7
  remove: () => void;
9
8
  }
10
9
  export declare const FieldWithActions: import("react").MemoExoticComponent<(props: FieldWithActionsProps) => import("react/jsx-runtime").JSX.Element>;
@@ -6,6 +6,11 @@ interface FormBuilderProps {
6
6
  onSave: FormBuilderSaveHandler;
7
7
  /** A revision of an existing form to edit. To create a new form, pass `undefined`. */
8
8
  revision?: UserFormRevision;
9
+ initialTitle?: string;
10
+ /** @default true */
11
+ showExplainerText?: boolean;
12
+ /** @default false */
13
+ showSectionsOnly?: boolean;
9
14
  }
10
15
  export declare const FormBuilder: import("react").MemoExoticComponent<import("react").ForwardRefExoticComponent<FormBuilderProps & import("react").RefAttributes<HTMLDivElement>>>;
11
16
  export {};
@@ -13,8 +13,6 @@ export declare function remove<T>(list: T[], index: number): T[];
13
13
  export declare const makeIdentifier: (existing: unknown, label: string) => string;
14
14
  export declare const findFieldByIdentifier: (fields: ISerializedField[], identifier?: string) => ISerializedField | null;
15
15
  export declare const makeConditionalSourceFields: (sections: SerializedFieldSection[], index: number) => (import("../typings").SerializedTextField | import("../typings").SerializedBooleanField | import("../typings").SerializedNumberField | import("../typings").SerializedDateField | import("../typings").SerializedStringField | import("../typings").SerializedSelectField | import("../typings").SerializedMultiStringField | import("../typings").SerializedMultiSelectField | import("../typings").SerializedUploadField)[];
16
- export declare const getTakenFieldLabels: (fields: SerializedFieldSection[]) => string[];
17
- export declare const incrementFieldLabel: (label: string, takenLabels: string[]) => string;
18
16
  export type NewFieldInitialValues = Omit<ISerializedField, "identifier"> & {
19
17
  label: string;
20
18
  };
@@ -5440,6 +5440,7 @@ class UserFormService extends BaseApiService {
5440
5440
  const activeWorkspaceId = state.workspaceReducer.activeWorkspaceId;
5441
5441
  const offlineFormPayload = offline({});
5442
5442
  const offlineRevisionPayload = offline(initialRevision);
5443
+ console.log(componentType);
5443
5444
  const retForm = {
5444
5445
  ...offlineFormPayload,
5445
5446
  index_workspace: activeWorkspaceId,
@@ -5449,6 +5450,7 @@ class UserFormService extends BaseApiService {
5449
5450
  ...componentType && { component_type: componentType },
5450
5451
  ...ownerAttrs
5451
5452
  };
5453
+ console.log(retForm);
5452
5454
  const { payloadWithoutImage, images } = await separateImageFromFields(offlineRevisionPayload);
5453
5455
  const retRevision = {
5454
5456
  ...payloadWithoutImage,
@@ -9070,21 +9072,10 @@ const findFieldByIdentifier = (fields, identifier) => {
9070
9072
  const makeConditionalSourceFields = (sections, index2) => {
9071
9073
  return sections.filter((_, i) => i < index2).flatMap((field) => field.fields);
9072
9074
  };
9073
- const getTakenFieldLabels = (fields) => {
9074
- return fields.flatMap((field) => [...field.fields.map((f) => f.label), field.label]).filter((id) => id !== null);
9075
- };
9076
- const incrementFieldLabel = (label, takenLabels) => {
9077
- let count = 1;
9078
- let newLabel = `${label} (${count})`;
9079
- while (takenLabels.includes(newLabel)) {
9080
- newLabel = `${label} (${++count})`;
9081
- }
9082
- return newLabel;
9083
- };
9084
- const createNewField = (parentPath, index2, initialValues2, values, setFieldValue) => {
9085
- const { label } = initialValues2;
9075
+ const createNewField = (parentPath, index2, initialValues, values, setFieldValue) => {
9076
+ const { label } = initialValues;
9086
9077
  const newField = {
9087
- ...initialValues2,
9078
+ ...initialValues,
9088
9079
  identifier: makeIdentifier(null, label)
9089
9080
  };
9090
9081
  const parent = get(values, parentPath);
@@ -9098,11 +9089,11 @@ const createNewField = (parentPath, index2, initialValues2, values, setFieldValu
9098
9089
  void setFieldValue(parentPath, updatedFields).then();
9099
9090
  };
9100
9091
  const createNewEmptySection = (index2, values, setFieldValue) => {
9101
- const initialValues2 = {
9092
+ const initialValues = {
9102
9093
  ...emptySection(),
9103
9094
  label: ""
9104
9095
  };
9105
- createNewField("fields", index2, initialValues2, values, setFieldValue);
9096
+ createNewField("fields", index2, initialValues, values, setFieldValue);
9106
9097
  };
9107
9098
  const useFieldReordering = () => {
9108
9099
  const { showError } = useToast();
@@ -10590,19 +10581,19 @@ PatchField.displayName = "PatchField";
10590
10581
  const PatchFormProvider = memo(
10591
10582
  forwardRef((props, ref) => {
10592
10583
  const { children, schema, values, onPatch, onError, requiresDiff = true, onDirtyChange, ...rest } = props;
10593
- const initialValues2 = useMemo(() => initialFormValues(schema.fields, values), [schema.fields, values]);
10584
+ const initialValues = useMemo(() => initialFormValues(schema.fields, values), [schema.fields, values]);
10594
10585
  const getDiff = useCallback(
10595
10586
  (values2) => {
10596
10587
  const diff = {};
10597
10588
  for (const key in values2) {
10598
10589
  const value = values2[key];
10599
- if (value !== initialValues2[key] && value !== void 0) {
10590
+ if (value !== initialValues[key] && value !== void 0) {
10600
10591
  diff[key] = value;
10601
10592
  }
10602
10593
  }
10603
10594
  return diff;
10604
10595
  },
10605
- [initialValues2]
10596
+ [initialValues]
10606
10597
  );
10607
10598
  const handlePatch = useCallback(
10608
10599
  (values2) => {
@@ -10628,7 +10619,7 @@ const PatchFormProvider = memo(
10628
10619
  [schema, onDirtyChange, onError, getDiff]
10629
10620
  );
10630
10621
  const formik = useFormik({
10631
- initialValues: initialValues2,
10622
+ initialValues,
10632
10623
  onSubmit: handlePatch,
10633
10624
  validate,
10634
10625
  // only validate the entire form on submit
@@ -10646,9 +10637,9 @@ const PatchFormProvider = memo(
10646
10637
  const { errors, resetForm } = formik;
10647
10638
  useEffect(() => {
10648
10639
  if (hasKeys(errors)) {
10649
- resetForm({ values: initialValues2, errors: {} });
10640
+ resetForm({ values: initialValues, errors: {} });
10650
10641
  }
10651
- }, [errors, initialValues2, resetForm]);
10642
+ }, [errors, initialValues, resetForm]);
10652
10643
  return /* @__PURE__ */ jsx(FormikProvider, { value: formik, children: /* @__PURE__ */ jsx("form", { ...rest, ref, onSubmit: formik.handleSubmit, onChange: handleChange, children }) });
10653
10644
  })
10654
10645
  );
@@ -11097,7 +11088,7 @@ const FieldBuilder = memo((props) => {
11097
11088
  });
11098
11089
  FieldBuilder.displayName = "FieldBuilder";
11099
11090
  const FieldWithActions = memo((props) => {
11100
- const { field, index: index2, sectionIndex, takenLabels, remove: remove2 } = props;
11091
+ const { field, index: index2, sectionIndex, remove: remove2 } = props;
11101
11092
  const { setFieldValue, values } = useFormikContext();
11102
11093
  const { reorderField } = useFieldReordering();
11103
11094
  const { showError } = useToast();
@@ -11114,10 +11105,10 @@ const FieldWithActions = memo((props) => {
11114
11105
  const label = field.label ?? "Unlabelled field";
11115
11106
  const duplicatedField = {
11116
11107
  ...field,
11117
- label: incrementFieldLabel(label, takenLabels)
11108
+ label
11118
11109
  };
11119
11110
  createNewField(parentPath, index2 + 1, duplicatedField, values, setFieldValue);
11120
- }, [field, takenLabels, parentPath, index2, values, setFieldValue]);
11111
+ }, [field, parentPath, index2, values, setFieldValue]);
11121
11112
  const moveField = useCallback(
11122
11113
  (direction) => {
11123
11114
  const srcSectionIndex = sectionIndex;
@@ -11204,7 +11195,6 @@ const FieldSectionWithActions = memo((props) => {
11204
11195
  const { setFieldValue, values } = useFormikContext();
11205
11196
  const alertDialog = useAlertDialog();
11206
11197
  const { reorderSection } = useFieldReordering();
11207
- const takenFieldLabels = getTakenFieldLabels(values.fields);
11208
11198
  const removeSectionConditions = useCallback(
11209
11199
  (sectionsToUpdate, allSections) => {
11210
11200
  for (const section of sectionsToUpdate) {
@@ -11308,18 +11298,15 @@ const FieldSectionWithActions = memo((props) => {
11308
11298
  );
11309
11299
  const duplicateSection = useCallback(() => {
11310
11300
  const fieldLabel = field.label ?? "Untitled section";
11311
- const newSectionLabel = incrementFieldLabel(fieldLabel, takenFieldLabels);
11312
- const newFields = field.fields.map((f) => {
11313
- const newLabel = incrementFieldLabel(f.label, takenFieldLabels);
11301
+ const newFields = field.fields.map((oldField) => {
11314
11302
  return {
11315
- ...f,
11316
- label: newLabel,
11317
- identifier: makeIdentifier(null, newLabel)
11303
+ ...oldField,
11304
+ identifier: makeIdentifier(null, oldField.label)
11318
11305
  };
11319
11306
  });
11320
- const duplicatedField = { ...field, label: newSectionLabel, fields: newFields };
11307
+ const duplicatedField = { ...field, label: fieldLabel, fields: newFields };
11321
11308
  createNewField("fields", sectionIndex + 1, duplicatedField, values, setFieldValue);
11322
- }, [field, takenFieldLabels, sectionIndex, values, setFieldValue]);
11309
+ }, [field, sectionIndex, values, setFieldValue]);
11323
11310
  const handleCreateField = useCallback(
11324
11311
  (type) => {
11325
11312
  createNewField(
@@ -11366,8 +11353,7 @@ const FieldSectionWithActions = memo((props) => {
11366
11353
  sectionIndex,
11367
11354
  remove: () => {
11368
11355
  removeField(i);
11369
- },
11370
- takenLabels: takenFieldLabels
11356
+ }
11371
11357
  },
11372
11358
  child.identifier
11373
11359
  )),
@@ -11542,17 +11528,12 @@ const FieldsEditor = memo(() => {
11542
11528
  ) }) });
11543
11529
  });
11544
11530
  FieldsEditor.displayName = "FieldsEditor";
11545
- const initialValues = {
11546
- title: "",
11547
- description: "",
11548
- fields: [{ ...emptySection(makeIdentifier(null, "")), label: "" }]
11549
- };
11550
11531
  const previewSubmit = () => {
11551
11532
  alert("This is a form preview, your data will not be saved.");
11552
11533
  };
11553
11534
  const FormBuilder = memo(
11554
11535
  forwardRef((props, ref) => {
11555
- const { onCancel, onSave, revision } = props;
11536
+ const { onCancel, onSave, revision, initialTitle, showExplainerText = true, showSectionsOnly = false } = props;
11556
11537
  const { showError } = useToast();
11557
11538
  const validate = useCallback(
11558
11539
  (form) => {
@@ -11598,6 +11579,14 @@ const FormBuilder = memo(
11598
11579
  },
11599
11580
  [showError]
11600
11581
  );
11582
+ const initialValues = useMemo(
11583
+ () => ({
11584
+ title: initialTitle ?? "",
11585
+ description: "",
11586
+ fields: [{ ...emptySection(makeIdentifier(null, "")), label: "" }]
11587
+ }),
11588
+ [initialTitle]
11589
+ );
11601
11590
  const formik = useFormik({
11602
11591
  initialValues: wrapRootFieldsWithFieldSection(revision) ?? initialValues,
11603
11592
  validate,
@@ -11618,7 +11607,7 @@ const FormBuilder = memo(
11618
11607
  ] }) })
11619
11608
  ] }),
11620
11609
  /* @__PURE__ */ jsxs(Tabs.Content, { value: "edit", children: [
11621
- /* @__PURE__ */ jsxs(Text, { children: [
11610
+ showExplainerText && /* @__PURE__ */ jsxs(Text, { children: [
11622
11611
  "Create your form using various field types. Sections can be",
11623
11612
  " ",
11624
11613
  /* @__PURE__ */ jsx("strong", { children: "conditionally rendered" }),
@@ -11628,60 +11617,62 @@ const FormBuilder = memo(
11628
11617
  ] }),
11629
11618
  /* @__PURE__ */ jsx(Flex, { asChild: true, direction: "column", gap: "2", mt: "3", children: /* @__PURE__ */ jsxs("form", { id: formId, onSubmit: formik.handleSubmit, children: [
11630
11619
  /* @__PURE__ */ jsxs(FormikProvider, { value: formik, children: [
11631
- /* @__PURE__ */ jsx(
11632
- PatchField,
11633
- {
11634
- name: "title",
11635
- render: ({ setValue, value, meta }) => /* @__PURE__ */ jsx(InputWithHelpText, { severity: "danger", helpText: meta.error ?? null, children: /* @__PURE__ */ jsx(
11636
- Input,
11637
- {
11638
- className: classNames$1(styles$7.title, {
11639
- [styles$7.error]: meta.error
11640
- }),
11641
- placeholder: "Form title",
11642
- value,
11643
- onChange: (event) => {
11644
- setValue(event.target.value);
11645
- },
11646
- maxLength: 100,
11647
- showInputLength: false,
11648
- variant: "ghost",
11649
- size: "large"
11650
- }
11651
- ) })
11652
- }
11653
- ),
11654
- /* @__PURE__ */ jsx(
11655
- PatchField,
11656
- {
11657
- name: "description",
11658
- render: ({ setValue, value }) => /* @__PURE__ */ jsx(
11659
- TextArea,
11660
- {
11661
- className: styles$7.description,
11662
- placeholder: "Explain the purpose of this form",
11663
- value,
11664
- onChange: (event) => {
11665
- setValue(event.target.value);
11666
- },
11667
- resize: "vertical",
11668
- maxLength: 1e3,
11669
- showInputLength: false,
11670
- variant: "ghost"
11671
- }
11672
- )
11673
- }
11674
- ),
11620
+ !showSectionsOnly && /* @__PURE__ */ jsxs(Fragment, { children: [
11621
+ /* @__PURE__ */ jsx(
11622
+ PatchField,
11623
+ {
11624
+ name: "title",
11625
+ render: ({ setValue, value, meta }) => /* @__PURE__ */ jsx(InputWithHelpText, { severity: "danger", helpText: meta.error ?? null, children: /* @__PURE__ */ jsx(
11626
+ Input,
11627
+ {
11628
+ className: classNames$1(styles$7.title, {
11629
+ [styles$7.error]: meta.error
11630
+ }),
11631
+ placeholder: "Form title",
11632
+ value,
11633
+ onChange: (event) => {
11634
+ setValue(event.target.value);
11635
+ },
11636
+ maxLength: 100,
11637
+ showInputLength: false,
11638
+ variant: "ghost",
11639
+ size: "large"
11640
+ }
11641
+ ) })
11642
+ }
11643
+ ),
11644
+ /* @__PURE__ */ jsx(
11645
+ PatchField,
11646
+ {
11647
+ name: "description",
11648
+ render: ({ setValue, value }) => /* @__PURE__ */ jsx(
11649
+ TextArea,
11650
+ {
11651
+ className: styles$7.description,
11652
+ placeholder: "Explain the purpose of this form",
11653
+ value,
11654
+ onChange: (event) => {
11655
+ setValue(event.target.value);
11656
+ },
11657
+ resize: "vertical",
11658
+ maxLength: 1e3,
11659
+ showInputLength: false,
11660
+ variant: "ghost"
11661
+ }
11662
+ )
11663
+ }
11664
+ )
11665
+ ] }),
11675
11666
  /* @__PURE__ */ jsx(FieldsEditor, {}),
11676
11667
  /* @__PURE__ */ jsx(Text, { severity: "danger", size: "1", children: typeof formik.errors.fields === "string" && formik.errors.fields })
11677
11668
  ] }),
11678
11669
  /* @__PURE__ */ jsxs(Flex, { className: styles$7.floatingButtonContainer, align: "center", justify: "end", gap: "2", children: [
11679
- /* @__PURE__ */ jsx(Button, { type: "button", variant: "solid", severity: "info", onClick: onCancel, children: "Cancel" }),
11670
+ onCancel && /* @__PURE__ */ jsx(Button, { type: "button", variant: "solid", severity: "info", onClick: onCancel, children: "Cancel" }),
11680
11671
  /* @__PURE__ */ jsx(Button, { type: "submit", children: "Save form" })
11681
11672
  ] })
11682
11673
  ] }) })
11683
11674
  ] }),
11684
- /* @__PURE__ */ jsx(Tabs.Content, { value: "preview", children: /* @__PURE__ */ jsx(FormRenderer, { schema: previewSchema, onSubmit: previewSubmit }) })
11675
+ /* @__PURE__ */ jsx(Tabs.Content, { value: "preview", children: /* @__PURE__ */ jsx(FormRenderer, { schema: previewSchema, onSubmit: previewSubmit, hideTitle: showSectionsOnly }) })
11685
11676
  ] }) });
11686
11677
  })
11687
11678
  );