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

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.
@@ -9066,21 +9066,10 @@ var __publicField = (obj, key, value) => {
9066
9066
  const makeConditionalSourceFields = (sections, index2) => {
9067
9067
  return sections.filter((_, i) => i < index2).flatMap((field) => field.fields);
9068
9068
  };
9069
- const getTakenFieldLabels = (fields) => {
9070
- return fields.flatMap((field) => [...field.fields.map((f) => f.label), field.label]).filter((id) => id !== null);
9071
- };
9072
- const incrementFieldLabel = (label, takenLabels) => {
9073
- let count = 1;
9074
- let newLabel = `${label} (${count})`;
9075
- while (takenLabels.includes(newLabel)) {
9076
- newLabel = `${label} (${++count})`;
9077
- }
9078
- return newLabel;
9079
- };
9080
- const createNewField = (parentPath, index2, initialValues2, values, setFieldValue) => {
9081
- const { label } = initialValues2;
9069
+ const createNewField = (parentPath, index2, initialValues, values, setFieldValue) => {
9070
+ const { label } = initialValues;
9082
9071
  const newField = {
9083
- ...initialValues2,
9072
+ ...initialValues,
9084
9073
  identifier: makeIdentifier(null, label)
9085
9074
  };
9086
9075
  const parent = get(values, parentPath);
@@ -9094,11 +9083,11 @@ var __publicField = (obj, key, value) => {
9094
9083
  void setFieldValue(parentPath, updatedFields).then();
9095
9084
  };
9096
9085
  const createNewEmptySection = (index2, values, setFieldValue) => {
9097
- const initialValues2 = {
9086
+ const initialValues = {
9098
9087
  ...emptySection(),
9099
9088
  label: ""
9100
9089
  };
9101
- createNewField("fields", index2, initialValues2, values, setFieldValue);
9090
+ createNewField("fields", index2, initialValues, values, setFieldValue);
9102
9091
  };
9103
9092
  const useFieldReordering = () => {
9104
9093
  const { showError } = blocks.useToast();
@@ -10586,19 +10575,19 @@ var __publicField = (obj, key, value) => {
10586
10575
  const PatchFormProvider = React.memo(
10587
10576
  React.forwardRef((props, ref) => {
10588
10577
  const { children, schema, values, onPatch, onError, requiresDiff = true, onDirtyChange, ...rest } = props;
10589
- const initialValues2 = React.useMemo(() => initialFormValues(schema.fields, values), [schema.fields, values]);
10578
+ const initialValues = React.useMemo(() => initialFormValues(schema.fields, values), [schema.fields, values]);
10590
10579
  const getDiff = React.useCallback(
10591
10580
  (values2) => {
10592
10581
  const diff = {};
10593
10582
  for (const key in values2) {
10594
10583
  const value = values2[key];
10595
- if (value !== initialValues2[key] && value !== void 0) {
10584
+ if (value !== initialValues[key] && value !== void 0) {
10596
10585
  diff[key] = value;
10597
10586
  }
10598
10587
  }
10599
10588
  return diff;
10600
10589
  },
10601
- [initialValues2]
10590
+ [initialValues]
10602
10591
  );
10603
10592
  const handlePatch = React.useCallback(
10604
10593
  (values2) => {
@@ -10624,7 +10613,7 @@ var __publicField = (obj, key, value) => {
10624
10613
  [schema, onDirtyChange, onError, getDiff]
10625
10614
  );
10626
10615
  const formik$1 = formik.useFormik({
10627
- initialValues: initialValues2,
10616
+ initialValues,
10628
10617
  onSubmit: handlePatch,
10629
10618
  validate,
10630
10619
  // only validate the entire form on submit
@@ -10642,9 +10631,9 @@ var __publicField = (obj, key, value) => {
10642
10631
  const { errors, resetForm } = formik$1;
10643
10632
  React.useEffect(() => {
10644
10633
  if (hasKeys(errors)) {
10645
- resetForm({ values: initialValues2, errors: {} });
10634
+ resetForm({ values: initialValues, errors: {} });
10646
10635
  }
10647
- }, [errors, initialValues2, resetForm]);
10636
+ }, [errors, initialValues, resetForm]);
10648
10637
  return /* @__PURE__ */ jsxRuntime.jsx(formik.FormikProvider, { value: formik$1, children: /* @__PURE__ */ jsxRuntime.jsx("form", { ...rest, ref, onSubmit: formik$1.handleSubmit, onChange: handleChange, children }) });
10649
10638
  })
10650
10639
  );
@@ -11093,7 +11082,7 @@ var __publicField = (obj, key, value) => {
11093
11082
  });
11094
11083
  FieldBuilder.displayName = "FieldBuilder";
11095
11084
  const FieldWithActions = React.memo((props) => {
11096
- const { field, index: index2, sectionIndex, takenLabels, remove: remove2 } = props;
11085
+ const { field, index: index2, sectionIndex, remove: remove2 } = props;
11097
11086
  const { setFieldValue, values } = formik.useFormikContext();
11098
11087
  const { reorderField } = useFieldReordering();
11099
11088
  const { showError } = blocks.useToast();
@@ -11110,10 +11099,10 @@ var __publicField = (obj, key, value) => {
11110
11099
  const label = field.label ?? "Unlabelled field";
11111
11100
  const duplicatedField = {
11112
11101
  ...field,
11113
- label: incrementFieldLabel(label, takenLabels)
11102
+ label
11114
11103
  };
11115
11104
  createNewField(parentPath, index2 + 1, duplicatedField, values, setFieldValue);
11116
- }, [field, takenLabels, parentPath, index2, values, setFieldValue]);
11105
+ }, [field, parentPath, index2, values, setFieldValue]);
11117
11106
  const moveField = React.useCallback(
11118
11107
  (direction) => {
11119
11108
  const srcSectionIndex = sectionIndex;
@@ -11200,7 +11189,6 @@ var __publicField = (obj, key, value) => {
11200
11189
  const { setFieldValue, values } = formik.useFormikContext();
11201
11190
  const alertDialog = blocks.useAlertDialog();
11202
11191
  const { reorderSection } = useFieldReordering();
11203
- const takenFieldLabels = getTakenFieldLabels(values.fields);
11204
11192
  const removeSectionConditions = React.useCallback(
11205
11193
  (sectionsToUpdate, allSections) => {
11206
11194
  for (const section of sectionsToUpdate) {
@@ -11304,18 +11292,15 @@ var __publicField = (obj, key, value) => {
11304
11292
  );
11305
11293
  const duplicateSection = React.useCallback(() => {
11306
11294
  const fieldLabel = field.label ?? "Untitled section";
11307
- const newSectionLabel = incrementFieldLabel(fieldLabel, takenFieldLabels);
11308
- const newFields = field.fields.map((f) => {
11309
- const newLabel = incrementFieldLabel(f.label, takenFieldLabels);
11295
+ const newFields = field.fields.map((oldField) => {
11310
11296
  return {
11311
- ...f,
11312
- label: newLabel,
11313
- identifier: makeIdentifier(null, newLabel)
11297
+ ...oldField,
11298
+ identifier: makeIdentifier(null, oldField.label)
11314
11299
  };
11315
11300
  });
11316
- const duplicatedField = { ...field, label: newSectionLabel, fields: newFields };
11301
+ const duplicatedField = { ...field, label: fieldLabel, fields: newFields };
11317
11302
  createNewField("fields", sectionIndex + 1, duplicatedField, values, setFieldValue);
11318
- }, [field, takenFieldLabels, sectionIndex, values, setFieldValue]);
11303
+ }, [field, sectionIndex, values, setFieldValue]);
11319
11304
  const handleCreateField = React.useCallback(
11320
11305
  (type) => {
11321
11306
  createNewField(
@@ -11362,8 +11347,7 @@ var __publicField = (obj, key, value) => {
11362
11347
  sectionIndex,
11363
11348
  remove: () => {
11364
11349
  removeField(i);
11365
- },
11366
- takenLabels: takenFieldLabels
11350
+ }
11367
11351
  },
11368
11352
  child.identifier
11369
11353
  )),
@@ -11538,17 +11522,12 @@ var __publicField = (obj, key, value) => {
11538
11522
  ) }) });
11539
11523
  });
11540
11524
  FieldsEditor.displayName = "FieldsEditor";
11541
- const initialValues = {
11542
- title: "",
11543
- description: "",
11544
- fields: [{ ...emptySection(makeIdentifier(null, "")), label: "" }]
11545
- };
11546
11525
  const previewSubmit = () => {
11547
11526
  alert("This is a form preview, your data will not be saved.");
11548
11527
  };
11549
11528
  const FormBuilder = React.memo(
11550
11529
  React.forwardRef((props, ref) => {
11551
- const { onCancel, onSave, revision } = props;
11530
+ const { onCancel, onSave, revision, initialTitle, showExplainerText = true, showSectionsOnly = false } = props;
11552
11531
  const { showError } = blocks.useToast();
11553
11532
  const validate = React.useCallback(
11554
11533
  (form) => {
@@ -11594,6 +11573,14 @@ var __publicField = (obj, key, value) => {
11594
11573
  },
11595
11574
  [showError]
11596
11575
  );
11576
+ const initialValues = React.useMemo(
11577
+ () => ({
11578
+ title: initialTitle ?? "",
11579
+ description: "",
11580
+ fields: [{ ...emptySection(makeIdentifier(null, "")), label: "" }]
11581
+ }),
11582
+ [initialTitle]
11583
+ );
11597
11584
  const formik$1 = formik.useFormik({
11598
11585
  initialValues: wrapRootFieldsWithFieldSection(revision) ?? initialValues,
11599
11586
  validate,
@@ -11614,7 +11601,7 @@ var __publicField = (obj, key, value) => {
11614
11601
  ] }) })
11615
11602
  ] }),
11616
11603
  /* @__PURE__ */ jsxRuntime.jsxs(Tabs.Content, { value: "edit", children: [
11617
- /* @__PURE__ */ jsxRuntime.jsxs(blocks.Text, { children: [
11604
+ showExplainerText && /* @__PURE__ */ jsxRuntime.jsxs(blocks.Text, { children: [
11618
11605
  "Create your form using various field types. Sections can be",
11619
11606
  " ",
11620
11607
  /* @__PURE__ */ jsxRuntime.jsx("strong", { children: "conditionally rendered" }),
@@ -11624,55 +11611,57 @@ var __publicField = (obj, key, value) => {
11624
11611
  ] }),
11625
11612
  /* @__PURE__ */ jsxRuntime.jsx(blocks.Flex, { asChild: true, direction: "column", gap: "2", mt: "3", children: /* @__PURE__ */ jsxRuntime.jsxs("form", { id: formId, onSubmit: formik$1.handleSubmit, children: [
11626
11613
  /* @__PURE__ */ jsxRuntime.jsxs(formik.FormikProvider, { value: formik$1, children: [
11627
- /* @__PURE__ */ jsxRuntime.jsx(
11628
- PatchField,
11629
- {
11630
- name: "title",
11631
- render: ({ setValue, value, meta }) => /* @__PURE__ */ jsxRuntime.jsx(InputWithHelpText, { severity: "danger", helpText: meta.error ?? null, children: /* @__PURE__ */ jsxRuntime.jsx(
11632
- blocks.Input,
11633
- {
11634
- className: classNames$1(styles$7.title, {
11635
- [styles$7.error]: meta.error
11636
- }),
11637
- placeholder: "Form title",
11638
- value,
11639
- onChange: (event) => {
11640
- setValue(event.target.value);
11641
- },
11642
- maxLength: 100,
11643
- showInputLength: false,
11644
- variant: "ghost",
11645
- size: "large"
11646
- }
11647
- ) })
11648
- }
11649
- ),
11650
- /* @__PURE__ */ jsxRuntime.jsx(
11651
- PatchField,
11652
- {
11653
- name: "description",
11654
- render: ({ setValue, value }) => /* @__PURE__ */ jsxRuntime.jsx(
11655
- blocks.TextArea,
11656
- {
11657
- className: styles$7.description,
11658
- placeholder: "Explain the purpose of this form",
11659
- value,
11660
- onChange: (event) => {
11661
- setValue(event.target.value);
11662
- },
11663
- resize: "vertical",
11664
- maxLength: 1e3,
11665
- showInputLength: false,
11666
- variant: "ghost"
11667
- }
11668
- )
11669
- }
11670
- ),
11614
+ !showSectionsOnly && /* @__PURE__ */ jsxRuntime.jsxs(jsxRuntime.Fragment, { children: [
11615
+ /* @__PURE__ */ jsxRuntime.jsx(
11616
+ PatchField,
11617
+ {
11618
+ name: "title",
11619
+ render: ({ setValue, value, meta }) => /* @__PURE__ */ jsxRuntime.jsx(InputWithHelpText, { severity: "danger", helpText: meta.error ?? null, children: /* @__PURE__ */ jsxRuntime.jsx(
11620
+ blocks.Input,
11621
+ {
11622
+ className: classNames$1(styles$7.title, {
11623
+ [styles$7.error]: meta.error
11624
+ }),
11625
+ placeholder: "Form title",
11626
+ value,
11627
+ onChange: (event) => {
11628
+ setValue(event.target.value);
11629
+ },
11630
+ maxLength: 100,
11631
+ showInputLength: false,
11632
+ variant: "ghost",
11633
+ size: "large"
11634
+ }
11635
+ ) })
11636
+ }
11637
+ ),
11638
+ /* @__PURE__ */ jsxRuntime.jsx(
11639
+ PatchField,
11640
+ {
11641
+ name: "description",
11642
+ render: ({ setValue, value }) => /* @__PURE__ */ jsxRuntime.jsx(
11643
+ blocks.TextArea,
11644
+ {
11645
+ className: styles$7.description,
11646
+ placeholder: "Explain the purpose of this form",
11647
+ value,
11648
+ onChange: (event) => {
11649
+ setValue(event.target.value);
11650
+ },
11651
+ resize: "vertical",
11652
+ maxLength: 1e3,
11653
+ showInputLength: false,
11654
+ variant: "ghost"
11655
+ }
11656
+ )
11657
+ }
11658
+ )
11659
+ ] }),
11671
11660
  /* @__PURE__ */ jsxRuntime.jsx(FieldsEditor, {}),
11672
11661
  /* @__PURE__ */ jsxRuntime.jsx(blocks.Text, { severity: "danger", size: "1", children: typeof formik$1.errors.fields === "string" && formik$1.errors.fields })
11673
11662
  ] }),
11674
11663
  /* @__PURE__ */ jsxRuntime.jsxs(blocks.Flex, { className: styles$7.floatingButtonContainer, align: "center", justify: "end", gap: "2", children: [
11675
- /* @__PURE__ */ jsxRuntime.jsx(blocks.Button, { type: "button", variant: "solid", severity: "info", onClick: onCancel, children: "Cancel" }),
11664
+ onCancel && /* @__PURE__ */ jsxRuntime.jsx(blocks.Button, { type: "button", variant: "solid", severity: "info", onClick: onCancel, children: "Cancel" }),
11676
11665
  /* @__PURE__ */ jsxRuntime.jsx(blocks.Button, { type: "submit", children: "Save form" })
11677
11666
  ] })
11678
11667
  ] }) })