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.
- package/README.md +5117 -12
- package/es/AddressInput/AddressInput.d.ts +10 -0
- package/es/AddressInput/AddressInput.js +10 -0
- package/es/ConfirmBaseForm/ConfirmBaseForm.d.ts +14 -0
- package/es/ConfirmBaseForm/ConfirmBaseForm.js +6 -0
- package/es/ConfirmDeleteForm/ConfirmDeleteForm.d.ts +13 -0
- package/es/ConfirmDeleteForm/ConfirmDeleteForm.js +5 -0
- package/es/DatePicker/DatePickerGroup.d.ts +5 -0
- package/es/DatePicker/DatePickerGroup.js +5 -0
- package/es/DatePicker/StringDateOnlyPickerGroup.d.ts +5 -0
- package/es/DatePicker/StringDateOnlyPickerGroup.js +5 -0
- package/es/DatePicker/StringDatePickerGroup.d.ts +5 -0
- package/es/DatePicker/StringDatePickerGroup.js +5 -0
- package/es/Field/Field.d.ts +6 -0
- package/es/Field/FieldErrorScrollTarget.d.ts +1 -0
- package/es/Field/FieldErrorScrollTarget.js +1 -0
- package/es/Field/StandAloneInput.d.ts +4 -0
- package/es/Field/StandAloneInput.js +1 -0
- package/es/Field/useStandardField.d.ts +6 -0
- package/es/Field/useStandardField.js +8 -9
- package/es/FieldArray/FieldArray.d.ts +9 -0
- package/es/FieldArray/FieldArray.js +5 -0
- package/es/File/FileGroup.d.ts +2 -0
- package/es/File/FileGroup.js +1 -0
- package/es/Form/FocusError.d.ts +2 -0
- package/es/Form/FocusError.js +1 -0
- package/es/Form/Form.d.ts +6 -0
- package/es/Form/Form.js +1 -0
- package/es/Form/FormBasedPreventNavigation.d.ts +3 -1
- package/es/Form/FormBasedPreventNavigation.js +12 -62
- package/es/Form/LegacyFormBasedPreventNavigation.d.ts +17 -0
- package/es/Form/LegacyFormBasedPreventNavigation.js +69 -0
- package/es/Form/NewFormBasedPreventNavigation.d.ts +14 -0
- package/es/Form/NewFormBasedPreventNavigation.js +39 -0
- package/es/Form/ServerErrorContext.d.ts +1 -0
- package/es/Form/ServerErrorContext.js +1 -0
- package/es/FormActions.d.ts +6 -0
- package/es/FormActions.js +1 -0
- package/es/FormDefaults.d.ts +1 -0
- package/es/FormDefaults.js +1 -0
- package/es/Group.d.ts +5 -3
- package/es/Group.js +2 -2
- package/es/Input/IconInputGroup.d.ts +3 -0
- package/es/Input/IconInputGroup.js +1 -0
- package/es/Input/InputGroup.d.ts +1 -1
- package/es/Input/InputGroup.js +2 -2
- package/es/Input/MoneyInputGroup.d.ts +2 -0
- package/es/Input/MoneyInputGroup.js +1 -0
- package/es/Input/NumberInputGroup.d.ts +2 -0
- package/es/Input/NumberInputGroup.js +1 -0
- package/es/Input/StringInputGroup.d.ts +1 -0
- package/es/Input/StringInputGroup.js +1 -0
- package/es/Normalization/normalizers.d.ts +2 -0
- package/es/Normalization/normalizers.js +2 -0
- package/es/Select/BooleanSelectGroup.d.ts +1 -0
- package/es/Select/BooleanSelectGroup.js +1 -0
- package/es/Select/NumberSelectGroup.d.ts +2 -0
- package/es/Select/NumberSelectGroup.js +2 -0
- package/es/Select/SelectGroup.d.ts +6 -0
- package/es/Select/SelectGroup.js +1 -0
- package/es/Select/StringSelectGroup.d.ts +2 -0
- package/es/Select/StringSelectGroup.js +2 -0
- package/es/StandardFormActions.d.ts +5 -0
- package/es/StandardFormActions.js +1 -0
- package/es/SubmitFormButton.d.ts +3 -0
- package/es/SubmitFormButton.js +1 -0
- package/es/TextArea/TextAreaGroup.d.ts +1 -0
- package/es/TextArea/TextAreaGroup.js +1 -1
- package/es/Validation/validators.d.ts +3 -0
- package/es/Validation/validators.js +3 -2
- package/es/setupTests.d.ts +1 -0
- package/es/setupTests.js +1 -0
- package/lib/AddressInput/AddressInput.d.ts +10 -0
- package/lib/AddressInput/AddressInput.js +10 -0
- package/lib/ConfirmBaseForm/ConfirmBaseForm.d.ts +14 -0
- package/lib/ConfirmBaseForm/ConfirmBaseForm.js +6 -0
- package/lib/ConfirmDeleteForm/ConfirmDeleteForm.d.ts +13 -0
- package/lib/ConfirmDeleteForm/ConfirmDeleteForm.js +5 -0
- package/lib/DatePicker/DatePickerGroup.d.ts +5 -0
- package/lib/DatePicker/DatePickerGroup.js +5 -0
- package/lib/DatePicker/StringDateOnlyPickerGroup.d.ts +5 -0
- package/lib/DatePicker/StringDateOnlyPickerGroup.js +5 -0
- package/lib/DatePicker/StringDatePickerGroup.d.ts +5 -0
- package/lib/DatePicker/StringDatePickerGroup.js +5 -0
- package/lib/Field/Field.d.ts +6 -0
- package/lib/Field/FieldErrorScrollTarget.d.ts +1 -0
- package/lib/Field/FieldErrorScrollTarget.js +1 -0
- package/lib/Field/StandAloneInput.d.ts +4 -0
- package/lib/Field/StandAloneInput.js +1 -0
- package/lib/Field/useStandardField.d.ts +6 -0
- package/lib/Field/useStandardField.js +8 -9
- package/lib/FieldArray/FieldArray.d.ts +9 -0
- package/lib/FieldArray/FieldArray.js +5 -0
- package/lib/File/FileGroup.d.ts +2 -0
- package/lib/File/FileGroup.js +1 -0
- package/lib/Form/FocusError.d.ts +2 -0
- package/lib/Form/FocusError.js +1 -0
- package/lib/Form/Form.d.ts +6 -0
- package/lib/Form/Form.js +1 -0
- package/lib/Form/FormBasedPreventNavigation.d.ts +3 -1
- package/lib/Form/FormBasedPreventNavigation.js +13 -60
- package/lib/Form/LegacyFormBasedPreventNavigation.d.ts +17 -0
- package/lib/Form/LegacyFormBasedPreventNavigation.js +72 -0
- package/lib/Form/NewFormBasedPreventNavigation.d.ts +14 -0
- package/lib/Form/NewFormBasedPreventNavigation.js +42 -0
- package/lib/Form/ServerErrorContext.d.ts +1 -0
- package/lib/Form/ServerErrorContext.js +1 -0
- package/lib/FormActions.d.ts +6 -0
- package/lib/FormActions.js +1 -0
- package/lib/FormDefaults.d.ts +1 -0
- package/lib/FormDefaults.js +1 -0
- package/lib/Group.d.ts +5 -3
- package/lib/Group.js +2 -2
- package/lib/Input/IconInputGroup.d.ts +3 -0
- package/lib/Input/IconInputGroup.js +1 -0
- package/lib/Input/InputGroup.d.ts +1 -1
- package/lib/Input/InputGroup.js +2 -2
- package/lib/Input/MoneyInputGroup.d.ts +2 -0
- package/lib/Input/MoneyInputGroup.js +1 -0
- package/lib/Input/NumberInputGroup.d.ts +2 -0
- package/lib/Input/NumberInputGroup.js +1 -0
- package/lib/Input/StringInputGroup.d.ts +1 -0
- package/lib/Input/StringInputGroup.js +1 -0
- package/lib/Normalization/normalizers.d.ts +2 -0
- package/lib/Normalization/normalizers.js +2 -0
- package/lib/Select/BooleanSelectGroup.d.ts +1 -0
- package/lib/Select/BooleanSelectGroup.js +1 -0
- package/lib/Select/NumberSelectGroup.d.ts +2 -0
- package/lib/Select/NumberSelectGroup.js +2 -0
- package/lib/Select/SelectGroup.d.ts +6 -0
- package/lib/Select/SelectGroup.js +1 -0
- package/lib/Select/StringSelectGroup.d.ts +2 -0
- package/lib/Select/StringSelectGroup.js +2 -0
- package/lib/StandardFormActions.d.ts +5 -0
- package/lib/StandardFormActions.js +1 -0
- package/lib/SubmitFormButton.d.ts +3 -0
- package/lib/SubmitFormButton.js +1 -0
- package/lib/TextArea/TextAreaGroup.d.ts +1 -0
- package/lib/TextArea/TextAreaGroup.js +1 -1
- package/lib/Validation/validators.d.ts +3 -0
- package/lib/Validation/validators.js +3 -2
- package/lib/setupTests.d.ts +1 -0
- package/lib/setupTests.js +3 -0
- package/package.json +9 -6
- package/src/AddressInput/AddressInput.tsx +10 -0
- package/src/AddressInput/__snapshots__/AddressInput.test.tsx.snap +4 -0
- package/src/ConfirmBaseForm/ConfirmBaseForm.tsx +14 -0
- package/src/ConfirmDeleteForm/ConfirmDeleteForm.tsx +13 -0
- package/src/DatePicker/DatePicker.test.tsx +1 -1
- package/src/DatePicker/DatePickerGroup.tsx +5 -2
- package/src/DatePicker/StringDateOnlyPickerGroup.tsx +5 -0
- package/src/DatePicker/StringDatePickerGroup.tsx +6 -0
- package/src/Field/Field.tsx +6 -1
- package/src/Field/FieldErrorScrollTarget.tsx +1 -0
- package/src/Field/StandAloneInput.tsx +4 -0
- package/src/Field/useStandardField.ts +13 -9
- package/src/FieldArray/FieldArray.tsx +9 -0
- package/src/File/FileGroup.tsx +2 -0
- package/src/Form/FocusError.tsx +3 -0
- package/src/Form/Form.tsx +6 -0
- package/src/Form/FormBasedPreventNavigation.tsx +26 -60
- package/src/Form/LegacyFormBasedPreventNavigation.tsx +77 -0
- package/src/Form/NewFormBasedPreventNavigation.tsx +59 -0
- package/src/Form/ServerErrorContext.ts +1 -0
- package/src/FormActions.tsx +7 -0
- package/src/FormDefaults.ts +1 -0
- package/src/Group.tsx +8 -4
- package/src/Input/IconInputGroup.tsx +3 -0
- package/src/Input/InputGroup.tsx +3 -1
- package/src/Input/MoneyInputGroup.tsx +2 -0
- package/src/Input/NumberInputGroup.tsx +2 -0
- package/src/Input/StringInputGroup.tsx +1 -0
- package/src/Input/__Tests__/__snapshots__/IconInputGroup.test.tsx.snap +1 -0
- package/src/Input/__Tests__/__snapshots__/MoneyInputGroup.test.tsx.snap +1 -0
- package/src/Input/__Tests__/__snapshots__/NumberInputGroup.test.tsx.snap +1 -0
- package/src/Input/__Tests__/__snapshots__/StringInputGroup.test.tsx.snap +1 -0
- package/src/Normalization/normalizers.ts +2 -0
- package/src/Select/BooleanSelectGroup.tsx +1 -0
- package/src/Select/NumberSelectGroup.tsx +2 -0
- package/src/Select/SelectGroup.tsx +6 -0
- package/src/Select/StringSelectGroup.tsx +2 -0
- package/src/StandardFormActions.tsx +5 -0
- package/src/SubmitFormButton.tsx +3 -0
- package/src/TextArea/TextAreaGroup.tsx +1 -1
- package/src/Validation/validators.ts +3 -2
- package/src/__Tests__/FormTestBase.tsx +7 -6
- package/src/__Tests__/RealisticForm.test.tsx +82 -0
- package/src/setupTests.ts +1 -0
- package/es/__Tests__/FormTestBase.d.ts +0 -27
- package/es/__Tests__/FormTestBase.js +0 -83
- package/es/__Tests__/index.d.ts +0 -2
- package/es/__Tests__/index.js +0 -2
- package/lib/__Tests__/FormTestBase.d.ts +0 -27
- package/lib/__Tests__/FormTestBase.js +0 -86
- package/lib/__Tests__/index.d.ts +0 -2
- 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
|
-
|
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
|
-
//
|
63
|
-
//
|
64
|
-
//
|
65
|
-
//
|
66
|
-
|
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
|
package/src/File/FileGroup.tsx
CHANGED
@@ -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>;
|
package/src/Form/FocusError.tsx
CHANGED
@@ -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
|
2
|
-
import { ContextType, useContext
|
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
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
}
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
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) => {
|
package/src/FormActions.tsx
CHANGED
@@ -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,
|
package/src/FormDefaults.ts
CHANGED
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
|
-
/**
|
12
|
+
/** Simple text label before the input. */
|
12
13
|
label?: string;
|
13
14
|
|
14
|
-
/**
|
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
|
-
/**
|
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
|
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>;
|
package/src/Input/InputGroup.tsx
CHANGED
@@ -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>;
|
@@ -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
|
}
|