envoc-form 4.0.1-5 → 4.0.1-7

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.
@@ -5,6 +5,8 @@ export interface ConfirmBaseFormProps {
5
5
  request: useAxiosRequestProps;
6
6
  style?: React.CSSProperties;
7
7
  title?: string;
8
+ confirmButtonText?: string;
9
+ confirmButtonClass?: string;
8
10
  children?: React.ReactNode;
9
11
  }
10
- export default function ConfirmBaseForm({ handleCancel, request, style, title, children, ...props }: ConfirmBaseFormProps): JSX.Element;
12
+ export default function ConfirmBaseForm({ handleCancel, request, style, title, confirmButtonText, confirmButtonClass, children, ...props }: ConfirmBaseFormProps): JSX.Element;
@@ -26,10 +26,10 @@ import { useAxiosRequest } from 'envoc-request';
26
26
  import { FormDefaults } from '../FormDefaults';
27
27
  // TODO: ask about how we should use 'children'
28
28
  export default function ConfirmBaseForm(_a) {
29
- var handleCancel = _a.handleCancel, request = _a.request, style = _a.style, title = _a.title, children = _a.children, props = __rest(_a, ["handleCancel", "request", "style", "title", "children"]);
29
+ var handleCancel = _a.handleCancel, request = _a.request, style = _a.style, title = _a.title, confirmButtonText = _a.confirmButtonText, confirmButtonClass = _a.confirmButtonClass, children = _a.children, props = __rest(_a, ["handleCancel", "request", "style", "title", "confirmButtonText", "confirmButtonClass", "children"]);
30
30
  var webRequest = useAxiosRequest(Object.assign({}, request, { autoExecute: false }));
31
31
  var onCancel = handleCancel !== null && handleCancel !== void 0 ? handleCancel : goBack;
32
- return (_jsxs("div", __assign({ style: __assign({ textAlign: 'center' }, style) }, props, { children: [_jsx("h3", { children: title }), children, _jsx("button", __assign({ className: classNames(FormDefaults.cssClassPrefix + 'confirm-base-form-yes-button'), type: "button", onClick: webRequest.submitRequest }, { children: "Yes" })), _jsx("button", __assign({ className: classNames(FormDefaults.cssClassPrefix + 'confirm-base-form-cancel-button'), type: "button", onClick: onCancel }, { children: "Cancel" }))] })));
32
+ return (_jsxs("div", __assign({ style: __assign({ textAlign: 'center' }, style) }, props, { children: [_jsx("h3", { children: title }), children, _jsx("button", __assign({ className: classNames(confirmButtonClass !== null && confirmButtonClass !== void 0 ? confirmButtonClass : FormDefaults.cssClassPrefix + 'confirm-base-form-confirm-button'), type: "button", onClick: webRequest.submitRequest }, { children: confirmButtonText !== null && confirmButtonText !== void 0 ? confirmButtonText : 'Confirm' })), _jsx("button", __assign({ className: classNames(FormDefaults.cssClassPrefix + 'confirm-base-form-cancel-button'), type: "button", onClick: onCancel }, { children: "Cancel" }))] })));
33
33
  function goBack() {
34
34
  window.history.back();
35
35
  }
@@ -24,6 +24,7 @@ import { jsx as _jsx } from "react/jsx-runtime";
24
24
  import { useNavigate, useParams } from 'react-router-dom';
25
25
  import { toast } from 'react-toastify';
26
26
  import ConfirmBaseForm from '../ConfirmBaseForm/ConfirmBaseForm';
27
+ import { FormDefaults } from '../FormDefaults';
27
28
  export default function ConfirmDeleteForm(_a) {
28
29
  var successUrl = _a.successUrl, form = _a.form, title = _a.title, handleComplete = _a.handleComplete, handleError = _a.handleError, children = _a.children, props = __rest(_a, ["successUrl", "form", "title", "handleComplete", "handleError", "children"]);
29
30
  var navigate = useNavigate();
@@ -42,7 +43,7 @@ export default function ConfirmDeleteForm(_a) {
42
43
  onComplete: onComplete,
43
44
  onError: onError,
44
45
  };
45
- return (_jsx(ConfirmBaseForm, __assign({ request: request, handleCancel: goBack, title: title !== null && title !== void 0 ? title : 'Are you sure you want to delete this?' }, props, { children: children })));
46
+ return (_jsx(ConfirmBaseForm, __assign({ request: request, handleCancel: goBack, title: title !== null && title !== void 0 ? title : 'Are you sure you want to delete this?', confirmButtonText: "Yes", confirmButtonClass: FormDefaults.cssClassPrefix + 'confirm-delete-form-yes-button' }, props, { children: children })));
46
47
  function goBack() {
47
48
  if (successUrl) {
48
49
  navigate(successUrl);
package/es/Form/Form.d.ts CHANGED
@@ -7,15 +7,20 @@ export declare type FormBuilderProp<TForm extends object> = {
7
7
  Field: <TProp extends keyof TForm, TRenderComponent extends ElementType>(props: FieldProps<TForm, TProp, TRenderComponent>) => JSX.Element;
8
8
  FieldArray: <TProp extends keyof TForm>(props: FieldArrayProps<TForm, TProp>) => JSX.Element;
9
9
  };
10
- export interface FormProps<TForm extends object> {
10
+ export interface FullFormProps<TForm extends object> {
11
11
  children: (formBuilder: FormBuilderProp<TForm>) => JSX.Element;
12
- onSubmit: (formValues: FormData | TForm, formikBag: FormikHelpers<TForm>) => Promise<ValidatedApiResult>;
12
+ onSubmit: (formValues: TForm, formikBag: FormikHelpers<TForm>) => Promise<ValidatedApiResult>;
13
+ onFormDataSubmit: (formValues: FormData, formikBag: FormikHelpers<TForm>) => Promise<ValidatedApiResult>;
13
14
  className?: string;
14
15
  style?: CSSProperties;
15
16
  ignoreLostChanges?: boolean;
16
17
  initialValues?: TForm;
17
18
  }
18
- declare function Form<TForm extends object>({ children, className, style, ignoreLostChanges, onSubmit, initialValues, ...props }: FormProps<TForm>): JSX.Element;
19
+ declare type RequireAtLeastOne<T, Keys extends keyof T = keyof T> = Pick<T, Exclude<keyof T, Keys>> & {
20
+ [K in Keys]-?: Required<Pick<T, K>> & Partial<Pick<T, Exclude<Keys, K>>>;
21
+ }[Keys];
22
+ export declare type FormProps<TForm extends object> = RequireAtLeastOne<FullFormProps<TForm>, 'onSubmit' | 'onFormDataSubmit'>;
23
+ declare function Form<TForm extends object>({ children, className, style, ignoreLostChanges, onSubmit, onFormDataSubmit, initialValues, ...props }: FormProps<TForm>): JSX.Element;
19
24
  declare namespace Form {
20
25
  var DisplayFormState: () => JSX.Element;
21
26
  }
package/es/Form/Form.js CHANGED
@@ -29,11 +29,11 @@ import FormBasedPreventNavigation from './FormBasedPreventNavigation';
29
29
  import { ServerErrorContext, } from './ServerErrorContext';
30
30
  import Field from '../Field/Field';
31
31
  import FieldArray from '../FieldArray/FieldArray';
32
+ import { FormDefaults } from '../FormDefaults';
32
33
  import objectContainsNonSerializableProperty from '../utils/objectContainsNonSerializableProperty';
33
34
  import objectToFormData from '../utils/objectToFormData';
34
- import { FormDefaults } from '../FormDefaults';
35
35
  export default function Form(_a) {
36
- var children = _a.children, className = _a.className, style = _a.style, ignoreLostChanges = _a.ignoreLostChanges, onSubmit = _a.onSubmit, initialValues = _a.initialValues, props = __rest(_a, ["children", "className", "style", "ignoreLostChanges", "onSubmit", "initialValues"]);
36
+ var children = _a.children, className = _a.className, style = _a.style, ignoreLostChanges = _a.ignoreLostChanges, onSubmit = _a.onSubmit, onFormDataSubmit = _a.onFormDataSubmit, initialValues = _a.initialValues, props = __rest(_a, ["children", "className", "style", "ignoreLostChanges", "onSubmit", "onFormDataSubmit", "initialValues"]);
37
37
  // formik resets all error on each blur (with our settings)
38
38
  // this means that ALL errors from the server disappear when any one field is blurred
39
39
  // So, we have to store server errors ourselves
@@ -60,6 +60,7 @@ export default function Form(_a) {
60
60
  })] })) })) })));
61
61
  function handleSubmit(values, formikBag) {
62
62
  var formData = undefined;
63
+ var submitFunc;
63
64
  if (objectContainsNonSerializableProperty(values)) {
64
65
  formData = objectToFormData(values, {
65
66
  indices: true,
@@ -67,8 +68,34 @@ export default function Form(_a) {
67
68
  allowEmptyArrays: true,
68
69
  noFileListBrackets: true,
69
70
  });
71
+ if (onFormDataSubmit === undefined) {
72
+ throw new Error('No onFormDataSubmit supplied for non-serializable properties.');
73
+ }
74
+ submitFunc = function () {
75
+ return onFormDataSubmit(formData !== null && formData !== void 0 ? formData : new FormData(), formikBag);
76
+ };
77
+ }
78
+ else {
79
+ if (onSubmit === undefined) {
80
+ formData = objectToFormData(values, {
81
+ indices: true,
82
+ dotNotation: true,
83
+ allowEmptyArrays: true,
84
+ noFileListBrackets: true,
85
+ });
86
+ if (onFormDataSubmit === undefined) {
87
+ // This error should never occur, as this case is covered by RequireAtLeastOne type safety
88
+ throw new Error('No onFormDataSubmit supplied for non-serializable properties.');
89
+ }
90
+ submitFunc = function () {
91
+ return onFormDataSubmit(formData !== null && formData !== void 0 ? formData : new FormData(), formikBag);
92
+ };
93
+ }
94
+ else {
95
+ submitFunc = function () { return onSubmit(values, formikBag); };
96
+ }
70
97
  }
71
- return onSubmit(formData !== null && formData !== void 0 ? formData : values, formikBag)
98
+ return submitFunc()
72
99
  .then(function (response) {
73
100
  return response;
74
101
  })
@@ -5,6 +5,8 @@ export interface ConfirmBaseFormProps {
5
5
  request: useAxiosRequestProps;
6
6
  style?: React.CSSProperties;
7
7
  title?: string;
8
+ confirmButtonText?: string;
9
+ confirmButtonClass?: string;
8
10
  children?: React.ReactNode;
9
11
  }
10
- export default function ConfirmBaseForm({ handleCancel, request, style, title, children, ...props }: ConfirmBaseFormProps): JSX.Element;
12
+ export default function ConfirmBaseForm({ handleCancel, request, style, title, confirmButtonText, confirmButtonClass, children, ...props }: ConfirmBaseFormProps): JSX.Element;
@@ -31,10 +31,10 @@ var envoc_request_1 = require("envoc-request");
31
31
  var FormDefaults_1 = require("../FormDefaults");
32
32
  // TODO: ask about how we should use 'children'
33
33
  function ConfirmBaseForm(_a) {
34
- var handleCancel = _a.handleCancel, request = _a.request, style = _a.style, title = _a.title, children = _a.children, props = __rest(_a, ["handleCancel", "request", "style", "title", "children"]);
34
+ var handleCancel = _a.handleCancel, request = _a.request, style = _a.style, title = _a.title, confirmButtonText = _a.confirmButtonText, confirmButtonClass = _a.confirmButtonClass, children = _a.children, props = __rest(_a, ["handleCancel", "request", "style", "title", "confirmButtonText", "confirmButtonClass", "children"]);
35
35
  var webRequest = (0, envoc_request_1.useAxiosRequest)(Object.assign({}, request, { autoExecute: false }));
36
36
  var onCancel = handleCancel !== null && handleCancel !== void 0 ? handleCancel : goBack;
37
- return ((0, jsx_runtime_1.jsxs)("div", __assign({ style: __assign({ textAlign: 'center' }, style) }, props, { children: [(0, jsx_runtime_1.jsx)("h3", { children: title }), children, (0, jsx_runtime_1.jsx)("button", __assign({ className: (0, classnames_1.default)(FormDefaults_1.FormDefaults.cssClassPrefix + 'confirm-base-form-yes-button'), type: "button", onClick: webRequest.submitRequest }, { children: "Yes" })), (0, jsx_runtime_1.jsx)("button", __assign({ className: (0, classnames_1.default)(FormDefaults_1.FormDefaults.cssClassPrefix + 'confirm-base-form-cancel-button'), type: "button", onClick: onCancel }, { children: "Cancel" }))] })));
37
+ return ((0, jsx_runtime_1.jsxs)("div", __assign({ style: __assign({ textAlign: 'center' }, style) }, props, { children: [(0, jsx_runtime_1.jsx)("h3", { children: title }), children, (0, jsx_runtime_1.jsx)("button", __assign({ className: (0, classnames_1.default)(confirmButtonClass !== null && confirmButtonClass !== void 0 ? confirmButtonClass : FormDefaults_1.FormDefaults.cssClassPrefix + 'confirm-base-form-confirm-button'), type: "button", onClick: webRequest.submitRequest }, { children: confirmButtonText !== null && confirmButtonText !== void 0 ? confirmButtonText : 'Confirm' })), (0, jsx_runtime_1.jsx)("button", __assign({ className: (0, classnames_1.default)(FormDefaults_1.FormDefaults.cssClassPrefix + 'confirm-base-form-cancel-button'), type: "button", onClick: onCancel }, { children: "Cancel" }))] })));
38
38
  function goBack() {
39
39
  window.history.back();
40
40
  }
@@ -29,6 +29,7 @@ var jsx_runtime_1 = require("react/jsx-runtime");
29
29
  var react_router_dom_1 = require("react-router-dom");
30
30
  var react_toastify_1 = require("react-toastify");
31
31
  var ConfirmBaseForm_1 = __importDefault(require("../ConfirmBaseForm/ConfirmBaseForm"));
32
+ var FormDefaults_1 = require("../FormDefaults");
32
33
  function ConfirmDeleteForm(_a) {
33
34
  var successUrl = _a.successUrl, form = _a.form, title = _a.title, handleComplete = _a.handleComplete, handleError = _a.handleError, children = _a.children, props = __rest(_a, ["successUrl", "form", "title", "handleComplete", "handleError", "children"]);
34
35
  var navigate = (0, react_router_dom_1.useNavigate)();
@@ -47,7 +48,7 @@ function ConfirmDeleteForm(_a) {
47
48
  onComplete: onComplete,
48
49
  onError: onError,
49
50
  };
50
- return ((0, jsx_runtime_1.jsx)(ConfirmBaseForm_1.default, __assign({ request: request, handleCancel: goBack, title: title !== null && title !== void 0 ? title : 'Are you sure you want to delete this?' }, props, { children: children })));
51
+ return ((0, jsx_runtime_1.jsx)(ConfirmBaseForm_1.default, __assign({ request: request, handleCancel: goBack, title: title !== null && title !== void 0 ? title : 'Are you sure you want to delete this?', confirmButtonText: "Yes", confirmButtonClass: FormDefaults_1.FormDefaults.cssClassPrefix + 'confirm-delete-form-yes-button' }, props, { children: children })));
51
52
  function goBack() {
52
53
  if (successUrl) {
53
54
  navigate(successUrl);
@@ -7,15 +7,20 @@ export declare type FormBuilderProp<TForm extends object> = {
7
7
  Field: <TProp extends keyof TForm, TRenderComponent extends ElementType>(props: FieldProps<TForm, TProp, TRenderComponent>) => JSX.Element;
8
8
  FieldArray: <TProp extends keyof TForm>(props: FieldArrayProps<TForm, TProp>) => JSX.Element;
9
9
  };
10
- export interface FormProps<TForm extends object> {
10
+ export interface FullFormProps<TForm extends object> {
11
11
  children: (formBuilder: FormBuilderProp<TForm>) => JSX.Element;
12
- onSubmit: (formValues: FormData | TForm, formikBag: FormikHelpers<TForm>) => Promise<ValidatedApiResult>;
12
+ onSubmit: (formValues: TForm, formikBag: FormikHelpers<TForm>) => Promise<ValidatedApiResult>;
13
+ onFormDataSubmit: (formValues: FormData, formikBag: FormikHelpers<TForm>) => Promise<ValidatedApiResult>;
13
14
  className?: string;
14
15
  style?: CSSProperties;
15
16
  ignoreLostChanges?: boolean;
16
17
  initialValues?: TForm;
17
18
  }
18
- declare function Form<TForm extends object>({ children, className, style, ignoreLostChanges, onSubmit, initialValues, ...props }: FormProps<TForm>): JSX.Element;
19
+ declare type RequireAtLeastOne<T, Keys extends keyof T = keyof T> = Pick<T, Exclude<keyof T, Keys>> & {
20
+ [K in Keys]-?: Required<Pick<T, K>> & Partial<Pick<T, Exclude<Keys, K>>>;
21
+ }[Keys];
22
+ export declare type FormProps<TForm extends object> = RequireAtLeastOne<FullFormProps<TForm>, 'onSubmit' | 'onFormDataSubmit'>;
23
+ declare function Form<TForm extends object>({ children, className, style, ignoreLostChanges, onSubmit, onFormDataSubmit, initialValues, ...props }: FormProps<TForm>): JSX.Element;
19
24
  declare namespace Form {
20
25
  var DisplayFormState: () => JSX.Element;
21
26
  }
package/lib/Form/Form.js CHANGED
@@ -34,11 +34,11 @@ var FormBasedPreventNavigation_1 = __importDefault(require("./FormBasedPreventNa
34
34
  var ServerErrorContext_1 = require("./ServerErrorContext");
35
35
  var Field_1 = __importDefault(require("../Field/Field"));
36
36
  var FieldArray_1 = __importDefault(require("../FieldArray/FieldArray"));
37
+ var FormDefaults_1 = require("../FormDefaults");
37
38
  var objectContainsNonSerializableProperty_1 = __importDefault(require("../utils/objectContainsNonSerializableProperty"));
38
39
  var objectToFormData_1 = __importDefault(require("../utils/objectToFormData"));
39
- var FormDefaults_1 = require("../FormDefaults");
40
40
  function Form(_a) {
41
- var children = _a.children, className = _a.className, style = _a.style, ignoreLostChanges = _a.ignoreLostChanges, onSubmit = _a.onSubmit, initialValues = _a.initialValues, props = __rest(_a, ["children", "className", "style", "ignoreLostChanges", "onSubmit", "initialValues"]);
41
+ var children = _a.children, className = _a.className, style = _a.style, ignoreLostChanges = _a.ignoreLostChanges, onSubmit = _a.onSubmit, onFormDataSubmit = _a.onFormDataSubmit, initialValues = _a.initialValues, props = __rest(_a, ["children", "className", "style", "ignoreLostChanges", "onSubmit", "onFormDataSubmit", "initialValues"]);
42
42
  // formik resets all error on each blur (with our settings)
43
43
  // this means that ALL errors from the server disappear when any one field is blurred
44
44
  // So, we have to store server errors ourselves
@@ -65,6 +65,7 @@ function Form(_a) {
65
65
  })] })) })) })));
66
66
  function handleSubmit(values, formikBag) {
67
67
  var formData = undefined;
68
+ var submitFunc;
68
69
  if ((0, objectContainsNonSerializableProperty_1.default)(values)) {
69
70
  formData = (0, objectToFormData_1.default)(values, {
70
71
  indices: true,
@@ -72,8 +73,34 @@ function Form(_a) {
72
73
  allowEmptyArrays: true,
73
74
  noFileListBrackets: true,
74
75
  });
76
+ if (onFormDataSubmit === undefined) {
77
+ throw new Error('No onFormDataSubmit supplied for non-serializable properties.');
78
+ }
79
+ submitFunc = function () {
80
+ return onFormDataSubmit(formData !== null && formData !== void 0 ? formData : new FormData(), formikBag);
81
+ };
82
+ }
83
+ else {
84
+ if (onSubmit === undefined) {
85
+ formData = (0, objectToFormData_1.default)(values, {
86
+ indices: true,
87
+ dotNotation: true,
88
+ allowEmptyArrays: true,
89
+ noFileListBrackets: true,
90
+ });
91
+ if (onFormDataSubmit === undefined) {
92
+ // This error should never occur, as this case is covered by RequireAtLeastOne type safety
93
+ throw new Error('No onFormDataSubmit supplied for non-serializable properties.');
94
+ }
95
+ submitFunc = function () {
96
+ return onFormDataSubmit(formData !== null && formData !== void 0 ? formData : new FormData(), formikBag);
97
+ };
98
+ }
99
+ else {
100
+ submitFunc = function () { return onSubmit(values, formikBag); };
101
+ }
75
102
  }
76
- return onSubmit(formData !== null && formData !== void 0 ? formData : values, formikBag)
103
+ return submitFunc()
77
104
  .then(function (response) {
78
105
  return response;
79
106
  })
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "envoc-form",
3
- "version": "4.0.1-5",
3
+ "version": "4.0.1-7",
4
4
  "description": "Envoc form components",
5
5
  "keywords": [
6
6
  "react-component",
@@ -37,7 +37,7 @@
37
37
  "axios": "^0.21.1",
38
38
  "classnames": "^2.3.1",
39
39
  "date-fns": "^2.22.1",
40
- "envoc-request": "^4.0.1-5",
40
+ "envoc-request": "^4.0.1-7",
41
41
  "lru-cache": "^6.0.0",
42
42
  "prop-types": "^15.7.2",
43
43
  "react-date-picker": "^8.2.0",
@@ -10,6 +10,8 @@ export interface ConfirmBaseFormProps {
10
10
  request: useAxiosRequestProps;
11
11
  style?: React.CSSProperties;
12
12
  title?: string;
13
+ confirmButtonText?: string;
14
+ confirmButtonClass?: string;
13
15
  children?: React.ReactNode;
14
16
  }
15
17
 
@@ -19,6 +21,8 @@ export default function ConfirmBaseForm({
19
21
  request,
20
22
  style,
21
23
  title,
24
+ confirmButtonText,
25
+ confirmButtonClass,
22
26
  children,
23
27
  ...props
24
28
  }: ConfirmBaseFormProps) {
@@ -33,11 +37,12 @@ export default function ConfirmBaseForm({
33
37
  {children}
34
38
  <button
35
39
  className={classNames(
36
- FormDefaults.cssClassPrefix + 'confirm-base-form-yes-button'
40
+ confirmButtonClass ??
41
+ FormDefaults.cssClassPrefix + 'confirm-base-form-confirm-button'
37
42
  )}
38
43
  type="button"
39
44
  onClick={webRequest.submitRequest}>
40
- Yes
45
+ {confirmButtonText ?? 'Confirm'}
41
46
  </button>
42
47
  <button
43
48
  className={classNames(
@@ -7,10 +7,10 @@ exports[`ConfirmBaseForm has matching snapshot 1`] = `
7
7
  >
8
8
  <h3 />
9
9
  <button
10
- class="envoc-form-confirm-base-form-yes-button"
10
+ class="envoc-form-confirm-base-form-confirm-button"
11
11
  type="button"
12
12
  >
13
- Yes
13
+ Confirm
14
14
  </button>
15
15
  <button
16
16
  class="envoc-form-confirm-base-form-cancel-button"
@@ -2,7 +2,10 @@ import React from 'react';
2
2
  import { useNavigate, useParams } from 'react-router-dom';
3
3
  import { toast } from 'react-toastify';
4
4
  import { useAxiosRequestProps } from 'envoc-request';
5
- import ConfirmBaseForm, { ConfirmBaseFormProps } from '../ConfirmBaseForm/ConfirmBaseForm';
5
+ import ConfirmBaseForm, {
6
+ ConfirmBaseFormProps,
7
+ } from '../ConfirmBaseForm/ConfirmBaseForm';
8
+ import { FormDefaults } from '../FormDefaults';
6
9
 
7
10
  export interface ConfirmDeleteFormProps
8
11
  extends Pick<ConfirmBaseFormProps, 'style'> {
@@ -52,6 +55,10 @@ export default function ConfirmDeleteForm({
52
55
  request={request}
53
56
  handleCancel={goBack}
54
57
  title={title ?? 'Are you sure you want to delete this?'}
58
+ confirmButtonText="Yes"
59
+ confirmButtonClass={
60
+ FormDefaults.cssClassPrefix + 'confirm-delete-form-yes-button'
61
+ }
55
62
  {...props}>
56
63
  {children}
57
64
  </ConfirmBaseForm>
@@ -9,7 +9,7 @@ exports[`ConfirmDeleteForm has matching snapshot 1`] = `
9
9
  Are you sure you want to delete this?
10
10
  </h3>
11
11
  <button
12
- class="envoc-form-confirm-base-form-yes-button"
12
+ class="envoc-form-confirm-delete-form-yes-button"
13
13
  type="button"
14
14
  >
15
15
  Yes
package/src/Form/Form.tsx CHANGED
@@ -21,11 +21,11 @@ import {
21
21
  } from './ServerErrorContext';
22
22
  import Field, { FieldProps } from '../Field/Field';
23
23
  import FieldArray, { FieldArrayProps } from '../FieldArray/FieldArray';
24
+ import { FormDefaults } from '../FormDefaults';
24
25
  import objectContainsNonSerializableProperty from '../utils/objectContainsNonSerializableProperty';
25
26
  import objectToFormData from '../utils/objectToFormData';
26
27
  import { ValidatedApiResult } from '../Validation/ValidatedApiResult';
27
28
  import { ValidationError } from '../Validation/ValidationError';
28
- import { FormDefaults } from '../FormDefaults';
29
29
 
30
30
  // This exposes the builder that ensures only "name" values on the given TForm can be used
31
31
  // Further, each Field can then infer the proper type given the name
@@ -40,10 +40,14 @@ export type FormBuilderProp<TForm extends object> = {
40
40
  ) => JSX.Element;
41
41
  };
42
42
 
43
- export interface FormProps<TForm extends object> {
43
+ export interface FullFormProps<TForm extends object> {
44
44
  children: (formBuilder: FormBuilderProp<TForm>) => JSX.Element;
45
45
  onSubmit: (
46
- formValues: FormData | TForm,
46
+ formValues: TForm,
47
+ formikBag: FormikHelpers<TForm>
48
+ ) => Promise<ValidatedApiResult>;
49
+ onFormDataSubmit: (
50
+ formValues: FormData,
47
51
  formikBag: FormikHelpers<TForm>
48
52
  ) => Promise<ValidatedApiResult>;
49
53
  className?: string;
@@ -52,12 +56,26 @@ export interface FormProps<TForm extends object> {
52
56
  initialValues?: TForm;
53
57
  }
54
58
 
59
+ type RequireAtLeastOne<T, Keys extends keyof T = keyof T> = Pick<
60
+ T,
61
+ Exclude<keyof T, Keys>
62
+ > &
63
+ {
64
+ [K in Keys]-?: Required<Pick<T, K>> & Partial<Pick<T, Exclude<Keys, K>>>;
65
+ }[Keys];
66
+
67
+ export type FormProps<TForm extends object> = RequireAtLeastOne<
68
+ FullFormProps<TForm>,
69
+ 'onSubmit' | 'onFormDataSubmit'
70
+ >;
71
+
55
72
  export default function Form<TForm extends object>({
56
73
  children,
57
74
  className,
58
75
  style,
59
76
  ignoreLostChanges,
60
77
  onSubmit,
78
+ onFormDataSubmit,
61
79
  initialValues,
62
80
  ...props
63
81
  }: FormProps<TForm>) {
@@ -114,6 +132,7 @@ export default function Form<TForm extends object>({
114
132
 
115
133
  function handleSubmit(values: TForm, formikBag: FormikHelpers<TForm>) {
116
134
  let formData: FormData | undefined = undefined;
135
+ let submitFunc: () => Promise<ValidatedApiResult>;
117
136
  if (objectContainsNonSerializableProperty(values)) {
118
137
  formData = objectToFormData(values, {
119
138
  indices: true,
@@ -121,9 +140,35 @@ export default function Form<TForm extends object>({
121
140
  allowEmptyArrays: true,
122
141
  noFileListBrackets: true,
123
142
  });
143
+ if (onFormDataSubmit === undefined) {
144
+ throw new Error(
145
+ 'No onFormDataSubmit supplied for non-serializable properties.'
146
+ );
147
+ }
148
+ submitFunc = () =>
149
+ onFormDataSubmit(formData ?? new FormData(), formikBag);
150
+ } else {
151
+ if (onSubmit === undefined) {
152
+ formData = objectToFormData(values, {
153
+ indices: true,
154
+ dotNotation: true,
155
+ allowEmptyArrays: true,
156
+ noFileListBrackets: true,
157
+ });
158
+ if (onFormDataSubmit === undefined) {
159
+ // This error should never occur, as this case is covered by RequireAtLeastOne type safety
160
+ throw new Error(
161
+ 'No onFormDataSubmit supplied for non-serializable properties.'
162
+ );
163
+ }
164
+ submitFunc = () =>
165
+ onFormDataSubmit(formData ?? new FormData(), formikBag);
166
+ } else {
167
+ submitFunc = () => onSubmit(values, formikBag);
168
+ }
124
169
  }
125
170
 
126
- return onSubmit(formData ?? values, formikBag)
171
+ return submitFunc()
127
172
  .then((response) => {
128
173
  return response;
129
174
  })