envoc-form 4.0.1-11 → 4.0.1-12

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 (196) hide show
  1. package/README.md +5117 -12
  2. package/es/AddressInput/AddressInput.d.ts +10 -0
  3. package/es/AddressInput/AddressInput.js +10 -0
  4. package/es/ConfirmBaseForm/ConfirmBaseForm.d.ts +14 -0
  5. package/es/ConfirmBaseForm/ConfirmBaseForm.js +6 -0
  6. package/es/ConfirmDeleteForm/ConfirmDeleteForm.d.ts +13 -0
  7. package/es/ConfirmDeleteForm/ConfirmDeleteForm.js +5 -0
  8. package/es/DatePicker/DatePickerGroup.d.ts +5 -0
  9. package/es/DatePicker/DatePickerGroup.js +5 -0
  10. package/es/DatePicker/StringDateOnlyPickerGroup.d.ts +5 -0
  11. package/es/DatePicker/StringDateOnlyPickerGroup.js +5 -0
  12. package/es/DatePicker/StringDatePickerGroup.d.ts +5 -0
  13. package/es/DatePicker/StringDatePickerGroup.js +5 -0
  14. package/es/Field/Field.d.ts +6 -0
  15. package/es/Field/FieldErrorScrollTarget.d.ts +1 -0
  16. package/es/Field/FieldErrorScrollTarget.js +1 -0
  17. package/es/Field/StandAloneInput.d.ts +4 -0
  18. package/es/Field/StandAloneInput.js +1 -0
  19. package/es/Field/useStandardField.d.ts +6 -0
  20. package/es/Field/useStandardField.js +8 -9
  21. package/es/FieldArray/FieldArray.d.ts +9 -0
  22. package/es/FieldArray/FieldArray.js +5 -0
  23. package/es/File/FileGroup.d.ts +2 -0
  24. package/es/File/FileGroup.js +1 -0
  25. package/es/Form/FocusError.d.ts +2 -0
  26. package/es/Form/FocusError.js +1 -0
  27. package/es/Form/Form.d.ts +6 -0
  28. package/es/Form/Form.js +1 -0
  29. package/es/Form/FormBasedPreventNavigation.d.ts +3 -1
  30. package/es/Form/FormBasedPreventNavigation.js +12 -62
  31. package/es/Form/LegacyFormBasedPreventNavigation.d.ts +17 -0
  32. package/es/Form/LegacyFormBasedPreventNavigation.js +69 -0
  33. package/es/Form/NewFormBasedPreventNavigation.d.ts +14 -0
  34. package/es/Form/NewFormBasedPreventNavigation.js +39 -0
  35. package/es/Form/ServerErrorContext.d.ts +1 -0
  36. package/es/Form/ServerErrorContext.js +1 -0
  37. package/es/FormActions.d.ts +6 -0
  38. package/es/FormActions.js +1 -0
  39. package/es/FormDefaults.d.ts +1 -0
  40. package/es/FormDefaults.js +1 -0
  41. package/es/Group.d.ts +5 -3
  42. package/es/Group.js +2 -2
  43. package/es/Input/IconInputGroup.d.ts +3 -0
  44. package/es/Input/IconInputGroup.js +1 -0
  45. package/es/Input/InputGroup.d.ts +1 -1
  46. package/es/Input/InputGroup.js +2 -2
  47. package/es/Input/MoneyInputGroup.d.ts +2 -0
  48. package/es/Input/MoneyInputGroup.js +1 -0
  49. package/es/Input/NumberInputGroup.d.ts +2 -0
  50. package/es/Input/NumberInputGroup.js +1 -0
  51. package/es/Input/StringInputGroup.d.ts +1 -0
  52. package/es/Input/StringInputGroup.js +1 -0
  53. package/es/Normalization/normalizers.d.ts +2 -0
  54. package/es/Normalization/normalizers.js +2 -0
  55. package/es/Select/BooleanSelectGroup.d.ts +1 -0
  56. package/es/Select/BooleanSelectGroup.js +1 -0
  57. package/es/Select/NumberSelectGroup.d.ts +2 -0
  58. package/es/Select/NumberSelectGroup.js +2 -0
  59. package/es/Select/SelectGroup.d.ts +6 -0
  60. package/es/Select/SelectGroup.js +1 -0
  61. package/es/Select/StringSelectGroup.d.ts +2 -0
  62. package/es/Select/StringSelectGroup.js +2 -0
  63. package/es/StandardFormActions.d.ts +5 -0
  64. package/es/StandardFormActions.js +1 -0
  65. package/es/SubmitFormButton.d.ts +3 -0
  66. package/es/SubmitFormButton.js +1 -0
  67. package/es/TextArea/TextAreaGroup.d.ts +1 -0
  68. package/es/TextArea/TextAreaGroup.js +1 -1
  69. package/es/Validation/validators.d.ts +3 -0
  70. package/es/Validation/validators.js +3 -2
  71. package/es/setupTests.d.ts +1 -0
  72. package/es/setupTests.js +1 -0
  73. package/lib/AddressInput/AddressInput.d.ts +10 -0
  74. package/lib/AddressInput/AddressInput.js +10 -0
  75. package/lib/ConfirmBaseForm/ConfirmBaseForm.d.ts +14 -0
  76. package/lib/ConfirmBaseForm/ConfirmBaseForm.js +6 -0
  77. package/lib/ConfirmDeleteForm/ConfirmDeleteForm.d.ts +13 -0
  78. package/lib/ConfirmDeleteForm/ConfirmDeleteForm.js +5 -0
  79. package/lib/DatePicker/DatePickerGroup.d.ts +5 -0
  80. package/lib/DatePicker/DatePickerGroup.js +5 -0
  81. package/lib/DatePicker/StringDateOnlyPickerGroup.d.ts +5 -0
  82. package/lib/DatePicker/StringDateOnlyPickerGroup.js +5 -0
  83. package/lib/DatePicker/StringDatePickerGroup.d.ts +5 -0
  84. package/lib/DatePicker/StringDatePickerGroup.js +5 -0
  85. package/lib/Field/Field.d.ts +6 -0
  86. package/lib/Field/FieldErrorScrollTarget.d.ts +1 -0
  87. package/lib/Field/FieldErrorScrollTarget.js +1 -0
  88. package/lib/Field/StandAloneInput.d.ts +4 -0
  89. package/lib/Field/StandAloneInput.js +1 -0
  90. package/lib/Field/useStandardField.d.ts +6 -0
  91. package/lib/Field/useStandardField.js +8 -9
  92. package/lib/FieldArray/FieldArray.d.ts +9 -0
  93. package/lib/FieldArray/FieldArray.js +5 -0
  94. package/lib/File/FileGroup.d.ts +2 -0
  95. package/lib/File/FileGroup.js +1 -0
  96. package/lib/Form/FocusError.d.ts +2 -0
  97. package/lib/Form/FocusError.js +1 -0
  98. package/lib/Form/Form.d.ts +6 -0
  99. package/lib/Form/Form.js +1 -0
  100. package/lib/Form/FormBasedPreventNavigation.d.ts +3 -1
  101. package/lib/Form/FormBasedPreventNavigation.js +13 -60
  102. package/lib/Form/LegacyFormBasedPreventNavigation.d.ts +17 -0
  103. package/lib/Form/LegacyFormBasedPreventNavigation.js +72 -0
  104. package/lib/Form/NewFormBasedPreventNavigation.d.ts +14 -0
  105. package/lib/Form/NewFormBasedPreventNavigation.js +42 -0
  106. package/lib/Form/ServerErrorContext.d.ts +1 -0
  107. package/lib/Form/ServerErrorContext.js +1 -0
  108. package/lib/FormActions.d.ts +6 -0
  109. package/lib/FormActions.js +1 -0
  110. package/lib/FormDefaults.d.ts +1 -0
  111. package/lib/FormDefaults.js +1 -0
  112. package/lib/Group.d.ts +5 -3
  113. package/lib/Group.js +2 -2
  114. package/lib/Input/IconInputGroup.d.ts +3 -0
  115. package/lib/Input/IconInputGroup.js +1 -0
  116. package/lib/Input/InputGroup.d.ts +1 -1
  117. package/lib/Input/InputGroup.js +2 -2
  118. package/lib/Input/MoneyInputGroup.d.ts +2 -0
  119. package/lib/Input/MoneyInputGroup.js +1 -0
  120. package/lib/Input/NumberInputGroup.d.ts +2 -0
  121. package/lib/Input/NumberInputGroup.js +1 -0
  122. package/lib/Input/StringInputGroup.d.ts +1 -0
  123. package/lib/Input/StringInputGroup.js +1 -0
  124. package/lib/Normalization/normalizers.d.ts +2 -0
  125. package/lib/Normalization/normalizers.js +2 -0
  126. package/lib/Select/BooleanSelectGroup.d.ts +1 -0
  127. package/lib/Select/BooleanSelectGroup.js +1 -0
  128. package/lib/Select/NumberSelectGroup.d.ts +2 -0
  129. package/lib/Select/NumberSelectGroup.js +2 -0
  130. package/lib/Select/SelectGroup.d.ts +6 -0
  131. package/lib/Select/SelectGroup.js +1 -0
  132. package/lib/Select/StringSelectGroup.d.ts +2 -0
  133. package/lib/Select/StringSelectGroup.js +2 -0
  134. package/lib/StandardFormActions.d.ts +5 -0
  135. package/lib/StandardFormActions.js +1 -0
  136. package/lib/SubmitFormButton.d.ts +3 -0
  137. package/lib/SubmitFormButton.js +1 -0
  138. package/lib/TextArea/TextAreaGroup.d.ts +1 -0
  139. package/lib/TextArea/TextAreaGroup.js +1 -1
  140. package/lib/Validation/validators.d.ts +3 -0
  141. package/lib/Validation/validators.js +3 -2
  142. package/lib/setupTests.d.ts +1 -0
  143. package/lib/setupTests.js +3 -0
  144. package/package.json +9 -6
  145. package/src/AddressInput/AddressInput.tsx +10 -0
  146. package/src/AddressInput/__snapshots__/AddressInput.test.tsx.snap +4 -0
  147. package/src/ConfirmBaseForm/ConfirmBaseForm.tsx +14 -0
  148. package/src/ConfirmDeleteForm/ConfirmDeleteForm.tsx +13 -0
  149. package/src/DatePicker/DatePicker.test.tsx +1 -1
  150. package/src/DatePicker/DatePickerGroup.tsx +5 -2
  151. package/src/DatePicker/StringDateOnlyPickerGroup.tsx +5 -0
  152. package/src/DatePicker/StringDatePickerGroup.tsx +6 -0
  153. package/src/Field/Field.tsx +6 -1
  154. package/src/Field/FieldErrorScrollTarget.tsx +1 -0
  155. package/src/Field/StandAloneInput.tsx +4 -0
  156. package/src/Field/useStandardField.ts +13 -9
  157. package/src/FieldArray/FieldArray.tsx +9 -0
  158. package/src/File/FileGroup.tsx +2 -0
  159. package/src/Form/FocusError.tsx +3 -0
  160. package/src/Form/Form.tsx +6 -0
  161. package/src/Form/FormBasedPreventNavigation.tsx +26 -60
  162. package/src/Form/LegacyFormBasedPreventNavigation.tsx +77 -0
  163. package/src/Form/NewFormBasedPreventNavigation.tsx +59 -0
  164. package/src/Form/ServerErrorContext.ts +1 -0
  165. package/src/FormActions.tsx +7 -0
  166. package/src/FormDefaults.ts +1 -0
  167. package/src/Group.tsx +8 -4
  168. package/src/Input/IconInputGroup.tsx +3 -0
  169. package/src/Input/InputGroup.tsx +3 -1
  170. package/src/Input/MoneyInputGroup.tsx +2 -0
  171. package/src/Input/NumberInputGroup.tsx +2 -0
  172. package/src/Input/StringInputGroup.tsx +1 -0
  173. package/src/Input/__Tests__/__snapshots__/IconInputGroup.test.tsx.snap +1 -0
  174. package/src/Input/__Tests__/__snapshots__/MoneyInputGroup.test.tsx.snap +1 -0
  175. package/src/Input/__Tests__/__snapshots__/NumberInputGroup.test.tsx.snap +1 -0
  176. package/src/Input/__Tests__/__snapshots__/StringInputGroup.test.tsx.snap +1 -0
  177. package/src/Normalization/normalizers.ts +2 -0
  178. package/src/Select/BooleanSelectGroup.tsx +1 -0
  179. package/src/Select/NumberSelectGroup.tsx +2 -0
  180. package/src/Select/SelectGroup.tsx +6 -0
  181. package/src/Select/StringSelectGroup.tsx +2 -0
  182. package/src/StandardFormActions.tsx +5 -0
  183. package/src/SubmitFormButton.tsx +3 -0
  184. package/src/TextArea/TextAreaGroup.tsx +1 -1
  185. package/src/Validation/validators.ts +3 -2
  186. package/src/__Tests__/FormTestBase.tsx +7 -6
  187. package/src/__Tests__/RealisticForm.test.tsx +82 -0
  188. package/src/setupTests.ts +1 -0
  189. package/es/__Tests__/FormTestBase.d.ts +0 -27
  190. package/es/__Tests__/FormTestBase.js +0 -83
  191. package/es/__Tests__/index.d.ts +0 -2
  192. package/es/__Tests__/index.js +0 -2
  193. package/lib/__Tests__/FormTestBase.d.ts +0 -27
  194. package/lib/__Tests__/FormTestBase.js +0 -86
  195. package/lib/__Tests__/index.d.ts +0 -2
  196. package/lib/__Tests__/index.js +0 -8
@@ -8,14 +8,19 @@ import { NormalizationFunction } from '../Normalization/NormalizationFunction';
8
8
  import { ValidationFunction } from '../Validation/ValidationFunction';
9
9
 
10
10
  export interface useStandardFieldProps<TValue> {
11
+ /** Id of the field. */
11
12
  id?: string;
13
+ /** Name of the field. */
12
14
  name: string;
15
+ /** Whether the field should be disabled. */
13
16
  disabled?: boolean;
17
+ /** Function to validate the field. */
14
18
  validate?: ValidationFunction<TValue> | ValidationFunction<TValue>[];
19
+ /** Function to modify the field value without making the form dirty. (e.g. phone number) */
15
20
  normalize?: NormalizationFunction<TValue>;
16
21
  }
17
22
 
18
- // provides a consistent way to deal with all form fields (non array)
23
+ /** Provides a consistent way to deal with all form fields (non array). */
19
24
  export default function useStandardField<TValue>({
20
25
  id: providedId,
21
26
  name: providedName,
@@ -51,7 +56,7 @@ export default function useStandardField<TValue>({
51
56
  disabled: disabled,
52
57
  validate: callAllValidators,
53
58
  });
54
- const { setFieldValue, isSubmitting } = useFormikContext();
59
+ const { setFieldTouched, setFieldValue, isSubmitting } = useFormikContext();
55
60
 
56
61
  const touched =
57
62
  formikMeta.touched !== false && formikMeta.touched !== undefined;
@@ -59,14 +64,13 @@ export default function useStandardField<TValue>({
59
64
  if (!touched && isSubmitting) {
60
65
  // because we do not always register all fields up front.
61
66
  // e.g. formik expects even a 'create' form to have all fields given, at least, blank values
62
- // that seems to be how it touches on submit:
63
- // see: https://codesandbox.io/s/formik-example-4n7n8 vs https://codesandbox.io/s/formik-example-kttk5
64
- // note in particular the change in line 24
65
- // thus, we manually touch all fields here by calling onBlur
66
- // there is a "setFieldTouched" from useFormikContext but it doesn't appear to work on field arrays
67
- handleBlur();
67
+ // It looks like this was going to be a thing: https://github.com/jaredpalmer/formik/issues/691
68
+ // Formik appears to not have an active maintainer: https://github.com/jaredpalmer/formik/discussions/3526
69
+ // We previously had a different fix in place using handleBlur, but it was causing an infinite update cycle.
70
+ // This was noted as existing, but there was a note about it not working for FieldArray (this does appear to work in my testing with FieldArray)
71
+ setFieldTouched(name);
68
72
  }
69
- });
73
+ }, [isSubmitting, name, setFieldTouched, touched]);
70
74
 
71
75
  // these are the props we expect consumers of this hook to pass directly to the input (or other control)
72
76
  const resultInput: CustomFieldInputProps<TValue> = {
@@ -11,9 +11,13 @@ export type FieldArrayProps<
11
11
  TProp extends keyof TForm
12
12
  > = TForm[TProp] extends Array<any> | undefined | null
13
13
  ? {
14
+ /** Name of the field, used on submission. If using codegen this must be the provided dto. */
14
15
  name: TProp;
16
+ /** Label of the field. */
15
17
  label?: string;
18
+ /** Whether the field should be disabled. */
16
19
  disabled?: boolean;
20
+ /** Function to validate the value. */
17
21
  validate?:
18
22
  | ValidationFunction<TForm[TProp]>
19
23
  | ValidationFunction<TForm[TProp]>[];
@@ -41,6 +45,11 @@ export type ArrayFormBuilderProp<TValue extends Array<any> | undefined | null> =
41
45
  : never
42
46
  : never;
43
47
 
48
+ /**
49
+ * An array of fields that allows the user to add multiple instances of the same field.
50
+ *
51
+ * Includes "Add Item" and "Remove Item" buttons to allow the user to speicify the number of fields.
52
+ */
44
53
  export default function FieldArray<
45
54
  TForm extends object,
46
55
  TProp extends keyof TForm
@@ -13,6 +13,7 @@ export interface FileGroupProps
13
13
  React.HTMLProps<HTMLInputElement>,
14
14
  keyof InjectedFieldProps<any> | 'children' | 'className' | 'label'
15
15
  > {
16
+ /** Allow multiple files to be uploaded. */
16
17
  multiple?: boolean | undefined;
17
18
  }
18
19
 
@@ -76,6 +77,7 @@ function FileGroup(
76
77
  );
77
78
  }
78
79
 
80
+ /** File upload input group. */
79
81
  const FileGroupWithRef = React.forwardRef(
80
82
  FileGroup
81
83
  ) as ComponentType<FileGroupProps>;
@@ -4,8 +4,11 @@ import smoothscroll from 'smoothscroll-polyfill';
4
4
  import { ServerErrorContextProps } from './ServerErrorContext';
5
5
 
6
6
  export interface FocusErrorProps {
7
+ /** Validation errors that have been received from the server. */
7
8
  serverErrors: ServerErrorContextProps;
8
9
  }
10
+
11
+ /** Function to scroll to the field that has an error. */
9
12
  export default function FocusError(props: FocusErrorProps) {
10
13
  const { errors, isSubmitting, isValidating } = useFormikContext();
11
14
  smoothscroll.polyfill();
package/src/Form/Form.tsx CHANGED
@@ -41,18 +41,23 @@ export type FormBuilderProp<TForm extends object> = {
41
41
  };
42
42
 
43
43
  export interface FullFormProps<TForm extends object> {
44
+ /** The `<Field/>` and `<FieldArray/>` components. */
44
45
  children: (formBuilder: FormBuilderProp<TForm>) => JSX.Element;
46
+ /** Submission handler */
45
47
  onSubmit: (
46
48
  formValues: TForm,
47
49
  formikBag: FormikHelpers<TForm>
48
50
  ) => Promise<ValidatedApiResult>;
51
+ /** Submission handler for forms that use [FormData](https://developer.mozilla.org/en-US/docs/Web/API/FormData).*/
49
52
  onFormDataSubmit: (
50
53
  formValues: FormData,
51
54
  formikBag: FormikHelpers<TForm>
52
55
  ) => Promise<ValidatedApiResult>;
53
56
  className?: string;
54
57
  style?: CSSProperties;
58
+ /** Prevent the user from leaving the form if they have edited any field. This is presented as a JS `alert()`. */
55
59
  ignoreLostChanges?: boolean;
60
+ /** The intitial values of the form. */
56
61
  initialValues?: TForm;
57
62
  }
58
63
 
@@ -69,6 +74,7 @@ export type FormProps<TForm extends object> = RequireAtLeastOne<
69
74
  'onSubmit' | 'onFormDataSubmit'
70
75
  >;
71
76
 
77
+ /** Define a form. Uses [formik](https://formik.org/docs/overview). Usually contains many `<Field/>` components. */
72
78
  export default function Form<TForm extends object>({
73
79
  children,
74
80
  className,
@@ -1,21 +1,18 @@
1
- import type { History, Blocker, Transition } from 'history';
2
- import { ContextType, useContext, useEffect } from 'react';
3
- // see: https://gist.github.com/rmorse/426ffcc579922a82749934826fa9f743
1
+ import type { History } from 'history';
2
+ import { ContextType, useContext } from 'react';
4
3
  import {
5
4
  Navigator as BaseNavigator,
6
5
  UNSAFE_NavigationContext as NavigationContext,
7
6
  } from 'react-router-dom';
8
7
  import { useFormikContext } from 'formik';
8
+ import LegacyFormBasedPreventNavigation from './LegacyFormBasedPreventNavigation';
9
+ import NewFormBasedPreventNavigation from './NewFormBasedPreventNavigation';
9
10
 
10
11
  interface Navigator extends BaseNavigator {
11
12
  block?: History['block'];
13
+ location: Location;
12
14
  }
13
15
 
14
- //The Current state of the world (2023-05-10) is that useBlocker exists in react-router, but is only available for data routers, which we currently don't use
15
- // `block` was available on UNSAFE_NavigationContext previously, but was removed
16
- // Modifying `push` prevents _most_ but not all navigations with a prompt, long term solution may be to remove FileSystemRoutes and swap to a data router (https://reactrouter.com/en/main/routers/create-browser-router)
17
-
18
- // see: https://github.com/remix-run/react-router/issues/8139#issuecomment-1023105785
19
16
  type NavigationContextWithBlock = ContextType<typeof NavigationContext> & {
20
17
  navigator: Navigator;
21
18
  };
@@ -24,6 +21,8 @@ export interface FormBasedPreventNavigationProps {
24
21
  ignoreLostChanges?: boolean;
25
22
  promptMessage?: string;
26
23
  }
24
+
25
+ /** Prevent the user from navigating away from a form if there are any changes. */
27
26
  export default function FormBasedPreventNavigation({
28
27
  ignoreLostChanges,
29
28
  promptMessage = 'Changes you made may not be saved.',
@@ -35,56 +34,23 @@ export default function FormBasedPreventNavigation({
35
34
  NavigationContext
36
35
  ) as NavigationContextWithBlock;
37
36
 
38
- useEffect(() => {
39
- if (!preventNavigate) {
40
- return;
41
- }
42
- let unblock = () => {};
43
- const push = navigator.push;
44
-
45
- // TODO: https://reactrouter.com/docs/en/v6/upgrading/v5#prompt-is-not-currently-supported
46
- // this is a workaround until we get native support for prompt on navigate
47
- if (navigator.block) {
48
- const blocker: Blocker = (tx) => {
49
- if (window.confirm(promptMessage)) {
50
- tx.retry();
51
- }
52
- };
53
- unblock = navigator.block((tx: Transition) => {
54
- const autoUnblockingTx = {
55
- ...tx,
56
- retry() {
57
- // Automatically unblock the transition so it can play all the way
58
- // through before retrying it. TODO: Figure out how to re-enable
59
- // this block if the transition is cancelled for some reason.
60
- unblock();
61
- tx.retry();
62
- },
63
- };
64
-
65
- blocker(autoUnblockingTx);
66
- });
67
- } else {
68
- //https://gist.github.com/MarksCode/64e438c82b0b2a1161e01c88ca0d0355
69
- navigator.push = (...args: Parameters<typeof push>) => {
70
- if (window.confirm(promptMessage)) {
71
- push(...args);
72
- }
73
- };
74
- }
75
-
76
- window.addEventListener('beforeunload', beforeUnload);
77
- return () => {
78
- unblock();
79
- navigator.push = push;
80
- window.removeEventListener('beforeunload', beforeUnload);
81
- };
82
-
83
- function beforeUnload(e: BeforeUnloadEvent) {
84
- e.preventDefault();
85
- e.returnValue = promptMessage;
86
- }
87
- }, [preventNavigate, promptMessage, navigator]);
88
-
89
- return null;
37
+ const isUsingDataRouter = navigator.location === undefined;
38
+
39
+ if (isUsingDataRouter) {
40
+ return (
41
+ <NewFormBasedPreventNavigation
42
+ promptMessage={promptMessage}
43
+ preventNavigate={preventNavigate}
44
+ navigator={navigator}
45
+ />
46
+ );
47
+ } else {
48
+ return (
49
+ <LegacyFormBasedPreventNavigation
50
+ promptMessage={promptMessage}
51
+ preventNavigate={preventNavigate}
52
+ navigator={navigator}
53
+ />
54
+ );
55
+ }
90
56
  }
@@ -0,0 +1,77 @@
1
+ import type { History, Blocker, Transition } from 'history';
2
+ import { useEffect } from 'react';
3
+ // see: https://gist.github.com/rmorse/426ffcc579922a82749934826fa9f743
4
+ import { Navigator as BaseNavigator } from 'react-router-dom';
5
+
6
+ interface Navigator extends BaseNavigator {
7
+ block?: History['block'];
8
+ }
9
+
10
+ export interface FormBasedPreventNavigationProps {
11
+ promptMessage?: string;
12
+ preventNavigate: boolean;
13
+ navigator: Navigator;
14
+ }
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
+ export default function LegacyFormBasedPreventNavigation({
21
+ promptMessage = 'Changes you made may not be saved.',
22
+ preventNavigate,
23
+ navigator,
24
+ }: FormBasedPreventNavigationProps) {
25
+ useEffect(() => {
26
+ if (!preventNavigate) {
27
+ return;
28
+ }
29
+ let unblock = () => {};
30
+ const push = navigator.push;
31
+
32
+ // TODO: https://reactrouter.com/docs/en/v6/upgrading/v5#prompt-is-not-currently-supported
33
+ // this is a workaround until we get native support for prompt on navigate
34
+ if (navigator.block) {
35
+ const blocker: Blocker = (tx) => {
36
+ if (window.confirm(promptMessage)) {
37
+ tx.retry();
38
+ }
39
+ };
40
+ unblock = navigator.block((tx: Transition) => {
41
+ const autoUnblockingTx = {
42
+ ...tx,
43
+ retry() {
44
+ // Automatically unblock the transition so it can play all the way
45
+ // through before retrying it. TODO: Figure out how to re-enable
46
+ // this block if the transition is cancelled for some reason.
47
+ unblock();
48
+ tx.retry();
49
+ },
50
+ };
51
+
52
+ blocker(autoUnblockingTx);
53
+ });
54
+ } else {
55
+ //https://gist.github.com/MarksCode/64e438c82b0b2a1161e01c88ca0d0355
56
+ navigator.push = (...args: Parameters<typeof push>) => {
57
+ if (window.confirm(promptMessage)) {
58
+ push(...args);
59
+ }
60
+ };
61
+ }
62
+
63
+ window.addEventListener('beforeunload', beforeUnload);
64
+ return () => {
65
+ unblock();
66
+ navigator.push = push;
67
+ window.removeEventListener('beforeunload', beforeUnload);
68
+ };
69
+
70
+ function beforeUnload(e: BeforeUnloadEvent) {
71
+ e.preventDefault();
72
+ e.returnValue = promptMessage;
73
+ }
74
+ }, [preventNavigate, promptMessage, navigator]);
75
+
76
+ return null;
77
+ }
@@ -0,0 +1,59 @@
1
+ import type { History, Blocker, Transition } from 'history';
2
+ import { useEffect } from 'react';
3
+ import {
4
+ Navigator as BaseNavigator,
5
+ unstable_usePrompt as usePrompt,
6
+ } from 'react-router-dom';
7
+
8
+ interface Navigator extends BaseNavigator {
9
+ block?: History['block'];
10
+ location: Location;
11
+ }
12
+
13
+ interface FormBasedPreventNavigationProps {
14
+ promptMessage?: string;
15
+ preventNavigate: boolean;
16
+ navigator: Navigator;
17
+ }
18
+
19
+ /** Temporary function to allow backwards compatibility with routers that do not use the new Data API */
20
+ export default function NewFormBasedPreventNavigation({
21
+ promptMessage = 'Changes you made may not be saved.',
22
+ preventNavigate,
23
+ navigator,
24
+ }: FormBasedPreventNavigationProps) {
25
+ usePrompt({
26
+ when: preventNavigate,
27
+ message: promptMessage,
28
+ });
29
+
30
+ // TODO: Once react-router fully supports usePrompt and when we do not want to support routers
31
+ // that do not support the react-router Data API we can remove this.
32
+ useEffect(() => {
33
+ if (!preventNavigate) {
34
+ return;
35
+ }
36
+ let unblock = () => {};
37
+ const push = navigator.push;
38
+
39
+ navigator.push = (...args: Parameters<typeof push>) => {
40
+ if (window.confirm(promptMessage)) {
41
+ push(...args);
42
+ }
43
+ };
44
+
45
+ window.addEventListener('beforeunload', beforeUnload);
46
+ return () => {
47
+ unblock();
48
+ navigator.push = push;
49
+ window.removeEventListener('beforeunload', beforeUnload);
50
+ };
51
+
52
+ function beforeUnload(e: BeforeUnloadEvent) {
53
+ e.preventDefault();
54
+ e.returnValue = promptMessage;
55
+ }
56
+ }, [preventNavigate, promptMessage, navigator]);
57
+
58
+ return null;
59
+ }
@@ -8,6 +8,7 @@ export interface ServerErrorContextProps {
8
8
  getError: (path: string) => string | undefined;
9
9
  setError: (path: string, errorMessage: string | undefined) => void;
10
10
  }
11
+ /** Context to store the API errors from the server for the form. */
11
12
  export const ServerErrorContext = React.createContext<ServerErrorContextProps>({
12
13
  errors: {},
13
14
  getError: (path: string) => {
@@ -3,10 +3,17 @@ import { useFormikContext } from 'formik';
3
3
  import SubmitFormButton from './SubmitFormButton';
4
4
 
5
5
  export interface FormActionsProps {
6
+ /** Allow the form to be submitted without any changes. By default this is not allowed. */
6
7
  allowPristineSubmit?: boolean;
8
+ /** The cancel button's `onClick`.
9
+ * @defaultValue `window.history.back()`
10
+ */
7
11
  handleCancel?: () => void;
12
+ /** Whether both buttons should be disabled. */
8
13
  disabled?: boolean;
9
14
  }
15
+
16
+ /** Standard cancel and submit buttons. */
10
17
  export default function FormActions({
11
18
  allowPristineSubmit,
12
19
  handleCancel,
@@ -1 +1,2 @@
1
+ /** `envoc-form-` CSS class prefix */
1
2
  export const FormDefaults = { cssClassPrefix: 'envoc-form-' };
package/src/Group.tsx CHANGED
@@ -4,22 +4,24 @@ import FieldErrorScrollTarget from './Field/FieldErrorScrollTarget';
4
4
  import { InjectedFieldProps } from './Field/InjectedFieldProps';
5
5
 
6
6
  export interface GroupProps extends InjectedFieldProps<any> {
7
+ /** Extra class names to apply. */
7
8
  className?: string;
8
9
 
9
10
  children: React.ReactNode;
10
11
 
11
- /** simple text label before the input */
12
+ /** Simple text label before the input. */
12
13
  label?: string;
13
14
 
14
- /** simple helper text after the input*/
15
+ /** Simple helper text after the input. */
15
16
  helpText?: string | React.ReactNode;
16
17
 
18
+ /** Whether the field should be disabled. */
17
19
  disabled?: boolean;
18
20
 
19
21
  required?: boolean;
20
22
  }
21
23
 
22
- /** contains standard field bits like a label, helper text, error scroll target, validation message container, etc */
24
+ /** Contains standard field bits like a label, helper text, error scroll target, validation message container, etc. */
23
25
  export default function Group({
24
26
  className,
25
27
  children,
@@ -46,7 +48,9 @@ export default function Group({
46
48
  <label htmlFor={input.id}>{label}</label>
47
49
  {children}
48
50
  {meta.error && (
49
- <div className={FormDefaults.cssClassPrefix + 'error'}>
51
+ <div
52
+ id={`${input.id}-error`}
53
+ className={FormDefaults.cssClassPrefix + 'error'}>
50
54
  {meta.error}
51
55
  </div>
52
56
  )}
@@ -11,7 +11,9 @@ export interface IconInputGroupProps
11
11
  'onChange' | 'type' | 'value'
12
12
  > {
13
13
  type?: 'color' | 'email' | 'search' | 'tel' | 'text' | 'url';
14
+ /** Icon to display on the input group. */
14
15
  icon: React.ReactNode;
16
+ /** Text to display after the input group to give more information to the user. */
15
17
  helpText?: string;
16
18
  }
17
19
 
@@ -44,6 +46,7 @@ function IconInputGroup(
44
46
  );
45
47
  }
46
48
 
49
+ /** Input group with an icon. */
47
50
  const IconInputGroupWithRef = React.forwardRef(
48
51
  IconInputGroup
49
52
  ) as React.ComponentType<IconInputGroupProps>;
@@ -20,7 +20,6 @@ export interface InputGroupProps<TValue>
20
20
  icon?: React.ReactNode;
21
21
  }
22
22
 
23
- /** generic controlled <input/> wrapped by a Group */
24
23
  function InputGroup<TValue>(
25
24
  {
26
25
  input,
@@ -53,6 +52,8 @@ function InputGroup<TValue>(
53
52
  <input
54
53
  {...input}
55
54
  {...rest}
55
+ aria-invalid={!!meta.error}
56
+ aria-errormessage={!!meta.error ? `${input.name}-error` : undefined}
56
57
  value={value ?? ''}
57
58
  onChange={onChange}
58
59
  ref={ref}
@@ -65,6 +66,7 @@ function InputGroup<TValue>(
65
66
  );
66
67
  }
67
68
 
69
+ /** Generic controlled `<input/>` wrapped by a `<Group/>` */
68
70
  const InputGroupWithRef = React.forwardRef(InputGroup) as typeof InputGroup;
69
71
 
70
72
  export default InputGroupWithRef;
@@ -8,6 +8,7 @@ export interface MoneyInputGroupProps
8
8
  InputGroupProps<number | undefined | null>,
9
9
  'onChange' | 'type' | 'value'
10
10
  > {
11
+ /** Custom parsing function when the value changes. */
11
12
  parseFunc?: typeof parseInt | typeof parseFloat;
12
13
  }
13
14
 
@@ -41,6 +42,7 @@ function MoneyInputGroup(
41
42
  );
42
43
  }
43
44
 
45
+ /** Input group for inputting money. */
44
46
  const MoneyInputGroupWithRef = React.forwardRef(
45
47
  MoneyInputGroup
46
48
  ) as React.FunctionComponent<MoneyInputGroupProps>;
@@ -8,6 +8,7 @@ export interface NumberInputGroupProps
8
8
  InputGroupProps<number | undefined | null>,
9
9
  'onChange' | 'type' | 'value'
10
10
  > {
11
+ /** Custom parsing function when the value changes. */
11
12
  parseFunc?: typeof parseInt | typeof parseFloat;
12
13
  }
13
14
 
@@ -39,6 +40,7 @@ function NumberInputGroup(
39
40
  );
40
41
  }
41
42
 
43
+ /** Standard number input group. By default allows for float values. */
42
44
  const NumberInputGroupWithRef = React.forwardRef(
43
45
  NumberInputGroup
44
46
  ) as React.ComponentType<NumberInputGroupProps>;
@@ -45,6 +45,7 @@ function StringInputGroup(
45
45
  );
46
46
  }
47
47
 
48
+ /** Standard string input group. */
48
49
  const StringInputGroupWithRef = React.forwardRef(
49
50
  StringInputGroup
50
51
  ) as React.ComponentType<StringInputGroupProps>;
@@ -20,6 +20,7 @@ exports[`IconInputGroup has matching snapshot 1`] = `
20
20
  Pretend this is an icon
21
21
  </span>
22
22
  <input
23
+ aria-invalid="false"
23
24
  class="envoc-form-icon-input-group envoc-form-input-group"
24
25
  id="nameWithIcon"
25
26
  name="nameWithIcon"
@@ -19,6 +19,7 @@ exports[`MoneyInputGroup has matching snapshot 1`] = `
19
19
  Salary
20
20
  </label>
21
21
  <input
22
+ aria-invalid="false"
22
23
  class="something-here envoc-form-money-group envoc-form-input-group"
23
24
  id="yearlySalaryUSD"
24
25
  min="0"
@@ -19,6 +19,7 @@ exports[`NumberInputGroup has matching snapshot 1`] = `
19
19
  Arm Count
20
20
  </label>
21
21
  <input
22
+ aria-invalid="false"
22
23
  class="envoc-form-number-group envoc-form-input-group"
23
24
  id="numberOfArms"
24
25
  name="numberOfArms"
@@ -19,6 +19,7 @@ exports[`StringInputGroup has matching snapshot 1`] = `
19
19
  Name
20
20
  </label>
21
21
  <input
22
+ aria-invalid="false"
22
23
  class="envoc-form-string-group envoc-form-input-group"
23
24
  id="name"
24
25
  name="name"
@@ -1,3 +1,4 @@
1
+ /** Normalizer for converting a string into a valid phone number. */
1
2
  export const phoneNumber = (value: string | undefined | null) => {
2
3
  if (!value) {
3
4
  return undefined;
@@ -30,6 +31,7 @@ export const phoneNumber = (value: string | undefined | null) => {
30
31
  return onlyNums;
31
32
  };
32
33
 
34
+ /** Normalizer for converting a string into a valid zip code. Allows for XXXXX and XXXXX-XXXX format. */
33
35
  export const zipCode = (value: string | undefined | null) => {
34
36
  if (!value) {
35
37
  return undefined;
@@ -16,6 +16,7 @@ export interface BooleanSelectGroupProps
16
16
  extends Omit<SelectGroupPropsHelper<boolean | undefined | null>, 'options'> {
17
17
  options?: SelectOption<boolean>[];
18
18
  }
19
+ /** A `<SelectGroup/>` dropdown with two options. Default options are 'Yes' and 'No' with values of `true` and `false`. */
19
20
  export function BooleanSelectGroup(props: BooleanSelectGroupProps) {
20
21
  return (
21
22
  <SelectGroup
@@ -3,12 +3,14 @@ import { SelectGroupPropsHelper } from './SelectGroupPropsHelper';
3
3
 
4
4
  export interface MultiNumberSelectGroupProps
5
5
  extends SelectGroupPropsHelper<number[] | undefined | null> {}
6
+ /** A `<SelectGroup/>` dropdown with values as numbers. Many options can be selected at a time. */
6
7
  export function MultiNumberSelectGroup(props: MultiNumberSelectGroupProps) {
7
8
  return <SelectGroup multiple={true} {...props} />;
8
9
  }
9
10
 
10
11
  export interface SingleNumberSelectGroupProps
11
12
  extends SelectGroupPropsHelper<number | undefined | null> {}
13
+ /** A `<SelectGroup/>` dropdown with values as numbers. Only one option can be selected at a time. */
12
14
  export function SingleNumberSelectGroup(props: SingleNumberSelectGroupProps) {
13
15
  return <SelectGroup multiple={false} {...props} />;
14
16
  }
@@ -5,7 +5,9 @@ import { FormDefaults } from '../FormDefaults';
5
5
  import Group, { GroupProps } from '../Group';
6
6
 
7
7
  export interface SelectOption<TValue> {
8
+ /** Value for the select. This will be sent to the API. */
8
9
  value: (TValue extends Array<infer P> ? P : TValue) | undefined;
10
+ /** Label for the select. This is displayed to the user. */
9
11
  label: string;
10
12
  }
11
13
 
@@ -28,12 +30,16 @@ export interface SelectGroupProps<TValue>
28
30
  // allows for "useService" or other handles to control the data - including cache
29
31
  // TODO: do we still want a version of select that has a "url" or maybe a promise func or something?
30
32
  // eventually we can just add the shape of, say, useQuery (TanStack) to the union type
33
+ /** Options for the dropdown. Includes the label and value. */
31
34
  options: SelectOption<TValue>[] | OptionsUseServiceResult<TValue>;
35
+ /** Whether the user should be able to have multiple values selected. */
32
36
  multiple: TValue extends Array<any> ? true : false;
37
+ /** Text diplayed when no value is selected. */
33
38
  placeholder?: string;
34
39
  }
35
40
 
36
41
  // TODO: we could also name this "ReactSelectGroup" or similar but the types are strongly defined now so kept the names consistent
42
+ /** Generic select dropdown. Uses [react-select](https://react-select.com/home). */
37
43
  export default function SelectGroup<TValue>({
38
44
  input,
39
45
  meta,
@@ -3,12 +3,14 @@ import { SelectGroupPropsHelper } from './SelectGroupPropsHelper';
3
3
 
4
4
  export interface MultiStringSelectGroupProps
5
5
  extends SelectGroupPropsHelper<string[]> {}
6
+ /** A `<SelectGroup/>` dropdown with values as strings. Multiple options can be selected at a time. */
6
7
  export function MultiStringSelectGroup(props: MultiStringSelectGroupProps) {
7
8
  return <SelectGroup multiple={true} {...props} />;
8
9
  }
9
10
 
10
11
  export interface SingleStringSelectGroupProps
11
12
  extends SelectGroupPropsHelper<string> {}
13
+ /** A `<SelectGroup/>` dropdown with the value as a string. Only one option can be selected at a time. */
12
14
  export function SingleStringSelectGroup(props: SingleStringSelectGroupProps) {
13
15
  return <SelectGroup multiple={false} {...props} />;
14
16
  }