envoc-form 4.1.0 → 5.0.1

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.
Files changed (223) hide show
  1. package/README.md +236 -180
  2. package/es/AddressInput/AddressInput.d.ts +24 -25
  3. package/es/AddressInput/AddressInput.js +20 -31
  4. package/es/AddressInput/UsStates.d.ts +3 -3
  5. package/es/AddressInput/UsStates.js +53 -53
  6. package/es/ConfirmBaseForm/ConfirmBaseForm.d.ts +26 -26
  7. package/es/ConfirmBaseForm/ConfirmBaseForm.js +42 -42
  8. package/es/ConfirmDeleteForm/ConfirmDeleteForm.d.ts +24 -24
  9. package/es/ConfirmDeleteForm/ConfirmDeleteForm.js +60 -60
  10. package/es/DatePicker/DatePickerGroup.d.ts +13 -14
  11. package/es/DatePicker/DatePickerGroup.js +87 -87
  12. package/es/DatePicker/DatePickerHelper.d.ts +3 -3
  13. package/es/DatePicker/DatePickerHelper.js +1 -1
  14. package/es/DatePicker/StringDateOnlyPickerGroup.d.ts +9 -10
  15. package/es/DatePicker/StringDateOnlyPickerGroup.js +29 -29
  16. package/es/DatePicker/StringDatePickerGroup.d.ts +9 -10
  17. package/es/DatePicker/StringDatePickerGroup.js +24 -24
  18. package/es/Field/CustomFieldInputProps.d.ts +7 -7
  19. package/es/Field/CustomFieldInputProps.js +1 -1
  20. package/es/Field/CustomFieldMetaProps.d.ts +4 -4
  21. package/es/Field/CustomFieldMetaProps.js +1 -1
  22. package/es/Field/Field.d.ts +28 -28
  23. package/es/Field/Field.js +51 -51
  24. package/es/Field/FieldErrorScrollTarget.d.ts +2 -3
  25. package/es/Field/FieldErrorScrollTarget.js +12 -12
  26. package/es/Field/FieldNameContext.d.ts +3 -3
  27. package/es/Field/FieldNameContext.js +3 -3
  28. package/es/Field/FieldSection.d.ts +7 -7
  29. package/es/Field/FieldSection.js +9 -20
  30. package/es/Field/InjectedFieldProps.d.ts +7 -7
  31. package/es/Field/InjectedFieldProps.js +1 -1
  32. package/es/Field/StandAloneInput.d.ts +13 -13
  33. package/es/Field/StandAloneInput.js +50 -50
  34. package/es/Field/useStandardField.d.ts +21 -21
  35. package/es/Field/useStandardField.js +92 -92
  36. package/es/FieldArray/FieldArray.d.ts +24 -24
  37. package/es/FieldArray/FieldArray.js +77 -88
  38. package/es/File/FileGroup.d.ts +10 -10
  39. package/es/File/FileGroup.js +53 -53
  40. package/es/File/FileList.d.ts +5 -6
  41. package/es/File/FileList.js +10 -21
  42. package/es/File/humanFileSize.d.ts +1 -1
  43. package/es/File/humanFileSize.js +6 -6
  44. package/es/Form/FocusError.d.ts +7 -7
  45. package/es/Form/FocusError.js +42 -42
  46. package/es/Form/Form.d.ts +33 -33
  47. package/es/Form/Form.js +141 -141
  48. package/es/Form/FormBasedPreventNavigation.d.ts +6 -7
  49. package/es/Form/FormBasedPreventNavigation.js +20 -20
  50. package/es/Form/LegacyFormBasedPreventNavigation.d.ts +17 -17
  51. package/es/Form/LegacyFormBasedPreventNavigation.js +69 -69
  52. package/es/Form/NewFormBasedPreventNavigation.d.ts +14 -14
  53. package/es/Form/NewFormBasedPreventNavigation.js +39 -39
  54. package/es/Form/ServerErrorContext.d.ts +11 -11
  55. package/es/Form/ServerErrorContext.js +9 -9
  56. package/es/FormActions.d.ts +12 -13
  57. package/es/FormActions.js +16 -27
  58. package/es/FormDefaults.d.ts +4 -4
  59. package/es/FormDefaults.js +2 -2
  60. package/es/Group.d.ts +16 -16
  61. package/es/Group.js +14 -25
  62. package/es/Input/IconInputGroup.d.ts +12 -12
  63. package/es/Input/IconInputGroup.js +43 -43
  64. package/es/Input/InputGroup.d.ts +12 -12
  65. package/es/Input/InputGroup.js +34 -34
  66. package/es/Input/MoneyInputGroup.d.ts +9 -9
  67. package/es/Input/MoneyInputGroup.js +43 -43
  68. package/es/Input/NumberInputGroup.d.ts +9 -9
  69. package/es/Input/NumberInputGroup.js +43 -43
  70. package/es/Input/StringInputGroup.d.ts +8 -8
  71. package/es/Input/StringInputGroup.js +43 -43
  72. package/es/Normalization/NormalizationFunction.d.ts +4 -4
  73. package/es/Normalization/NormalizationFunction.js +1 -1
  74. package/es/Normalization/normalizers.d.ts +4 -4
  75. package/es/Normalization/normalizers.js +32 -32
  76. package/es/Select/BooleanSelectGroup.d.ts +7 -8
  77. package/es/Select/BooleanSelectGroup.js +28 -28
  78. package/es/Select/NumberSelectGroup.d.ts +9 -10
  79. package/es/Select/NumberSelectGroup.js +21 -21
  80. package/es/Select/SelectGroup.d.ts +27 -28
  81. package/es/Select/SelectGroup.js +65 -65
  82. package/es/Select/SelectGroupPropsHelper.d.ts +3 -3
  83. package/es/Select/SelectGroupPropsHelper.js +1 -1
  84. package/es/Select/StringSelectGroup.d.ts +9 -10
  85. package/es/Select/StringSelectGroup.js +21 -21
  86. package/es/StandardFormActions.d.ts +11 -11
  87. package/es/StandardFormActions.js +14 -25
  88. package/es/SubmitFormButton.d.ts +10 -10
  89. package/es/SubmitFormButton.js +40 -40
  90. package/es/TextArea/TextAreaGroup.d.ts +9 -9
  91. package/es/TextArea/TextAreaGroup.js +35 -35
  92. package/es/Validation/ValidatedApiResult.d.ts +6 -6
  93. package/es/Validation/ValidatedApiResult.js +1 -1
  94. package/es/Validation/ValidationError.d.ts +5 -5
  95. package/es/Validation/ValidationError.js +1 -1
  96. package/es/Validation/ValidationFunction.d.ts +4 -4
  97. package/es/Validation/ValidationFunction.js +1 -1
  98. package/es/Validation/validators.d.ts +18 -18
  99. package/es/Validation/validators.js +77 -77
  100. package/es/index.d.ts +73 -73
  101. package/es/index.js +50 -50
  102. package/es/setupTests.d.ts +1 -1
  103. package/es/setupTests.js +1 -1
  104. package/es/utils/objectContainsNonSerializableProperty.d.ts +1 -1
  105. package/es/utils/objectContainsNonSerializableProperty.js +14 -14
  106. package/es/utils/objectToFormData.d.ts +10 -10
  107. package/es/utils/objectToFormData.js +77 -77
  108. package/es/utils/typeChecks.d.ts +8 -8
  109. package/es/utils/typeChecks.js +18 -18
  110. package/lib/AddressInput/AddressInput.d.ts +24 -25
  111. package/lib/AddressInput/AddressInput.js +26 -37
  112. package/lib/AddressInput/UsStates.d.ts +3 -3
  113. package/lib/AddressInput/UsStates.js +55 -55
  114. package/lib/ConfirmBaseForm/ConfirmBaseForm.d.ts +26 -26
  115. package/lib/ConfirmBaseForm/ConfirmBaseForm.js +48 -48
  116. package/lib/ConfirmDeleteForm/ConfirmDeleteForm.d.ts +24 -24
  117. package/lib/ConfirmDeleteForm/ConfirmDeleteForm.js +66 -66
  118. package/lib/DatePicker/DatePickerGroup.d.ts +13 -14
  119. package/lib/DatePicker/DatePickerGroup.js +95 -95
  120. package/lib/DatePicker/DatePickerHelper.d.ts +3 -3
  121. package/lib/DatePicker/DatePickerHelper.js +2 -2
  122. package/lib/DatePicker/StringDateOnlyPickerGroup.d.ts +9 -10
  123. package/lib/DatePicker/StringDateOnlyPickerGroup.js +35 -35
  124. package/lib/DatePicker/StringDatePickerGroup.d.ts +9 -10
  125. package/lib/DatePicker/StringDatePickerGroup.js +30 -30
  126. package/lib/Field/CustomFieldInputProps.d.ts +7 -7
  127. package/lib/Field/CustomFieldInputProps.js +2 -2
  128. package/lib/Field/CustomFieldMetaProps.d.ts +4 -4
  129. package/lib/Field/CustomFieldMetaProps.js +2 -2
  130. package/lib/Field/Field.d.ts +28 -28
  131. package/lib/Field/Field.js +56 -56
  132. package/lib/Field/FieldErrorScrollTarget.d.ts +2 -3
  133. package/lib/Field/FieldErrorScrollTarget.js +15 -15
  134. package/lib/Field/FieldNameContext.d.ts +3 -3
  135. package/lib/Field/FieldNameContext.js +9 -9
  136. package/lib/Field/FieldSection.d.ts +7 -7
  137. package/lib/Field/FieldSection.js +12 -23
  138. package/lib/Field/InjectedFieldProps.d.ts +7 -7
  139. package/lib/Field/InjectedFieldProps.js +2 -2
  140. package/lib/Field/StandAloneInput.d.ts +13 -13
  141. package/lib/Field/StandAloneInput.js +76 -76
  142. package/lib/Field/useStandardField.d.ts +21 -21
  143. package/lib/Field/useStandardField.js +95 -95
  144. package/lib/FieldArray/FieldArray.d.ts +24 -24
  145. package/lib/FieldArray/FieldArray.js +83 -94
  146. package/lib/File/FileGroup.d.ts +10 -10
  147. package/lib/File/FileGroup.js +58 -58
  148. package/lib/File/FileList.d.ts +5 -6
  149. package/lib/File/FileList.js +13 -24
  150. package/lib/File/humanFileSize.d.ts +1 -1
  151. package/lib/File/humanFileSize.js +10 -10
  152. package/lib/Form/FocusError.d.ts +7 -7
  153. package/lib/Form/FocusError.js +48 -48
  154. package/lib/Form/Form.d.ts +33 -33
  155. package/lib/Form/Form.js +147 -147
  156. package/lib/Form/FormBasedPreventNavigation.d.ts +6 -7
  157. package/lib/Form/FormBasedPreventNavigation.js +26 -26
  158. package/lib/Form/LegacyFormBasedPreventNavigation.d.ts +17 -17
  159. package/lib/Form/LegacyFormBasedPreventNavigation.js +72 -72
  160. package/lib/Form/NewFormBasedPreventNavigation.d.ts +14 -14
  161. package/lib/Form/NewFormBasedPreventNavigation.js +42 -42
  162. package/lib/Form/ServerErrorContext.d.ts +11 -11
  163. package/lib/Form/ServerErrorContext.js +15 -15
  164. package/lib/FormActions.d.ts +12 -13
  165. package/lib/FormActions.js +22 -33
  166. package/lib/FormDefaults.d.ts +4 -4
  167. package/lib/FormDefaults.js +5 -5
  168. package/lib/Group.d.ts +16 -16
  169. package/lib/Group.js +20 -31
  170. package/lib/Input/IconInputGroup.d.ts +12 -12
  171. package/lib/Input/IconInputGroup.js +48 -48
  172. package/lib/Input/InputGroup.d.ts +12 -12
  173. package/lib/Input/InputGroup.js +39 -39
  174. package/lib/Input/MoneyInputGroup.d.ts +9 -9
  175. package/lib/Input/MoneyInputGroup.js +48 -48
  176. package/lib/Input/NumberInputGroup.d.ts +9 -9
  177. package/lib/Input/NumberInputGroup.js +48 -48
  178. package/lib/Input/StringInputGroup.d.ts +8 -8
  179. package/lib/Input/StringInputGroup.js +48 -48
  180. package/lib/Normalization/NormalizationFunction.d.ts +4 -4
  181. package/lib/Normalization/NormalizationFunction.js +2 -2
  182. package/lib/Normalization/normalizers.d.ts +4 -4
  183. package/lib/Normalization/normalizers.js +37 -37
  184. package/lib/Select/BooleanSelectGroup.d.ts +7 -8
  185. package/lib/Select/BooleanSelectGroup.js +35 -35
  186. package/lib/Select/NumberSelectGroup.d.ts +9 -10
  187. package/lib/Select/NumberSelectGroup.js +29 -29
  188. package/lib/Select/SelectGroup.d.ts +27 -28
  189. package/lib/Select/SelectGroup.js +71 -71
  190. package/lib/Select/SelectGroupPropsHelper.d.ts +3 -3
  191. package/lib/Select/SelectGroupPropsHelper.js +2 -2
  192. package/lib/Select/StringSelectGroup.d.ts +9 -10
  193. package/lib/Select/StringSelectGroup.js +29 -29
  194. package/lib/StandardFormActions.d.ts +11 -11
  195. package/lib/StandardFormActions.js +20 -31
  196. package/lib/SubmitFormButton.d.ts +10 -10
  197. package/lib/SubmitFormButton.js +45 -45
  198. package/lib/TextArea/TextAreaGroup.d.ts +9 -9
  199. package/lib/TextArea/TextAreaGroup.js +40 -40
  200. package/lib/Validation/ValidatedApiResult.d.ts +6 -6
  201. package/lib/Validation/ValidatedApiResult.js +2 -2
  202. package/lib/Validation/ValidationError.d.ts +5 -5
  203. package/lib/Validation/ValidationError.js +2 -2
  204. package/lib/Validation/ValidationFunction.d.ts +4 -4
  205. package/lib/Validation/ValidationFunction.js +2 -2
  206. package/lib/Validation/validators.d.ts +18 -18
  207. package/lib/Validation/validators.js +90 -90
  208. package/lib/index.d.ts +73 -73
  209. package/lib/index.js +117 -117
  210. package/lib/setupTests.d.ts +1 -1
  211. package/lib/setupTests.js +3 -3
  212. package/lib/utils/objectContainsNonSerializableProperty.d.ts +1 -1
  213. package/lib/utils/objectContainsNonSerializableProperty.js +17 -17
  214. package/lib/utils/objectToFormData.d.ts +10 -10
  215. package/lib/utils/objectToFormData.js +79 -79
  216. package/lib/utils/typeChecks.d.ts +8 -8
  217. package/lib/utils/typeChecks.js +29 -29
  218. package/package.json +12 -12
  219. package/src/AddressInput/__snapshots__/AddressInput.test.tsx.snap +6 -8
  220. package/src/Select/__tests__/__snapshots__/BooleanSelectGroup.test.tsx.snap +6 -8
  221. package/src/Select/__tests__/__snapshots__/NumberSelectGroup.test.tsx.snap +12 -16
  222. package/src/Select/__tests__/__snapshots__/StringSelectGroup.test.tsx.snap +12 -16
  223. package/src/utils/objectToFormData.test.tsx +1 -1
package/lib/Form/Form.js CHANGED
@@ -1,147 +1,147 @@
1
- "use strict";
2
- var __assign = (this && this.__assign) || function () {
3
- __assign = Object.assign || function(t) {
4
- for (var s, i = 1, n = arguments.length; i < n; i++) {
5
- s = arguments[i];
6
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7
- t[p] = s[p];
8
- }
9
- return t;
10
- };
11
- return __assign.apply(this, arguments);
12
- };
13
- var __rest = (this && this.__rest) || function (s, e) {
14
- var t = {};
15
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
16
- t[p] = s[p];
17
- if (s != null && typeof Object.getOwnPropertySymbols === "function")
18
- for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
19
- if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
20
- t[p[i]] = s[p[i]];
21
- }
22
- return t;
23
- };
24
- var __importDefault = (this && this.__importDefault) || function (mod) {
25
- return (mod && mod.__esModule) ? mod : { "default": mod };
26
- };
27
- Object.defineProperty(exports, "__esModule", { value: true });
28
- var jsx_runtime_1 = require("react/jsx-runtime");
29
- var react_1 = require("react");
30
- var classnames_1 = __importDefault(require("classnames"));
31
- var formik_1 = require("formik");
32
- var FocusError_1 = __importDefault(require("./FocusError"));
33
- var FormBasedPreventNavigation_1 = __importDefault(require("./FormBasedPreventNavigation"));
34
- var ServerErrorContext_1 = require("./ServerErrorContext");
35
- var Field_1 = __importDefault(require("../Field/Field"));
36
- var FieldArray_1 = __importDefault(require("../FieldArray/FieldArray"));
37
- var FormDefaults_1 = require("../FormDefaults");
38
- var objectContainsNonSerializableProperty_1 = __importDefault(require("../utils/objectContainsNonSerializableProperty"));
39
- var objectToFormData_1 = __importDefault(require("../utils/objectToFormData"));
40
- /** Define a form. Uses [formik](https://formik.org/docs/overview). Usually contains many `<Field/>` components. */
41
- function Form(_a) {
42
- 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"]);
43
- // formik resets all error on each blur (with our settings)
44
- // this means that ALL errors from the server disappear when any one field is blurred
45
- // So, we have to store server errors ourselves
46
- // Since we only use useStandardFormInput, that means there is only one consumer
47
- var _b = (0, react_1.useState)({}), serverErrors = _b[0], setServerErrors = _b[1];
48
- var serverErrorContextValue = (0, react_1.useMemo)(function () { return ({
49
- errors: serverErrors,
50
- getError: function (path) {
51
- var lowered = path.toLowerCase();
52
- return serverErrors && serverErrors[lowered];
53
- },
54
- setError: function (path, errorMessage) {
55
- var _a;
56
- var lowered = path.toLowerCase();
57
- setServerErrors(Object.assign({}, serverErrors, (_a = {},
58
- _a[lowered] = !errorMessage ? undefined : errorMessage,
59
- _a)));
60
- },
61
- }); }, [serverErrors]);
62
- return ((0, jsx_runtime_1.jsx)(formik_1.Formik, __assign({ validateOnChange: false, validateOnBlur: true, validateOnMount: false, initialValues: initialValues || {}, onSubmit: handleSubmit }, props, { children: (0, jsx_runtime_1.jsx)(ServerErrorContext_1.ServerErrorContext.Provider, __assign({ value: serverErrorContextValue }, { children: (0, jsx_runtime_1.jsxs)(formik_1.Form, __assign({ className: (0, classnames_1.default)(className, FormDefaults_1.FormDefaults.cssClassPrefix + 'form'), style: style }, { children: [(0, jsx_runtime_1.jsx)(FocusError_1.default, { serverErrors: serverErrorContextValue }), (0, jsx_runtime_1.jsx)(FormBasedPreventNavigation_1.default, { ignoreLostChanges: ignoreLostChanges }), children({
63
- // hack for ref forwarding
64
- Field: Field_1.default,
65
- FieldArray: FieldArray_1.default,
66
- })] })) })) })));
67
- function handleSubmit(values, formikBag) {
68
- var formData = undefined;
69
- var submitFunc;
70
- if ((0, objectContainsNonSerializableProperty_1.default)(values)) {
71
- formData = (0, objectToFormData_1.default)(values, {
72
- indices: true,
73
- dotNotation: true,
74
- allowEmptyArrays: true,
75
- noFileListBrackets: true,
76
- });
77
- if (onFormDataSubmit === undefined) {
78
- throw new Error('No onFormDataSubmit supplied for non-serializable properties.');
79
- }
80
- submitFunc = function () {
81
- return onFormDataSubmit(formData !== null && formData !== void 0 ? formData : new FormData(), formikBag);
82
- };
83
- }
84
- else {
85
- if (onSubmit === undefined) {
86
- formData = (0, objectToFormData_1.default)(values, {
87
- indices: true,
88
- dotNotation: true,
89
- allowEmptyArrays: true,
90
- noFileListBrackets: true,
91
- });
92
- if (onFormDataSubmit === undefined) {
93
- // This error should never occur, as this case is covered by RequireAtLeastOne type safety
94
- throw new Error('No onFormDataSubmit supplied for non-serializable properties.');
95
- }
96
- submitFunc = function () {
97
- return onFormDataSubmit(formData !== null && formData !== void 0 ? formData : new FormData(), formikBag);
98
- };
99
- }
100
- else {
101
- submitFunc = function () { return onSubmit(values, formikBag); };
102
- }
103
- }
104
- return submitFunc()
105
- .then(function (response) {
106
- return response;
107
- })
108
- .catch(function (err) {
109
- //this is an http error
110
- if (err &&
111
- err.response &&
112
- err.response.data &&
113
- err.response.data.validationFailures) {
114
- try {
115
- var serverErrors_1 = err.response.data.validationFailures.reduce(function (acc, value) {
116
- var _a;
117
- // for simplicity, just keep it to one server error at a time per path
118
- // don't care of the property name case changes
119
- var path = (_a = value.propertyName) === null || _a === void 0 ? void 0 : _a.toLowerCase();
120
- if (!!path && !!value.errorMessage) {
121
- acc[path] = value.errorMessage;
122
- }
123
- return acc;
124
- }, {});
125
- setServerErrors(serverErrors_1);
126
- }
127
- catch (err) {
128
- console.error('Failure to getErrorObject');
129
- console.error(err);
130
- throw err;
131
- }
132
- }
133
- throw err;
134
- });
135
- }
136
- }
137
- exports.default = Form;
138
- Form.DisplayFormState = DisplayFormState;
139
- function DisplayFormState() {
140
- var formState = (0, formik_1.useFormikContext)();
141
- var serverErrorContext = (0, react_1.useContext)(ServerErrorContext_1.ServerErrorContext);
142
- return ((0, jsx_runtime_1.jsx)("div", __assign({ style: { margin: '1rem 0' } }, { children: (0, jsx_runtime_1.jsxs)("pre", __assign({ style: {
143
- background: '#f6f8fa',
144
- fontSize: '.65rem',
145
- padding: '.5rem',
146
- } }, { children: [serverErrorContext && serverErrorContext.errors && ((0, jsx_runtime_1.jsxs)("div", { children: [(0, jsx_runtime_1.jsx)("strong", { children: "serverErrors = " }), JSON.stringify(serverErrorContext.errors, null, 2)] })), (0, jsx_runtime_1.jsx)("strong", { children: "formState = " }), JSON.stringify(formState, null, 2)] })) })));
147
- }
1
+ "use strict";
2
+ var __assign = (this && this.__assign) || function () {
3
+ __assign = Object.assign || function(t) {
4
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
5
+ s = arguments[i];
6
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7
+ t[p] = s[p];
8
+ }
9
+ return t;
10
+ };
11
+ return __assign.apply(this, arguments);
12
+ };
13
+ var __rest = (this && this.__rest) || function (s, e) {
14
+ var t = {};
15
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
16
+ t[p] = s[p];
17
+ if (s != null && typeof Object.getOwnPropertySymbols === "function")
18
+ for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
19
+ if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
20
+ t[p[i]] = s[p[i]];
21
+ }
22
+ return t;
23
+ };
24
+ var __importDefault = (this && this.__importDefault) || function (mod) {
25
+ return (mod && mod.__esModule) ? mod : { "default": mod };
26
+ };
27
+ Object.defineProperty(exports, "__esModule", { value: true });
28
+ var jsx_runtime_1 = require("react/jsx-runtime");
29
+ var react_1 = require("react");
30
+ var classnames_1 = __importDefault(require("classnames"));
31
+ var formik_1 = require("formik");
32
+ var FocusError_1 = __importDefault(require("./FocusError"));
33
+ var FormBasedPreventNavigation_1 = __importDefault(require("./FormBasedPreventNavigation"));
34
+ var ServerErrorContext_1 = require("./ServerErrorContext");
35
+ var Field_1 = __importDefault(require("../Field/Field"));
36
+ var FieldArray_1 = __importDefault(require("../FieldArray/FieldArray"));
37
+ var FormDefaults_1 = require("../FormDefaults");
38
+ var objectContainsNonSerializableProperty_1 = __importDefault(require("../utils/objectContainsNonSerializableProperty"));
39
+ var objectToFormData_1 = __importDefault(require("../utils/objectToFormData"));
40
+ /** Define a form. Uses [formik](https://formik.org/docs/overview). Usually contains many `<Field/>` components. */
41
+ function Form(_a) {
42
+ 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"]);
43
+ // formik resets all error on each blur (with our settings)
44
+ // this means that ALL errors from the server disappear when any one field is blurred
45
+ // So, we have to store server errors ourselves
46
+ // Since we only use useStandardFormInput, that means there is only one consumer
47
+ var _b = (0, react_1.useState)({}), serverErrors = _b[0], setServerErrors = _b[1];
48
+ var serverErrorContextValue = (0, react_1.useMemo)(function () { return ({
49
+ errors: serverErrors,
50
+ getError: function (path) {
51
+ var lowered = path.toLowerCase();
52
+ return serverErrors && serverErrors[lowered];
53
+ },
54
+ setError: function (path, errorMessage) {
55
+ var _a;
56
+ var lowered = path.toLowerCase();
57
+ setServerErrors(Object.assign({}, serverErrors, (_a = {},
58
+ _a[lowered] = !errorMessage ? undefined : errorMessage,
59
+ _a)));
60
+ },
61
+ }); }, [serverErrors]);
62
+ return ((0, jsx_runtime_1.jsx)(formik_1.Formik, __assign({ validateOnChange: false, validateOnBlur: true, validateOnMount: false, initialValues: initialValues || {}, onSubmit: handleSubmit }, props, { children: (0, jsx_runtime_1.jsx)(ServerErrorContext_1.ServerErrorContext.Provider, { value: serverErrorContextValue, children: (0, jsx_runtime_1.jsxs)(formik_1.Form, { className: (0, classnames_1.default)(className, FormDefaults_1.FormDefaults.cssClassPrefix + 'form'), style: style, children: [(0, jsx_runtime_1.jsx)(FocusError_1.default, { serverErrors: serverErrorContextValue }), (0, jsx_runtime_1.jsx)(FormBasedPreventNavigation_1.default, { ignoreLostChanges: ignoreLostChanges }), children({
63
+ // hack for ref forwarding
64
+ Field: Field_1.default,
65
+ FieldArray: FieldArray_1.default,
66
+ })] }) }) })));
67
+ function handleSubmit(values, formikBag) {
68
+ var formData = undefined;
69
+ var submitFunc;
70
+ if ((0, objectContainsNonSerializableProperty_1.default)(values)) {
71
+ formData = (0, objectToFormData_1.default)(values, {
72
+ indices: true,
73
+ dotNotation: true,
74
+ allowEmptyArrays: true,
75
+ noFileListBrackets: true,
76
+ });
77
+ if (onFormDataSubmit === undefined) {
78
+ throw new Error('No onFormDataSubmit supplied for non-serializable properties.');
79
+ }
80
+ submitFunc = function () {
81
+ return onFormDataSubmit(formData !== null && formData !== void 0 ? formData : new FormData(), formikBag);
82
+ };
83
+ }
84
+ else {
85
+ if (onSubmit === undefined) {
86
+ formData = (0, objectToFormData_1.default)(values, {
87
+ indices: true,
88
+ dotNotation: true,
89
+ allowEmptyArrays: true,
90
+ noFileListBrackets: true,
91
+ });
92
+ if (onFormDataSubmit === undefined) {
93
+ // This error should never occur, as this case is covered by RequireAtLeastOne type safety
94
+ throw new Error('No onFormDataSubmit supplied for non-serializable properties.');
95
+ }
96
+ submitFunc = function () {
97
+ return onFormDataSubmit(formData !== null && formData !== void 0 ? formData : new FormData(), formikBag);
98
+ };
99
+ }
100
+ else {
101
+ submitFunc = function () { return onSubmit(values, formikBag); };
102
+ }
103
+ }
104
+ return submitFunc()
105
+ .then(function (response) {
106
+ return response;
107
+ })
108
+ .catch(function (err) {
109
+ //this is an http error
110
+ if (err &&
111
+ err.response &&
112
+ err.response.data &&
113
+ err.response.data.validationFailures) {
114
+ try {
115
+ var serverErrors_1 = err.response.data.validationFailures.reduce(function (acc, value) {
116
+ var _a;
117
+ // for simplicity, just keep it to one server error at a time per path
118
+ // don't care of the property name case changes
119
+ var path = (_a = value.propertyName) === null || _a === void 0 ? void 0 : _a.toLowerCase();
120
+ if (!!path && !!value.errorMessage) {
121
+ acc[path] = value.errorMessage;
122
+ }
123
+ return acc;
124
+ }, {});
125
+ setServerErrors(serverErrors_1);
126
+ }
127
+ catch (err) {
128
+ console.error('Failure to getErrorObject');
129
+ console.error(err);
130
+ throw err;
131
+ }
132
+ }
133
+ throw err;
134
+ });
135
+ }
136
+ }
137
+ exports.default = Form;
138
+ Form.DisplayFormState = DisplayFormState;
139
+ function DisplayFormState() {
140
+ var formState = (0, formik_1.useFormikContext)();
141
+ var serverErrorContext = (0, react_1.useContext)(ServerErrorContext_1.ServerErrorContext);
142
+ return ((0, jsx_runtime_1.jsx)("div", { style: { margin: '1rem 0' }, children: (0, jsx_runtime_1.jsxs)("pre", { style: {
143
+ background: '#f6f8fa',
144
+ fontSize: '.65rem',
145
+ padding: '.5rem',
146
+ }, children: [serverErrorContext && serverErrorContext.errors && ((0, jsx_runtime_1.jsxs)("div", { children: [(0, jsx_runtime_1.jsx)("strong", { children: "serverErrors = " }), JSON.stringify(serverErrorContext.errors, null, 2)] })), (0, jsx_runtime_1.jsx)("strong", { children: "formState = " }), JSON.stringify(formState, null, 2)] }) }));
147
+ }
@@ -1,7 +1,6 @@
1
- /// <reference types="react" />
2
- export interface FormBasedPreventNavigationProps {
3
- ignoreLostChanges?: boolean;
4
- promptMessage?: string;
5
- }
6
- /** Prevent the user from navigating away from a form if there are any changes. */
7
- export default function FormBasedPreventNavigation({ ignoreLostChanges, promptMessage, }: FormBasedPreventNavigationProps): JSX.Element;
1
+ export interface FormBasedPreventNavigationProps {
2
+ ignoreLostChanges?: boolean;
3
+ promptMessage?: string;
4
+ }
5
+ /** Prevent the user from navigating away from a form if there are any changes. */
6
+ export default function FormBasedPreventNavigation({ ignoreLostChanges, promptMessage, }: FormBasedPreventNavigationProps): import("react/jsx-runtime").JSX.Element;
@@ -1,26 +1,26 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- var jsx_runtime_1 = require("react/jsx-runtime");
7
- var react_1 = require("react");
8
- var react_router_dom_1 = require("react-router-dom");
9
- var formik_1 = require("formik");
10
- var LegacyFormBasedPreventNavigation_1 = __importDefault(require("./LegacyFormBasedPreventNavigation"));
11
- var NewFormBasedPreventNavigation_1 = __importDefault(require("./NewFormBasedPreventNavigation"));
12
- /** Prevent the user from navigating away from a form if there are any changes. */
13
- function FormBasedPreventNavigation(_a) {
14
- var ignoreLostChanges = _a.ignoreLostChanges, _b = _a.promptMessage, promptMessage = _b === void 0 ? 'Changes you made may not be saved.' : _b;
15
- var _c = (0, formik_1.useFormikContext)(), dirty = _c.dirty, isSubmitting = _c.isSubmitting;
16
- var preventNavigate = !ignoreLostChanges && dirty && !isSubmitting;
17
- var navigator = (0, react_1.useContext)(react_router_dom_1.UNSAFE_NavigationContext).navigator;
18
- var isUsingDataRouter = navigator.location === undefined;
19
- if (isUsingDataRouter) {
20
- return ((0, jsx_runtime_1.jsx)(NewFormBasedPreventNavigation_1.default, { promptMessage: promptMessage, preventNavigate: preventNavigate, navigator: navigator }));
21
- }
22
- else {
23
- return ((0, jsx_runtime_1.jsx)(LegacyFormBasedPreventNavigation_1.default, { promptMessage: promptMessage, preventNavigate: preventNavigate, navigator: navigator }));
24
- }
25
- }
26
- exports.default = FormBasedPreventNavigation;
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ var jsx_runtime_1 = require("react/jsx-runtime");
7
+ var react_1 = require("react");
8
+ var react_router_dom_1 = require("react-router-dom");
9
+ var formik_1 = require("formik");
10
+ var LegacyFormBasedPreventNavigation_1 = __importDefault(require("./LegacyFormBasedPreventNavigation"));
11
+ var NewFormBasedPreventNavigation_1 = __importDefault(require("./NewFormBasedPreventNavigation"));
12
+ /** Prevent the user from navigating away from a form if there are any changes. */
13
+ function FormBasedPreventNavigation(_a) {
14
+ var ignoreLostChanges = _a.ignoreLostChanges, _b = _a.promptMessage, promptMessage = _b === void 0 ? 'Changes you made may not be saved.' : _b;
15
+ var _c = (0, formik_1.useFormikContext)(), dirty = _c.dirty, isSubmitting = _c.isSubmitting;
16
+ var preventNavigate = !ignoreLostChanges && dirty && !isSubmitting;
17
+ var navigator = (0, react_1.useContext)(react_router_dom_1.UNSAFE_NavigationContext).navigator;
18
+ var isUsingDataRouter = navigator.location === undefined;
19
+ if (isUsingDataRouter) {
20
+ return ((0, jsx_runtime_1.jsx)(NewFormBasedPreventNavigation_1.default, { promptMessage: promptMessage, preventNavigate: preventNavigate, navigator: navigator }));
21
+ }
22
+ else {
23
+ return ((0, jsx_runtime_1.jsx)(LegacyFormBasedPreventNavigation_1.default, { promptMessage: promptMessage, preventNavigate: preventNavigate, navigator: navigator }));
24
+ }
25
+ }
26
+ exports.default = FormBasedPreventNavigation;
@@ -1,17 +1,17 @@
1
- import type { History } from 'history';
2
- import { Navigator as BaseNavigator } from 'react-router-dom';
3
- interface Navigator extends BaseNavigator {
4
- block?: History['block'];
5
- }
6
- export interface FormBasedPreventNavigationProps {
7
- promptMessage?: string;
8
- preventNavigate: boolean;
9
- navigator: Navigator;
10
- }
11
- /** Only use this if the project uses `<BrowserRouter/>` or any router that does not support the new
12
- * [react-router Data API](https://reactrouter.com/en/main/routers/picking-a-router#using-v64-data-apis)
13
- *
14
- * Legacy function to prevent the user from navigating away from a form if there are any changes.
15
- */
16
- export default function LegacyFormBasedPreventNavigation({ promptMessage, preventNavigate, navigator, }: FormBasedPreventNavigationProps): null;
17
- export {};
1
+ import type { History } from 'history';
2
+ import { Navigator as BaseNavigator } from 'react-router-dom';
3
+ interface Navigator extends BaseNavigator {
4
+ block?: History['block'];
5
+ }
6
+ export interface FormBasedPreventNavigationProps {
7
+ promptMessage?: string;
8
+ preventNavigate: boolean;
9
+ navigator: Navigator;
10
+ }
11
+ /** Only use this if the project uses `<BrowserRouter/>` or any router that does not support the new
12
+ * [react-router Data API](https://reactrouter.com/en/main/routers/picking-a-router#using-v64-data-apis)
13
+ *
14
+ * Legacy function to prevent the user from navigating away from a form if there are any changes.
15
+ */
16
+ export default function LegacyFormBasedPreventNavigation({ promptMessage, preventNavigate, navigator, }: FormBasedPreventNavigationProps): null;
17
+ export {};
@@ -1,72 +1,72 @@
1
- "use strict";
2
- var __assign = (this && this.__assign) || function () {
3
- __assign = Object.assign || function(t) {
4
- for (var s, i = 1, n = arguments.length; i < n; i++) {
5
- s = arguments[i];
6
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7
- t[p] = s[p];
8
- }
9
- return t;
10
- };
11
- return __assign.apply(this, arguments);
12
- };
13
- Object.defineProperty(exports, "__esModule", { value: true });
14
- var react_1 = require("react");
15
- /** Only use this if the project uses `<BrowserRouter/>` or any router that does not support the new
16
- * [react-router Data API](https://reactrouter.com/en/main/routers/picking-a-router#using-v64-data-apis)
17
- *
18
- * Legacy function to prevent the user from navigating away from a form if there are any changes.
19
- */
20
- function LegacyFormBasedPreventNavigation(_a) {
21
- var _b = _a.promptMessage, promptMessage = _b === void 0 ? 'Changes you made may not be saved.' : _b, preventNavigate = _a.preventNavigate, navigator = _a.navigator;
22
- (0, react_1.useEffect)(function () {
23
- if (!preventNavigate) {
24
- return;
25
- }
26
- var unblock = function () { };
27
- var push = navigator.push;
28
- // TODO: https://reactrouter.com/docs/en/v6/upgrading/v5#prompt-is-not-currently-supported
29
- // this is a workaround until we get native support for prompt on navigate
30
- if (navigator.block) {
31
- var blocker_1 = function (tx) {
32
- if (window.confirm(promptMessage)) {
33
- tx.retry();
34
- }
35
- };
36
- unblock = navigator.block(function (tx) {
37
- var autoUnblockingTx = __assign(__assign({}, tx), { retry: function () {
38
- // Automatically unblock the transition so it can play all the way
39
- // through before retrying it. TODO: Figure out how to re-enable
40
- // this block if the transition is cancelled for some reason.
41
- unblock();
42
- tx.retry();
43
- } });
44
- blocker_1(autoUnblockingTx);
45
- });
46
- }
47
- else {
48
- //https://gist.github.com/MarksCode/64e438c82b0b2a1161e01c88ca0d0355
49
- navigator.push = function () {
50
- var args = [];
51
- for (var _i = 0; _i < arguments.length; _i++) {
52
- args[_i] = arguments[_i];
53
- }
54
- if (window.confirm(promptMessage)) {
55
- push.apply(void 0, args);
56
- }
57
- };
58
- }
59
- window.addEventListener('beforeunload', beforeUnload);
60
- return function () {
61
- unblock();
62
- navigator.push = push;
63
- window.removeEventListener('beforeunload', beforeUnload);
64
- };
65
- function beforeUnload(e) {
66
- e.preventDefault();
67
- e.returnValue = promptMessage;
68
- }
69
- }, [preventNavigate, promptMessage, navigator]);
70
- return null;
71
- }
72
- exports.default = LegacyFormBasedPreventNavigation;
1
+ "use strict";
2
+ var __assign = (this && this.__assign) || function () {
3
+ __assign = Object.assign || function(t) {
4
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
5
+ s = arguments[i];
6
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7
+ t[p] = s[p];
8
+ }
9
+ return t;
10
+ };
11
+ return __assign.apply(this, arguments);
12
+ };
13
+ Object.defineProperty(exports, "__esModule", { value: true });
14
+ var react_1 = require("react");
15
+ /** Only use this if the project uses `<BrowserRouter/>` or any router that does not support the new
16
+ * [react-router Data API](https://reactrouter.com/en/main/routers/picking-a-router#using-v64-data-apis)
17
+ *
18
+ * Legacy function to prevent the user from navigating away from a form if there are any changes.
19
+ */
20
+ function LegacyFormBasedPreventNavigation(_a) {
21
+ var _b = _a.promptMessage, promptMessage = _b === void 0 ? 'Changes you made may not be saved.' : _b, preventNavigate = _a.preventNavigate, navigator = _a.navigator;
22
+ (0, react_1.useEffect)(function () {
23
+ if (!preventNavigate) {
24
+ return;
25
+ }
26
+ var unblock = function () { };
27
+ var push = navigator.push;
28
+ // TODO: https://reactrouter.com/docs/en/v6/upgrading/v5#prompt-is-not-currently-supported
29
+ // this is a workaround until we get native support for prompt on navigate
30
+ if (navigator.block) {
31
+ var blocker_1 = function (tx) {
32
+ if (window.confirm(promptMessage)) {
33
+ tx.retry();
34
+ }
35
+ };
36
+ unblock = navigator.block(function (tx) {
37
+ var autoUnblockingTx = __assign(__assign({}, tx), { retry: function () {
38
+ // Automatically unblock the transition so it can play all the way
39
+ // through before retrying it. TODO: Figure out how to re-enable
40
+ // this block if the transition is cancelled for some reason.
41
+ unblock();
42
+ tx.retry();
43
+ } });
44
+ blocker_1(autoUnblockingTx);
45
+ });
46
+ }
47
+ else {
48
+ //https://gist.github.com/MarksCode/64e438c82b0b2a1161e01c88ca0d0355
49
+ navigator.push = function () {
50
+ var args = [];
51
+ for (var _i = 0; _i < arguments.length; _i++) {
52
+ args[_i] = arguments[_i];
53
+ }
54
+ if (window.confirm(promptMessage)) {
55
+ push.apply(void 0, args);
56
+ }
57
+ };
58
+ }
59
+ window.addEventListener('beforeunload', beforeUnload);
60
+ return function () {
61
+ unblock();
62
+ navigator.push = push;
63
+ window.removeEventListener('beforeunload', beforeUnload);
64
+ };
65
+ function beforeUnload(e) {
66
+ e.preventDefault();
67
+ e.returnValue = promptMessage;
68
+ }
69
+ }, [preventNavigate, promptMessage, navigator]);
70
+ return null;
71
+ }
72
+ exports.default = LegacyFormBasedPreventNavigation;
@@ -1,14 +1,14 @@
1
- import type { History } from 'history';
2
- import { Navigator as BaseNavigator } from 'react-router-dom';
3
- interface Navigator extends BaseNavigator {
4
- block?: History['block'];
5
- location: Location;
6
- }
7
- interface FormBasedPreventNavigationProps {
8
- promptMessage?: string;
9
- preventNavigate: boolean;
10
- navigator: Navigator;
11
- }
12
- /** Temporary function to allow backwards compatibility with routers that do not use the new Data API */
13
- export default function NewFormBasedPreventNavigation({ promptMessage, preventNavigate, navigator, }: FormBasedPreventNavigationProps): null;
14
- export {};
1
+ import type { History } from 'history';
2
+ import { Navigator as BaseNavigator } from 'react-router-dom';
3
+ interface Navigator extends BaseNavigator {
4
+ block?: History['block'];
5
+ location: Location;
6
+ }
7
+ interface FormBasedPreventNavigationProps {
8
+ promptMessage?: string;
9
+ preventNavigate: boolean;
10
+ navigator: Navigator;
11
+ }
12
+ /** Temporary function to allow backwards compatibility with routers that do not use the new Data API */
13
+ export default function NewFormBasedPreventNavigation({ promptMessage, preventNavigate, navigator, }: FormBasedPreventNavigationProps): null;
14
+ export {};