envoc-form 4.0.1-9 → 4.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (239) hide show
  1. package/README.md +9443 -12
  2. package/es/AddressInput/AddressInput.d.ts +15 -5
  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 +7 -2
  9. package/es/DatePicker/DatePickerGroup.js +18 -7
  10. package/es/DatePicker/StringDateOnlyPickerGroup.d.ts +6 -1
  11. package/es/DatePicker/StringDateOnlyPickerGroup.js +5 -0
  12. package/es/DatePicker/StringDatePickerGroup.d.ts +6 -1
  13. package/es/DatePicker/StringDatePickerGroup.js +5 -0
  14. package/es/DateTimePicker/DateTimePickerGroup.d.ts +13 -0
  15. package/es/DateTimePicker/DateTimePickerGroup.js +87 -0
  16. package/es/DateTimePicker/DateTimePickerHelper.d.ts +3 -0
  17. package/es/DateTimePicker/DateTimePickerHelper.js +1 -0
  18. package/es/DateTimePicker/StringDateTimePickerGroup.d.ts +10 -0
  19. package/es/DateTimePicker/StringDateTimePickerGroup.js +53 -0
  20. package/es/Field/Field.d.ts +9 -3
  21. package/es/Field/Field.js +9 -4
  22. package/es/Field/FieldErrorScrollTarget.d.ts +1 -0
  23. package/es/Field/FieldErrorScrollTarget.js +1 -0
  24. package/es/Field/StandAloneInput.d.ts +4 -0
  25. package/es/Field/StandAloneInput.js +1 -0
  26. package/es/Field/useStandardField.d.ts +6 -0
  27. package/es/Field/useStandardField.js +8 -9
  28. package/es/FieldArray/FieldArray.d.ts +11 -2
  29. package/es/FieldArray/FieldArray.js +7 -2
  30. package/es/File/FileGroup.d.ts +3 -1
  31. package/es/File/FileGroup.js +4 -3
  32. package/es/File/FileList.d.ts +2 -2
  33. package/es/Form/FocusError.d.ts +2 -0
  34. package/es/Form/FocusError.js +1 -0
  35. package/es/Form/Form.d.ts +6 -0
  36. package/es/Form/Form.js +1 -0
  37. package/es/Form/FormBasedPreventNavigation.d.ts +3 -1
  38. package/es/Form/FormBasedPreventNavigation.js +12 -45
  39. package/es/Form/LegacyFormBasedPreventNavigation.d.ts +17 -0
  40. package/es/Form/LegacyFormBasedPreventNavigation.js +69 -0
  41. package/es/Form/NewFormBasedPreventNavigation.d.ts +14 -0
  42. package/es/Form/NewFormBasedPreventNavigation.js +39 -0
  43. package/es/Form/ServerErrorContext.d.ts +1 -0
  44. package/es/Form/ServerErrorContext.js +1 -0
  45. package/es/FormActions.d.ts +6 -0
  46. package/es/FormActions.js +1 -0
  47. package/es/FormDefaults.d.ts +1 -0
  48. package/es/FormDefaults.js +1 -0
  49. package/es/Group.d.ts +7 -4
  50. package/es/Group.js +4 -3
  51. package/es/Input/IconInputGroup.d.ts +4 -1
  52. package/es/Input/IconInputGroup.js +3 -1
  53. package/es/Input/InputGroup.d.ts +4 -4
  54. package/es/Input/InputGroup.js +4 -4
  55. package/es/Input/MoneyInputGroup.d.ts +3 -1
  56. package/es/Input/MoneyInputGroup.js +1 -0
  57. package/es/Input/NumberInputGroup.d.ts +3 -1
  58. package/es/Input/NumberInputGroup.js +1 -0
  59. package/es/Input/PhoneNumberInputGroup.d.ts +10 -0
  60. package/es/Input/PhoneNumberInputGroup.js +47 -0
  61. package/es/Input/StringInputGroup.d.ts +2 -1
  62. package/es/Input/StringInputGroup.js +3 -1
  63. package/es/Normalization/normalizers.d.ts +4 -2
  64. package/es/Normalization/normalizers.js +2 -0
  65. package/es/Select/BooleanSelectGroup.d.ts +2 -1
  66. package/es/Select/BooleanSelectGroup.js +1 -0
  67. package/es/Select/NumberSelectGroup.d.ts +4 -2
  68. package/es/Select/NumberSelectGroup.js +2 -0
  69. package/es/Select/SelectGroup.d.ts +8 -2
  70. package/es/Select/SelectGroup.js +5 -4
  71. package/es/Select/StringSelectGroup.d.ts +2 -0
  72. package/es/Select/StringSelectGroup.js +2 -0
  73. package/es/StandardFormActions.d.ts +5 -0
  74. package/es/StandardFormActions.js +1 -0
  75. package/es/SubmitFormButton.d.ts +4 -1
  76. package/es/SubmitFormButton.js +1 -0
  77. package/es/TextArea/TextAreaGroup.d.ts +2 -1
  78. package/es/TextArea/TextAreaGroup.js +5 -4
  79. package/es/Validation/validators.d.ts +11 -8
  80. package/es/Validation/validators.js +6 -2
  81. package/es/hooks/index.d.ts +2 -0
  82. package/es/hooks/index.js +2 -0
  83. package/es/hooks/useFormValue.d.ts +2 -0
  84. package/es/hooks/useFormValue.js +7 -0
  85. package/es/index.d.ts +10 -4
  86. package/es/index.js +4 -0
  87. package/es/setupTests.d.ts +1 -0
  88. package/es/setupTests.js +1 -0
  89. package/lib/AddressInput/AddressInput.d.ts +15 -5
  90. package/lib/AddressInput/AddressInput.js +10 -0
  91. package/lib/ConfirmBaseForm/ConfirmBaseForm.d.ts +14 -0
  92. package/lib/ConfirmBaseForm/ConfirmBaseForm.js +6 -0
  93. package/lib/ConfirmDeleteForm/ConfirmDeleteForm.d.ts +13 -0
  94. package/lib/ConfirmDeleteForm/ConfirmDeleteForm.js +5 -0
  95. package/lib/DatePicker/DatePickerGroup.d.ts +7 -2
  96. package/lib/DatePicker/DatePickerGroup.js +18 -7
  97. package/lib/DatePicker/StringDateOnlyPickerGroup.d.ts +6 -1
  98. package/lib/DatePicker/StringDateOnlyPickerGroup.js +5 -0
  99. package/lib/DatePicker/StringDatePickerGroup.d.ts +6 -1
  100. package/lib/DatePicker/StringDatePickerGroup.js +5 -0
  101. package/lib/DateTimePicker/DateTimePickerGroup.d.ts +13 -0
  102. package/lib/DateTimePicker/DateTimePickerGroup.js +93 -0
  103. package/lib/DateTimePicker/DateTimePickerHelper.d.ts +3 -0
  104. package/lib/DateTimePicker/DateTimePickerHelper.js +2 -0
  105. package/lib/DateTimePicker/StringDateTimePickerGroup.d.ts +10 -0
  106. package/lib/DateTimePicker/StringDateTimePickerGroup.js +59 -0
  107. package/lib/Field/Field.d.ts +9 -3
  108. package/lib/Field/Field.js +9 -4
  109. package/lib/Field/FieldErrorScrollTarget.d.ts +1 -0
  110. package/lib/Field/FieldErrorScrollTarget.js +1 -0
  111. package/lib/Field/StandAloneInput.d.ts +4 -0
  112. package/lib/Field/StandAloneInput.js +1 -0
  113. package/lib/Field/useStandardField.d.ts +6 -0
  114. package/lib/Field/useStandardField.js +8 -9
  115. package/lib/FieldArray/FieldArray.d.ts +11 -2
  116. package/lib/FieldArray/FieldArray.js +7 -2
  117. package/lib/File/FileGroup.d.ts +3 -1
  118. package/lib/File/FileGroup.js +4 -3
  119. package/lib/File/FileList.d.ts +2 -2
  120. package/lib/Form/FocusError.d.ts +2 -0
  121. package/lib/Form/FocusError.js +1 -0
  122. package/lib/Form/Form.d.ts +6 -0
  123. package/lib/Form/Form.js +1 -0
  124. package/lib/Form/FormBasedPreventNavigation.d.ts +3 -1
  125. package/lib/Form/FormBasedPreventNavigation.js +13 -43
  126. package/lib/Form/LegacyFormBasedPreventNavigation.d.ts +17 -0
  127. package/lib/Form/LegacyFormBasedPreventNavigation.js +72 -0
  128. package/lib/Form/NewFormBasedPreventNavigation.d.ts +14 -0
  129. package/lib/Form/NewFormBasedPreventNavigation.js +42 -0
  130. package/lib/Form/ServerErrorContext.d.ts +1 -0
  131. package/lib/Form/ServerErrorContext.js +1 -0
  132. package/lib/FormActions.d.ts +6 -0
  133. package/lib/FormActions.js +1 -0
  134. package/lib/FormDefaults.d.ts +1 -0
  135. package/lib/FormDefaults.js +1 -0
  136. package/lib/Group.d.ts +7 -4
  137. package/lib/Group.js +4 -3
  138. package/lib/Input/IconInputGroup.d.ts +4 -1
  139. package/lib/Input/IconInputGroup.js +3 -1
  140. package/lib/Input/InputGroup.d.ts +4 -4
  141. package/lib/Input/InputGroup.js +4 -4
  142. package/lib/Input/MoneyInputGroup.d.ts +3 -1
  143. package/lib/Input/MoneyInputGroup.js +1 -0
  144. package/lib/Input/NumberInputGroup.d.ts +3 -1
  145. package/lib/Input/NumberInputGroup.js +1 -0
  146. package/lib/Input/PhoneNumberInputGroup.d.ts +10 -0
  147. package/lib/Input/PhoneNumberInputGroup.js +52 -0
  148. package/lib/Input/StringInputGroup.d.ts +2 -1
  149. package/lib/Input/StringInputGroup.js +3 -1
  150. package/lib/Normalization/normalizers.d.ts +4 -2
  151. package/lib/Normalization/normalizers.js +2 -0
  152. package/lib/Select/BooleanSelectGroup.d.ts +2 -1
  153. package/lib/Select/BooleanSelectGroup.js +1 -0
  154. package/lib/Select/NumberSelectGroup.d.ts +4 -2
  155. package/lib/Select/NumberSelectGroup.js +2 -0
  156. package/lib/Select/SelectGroup.d.ts +8 -2
  157. package/lib/Select/SelectGroup.js +5 -4
  158. package/lib/Select/StringSelectGroup.d.ts +2 -0
  159. package/lib/Select/StringSelectGroup.js +2 -0
  160. package/lib/StandardFormActions.d.ts +5 -0
  161. package/lib/StandardFormActions.js +1 -0
  162. package/lib/SubmitFormButton.d.ts +4 -1
  163. package/lib/SubmitFormButton.js +1 -0
  164. package/lib/TextArea/TextAreaGroup.d.ts +2 -1
  165. package/lib/TextArea/TextAreaGroup.js +5 -4
  166. package/lib/Validation/validators.d.ts +11 -8
  167. package/lib/Validation/validators.js +6 -2
  168. package/lib/hooks/index.d.ts +2 -0
  169. package/lib/{__Tests__ → hooks}/index.js +3 -3
  170. package/lib/hooks/useFormValue.d.ts +2 -0
  171. package/lib/hooks/useFormValue.js +10 -0
  172. package/lib/index.d.ts +10 -4
  173. package/lib/index.js +8 -1
  174. package/lib/setupTests.d.ts +1 -0
  175. package/lib/setupTests.js +3 -0
  176. package/package.json +12 -8
  177. package/src/AddressInput/AddressInput.tsx +15 -5
  178. package/src/AddressInput/__snapshots__/AddressInput.test.tsx.snap +8 -4
  179. package/src/ConfirmBaseForm/ConfirmBaseForm.tsx +14 -0
  180. package/src/ConfirmDeleteForm/ConfirmDeleteForm.tsx +13 -0
  181. package/src/DatePicker/DatePicker.test.tsx +1 -1
  182. package/src/DatePicker/DatePickerGroup.tsx +23 -7
  183. package/src/DatePicker/StringDateOnlyPickerGroup.tsx +7 -2
  184. package/src/DatePicker/StringDatePickerGroup.tsx +7 -1
  185. package/src/DateTimePicker/DateTimePicker.test.tsx +243 -0
  186. package/src/DateTimePicker/DateTimePickerGroup.tsx +116 -0
  187. package/src/DateTimePicker/DateTimePickerHelper.ts +4 -0
  188. package/src/DateTimePicker/StringDateTimePickerGroup.tsx +61 -0
  189. package/src/DateTimePicker/__snapshots__/DateTimePicker.test.tsx.snap +216 -0
  190. package/src/Field/Field.tsx +20 -7
  191. package/src/Field/FieldErrorScrollTarget.tsx +1 -0
  192. package/src/Field/StandAloneInput.tsx +4 -0
  193. package/src/Field/useStandardField.ts +13 -9
  194. package/src/FieldArray/FieldArray.tsx +14 -5
  195. package/src/File/FileGroup.tsx +9 -3
  196. package/src/File/FileList.tsx +2 -2
  197. package/src/Form/FocusError.tsx +3 -0
  198. package/src/Form/Form.tsx +6 -0
  199. package/src/Form/FormBasedPreventNavigation.tsx +28 -46
  200. package/src/Form/LegacyFormBasedPreventNavigation.tsx +77 -0
  201. package/src/Form/NewFormBasedPreventNavigation.tsx +59 -0
  202. package/src/Form/ServerErrorContext.ts +1 -0
  203. package/src/FormActions.tsx +7 -0
  204. package/src/FormDefaults.ts +1 -0
  205. package/src/Group.tsx +12 -4
  206. package/src/Input/IconInputGroup.tsx +5 -2
  207. package/src/Input/InputGroup.tsx +13 -5
  208. package/src/Input/MoneyInputGroup.tsx +3 -1
  209. package/src/Input/NumberInputGroup.tsx +3 -1
  210. package/src/Input/PhoneNumberInputGroup.tsx +49 -0
  211. package/src/Input/StringInputGroup.tsx +3 -2
  212. package/src/Input/__Tests__/PhoneNumberInputGroup.test.tsx +37 -0
  213. package/src/Input/__Tests__/__snapshots__/IconInputGroup.test.tsx.snap +1 -0
  214. package/src/Input/__Tests__/__snapshots__/MoneyInputGroup.test.tsx.snap +1 -0
  215. package/src/Input/__Tests__/__snapshots__/NumberInputGroup.test.tsx.snap +1 -0
  216. package/src/Input/__Tests__/__snapshots__/PhoneNumberInputGroup.test.tsx.snap +33 -0
  217. package/src/Input/__Tests__/__snapshots__/StringInputGroup.test.tsx.snap +1 -0
  218. package/src/Normalization/normalizers.ts +4 -2
  219. package/src/Select/BooleanSelectGroup.tsx +2 -1
  220. package/src/Select/NumberSelectGroup.tsx +4 -2
  221. package/src/Select/SelectGroup.tsx +13 -2
  222. package/src/Select/StringSelectGroup.tsx +2 -0
  223. package/src/StandardFormActions.tsx +5 -0
  224. package/src/SubmitFormButton.tsx +5 -1
  225. package/src/TextArea/TextAreaGroup.tsx +6 -4
  226. package/src/Validation/validators.ts +19 -14
  227. package/src/__Tests__/FormTestBase.tsx +10 -8
  228. package/src/__Tests__/RealisticForm.test.tsx +82 -0
  229. package/src/hooks/index.ts +3 -0
  230. package/src/hooks/useFormValue.ts +16 -0
  231. package/src/index.ts +14 -4
  232. package/src/setupTests.ts +1 -0
  233. package/es/__Tests__/FormTestBase.d.ts +0 -27
  234. package/es/__Tests__/FormTestBase.js +0 -83
  235. package/es/__Tests__/index.d.ts +0 -2
  236. package/es/__Tests__/index.js +0 -2
  237. package/lib/__Tests__/FormTestBase.d.ts +0 -27
  238. package/lib/__Tests__/FormTestBase.js +0 -86
  239. package/lib/__Tests__/index.d.ts +0 -2
@@ -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,20 +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;
20
+
21
+ required?: boolean;
18
22
  }
19
23
 
20
- /** 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. */
21
25
  export default function Group({
22
26
  className,
23
27
  children,
@@ -26,12 +30,14 @@ export default function Group({
26
30
  meta,
27
31
  input,
28
32
  disabled,
33
+ required,
29
34
  }: GroupProps) {
30
35
  return (
31
36
  <div
32
37
  className={classNames(className, FormDefaults.cssClassPrefix + 'group', {
33
38
  [FormDefaults.cssClassPrefix + 'invalid']: meta.error,
34
39
  [FormDefaults.cssClassPrefix + 'disabled']: disabled,
40
+ [FormDefaults.cssClassPrefix + 'required']: required,
35
41
  })}>
36
42
  <FieldErrorScrollTarget />
37
43
  {meta.warning && (
@@ -42,7 +48,9 @@ export default function Group({
42
48
  <label htmlFor={input.id}>{label}</label>
43
49
  {children}
44
50
  {meta.error && (
45
- <div className={FormDefaults.cssClassPrefix + 'error'}>
51
+ <div
52
+ id={`${input.id}-error`}
53
+ className={FormDefaults.cssClassPrefix + 'error'}>
46
54
  {meta.error}
47
55
  </div>
48
56
  )}
@@ -7,11 +7,13 @@ import { FormDefaults } from '../FormDefaults';
7
7
  // or should we just give a div a className and let each project decide? (this seems to be the patern)
8
8
  export interface IconInputGroupProps
9
9
  extends Omit<
10
- InputGroupProps<string | undefined>,
10
+ InputGroupProps<string | undefined | null>,
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
 
@@ -30,7 +32,7 @@ function IconInputGroup(
30
32
  className,
31
33
  FormDefaults.cssClassPrefix + 'icon-input-group'
32
34
  )}
33
- value={input.value || ''}
35
+ value={input.value ?? ''}
34
36
  onChange={(e) => {
35
37
  if (!e.target.value) {
36
38
  input.onChange(undefined);
@@ -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>;
@@ -1,22 +1,25 @@
1
1
  import React, { ChangeEventHandler, LegacyRef } from 'react';
2
2
  import classNames from 'classnames';
3
3
  import { InjectedFieldProps } from '../Field/InjectedFieldProps';
4
- import Group, { GroupProps } from '../Group';
5
4
  import { FormDefaults } from '../FormDefaults';
5
+ import Group, { GroupProps } from '../Group';
6
6
 
7
7
  export interface InputGroupProps<TValue>
8
8
  extends InjectedFieldProps<TValue>,
9
9
  Omit<
10
10
  React.HTMLProps<HTMLInputElement>,
11
- keyof InjectedFieldProps<any> | 'children' | 'className' | 'label'
11
+ | keyof InjectedFieldProps<any>
12
+ | 'children'
13
+ | 'className'
14
+ | 'label'
15
+ | 'value'
12
16
  >,
13
17
  Omit<GroupProps, keyof InjectedFieldProps<any> | 'children'> {
14
18
  onChange: ChangeEventHandler<HTMLInputElement>;
15
- value: string | number;
19
+ value: string | number | null | undefined;
16
20
  icon?: React.ReactNode;
17
21
  }
18
22
 
19
- /** generic controlled <input/> wrapped by a Group */
20
23
  function InputGroup<TValue>(
21
24
  {
22
25
  input,
@@ -24,6 +27,7 @@ function InputGroup<TValue>(
24
27
  label,
25
28
  helpText,
26
29
  className,
30
+ required,
27
31
  disabled,
28
32
  onChange,
29
33
  value,
@@ -36,6 +40,7 @@ function InputGroup<TValue>(
36
40
  <Group
37
41
  input={input}
38
42
  meta={meta}
43
+ required={required}
39
44
  disabled={disabled}
40
45
  label={label}
41
46
  helpText={helpText}
@@ -47,7 +52,9 @@ function InputGroup<TValue>(
47
52
  <input
48
53
  {...input}
49
54
  {...rest}
50
- value={value}
55
+ aria-invalid={!!meta.error}
56
+ aria-errormessage={!!meta.error ? `${input.name}-error` : undefined}
57
+ value={value ?? ''}
51
58
  onChange={onChange}
52
59
  ref={ref}
53
60
  className={classNames(
@@ -59,6 +66,7 @@ function InputGroup<TValue>(
59
66
  );
60
67
  }
61
68
 
69
+ /** Generic controlled `<input/>` wrapped by a `<Group/>` */
62
70
  const InputGroupWithRef = React.forwardRef(InputGroup) as typeof InputGroup;
63
71
 
64
72
  export default InputGroupWithRef;
@@ -5,9 +5,10 @@ import { FormDefaults } from '../FormDefaults';
5
5
 
6
6
  export interface MoneyInputGroupProps
7
7
  extends Omit<
8
- InputGroupProps<number | undefined>,
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>;
@@ -5,9 +5,10 @@ import { FormDefaults } from '../FormDefaults';
5
5
 
6
6
  export interface NumberInputGroupProps
7
7
  extends Omit<
8
- InputGroupProps<number | undefined>,
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>;
@@ -0,0 +1,49 @@
1
+ import React, { LegacyRef } from 'react';
2
+ import classNames from 'classnames';
3
+ import InputGroup, { InputGroupProps } from './InputGroup';
4
+ import { FormDefaults } from '../FormDefaults';
5
+ import { phoneNumber } from '../Normalization/normalizers';
6
+
7
+ export interface PhoneNumberInputGroupProps
8
+ extends Omit<
9
+ InputGroupProps<string | undefined | null>,
10
+ 'onChange' | 'type' | 'value'
11
+ > {}
12
+
13
+ function PhoneNumberInputGroup(
14
+ { className, ...rest }: PhoneNumberInputGroupProps,
15
+ ref: LegacyRef<HTMLInputElement>
16
+ ) {
17
+ const { input } = rest;
18
+
19
+ return (
20
+ <InputGroup
21
+ ref={ref}
22
+ autoComplete="tel-national"
23
+ {...rest}
24
+ type="text"
25
+ className={classNames(
26
+ className,
27
+ FormDefaults.cssClassPrefix + 'phone-number-group'
28
+ )}
29
+ value={input.value ?? ''}
30
+ onChange={(e) => {
31
+ if (!e.target.value) {
32
+ input.onChange(undefined);
33
+ } else {
34
+ input.onChange(phoneNumber(e.target.value));
35
+ }
36
+ }}
37
+ />
38
+ );
39
+ }
40
+
41
+ /**A standard phone number input. Will display to the user in `+XXX XXX-XXX-XXXX` format as the number is being inputted.
42
+ * The data will get sent in the same format as displayed. Uses `normalizers.phoneNumber`.
43
+ * @default autoComplete="tel-national"
44
+ */
45
+ const PhoneNumberInputGroupWithRef = React.forwardRef(
46
+ PhoneNumberInputGroup
47
+ ) as React.ComponentType<PhoneNumberInputGroupProps>;
48
+
49
+ export default PhoneNumberInputGroupWithRef;
@@ -5,7 +5,7 @@ import { FormDefaults } from '../FormDefaults';
5
5
 
6
6
  export interface StringInputGroupProps
7
7
  extends Omit<
8
- InputGroupProps<string | undefined>,
8
+ InputGroupProps<string | undefined | null>,
9
9
  'onChange' | 'type' | 'value'
10
10
  > {
11
11
  type?:
@@ -33,7 +33,7 @@ function StringInputGroup(
33
33
  className,
34
34
  FormDefaults.cssClassPrefix + 'string-group'
35
35
  )}
36
- value={input.value || ''}
36
+ value={input.value ?? ''}
37
37
  onChange={(e) => {
38
38
  if (!e.target.value) {
39
39
  input.onChange(undefined);
@@ -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>;
@@ -0,0 +1,37 @@
1
+ import React from 'react';
2
+ import { render } from '@testing-library/react';
3
+ import FormTestBase from '../../__Tests__/FormTestBase';
4
+ import PhoneNumberInputGroup from '../PhoneNumberInputGroup';
5
+
6
+ describe('PhoneNumberInputGroup', () => {
7
+ it('renders without crashing', () => {
8
+ render(
9
+ <FormTestBase>
10
+ {({ Field }) => (
11
+ <Field
12
+ name="phoneNumber"
13
+ Component={PhoneNumberInputGroup}
14
+ label="Phone Number"
15
+ className="something-here"
16
+ />
17
+ )}
18
+ </FormTestBase>
19
+ );
20
+ });
21
+
22
+ it('has matching snapshot', () => {
23
+ const renderResult = render(
24
+ <FormTestBase>
25
+ {({ Field }) => (
26
+ <Field
27
+ name="phoneNumber"
28
+ Component={PhoneNumberInputGroup}
29
+ label="Phone Number"
30
+ className="something-here"
31
+ />
32
+ )}
33
+ </FormTestBase>
34
+ );
35
+ expect(renderResult.asFragment()).toMatchSnapshot();
36
+ });
37
+ });
@@ -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"
@@ -0,0 +1,33 @@
1
+ // Jest Snapshot v1, https://goo.gl/fbAQLP
2
+
3
+ exports[`PhoneNumberInputGroup has matching snapshot 1`] = `
4
+ <DocumentFragment>
5
+ <form
6
+ action="#"
7
+ class="envoc-form-form"
8
+ >
9
+ <div
10
+ class="something-here envoc-form-phone-number-group envoc-form-input-group envoc-form-group"
11
+ >
12
+ <div
13
+ id="phonenumber-error-scroll-target"
14
+ style="display: none;"
15
+ />
16
+ <label
17
+ for="phoneNumber"
18
+ >
19
+ Phone Number
20
+ </label>
21
+ <input
22
+ aria-invalid="false"
23
+ autocomplete="tel-national"
24
+ class="something-here envoc-form-phone-number-group envoc-form-input-group"
25
+ id="phoneNumber"
26
+ name="phoneNumber"
27
+ type="text"
28
+ value=""
29
+ />
30
+ </div>
31
+ </form>
32
+ </DocumentFragment>
33
+ `;
@@ -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,4 +1,5 @@
1
- export const phoneNumber = (value: string | undefined) => {
1
+ /** Normalizer for converting a string into a valid phone number. */
2
+ export const phoneNumber = (value: string | undefined | null) => {
2
3
  if (!value) {
3
4
  return undefined;
4
5
  }
@@ -30,7 +31,8 @@ export const phoneNumber = (value: string | undefined) => {
30
31
  return onlyNums;
31
32
  };
32
33
 
33
- export const zipCode = (value: string | undefined) => {
34
+ /** Normalizer for converting a string into a valid zip code. Allows for XXXXX and XXXXX-XXXX format. */
35
+ export const zipCode = (value: string | undefined | null) => {
34
36
  if (!value) {
35
37
  return undefined;
36
38
  }
@@ -13,9 +13,10 @@ const booleanOptions: SelectOption<boolean>[] = [
13
13
  ];
14
14
 
15
15
  export interface BooleanSelectGroupProps
16
- extends Omit<SelectGroupPropsHelper<boolean>, 'options'> {
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
@@ -2,13 +2,15 @@ import SelectGroup from './SelectGroup';
2
2
  import { SelectGroupPropsHelper } from './SelectGroupPropsHelper';
3
3
 
4
4
  export interface MultiNumberSelectGroupProps
5
- extends SelectGroupPropsHelper<number[]> {}
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
- extends SelectGroupPropsHelper<number> {}
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
  }
@@ -1,11 +1,13 @@
1
1
  import { default as ReactSelect } from 'react-select';
2
2
  import classNames from 'classnames';
3
3
  import { InjectedFieldProps } from '../Field/InjectedFieldProps';
4
- import Group, { GroupProps } from '../Group';
5
4
  import { FormDefaults } from '../FormDefaults';
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
 
@@ -23,21 +25,27 @@ interface OptionsUseServiceResult<TValue> {
23
25
  }
24
26
 
25
27
  export interface SelectGroupProps<TValue>
26
- extends InjectedFieldProps<TValue | undefined>,
28
+ extends InjectedFieldProps<TValue | undefined | null>,
27
29
  Omit<GroupProps, 'input' | 'meta' | 'children'> {
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,
40
46
  className,
47
+ required,
48
+ disabled,
41
49
  options,
42
50
  multiple,
43
51
  placeholder,
@@ -64,6 +72,8 @@ export default function SelectGroup<TValue>({
64
72
  {...rest}
65
73
  input={input}
66
74
  meta={meta}
75
+ required={required}
76
+ disabled={disabled}
67
77
  className={classNames(
68
78
  className,
69
79
  {
@@ -78,6 +88,7 @@ export default function SelectGroup<TValue>({
78
88
  >
79
89
  inputId={input.id}
80
90
  isMulti={multiple}
91
+ isDisabled={disabled}
81
92
  options={effectiveOptions}
82
93
  onBlur={input.onBlur}
83
94
  value={getValue()}
@@ -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
  }