@overmap-ai/forms 1.0.6 → 1.0.9-handle-patchform-errors.0

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.
@@ -1498,6 +1498,7 @@ var __publicField = (obj, key, value) => {
1498
1498
  cancelText,
1499
1499
  onCancel,
1500
1500
  onDirty,
1501
+ onDirtyChange,
1501
1502
  // if the title isn't provided, hide it by default
1502
1503
  hideTitle = !schema.title,
1503
1504
  hideDescription,
@@ -1526,7 +1527,9 @@ var __publicField = (obj, key, value) => {
1526
1527
  React.useEffect(() => {
1527
1528
  if (dirty && onDirty)
1528
1529
  onDirty();
1529
- }, [dirty, onDirty]);
1530
+ if (onDirtyChange)
1531
+ onDirtyChange(dirty);
1532
+ }, [dirty, onDirty, onDirtyChange]);
1530
1533
  return /* @__PURE__ */ jsxRuntime.jsx(formik.FormikProvider, { value: formik$1, children: /* @__PURE__ */ jsxRuntime.jsx(blocks.Flex, { ref, direction: "column", gap: "2", className, asChild: true, children: /* @__PURE__ */ jsxRuntime.jsxs("form", { id: formId2, onSubmit: formik$1.handleSubmit, children: [
1531
1534
  !hideTitle && /* @__PURE__ */ jsxRuntime.jsx(blocks.Card, { children: /* @__PURE__ */ jsxRuntime.jsxs(blocks.Flex, { direction: "column", gap: "1", children: [
1532
1535
  Title,
@@ -1825,6 +1828,7 @@ var __publicField = (obj, key, value) => {
1825
1828
  const PatchFormProvider = React.memo(
1826
1829
  React.forwardRef((props, ref) => {
1827
1830
  const { children, schema, values, onPatch, onError, ...rest } = props;
1831
+ const { showError } = blocks.useToast();
1828
1832
  const initialValues2 = React.useMemo(() => initialFormValues(schema.fields, values), [schema.fields, values]);
1829
1833
  const handlePatch = React.useCallback(
1830
1834
  (values2) => {
@@ -1837,6 +1841,7 @@ var __publicField = (obj, key, value) => {
1837
1841
  }
1838
1842
  if (!hasKeys(diff))
1839
1843
  return;
1844
+ console.log("onpatch runs", diff);
1840
1845
  onPatch(diff);
1841
1846
  },
1842
1847
  [initialValues2, onPatch]
@@ -1862,9 +1867,13 @@ var __publicField = (obj, key, value) => {
1862
1867
  const { errors, resetForm } = formik$1;
1863
1868
  React.useEffect(() => {
1864
1869
  if (hasKeys(errors)) {
1865
- resetForm({ values: initialValues2, errors: {} });
1870
+ console.log(errors);
1871
+ showError({
1872
+ title: "test title",
1873
+ description: "Sections with conditions must be below the fields they reference."
1874
+ });
1866
1875
  }
1867
- }, [errors, initialValues2, resetForm]);
1876
+ }, [showError, errors, initialValues2, resetForm]);
1868
1877
  return /* @__PURE__ */ jsxRuntime.jsx(formik.FormikProvider, { value: formik$1, children: /* @__PURE__ */ jsxRuntime.jsx("form", { ...rest, ref, onSubmit: formik$1.handleSubmit, children }) });
1869
1878
  })
1870
1879
  );
@@ -1938,7 +1947,7 @@ var __publicField = (obj, key, value) => {
1938
1947
  ];
1939
1948
  };
1940
1949
  const FieldOptionsForm = React.memo(function FieldOptionsForm2(props) {
1941
- const { fieldType, handleCancel, handleCreateField, defaultField, conditionalSourceFields } = props;
1950
+ const { fieldType, handleCancel, handleCreateField, handleDirtyChange, defaultField, conditionalSourceFields } = props;
1942
1951
  const fieldCls = CompleteFieldTypeToClsMapping[fieldType];
1943
1952
  const formik$1 = formik.useFormikContext();
1944
1953
  const schema = React.useMemo(() => {
@@ -1969,26 +1978,44 @@ var __publicField = (obj, key, value) => {
1969
1978
  values: defaultField,
1970
1979
  onSubmit: handleCreateField,
1971
1980
  cancelText: defaultField ? void 0 : "Back",
1972
- onCancel: handleCancel
1981
+ onCancel: handleCancel,
1982
+ onDirtyChange: handleDirtyChange
1973
1983
  }
1974
1984
  );
1975
1985
  });
1976
1986
  const FieldBuilder = React.memo(function FieldBuilder2(props) {
1977
1987
  const { parentPath, index, children, initial, editing, conditionalSourceFields } = props;
1978
1988
  const [fieldType, setFieldType] = React.useState();
1989
+ const [formIsDirty, setFormIsDirty] = React.useState(false);
1979
1990
  const type = (initial == null ? void 0 : initial.type) ?? fieldType;
1980
1991
  const typeName = type ? CompleteFieldTypeToClsMapping[type].fieldTypeName : void 0;
1981
1992
  const { setFieldValue, values } = formik.useFormikContext();
1982
1993
  if (editing && !initial)
1983
1994
  throw new Error("Initial field must be provided if editing is true.");
1995
+ const openConfirmDiscardChangesDialog = blocks.useDiscardAlertDialog();
1984
1996
  const showChooseField = !type && !editing && !initial;
1985
1997
  const title2 = showChooseField ? "Choose a field type" : `${typeName} settings`;
1986
1998
  const description2 = showChooseField ? "Select a field type to add to this section." : (typeName == null ? void 0 : typeName.toLowerCase()) === "section" ? "Customize your section" : `Customize your ${typeName == null ? void 0 : typeName.toLowerCase()} field.`;
1987
- const handleCancel = React.useCallback(() => setFieldType(void 0), []);
1988
- const handleCloseInterrupt = React.useCallback((confirmClose) => {
1999
+ const handleCancel = React.useCallback(() => {
1989
2000
  setFieldType(void 0);
1990
- confirmClose();
2001
+ setFormIsDirty(false);
1991
2002
  }, []);
2003
+ const handleCloseInterrupt = React.useCallback(
2004
+ (confirmClose) => {
2005
+ if (formIsDirty) {
2006
+ openConfirmDiscardChangesDialog({
2007
+ onDiscard: () => {
2008
+ setFieldType(void 0);
2009
+ confirmClose();
2010
+ }
2011
+ });
2012
+ } else {
2013
+ setFieldType(void 0);
2014
+ confirmClose();
2015
+ }
2016
+ },
2017
+ [formIsDirty, openConfirmDiscardChangesDialog]
2018
+ );
1992
2019
  const handleCreateField = React.useCallback(
1993
2020
  (form, closeDialog) => {
1994
2021
  const { label } = form;
@@ -2014,10 +2041,11 @@ var __publicField = (obj, key, value) => {
2014
2041
  newFields = insert(parent, index, field);
2015
2042
  }
2016
2043
  setFieldValue(parentPath, newFields).then();
2017
- closeDialog();
2044
+ closeDialog({ force: true });
2018
2045
  },
2019
2046
  [editing, type, values, parentPath, setFieldValue, index]
2020
2047
  );
2048
+ const handleDirtyChange = React.useCallback((dirty) => setFormIsDirty(dirty), []);
2021
2049
  const dialogContent = React.useCallback(
2022
2050
  (closeDialog) => {
2023
2051
  if (showChooseField) {
@@ -2030,11 +2058,12 @@ var __publicField = (obj, key, value) => {
2030
2058
  handleCancel,
2031
2059
  handleCreateField: (form) => handleCreateField(form, closeDialog),
2032
2060
  fieldType: type,
2033
- defaultField: initial
2061
+ defaultField: initial,
2062
+ handleDirtyChange
2034
2063
  }
2035
2064
  );
2036
2065
  },
2037
- [conditionalSourceFields, handleCancel, handleCreateField, initial, showChooseField, type]
2066
+ [conditionalSourceFields, handleCancel, handleCreateField, handleDirtyChange, initial, showChooseField, type]
2038
2067
  );
2039
2068
  return /* @__PURE__ */ jsxRuntime.jsx(blocks.Dialog, { onCloseInterrupt: handleCloseInterrupt, title: title2, description: description2, content: dialogContent, children });
2040
2069
  });
@@ -2175,7 +2204,7 @@ var __publicField = (obj, key, value) => {
2175
2204
  ) });
2176
2205
  });
2177
2206
  const FieldSectionWithActions = React.memo(function FieldSectionWithActions2(props) {
2178
- var _a, _b, _c, _d, _e, _f, _g;
2207
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i;
2179
2208
  const { field, index: sectionIndex, dropState } = props;
2180
2209
  const isDropDisabled = (_a = dropState[field.identifier]) == null ? void 0 : _a.disabled;
2181
2210
  const { setFieldValue, values } = formik.useFormikContext();
@@ -2328,7 +2357,7 @@ var __publicField = (obj, key, value) => {
2328
2357
  const conditionComparison = Array.isArray((_c = field.condition) == null ? void 0 : _c.value) ? "contains all of" : "equals";
2329
2358
  if (valueIsFile((_d = field.condition) == null ? void 0 : _d.value))
2330
2359
  throw new Error("File values are not supported for conditions.");
2331
- const conditionValue = Array.isArray((_e = field.condition) == null ? void 0 : _e.value) ? (_f = field.condition) == null ? void 0 : _f.value.map((v) => typeof v === "string" ? v : v.label).join(", ") : (_g = field.condition) == null ? void 0 : _g.value.toString();
2360
+ const conditionValue = Array.isArray((_e = field.condition) == null ? void 0 : _e.value) ? (_g = (_f = field.condition) == null ? void 0 : _f.value) == null ? void 0 : _g.map((v) => typeof v === "string" ? v : v.label).join(", ") : (_i = (_h = field.condition) == null ? void 0 : _h.value) == null ? void 0 : _i.toString();
2332
2361
  return /* @__PURE__ */ jsxRuntime.jsx(dnd.Draggable, { draggableId: field.identifier, index: sectionIndex, children: (draggableProvided) => /* @__PURE__ */ jsxRuntime.jsx(
2333
2362
  blocks.Card,
2334
2363
  {