envoc-form 4.0.1-9 → 5.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +5173 -12
- package/es/AddressInput/AddressInput.d.ts +24 -15
- package/es/AddressInput/AddressInput.js +20 -21
- package/es/AddressInput/UsStates.d.ts +3 -3
- package/es/AddressInput/UsStates.js +53 -53
- package/es/ConfirmBaseForm/ConfirmBaseForm.d.ts +26 -12
- package/es/ConfirmBaseForm/ConfirmBaseForm.js +42 -36
- package/es/ConfirmDeleteForm/ConfirmDeleteForm.d.ts +24 -11
- package/es/ConfirmDeleteForm/ConfirmDeleteForm.js +60 -55
- package/es/DatePicker/DatePickerGroup.d.ts +13 -9
- package/es/DatePicker/DatePickerGroup.js +87 -77
- package/es/DatePicker/DatePickerHelper.d.ts +3 -3
- package/es/DatePicker/DatePickerHelper.js +1 -1
- package/es/DatePicker/StringDateOnlyPickerGroup.d.ts +9 -5
- package/es/DatePicker/StringDateOnlyPickerGroup.js +29 -24
- package/es/DatePicker/StringDatePickerGroup.d.ts +9 -5
- package/es/DatePicker/StringDatePickerGroup.js +24 -19
- package/es/Field/CustomFieldInputProps.d.ts +7 -7
- package/es/Field/CustomFieldInputProps.js +1 -1
- package/es/Field/CustomFieldMetaProps.d.ts +4 -4
- package/es/Field/CustomFieldMetaProps.js +1 -1
- package/es/Field/Field.d.ts +28 -22
- package/es/Field/Field.js +51 -46
- package/es/Field/FieldErrorScrollTarget.d.ts +2 -2
- package/es/Field/FieldErrorScrollTarget.js +12 -11
- package/es/Field/FieldNameContext.d.ts +3 -3
- package/es/Field/FieldNameContext.js +3 -3
- package/es/Field/FieldSection.d.ts +7 -7
- package/es/Field/FieldSection.js +9 -20
- package/es/Field/InjectedFieldProps.d.ts +7 -7
- package/es/Field/InjectedFieldProps.js +1 -1
- package/es/Field/StandAloneInput.d.ts +13 -9
- package/es/Field/StandAloneInput.js +50 -49
- package/es/Field/useStandardField.d.ts +21 -15
- package/es/Field/useStandardField.js +92 -93
- package/es/FieldArray/FieldArray.d.ts +24 -15
- package/es/FieldArray/FieldArray.js +77 -83
- package/es/File/FileGroup.d.ts +10 -8
- package/es/File/FileGroup.js +53 -52
- package/es/File/FileList.d.ts +5 -6
- package/es/File/FileList.js +10 -21
- package/es/File/humanFileSize.d.ts +1 -1
- package/es/File/humanFileSize.js +6 -6
- package/es/Form/FocusError.d.ts +7 -5
- package/es/Form/FocusError.js +42 -41
- package/es/Form/Form.d.ts +33 -27
- package/es/Form/Form.js +141 -140
- package/es/Form/FormBasedPreventNavigation.d.ts +6 -5
- package/es/Form/FormBasedPreventNavigation.js +20 -53
- 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 +11 -10
- package/es/Form/ServerErrorContext.js +9 -8
- package/es/FormActions.d.ts +12 -7
- package/es/FormActions.js +16 -26
- package/es/FormDefaults.d.ts +4 -3
- package/es/FormDefaults.js +2 -1
- package/es/Group.d.ts +16 -13
- package/es/Group.js +14 -24
- package/es/Input/IconInputGroup.d.ts +12 -9
- package/es/Input/IconInputGroup.js +43 -41
- package/es/Input/InputGroup.d.ts +12 -12
- package/es/Input/InputGroup.js +34 -34
- package/es/Input/MoneyInputGroup.d.ts +9 -7
- package/es/Input/MoneyInputGroup.js +43 -42
- package/es/Input/NumberInputGroup.d.ts +9 -7
- package/es/Input/NumberInputGroup.js +43 -42
- package/es/Input/StringInputGroup.d.ts +8 -7
- package/es/Input/StringInputGroup.js +43 -41
- package/es/Normalization/NormalizationFunction.d.ts +4 -4
- package/es/Normalization/NormalizationFunction.js +1 -1
- package/es/Normalization/normalizers.d.ts +4 -2
- package/es/Normalization/normalizers.js +32 -30
- package/es/Select/BooleanSelectGroup.d.ts +7 -7
- package/es/Select/BooleanSelectGroup.js +28 -27
- package/es/Select/NumberSelectGroup.d.ts +9 -8
- package/es/Select/NumberSelectGroup.js +21 -19
- package/es/Select/SelectGroup.d.ts +27 -22
- package/es/Select/SelectGroup.js +65 -64
- package/es/Select/SelectGroupPropsHelper.d.ts +3 -3
- package/es/Select/SelectGroupPropsHelper.js +1 -1
- package/es/Select/StringSelectGroup.d.ts +9 -8
- package/es/Select/StringSelectGroup.js +21 -19
- package/es/StandardFormActions.d.ts +11 -6
- package/es/StandardFormActions.js +14 -24
- package/es/SubmitFormButton.d.ts +10 -7
- package/es/SubmitFormButton.js +40 -39
- package/es/TextArea/TextAreaGroup.d.ts +9 -8
- package/es/TextArea/TextAreaGroup.js +35 -34
- package/es/Validation/ValidatedApiResult.d.ts +6 -6
- package/es/Validation/ValidatedApiResult.js +1 -1
- package/es/Validation/ValidationError.d.ts +5 -5
- package/es/Validation/ValidationError.js +1 -1
- package/es/Validation/ValidationFunction.d.ts +4 -4
- package/es/Validation/ValidationFunction.js +1 -1
- package/es/Validation/validators.d.ts +18 -15
- package/es/Validation/validators.js +77 -73
- package/es/index.d.ts +73 -73
- package/es/index.js +50 -50
- package/es/setupTests.d.ts +1 -0
- package/es/setupTests.js +1 -0
- package/es/utils/objectContainsNonSerializableProperty.d.ts +1 -1
- package/es/utils/objectContainsNonSerializableProperty.js +14 -14
- package/es/utils/objectToFormData.d.ts +10 -10
- package/es/utils/objectToFormData.js +77 -77
- package/es/utils/typeChecks.d.ts +8 -8
- package/es/utils/typeChecks.js +18 -18
- package/lib/AddressInput/AddressInput.d.ts +24 -15
- package/lib/AddressInput/AddressInput.js +26 -27
- package/lib/AddressInput/UsStates.d.ts +3 -3
- package/lib/AddressInput/UsStates.js +55 -55
- package/lib/ConfirmBaseForm/ConfirmBaseForm.d.ts +26 -12
- package/lib/ConfirmBaseForm/ConfirmBaseForm.js +48 -42
- package/lib/ConfirmDeleteForm/ConfirmDeleteForm.d.ts +24 -11
- package/lib/ConfirmDeleteForm/ConfirmDeleteForm.js +66 -61
- package/lib/DatePicker/DatePickerGroup.d.ts +13 -9
- package/lib/DatePicker/DatePickerGroup.js +95 -85
- package/lib/DatePicker/DatePickerHelper.d.ts +3 -3
- package/lib/DatePicker/DatePickerHelper.js +2 -2
- package/lib/DatePicker/StringDateOnlyPickerGroup.d.ts +9 -5
- package/lib/DatePicker/StringDateOnlyPickerGroup.js +35 -30
- package/lib/DatePicker/StringDatePickerGroup.d.ts +9 -5
- package/lib/DatePicker/StringDatePickerGroup.js +30 -25
- package/lib/Field/CustomFieldInputProps.d.ts +7 -7
- package/lib/Field/CustomFieldInputProps.js +2 -2
- package/lib/Field/CustomFieldMetaProps.d.ts +4 -4
- package/lib/Field/CustomFieldMetaProps.js +2 -2
- package/lib/Field/Field.d.ts +28 -22
- package/lib/Field/Field.js +56 -51
- package/lib/Field/FieldErrorScrollTarget.d.ts +2 -2
- package/lib/Field/FieldErrorScrollTarget.js +15 -14
- package/lib/Field/FieldNameContext.d.ts +3 -3
- package/lib/Field/FieldNameContext.js +9 -9
- package/lib/Field/FieldSection.d.ts +7 -7
- package/lib/Field/FieldSection.js +12 -23
- package/lib/Field/InjectedFieldProps.d.ts +7 -7
- package/lib/Field/InjectedFieldProps.js +2 -2
- package/lib/Field/StandAloneInput.d.ts +13 -9
- package/lib/Field/StandAloneInput.js +76 -75
- package/lib/Field/useStandardField.d.ts +21 -15
- package/lib/Field/useStandardField.js +95 -96
- package/lib/FieldArray/FieldArray.d.ts +24 -15
- package/lib/FieldArray/FieldArray.js +83 -89
- package/lib/File/FileGroup.d.ts +10 -8
- package/lib/File/FileGroup.js +58 -57
- package/lib/File/FileList.d.ts +5 -6
- package/lib/File/FileList.js +13 -24
- package/lib/File/humanFileSize.d.ts +1 -1
- package/lib/File/humanFileSize.js +10 -10
- package/lib/Form/FocusError.d.ts +7 -5
- package/lib/Form/FocusError.js +48 -47
- package/lib/Form/Form.d.ts +33 -27
- package/lib/Form/Form.js +147 -146
- package/lib/Form/FormBasedPreventNavigation.d.ts +6 -5
- package/lib/Form/FormBasedPreventNavigation.js +26 -56
- 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 +11 -10
- package/lib/Form/ServerErrorContext.js +15 -14
- package/lib/FormActions.d.ts +12 -7
- package/lib/FormActions.js +22 -32
- package/lib/FormDefaults.d.ts +4 -3
- package/lib/FormDefaults.js +5 -4
- package/lib/Group.d.ts +16 -13
- package/lib/Group.js +20 -30
- package/lib/Input/IconInputGroup.d.ts +12 -9
- package/lib/Input/IconInputGroup.js +48 -46
- package/lib/Input/InputGroup.d.ts +12 -12
- package/lib/Input/InputGroup.js +39 -39
- package/lib/Input/MoneyInputGroup.d.ts +9 -7
- package/lib/Input/MoneyInputGroup.js +48 -47
- package/lib/Input/NumberInputGroup.d.ts +9 -7
- package/lib/Input/NumberInputGroup.js +48 -47
- package/lib/Input/StringInputGroup.d.ts +8 -7
- package/lib/Input/StringInputGroup.js +48 -46
- package/lib/Normalization/NormalizationFunction.d.ts +4 -4
- package/lib/Normalization/NormalizationFunction.js +2 -2
- package/lib/Normalization/normalizers.d.ts +4 -2
- package/lib/Normalization/normalizers.js +37 -35
- package/lib/Select/BooleanSelectGroup.d.ts +7 -7
- package/lib/Select/BooleanSelectGroup.js +35 -34
- package/lib/Select/NumberSelectGroup.d.ts +9 -8
- package/lib/Select/NumberSelectGroup.js +29 -27
- package/lib/Select/SelectGroup.d.ts +27 -22
- package/lib/Select/SelectGroup.js +71 -70
- package/lib/Select/SelectGroupPropsHelper.d.ts +3 -3
- package/lib/Select/SelectGroupPropsHelper.js +2 -2
- package/lib/Select/StringSelectGroup.d.ts +9 -8
- package/lib/Select/StringSelectGroup.js +29 -27
- package/lib/StandardFormActions.d.ts +11 -6
- package/lib/StandardFormActions.js +20 -30
- package/lib/SubmitFormButton.d.ts +10 -7
- package/lib/SubmitFormButton.js +45 -44
- package/lib/TextArea/TextAreaGroup.d.ts +9 -8
- package/lib/TextArea/TextAreaGroup.js +40 -39
- package/lib/Validation/ValidatedApiResult.d.ts +6 -6
- package/lib/Validation/ValidatedApiResult.js +2 -2
- package/lib/Validation/ValidationError.d.ts +5 -5
- package/lib/Validation/ValidationError.js +2 -2
- package/lib/Validation/ValidationFunction.d.ts +4 -4
- package/lib/Validation/ValidationFunction.js +2 -2
- package/lib/Validation/validators.d.ts +18 -15
- package/lib/Validation/validators.js +90 -86
- package/lib/index.d.ts +73 -73
- package/lib/index.js +117 -117
- package/lib/setupTests.d.ts +1 -0
- package/lib/setupTests.js +3 -0
- package/lib/utils/objectContainsNonSerializableProperty.d.ts +1 -1
- package/lib/utils/objectContainsNonSerializableProperty.js +17 -17
- package/lib/utils/objectToFormData.d.ts +10 -10
- package/lib/utils/objectToFormData.js +79 -79
- package/lib/utils/typeChecks.d.ts +8 -8
- package/lib/utils/typeChecks.js +29 -29
- package/package.json +14 -11
- package/src/AddressInput/AddressInput.tsx +15 -5
- package/src/AddressInput/__snapshots__/AddressInput.test.tsx.snap +14 -12
- 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 +18 -7
- package/src/DatePicker/StringDateOnlyPickerGroup.tsx +7 -2
- package/src/DatePicker/StringDatePickerGroup.tsx +7 -1
- package/src/Field/Field.tsx +20 -7
- 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 +14 -5
- package/src/File/FileGroup.tsx +9 -3
- package/src/File/FileList.tsx +2 -2
- package/src/Form/FocusError.tsx +3 -0
- package/src/Form/Form.tsx +6 -0
- package/src/Form/FormBasedPreventNavigation.tsx +28 -46
- 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 +12 -4
- package/src/Input/IconInputGroup.tsx +5 -2
- package/src/Input/InputGroup.tsx +13 -5
- package/src/Input/MoneyInputGroup.tsx +3 -1
- package/src/Input/NumberInputGroup.tsx +3 -1
- package/src/Input/StringInputGroup.tsx +3 -2
- 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 +4 -2
- package/src/Select/BooleanSelectGroup.tsx +2 -1
- package/src/Select/NumberSelectGroup.tsx +4 -2
- package/src/Select/SelectGroup.tsx +13 -2
- package/src/Select/StringSelectGroup.tsx +2 -0
- package/src/Select/__tests__/__snapshots__/BooleanSelectGroup.test.tsx.snap +6 -8
- package/src/Select/__tests__/__snapshots__/NumberSelectGroup.test.tsx.snap +12 -16
- package/src/Select/__tests__/__snapshots__/StringSelectGroup.test.tsx.snap +12 -16
- package/src/StandardFormActions.tsx +5 -0
- package/src/SubmitFormButton.tsx +5 -1
- package/src/TextArea/TextAreaGroup.tsx +6 -4
- package/src/Validation/validators.ts +19 -14
- package/src/__Tests__/FormTestBase.tsx +9 -8
- package/src/__Tests__/RealisticForm.test.tsx +82 -0
- package/src/index.ts +4 -4
- package/src/setupTests.ts +1 -0
- package/src/utils/objectToFormData.test.tsx +1 -1
- 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
@@ -1,16 +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
|
-
block
|
12
|
+
block?: History['block'];
|
13
|
+
location: Location;
|
12
14
|
}
|
13
|
-
|
15
|
+
|
14
16
|
type NavigationContextWithBlock = ContextType<typeof NavigationContext> & {
|
15
17
|
navigator: Navigator;
|
16
18
|
};
|
@@ -19,6 +21,8 @@ export interface FormBasedPreventNavigationProps {
|
|
19
21
|
ignoreLostChanges?: boolean;
|
20
22
|
promptMessage?: string;
|
21
23
|
}
|
24
|
+
|
25
|
+
/** Prevent the user from navigating away from a form if there are any changes. */
|
22
26
|
export default function FormBasedPreventNavigation({
|
23
27
|
ignoreLostChanges,
|
24
28
|
promptMessage = 'Changes you made may not be saved.',
|
@@ -30,45 +34,23 @@ export default function FormBasedPreventNavigation({
|
|
30
34
|
NavigationContext
|
31
35
|
) as NavigationContextWithBlock;
|
32
36
|
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
// this block if the transition is cancelled for some reason.
|
53
|
-
unblock();
|
54
|
-
tx.retry();
|
55
|
-
},
|
56
|
-
};
|
57
|
-
|
58
|
-
blocker(autoUnblockingTx);
|
59
|
-
});
|
60
|
-
|
61
|
-
window.addEventListener('beforeunload', beforeUnload);
|
62
|
-
return () => {
|
63
|
-
unblock();
|
64
|
-
window.removeEventListener('beforeunload', beforeUnload);
|
65
|
-
};
|
66
|
-
|
67
|
-
function beforeUnload(e: BeforeUnloadEvent) {
|
68
|
-
e.preventDefault();
|
69
|
-
e.returnValue = promptMessage;
|
70
|
-
}
|
71
|
-
}, [preventNavigate, promptMessage, navigator]);
|
72
|
-
|
73
|
-
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
|
+
}
|
74
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,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
|
-
/**
|
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;
|
20
|
+
|
21
|
+
required?: boolean;
|
18
22
|
}
|
19
23
|
|
20
|
-
/**
|
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
|
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>;
|
package/src/Input/InputGroup.tsx
CHANGED
@@ -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>
|
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
|
-
|
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>;
|
@@ -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>;
|
@@ -1,4 +1,5 @@
|
|
1
|
-
|
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
|
-
|
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
|
}
|