envoc-form 5.0.3 → 5.0.6
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 +163 -20
- package/es/ConfirmBaseForm/ConfirmBaseForm.js +2 -2
- package/es/DatePicker/DatePickerGroup.js +2 -2
- package/es/FieldArray/FieldArray.js +4 -4
- package/es/File/FileGroup.js +3 -3
- package/es/Form/Form.js +2 -2
- package/es/Group.js +3 -3
- package/es/Input/CheckboxGroup.d.ts +6 -0
- package/es/Input/CheckboxGroup.js +14 -0
- package/es/Input/CheckboxInputGroup.d.ts +13 -0
- package/es/Input/CheckboxInputGroup.js +41 -0
- package/es/Input/IconInputGroup.js +2 -2
- package/es/Input/InputGroup.js +2 -2
- package/es/Input/MoneyInputGroup.js +2 -2
- package/es/Input/NumberInputGroup.js +2 -2
- package/es/Input/PhoneNumberInputGroup.js +2 -2
- package/es/Input/StringInputGroup.js +2 -2
- package/es/Select/SelectGroup.js +3 -3
- package/es/StandardFormActions.js +3 -3
- package/es/SubmitFormButton.js +2 -2
- package/es/TextArea/TextAreaGroup.js +2 -2
- package/es/index.d.ts +2 -0
- package/es/index.js +1 -0
- package/lib/ConfirmBaseForm/ConfirmBaseForm.js +2 -5
- package/lib/DatePicker/DatePickerGroup.js +2 -2
- package/lib/FieldArray/FieldArray.js +4 -4
- package/lib/File/FileGroup.js +3 -3
- package/lib/Form/Form.js +2 -2
- package/lib/Group.js +3 -3
- package/lib/Input/CheckboxGroup.d.ts +6 -0
- package/lib/Input/CheckboxGroup.js +20 -0
- package/lib/Input/CheckboxInputGroup.d.ts +13 -0
- package/lib/Input/CheckboxInputGroup.js +46 -0
- package/lib/Input/IconInputGroup.js +2 -2
- package/lib/Input/InputGroup.js +2 -2
- package/lib/Input/MoneyInputGroup.js +2 -2
- package/lib/Input/NumberInputGroup.js +2 -2
- package/lib/Input/PhoneNumberInputGroup.js +2 -2
- package/lib/Input/StringInputGroup.js +2 -2
- package/lib/Select/SelectGroup.js +3 -3
- package/lib/StandardFormActions.js +3 -3
- package/lib/SubmitFormButton.js +2 -2
- package/lib/TextArea/TextAreaGroup.js +2 -2
- package/lib/index.d.ts +2 -0
- package/lib/index.js +3 -1
- package/package.json +111 -111
- package/src/AddressInput/AddressInput.test.tsx +27 -27
- package/src/AddressInput/AddressInput.tsx +82 -82
- package/src/AddressInput/UsStates.ts +55 -55
- package/src/AddressInput/__snapshots__/AddressInput.test.tsx.snap +182 -182
- package/src/ConfirmBaseForm/ConfirmBaseForm.test.tsx +24 -24
- package/src/ConfirmBaseForm/ConfirmBaseForm.tsx +74 -74
- package/src/ConfirmBaseForm/__snapshots__/ConfirmBaseForm.test.tsx.snap +23 -23
- package/src/ConfirmDeleteForm/ConfirmDeleteForm.test.tsx +24 -24
- package/src/ConfirmDeleteForm/ConfirmDeleteForm.tsx +87 -87
- package/src/ConfirmDeleteForm/__snapshots__/ConfirmDeleteForm.test.tsx.snap +25 -25
- package/src/DatePicker/DatePicker.test.tsx +48 -48
- package/src/DatePicker/DatePickerGroup.tsx +109 -115
- package/src/DatePicker/DatePickerHelper.ts +4 -4
- package/src/DatePicker/StringDateOnlyPickerGroup.tsx +28 -28
- package/src/DatePicker/StringDatePickerGroup.tsx +20 -20
- package/src/DatePicker/__snapshots__/DatePicker.test.tsx.snap +152 -152
- package/src/Field/CustomFieldInputProps.ts +10 -10
- package/src/Field/CustomFieldMetaProps.ts +5 -5
- package/src/Field/Field.tsx +113 -113
- package/src/Field/FieldErrorScrollTarget.tsx +12 -12
- package/src/Field/FieldNameContext.ts +6 -6
- package/src/Field/FieldSection.tsx +18 -18
- package/src/Field/InjectedFieldProps.ts +8 -8
- package/src/Field/StandAloneInput.tsx +55 -55
- package/src/Field/useStandardField.ts +125 -125
- package/src/FieldArray/FieldArray.tsx +154 -154
- package/src/File/FileGroup.test.tsx +35 -35
- package/src/File/FileGroup.tsx +82 -85
- package/src/File/FileList.tsx +21 -21
- package/src/File/__snapshots__/FileGroup.test.tsx.snap +34 -34
- package/src/File/humanFileSize.ts +8 -8
- package/src/Form/FocusError.tsx +55 -55
- package/src/Form/Form.test.tsx +14 -14
- package/src/Form/Form.tsx +234 -237
- package/src/Form/FormBasedPreventNavigation.tsx +56 -56
- package/src/Form/LegacyFormBasedPreventNavigation.tsx +77 -77
- package/src/Form/NewFormBasedPreventNavigation.tsx +59 -59
- package/src/Form/ServerErrorContext.ts +18 -18
- package/src/Form/__snapshots__/Form.test.tsx.snap +10 -10
- package/src/FormActions.tsx +47 -47
- package/src/FormDefaults.ts +2 -2
- package/src/Group.tsx +62 -62
- package/src/Input/CheckboxGroup.tsx +60 -0
- package/src/Input/CheckboxInputGroup.tsx +78 -0
- package/src/Input/IconInputGroup.tsx +54 -54
- package/src/Input/InputGroup.tsx +66 -72
- package/src/Input/MoneyInputGroup.tsx +47 -50
- package/src/Input/NumberInputGroup.tsx +45 -48
- package/src/Input/PhoneNumberInputGroup.tsx +45 -45
- package/src/Input/StringInputGroup.tsx +50 -53
- package/src/Input/__Tests__/CheckboxInputGroup.test.tsx +26 -0
- package/src/Input/__Tests__/IconInputGroup.test.tsx +35 -35
- package/src/Input/__Tests__/MoneyInputGroup.test.tsx +37 -37
- package/src/Input/__Tests__/NumberInputGroup.test.tsx +35 -35
- package/src/Input/__Tests__/PhoneNumberInputGroup.test.tsx +36 -36
- package/src/Input/__Tests__/StringInputGroup.test.tsx +27 -27
- package/src/Input/__Tests__/__snapshots__/CheckboxInputGroup.test.tsx.snap +33 -0
- package/src/Input/__Tests__/__snapshots__/IconInputGroup.test.tsx.snap +32 -32
- package/src/Input/__Tests__/__snapshots__/MoneyInputGroup.test.tsx.snap +34 -34
- package/src/Input/__Tests__/__snapshots__/NumberInputGroup.test.tsx.snap +32 -32
- package/src/Input/__Tests__/__snapshots__/PhoneNumberInputGroup.test.tsx.snap +33 -33
- package/src/Input/__Tests__/__snapshots__/StringInputGroup.test.tsx.snap +31 -31
- package/src/Normalization/NormalizationFunction.ts +4 -4
- package/src/Normalization/normalizers.ts +44 -44
- package/src/Select/BooleanSelectGroup.tsx +28 -28
- package/src/Select/NumberSelectGroup.tsx +16 -16
- package/src/Select/SelectGroup.tsx +124 -124
- package/src/Select/SelectGroupPropsHelper.ts +4 -4
- package/src/Select/StringSelectGroup.tsx +16 -16
- package/src/Select/__tests__/BooleanSelectGroup.test.tsx +35 -35
- package/src/Select/__tests__/NumberSelectGroup.test.tsx +87 -87
- package/src/Select/__tests__/StringSelectGroup.test.tsx +89 -89
- package/src/Select/__tests__/__snapshots__/BooleanSelectGroup.test.tsx.snap +98 -98
- package/src/Select/__tests__/__snapshots__/NumberSelectGroup.test.tsx.snap +195 -195
- package/src/Select/__tests__/__snapshots__/StringSelectGroup.test.tsx.snap +195 -195
- package/src/StandardFormActions.tsx +41 -41
- package/src/SubmitFormButton.tsx +54 -54
- package/src/TextArea/TextAreaGroup.tsx +64 -64
- package/src/Validation/ValidatedApiResult.ts +8 -8
- package/src/Validation/ValidationError.ts +6 -6
- package/src/Validation/ValidationFunction.ts +4 -4
- package/src/Validation/validators.test.tsx +81 -81
- package/src/Validation/validators.ts +97 -97
- package/src/__Tests__/FormTestBase.tsx +65 -64
- package/src/__Tests__/RealisticForm.test.tsx +82 -82
- package/src/__Tests__/StandardFormActions.test.tsx +17 -17
- package/src/__Tests__/SubmitFormButton.test.tsx +17 -17
- package/src/__Tests__/__snapshots__/StandardFormActions.test.tsx.snap +27 -27
- package/src/__Tests__/__snapshots__/SubmitFormButton.test.tsx.snap +20 -20
- package/src/__Tests__/index.ts +3 -3
- package/src/_variables.scss +11 -11
- package/src/index.ts +156 -153
- package/src/react-app-env.d.ts +1 -1
- package/src/setupTests.ts +1 -1
- package/src/utils/objectContainsNonSerializableProperty.test.tsx +49 -49
- package/src/utils/objectContainsNonSerializableProperty.ts +17 -17
- package/src/utils/objectToFormData.test.tsx +76 -76
- package/src/utils/objectToFormData.ts +105 -105
- package/src/utils/typeChecks.ts +18 -18
@@ -1,55 +1,55 @@
|
|
1
|
-
import { ElementType, useRef } from 'react';
|
2
|
-
import * as uuid from 'uuid';
|
3
|
-
import { CustomFieldInputProps } from './CustomFieldInputProps';
|
4
|
-
import { CustomFieldMetaProps } from './CustomFieldMetaProps';
|
5
|
-
import { RenderComponent, RenderComponentProps } from './Field';
|
6
|
-
import { InjectedFieldProps } from './InjectedFieldProps';
|
7
|
-
|
8
|
-
export type StandAloneInputProps<
|
9
|
-
TValue,
|
10
|
-
TRenderComponent extends ElementType
|
11
|
-
> = {
|
12
|
-
/** Component to render inside of the field. */
|
13
|
-
Component: RenderComponent<TValue, TRenderComponent>;
|
14
|
-
/** Value of the field. */
|
15
|
-
value: TValue;
|
16
|
-
/** Function to change the value of the field. */
|
17
|
-
onChange: (arg: TValue) => void;
|
18
|
-
} & Omit<
|
19
|
-
RenderComponentProps<TValue, TRenderComponent>,
|
20
|
-
| keyof InjectedFieldProps<TValue>
|
21
|
-
| 'value'
|
22
|
-
| 'onChange'
|
23
|
-
| 'normalize'
|
24
|
-
| 'validate'
|
25
|
-
>;
|
26
|
-
|
27
|
-
/** The stand alone version of `<Field/>`. Can use a type of input component outside of a `<Form/>`. */
|
28
|
-
export default function StandAloneInput<TValue, TComponent extends ElementType>(
|
29
|
-
props: StandAloneInputProps<TValue, TComponent>
|
30
|
-
) {
|
31
|
-
const { Component, value, onChange, disabled, ...rest } = props;
|
32
|
-
const handleChange = (arg: TValue) => {
|
33
|
-
if (!disabled) {
|
34
|
-
onChange(arg);
|
35
|
-
}
|
36
|
-
};
|
37
|
-
const Wrapped = Component as React.ComponentType<InjectedFieldProps<TValue>>;
|
38
|
-
const idRef = useRef(uuid.v4());
|
39
|
-
const input: CustomFieldInputProps<TValue> = {
|
40
|
-
id: idRef.current,
|
41
|
-
name: idRef.current,
|
42
|
-
value: value,
|
43
|
-
onChange: handleChange,
|
44
|
-
onBlur: () => {},
|
45
|
-
};
|
46
|
-
|
47
|
-
const meta: CustomFieldMetaProps<TValue> = {
|
48
|
-
warning: undefined,
|
49
|
-
value: value,
|
50
|
-
touched: false,
|
51
|
-
initialTouched: false,
|
52
|
-
};
|
53
|
-
|
54
|
-
return <Wrapped {...rest} disabled={disabled} input={input} meta={meta} />;
|
55
|
-
}
|
1
|
+
import { ElementType, useRef } from 'react';
|
2
|
+
import * as uuid from 'uuid';
|
3
|
+
import { CustomFieldInputProps } from './CustomFieldInputProps';
|
4
|
+
import { CustomFieldMetaProps } from './CustomFieldMetaProps';
|
5
|
+
import { RenderComponent, RenderComponentProps } from './Field';
|
6
|
+
import { InjectedFieldProps } from './InjectedFieldProps';
|
7
|
+
|
8
|
+
export type StandAloneInputProps<
|
9
|
+
TValue,
|
10
|
+
TRenderComponent extends ElementType
|
11
|
+
> = {
|
12
|
+
/** Component to render inside of the field. */
|
13
|
+
Component: RenderComponent<TValue, TRenderComponent>;
|
14
|
+
/** Value of the field. */
|
15
|
+
value: TValue;
|
16
|
+
/** Function to change the value of the field. */
|
17
|
+
onChange: (arg: TValue) => void;
|
18
|
+
} & Omit<
|
19
|
+
RenderComponentProps<TValue, TRenderComponent>,
|
20
|
+
| keyof InjectedFieldProps<TValue>
|
21
|
+
| 'value'
|
22
|
+
| 'onChange'
|
23
|
+
| 'normalize'
|
24
|
+
| 'validate'
|
25
|
+
>;
|
26
|
+
|
27
|
+
/** The stand alone version of `<Field/>`. Can use a type of input component outside of a `<Form/>`. */
|
28
|
+
export default function StandAloneInput<TValue, TComponent extends ElementType>(
|
29
|
+
props: StandAloneInputProps<TValue, TComponent>
|
30
|
+
) {
|
31
|
+
const { Component, value, onChange, disabled, ...rest } = props;
|
32
|
+
const handleChange = (arg: TValue) => {
|
33
|
+
if (!disabled) {
|
34
|
+
onChange(arg);
|
35
|
+
}
|
36
|
+
};
|
37
|
+
const Wrapped = Component as React.ComponentType<InjectedFieldProps<TValue>>;
|
38
|
+
const idRef = useRef(uuid.v4());
|
39
|
+
const input: CustomFieldInputProps<TValue> = {
|
40
|
+
id: idRef.current,
|
41
|
+
name: idRef.current,
|
42
|
+
value: value,
|
43
|
+
onChange: handleChange,
|
44
|
+
onBlur: () => {},
|
45
|
+
};
|
46
|
+
|
47
|
+
const meta: CustomFieldMetaProps<TValue> = {
|
48
|
+
warning: undefined,
|
49
|
+
value: value,
|
50
|
+
touched: false,
|
51
|
+
initialTouched: false,
|
52
|
+
};
|
53
|
+
|
54
|
+
return <Wrapped {...rest} disabled={disabled} input={input} meta={meta} />;
|
55
|
+
}
|
@@ -1,125 +1,125 @@
|
|
1
|
-
import { useContext, useEffect } from 'react';
|
2
|
-
import { useField, useFormikContext } from 'formik';
|
3
|
-
import { CustomFieldInputProps } from './CustomFieldInputProps';
|
4
|
-
import { CustomFieldMetaProps } from './CustomFieldMetaProps';
|
5
|
-
import { FieldNameContext } from './FieldNameContext';
|
6
|
-
import { ServerErrorContext } from '../Form/ServerErrorContext';
|
7
|
-
import { NormalizationFunction } from '../Normalization/NormalizationFunction';
|
8
|
-
import { ValidationFunction } from '../Validation/ValidationFunction';
|
9
|
-
|
10
|
-
export interface useStandardFieldProps<TValue> {
|
11
|
-
/** Id of the field. */
|
12
|
-
id?: string;
|
13
|
-
/** Name of the field. */
|
14
|
-
name: string;
|
15
|
-
/** Whether the field should be disabled. */
|
16
|
-
disabled?: boolean;
|
17
|
-
/** Function to validate the field. */
|
18
|
-
validate?: ValidationFunction<TValue> | ValidationFunction<TValue>[];
|
19
|
-
/** Function to modify the field value without making the form dirty. (e.g. phone number) */
|
20
|
-
normalize?: NormalizationFunction<TValue>;
|
21
|
-
}
|
22
|
-
|
23
|
-
/** Provides a consistent way to deal with all form fields (non array). */
|
24
|
-
export default function useStandardField<TValue>({
|
25
|
-
id: providedId,
|
26
|
-
name: providedName,
|
27
|
-
disabled,
|
28
|
-
validate,
|
29
|
-
normalize,
|
30
|
-
}: useStandardFieldProps<TValue>): [
|
31
|
-
CustomFieldInputProps<TValue>,
|
32
|
-
CustomFieldMetaProps<TValue>
|
33
|
-
] {
|
34
|
-
// because the formik errors are evaluated all at the same time we need to keep server errors separate
|
35
|
-
const { getError: getServerError, setError: setServerError } =
|
36
|
-
useContext(ServerErrorContext);
|
37
|
-
|
38
|
-
// ensure that form section values are obeyed, e.g. homeAddress.zipCode
|
39
|
-
const fieldNameContextValue = useContext(FieldNameContext);
|
40
|
-
const name = fieldNameContextValue
|
41
|
-
? `${fieldNameContextValue}.${providedName}`
|
42
|
-
: providedName;
|
43
|
-
|
44
|
-
// ensure that nested contexts don't have duplicate id issues when an id is specified
|
45
|
-
const id = providedId
|
46
|
-
? fieldNameContextValue
|
47
|
-
? `${fieldNameContextValue}.${providedId}`
|
48
|
-
: providedId
|
49
|
-
: name;
|
50
|
-
|
51
|
-
// ensure that our custom validation rules are handled
|
52
|
-
// e.g. we allow arrays of validators
|
53
|
-
const [formikInput, formikMeta] = useField<TValue>({
|
54
|
-
name,
|
55
|
-
id: id ? id : name,
|
56
|
-
disabled: disabled,
|
57
|
-
validate: callAllValidators,
|
58
|
-
});
|
59
|
-
const { setFieldTouched, setFieldValue, isSubmitting } = useFormikContext();
|
60
|
-
|
61
|
-
const touched =
|
62
|
-
formikMeta.touched !== false && formikMeta.touched !== undefined;
|
63
|
-
useEffect(() => {
|
64
|
-
if (!touched && isSubmitting) {
|
65
|
-
// because we do not always register all fields up front.
|
66
|
-
// e.g. formik expects even a 'create' form to have all fields given, at least, blank values
|
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);
|
72
|
-
}
|
73
|
-
}, [isSubmitting, name, setFieldTouched, touched]);
|
74
|
-
|
75
|
-
// these are the props we expect consumers of this hook to pass directly to the input (or other control)
|
76
|
-
const resultInput: CustomFieldInputProps<TValue> = {
|
77
|
-
name: formikInput.name,
|
78
|
-
// pass any direct from server props through normalize without making the form dirty (e.g. phone number)
|
79
|
-
value: normalize ? normalize(formikInput.value) : formikInput.value,
|
80
|
-
onChange: handleChange,
|
81
|
-
onBlur: handleBlur,
|
82
|
-
// extensions to formik
|
83
|
-
id: id,
|
84
|
-
};
|
85
|
-
|
86
|
-
const resultMeta: CustomFieldMetaProps<TValue> = {
|
87
|
-
...formikMeta,
|
88
|
-
error: getServerError(name) || (touched ? formikMeta.error : undefined),
|
89
|
-
// extensions to formik
|
90
|
-
warning: undefined, // TODO - did this never work?
|
91
|
-
touched: touched,
|
92
|
-
};
|
93
|
-
|
94
|
-
return [resultInput, resultMeta];
|
95
|
-
|
96
|
-
function handleBlur() {
|
97
|
-
formikInput.onBlur({ target: { name: name } });
|
98
|
-
}
|
99
|
-
|
100
|
-
function handleChange(value: TValue) {
|
101
|
-
if (disabled) {
|
102
|
-
return;
|
103
|
-
}
|
104
|
-
const normalized = normalize ? normalize(value) : value;
|
105
|
-
setFieldValue(name, normalized);
|
106
|
-
setServerError(name, undefined);
|
107
|
-
}
|
108
|
-
|
109
|
-
function callAllValidators(value: TValue) {
|
110
|
-
if (disabled || !validate) {
|
111
|
-
return;
|
112
|
-
}
|
113
|
-
|
114
|
-
if (!Array.isArray(validate)) {
|
115
|
-
return validate(value);
|
116
|
-
}
|
117
|
-
|
118
|
-
for (let i = 0; i < validate.length; i++) {
|
119
|
-
const result = validate[i](value);
|
120
|
-
if (result) {
|
121
|
-
return result;
|
122
|
-
}
|
123
|
-
}
|
124
|
-
}
|
125
|
-
}
|
1
|
+
import { useContext, useEffect } from 'react';
|
2
|
+
import { useField, useFormikContext } from 'formik';
|
3
|
+
import { CustomFieldInputProps } from './CustomFieldInputProps';
|
4
|
+
import { CustomFieldMetaProps } from './CustomFieldMetaProps';
|
5
|
+
import { FieldNameContext } from './FieldNameContext';
|
6
|
+
import { ServerErrorContext } from '../Form/ServerErrorContext';
|
7
|
+
import { NormalizationFunction } from '../Normalization/NormalizationFunction';
|
8
|
+
import { ValidationFunction } from '../Validation/ValidationFunction';
|
9
|
+
|
10
|
+
export interface useStandardFieldProps<TValue> {
|
11
|
+
/** Id of the field. */
|
12
|
+
id?: string;
|
13
|
+
/** Name of the field. */
|
14
|
+
name: string;
|
15
|
+
/** Whether the field should be disabled. */
|
16
|
+
disabled?: boolean;
|
17
|
+
/** Function to validate the field. */
|
18
|
+
validate?: ValidationFunction<TValue> | ValidationFunction<TValue>[];
|
19
|
+
/** Function to modify the field value without making the form dirty. (e.g. phone number) */
|
20
|
+
normalize?: NormalizationFunction<TValue>;
|
21
|
+
}
|
22
|
+
|
23
|
+
/** Provides a consistent way to deal with all form fields (non array). */
|
24
|
+
export default function useStandardField<TValue>({
|
25
|
+
id: providedId,
|
26
|
+
name: providedName,
|
27
|
+
disabled,
|
28
|
+
validate,
|
29
|
+
normalize,
|
30
|
+
}: useStandardFieldProps<TValue>): [
|
31
|
+
CustomFieldInputProps<TValue>,
|
32
|
+
CustomFieldMetaProps<TValue>
|
33
|
+
] {
|
34
|
+
// because the formik errors are evaluated all at the same time we need to keep server errors separate
|
35
|
+
const { getError: getServerError, setError: setServerError } =
|
36
|
+
useContext(ServerErrorContext);
|
37
|
+
|
38
|
+
// ensure that form section values are obeyed, e.g. homeAddress.zipCode
|
39
|
+
const fieldNameContextValue = useContext(FieldNameContext);
|
40
|
+
const name = fieldNameContextValue
|
41
|
+
? `${fieldNameContextValue}.${providedName}`
|
42
|
+
: providedName;
|
43
|
+
|
44
|
+
// ensure that nested contexts don't have duplicate id issues when an id is specified
|
45
|
+
const id = providedId
|
46
|
+
? fieldNameContextValue
|
47
|
+
? `${fieldNameContextValue}.${providedId}`
|
48
|
+
: providedId
|
49
|
+
: name;
|
50
|
+
|
51
|
+
// ensure that our custom validation rules are handled
|
52
|
+
// e.g. we allow arrays of validators
|
53
|
+
const [formikInput, formikMeta] = useField<TValue>({
|
54
|
+
name,
|
55
|
+
id: id ? id : name,
|
56
|
+
disabled: disabled,
|
57
|
+
validate: callAllValidators,
|
58
|
+
});
|
59
|
+
const { setFieldTouched, setFieldValue, isSubmitting } = useFormikContext();
|
60
|
+
|
61
|
+
const touched =
|
62
|
+
formikMeta.touched !== false && formikMeta.touched !== undefined;
|
63
|
+
useEffect(() => {
|
64
|
+
if (!touched && isSubmitting) {
|
65
|
+
// because we do not always register all fields up front.
|
66
|
+
// e.g. formik expects even a 'create' form to have all fields given, at least, blank values
|
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);
|
72
|
+
}
|
73
|
+
}, [isSubmitting, name, setFieldTouched, touched]);
|
74
|
+
|
75
|
+
// these are the props we expect consumers of this hook to pass directly to the input (or other control)
|
76
|
+
const resultInput: CustomFieldInputProps<TValue> = {
|
77
|
+
name: formikInput.name,
|
78
|
+
// pass any direct from server props through normalize without making the form dirty (e.g. phone number)
|
79
|
+
value: normalize ? normalize(formikInput.value) : formikInput.value,
|
80
|
+
onChange: handleChange,
|
81
|
+
onBlur: handleBlur,
|
82
|
+
// extensions to formik
|
83
|
+
id: id,
|
84
|
+
};
|
85
|
+
|
86
|
+
const resultMeta: CustomFieldMetaProps<TValue> = {
|
87
|
+
...formikMeta,
|
88
|
+
error: getServerError(name) || (touched ? formikMeta.error : undefined),
|
89
|
+
// extensions to formik
|
90
|
+
warning: undefined, // TODO - did this never work?
|
91
|
+
touched: touched,
|
92
|
+
};
|
93
|
+
|
94
|
+
return [resultInput, resultMeta];
|
95
|
+
|
96
|
+
function handleBlur() {
|
97
|
+
formikInput.onBlur({ target: { name: name } });
|
98
|
+
}
|
99
|
+
|
100
|
+
function handleChange(value: TValue) {
|
101
|
+
if (disabled) {
|
102
|
+
return;
|
103
|
+
}
|
104
|
+
const normalized = normalize ? normalize(value) : value;
|
105
|
+
setFieldValue(name, normalized);
|
106
|
+
setServerError(name, undefined);
|
107
|
+
}
|
108
|
+
|
109
|
+
function callAllValidators(value: TValue) {
|
110
|
+
if (disabled || !validate) {
|
111
|
+
return;
|
112
|
+
}
|
113
|
+
|
114
|
+
if (!Array.isArray(validate)) {
|
115
|
+
return validate(value);
|
116
|
+
}
|
117
|
+
|
118
|
+
for (let i = 0; i < validate.length; i++) {
|
119
|
+
const result = validate[i](value);
|
120
|
+
if (result) {
|
121
|
+
return result;
|
122
|
+
}
|
123
|
+
}
|
124
|
+
}
|
125
|
+
}
|
@@ -1,154 +1,154 @@
|
|
1
|
-
import { ElementType } from 'react';
|
2
|
-
import
|
3
|
-
import Field, { FieldProps } from '../Field/Field';
|
4
|
-
import { FieldNameContext } from '../Field/FieldNameContext';
|
5
|
-
import useStandardFormInput from '../Field/useStandardField';
|
6
|
-
import { FormDefaults } from '../FormDefaults';
|
7
|
-
import { ValidationFunction } from '../Validation/ValidationFunction';
|
8
|
-
|
9
|
-
export type FieldArrayProps<
|
10
|
-
TForm extends object,
|
11
|
-
TProp extends keyof TForm
|
12
|
-
> = TForm[TProp] extends Array<any> | undefined | null
|
13
|
-
? {
|
14
|
-
/** Name of the field, used on submission. If using codegen this must be the provided dto. */
|
15
|
-
name: TProp;
|
16
|
-
/** Label of the field. */
|
17
|
-
label?: string;
|
18
|
-
/** Whether the field should be disabled. */
|
19
|
-
disabled?: boolean;
|
20
|
-
/** Function to validate the value. */
|
21
|
-
validate?:
|
22
|
-
| ValidationFunction<TForm[TProp]>
|
23
|
-
| ValidationFunction<TForm[TProp]>[];
|
24
|
-
children: (
|
25
|
-
formBuilder: ArrayFormBuilderProp<TForm[TProp]>
|
26
|
-
) => JSX.Element;
|
27
|
-
}
|
28
|
-
: never;
|
29
|
-
|
30
|
-
export type ArrayFormBuilderProp<TValue extends Array<any> | undefined | null> =
|
31
|
-
TValue extends Array<infer TForm> | undefined | null
|
32
|
-
? TForm extends object
|
33
|
-
? {
|
34
|
-
Field: <
|
35
|
-
TProp extends keyof TForm,
|
36
|
-
TRenderComponent extends ElementType
|
37
|
-
>(
|
38
|
-
props: FieldProps<TForm, TProp, TRenderComponent>
|
39
|
-
) => JSX.Element; // assumes this is never null - thought he final component may not render
|
40
|
-
|
41
|
-
FieldArray: <TProp extends keyof TForm>(
|
42
|
-
props: FieldArrayProps<TForm, TProp>
|
43
|
-
) => JSX.Element;
|
44
|
-
}
|
45
|
-
: never
|
46
|
-
: never;
|
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
|
-
*/
|
53
|
-
export default function FieldArray<
|
54
|
-
TForm extends object,
|
55
|
-
TProp extends keyof TForm
|
56
|
-
>({
|
57
|
-
name,
|
58
|
-
label,
|
59
|
-
validate,
|
60
|
-
disabled,
|
61
|
-
children,
|
62
|
-
...rest
|
63
|
-
}: FieldArrayProps<TForm, TProp>) {
|
64
|
-
const [input] = useStandardFormInput<TForm[TProp]>({
|
65
|
-
name: String(name),
|
66
|
-
validate: validate,
|
67
|
-
disabled: disabled,
|
68
|
-
});
|
69
|
-
|
70
|
-
const values: any[] = !input.value
|
71
|
-
? []
|
72
|
-
: Array.isArray(input.value)
|
73
|
-
? input.value
|
74
|
-
: [];
|
75
|
-
|
76
|
-
return (
|
77
|
-
<div className={FormDefaults.cssClassPrefix + 'field-array'}>
|
78
|
-
<div className={FormDefaults.cssClassPrefix + 'field-array-header'}>
|
79
|
-
<div className={FormDefaults.cssClassPrefix + 'field-array-title'}>
|
80
|
-
{label}
|
81
|
-
</div>
|
82
|
-
<button
|
83
|
-
className={
|
84
|
-
FormDefaults.cssClassPrefix + 'add-array-item-button',
|
85
|
-
{ [FormDefaults.cssClassPrefix + 'disabled']: disabled }
|
86
|
-
)}
|
87
|
-
title="Add Item"
|
88
|
-
type="button"
|
89
|
-
onClick={addItem}>
|
90
|
-
+
|
91
|
-
</button>
|
92
|
-
</div>
|
93
|
-
<div className={FormDefaults.cssClassPrefix + 'field-array-body'}>
|
94
|
-
{values.map((value, index) => {
|
95
|
-
const itemName = `${input.name}[${index}]`;
|
96
|
-
return (
|
97
|
-
<div
|
98
|
-
key={
|
99
|
-
(value && value['form-input-array-key']) ||
|
100
|
-
(value && value['id']) ||
|
101
|
-
itemName
|
102
|
-
}
|
103
|
-
className={
|
104
|
-
FormDefaults.cssClassPrefix + 'field-array-item',
|
105
|
-
{
|
106
|
-
[FormDefaults.cssClassPrefix + 'removed']: value.isDeleted,
|
107
|
-
}
|
108
|
-
)}
|
109
|
-
role="listitem">
|
110
|
-
<FieldNameContext.Provider value={itemName}>
|
111
|
-
{children({
|
112
|
-
Field: Field,
|
113
|
-
FieldArray: FieldArray,
|
114
|
-
} as any)}
|
115
|
-
</FieldNameContext.Provider>
|
116
|
-
<button
|
117
|
-
className={
|
118
|
-
FormDefaults.cssClassPrefix + 'remove-array-item-button',
|
119
|
-
{ [FormDefaults.cssClassPrefix + 'disabled']: disabled }
|
120
|
-
)}
|
121
|
-
type="button"
|
122
|
-
title="Remove Item"
|
123
|
-
onClick={() => removeItem(value)}>
|
124
|
-
-
|
125
|
-
</button>
|
126
|
-
</div>
|
127
|
-
);
|
128
|
-
})}
|
129
|
-
</div>
|
130
|
-
</div>
|
131
|
-
);
|
132
|
-
|
133
|
-
function addItem() {
|
134
|
-
if (disabled) {
|
135
|
-
return;
|
136
|
-
}
|
137
|
-
input.onChange([...values, {}] as any);
|
138
|
-
}
|
139
|
-
|
140
|
-
function removeItem(item: any) {
|
141
|
-
if (disabled) {
|
142
|
-
return;
|
143
|
-
}
|
144
|
-
// assumes anything from the server has an 'id' value sent
|
145
|
-
if (!item.id) {
|
146
|
-
input.onChange(values.filter((x) => x !== item) as any);
|
147
|
-
return;
|
148
|
-
}
|
149
|
-
const mapped = values.map((x) =>
|
150
|
-
x !== item ? x : Object.assign({}, x, { isDeleted: true })
|
151
|
-
);
|
152
|
-
input.onChange(mapped as any);
|
153
|
-
}
|
154
|
-
}
|
1
|
+
import { ElementType } from 'react';
|
2
|
+
import { clsx } from 'clsx';
|
3
|
+
import Field, { FieldProps } from '../Field/Field';
|
4
|
+
import { FieldNameContext } from '../Field/FieldNameContext';
|
5
|
+
import useStandardFormInput from '../Field/useStandardField';
|
6
|
+
import { FormDefaults } from '../FormDefaults';
|
7
|
+
import { ValidationFunction } from '../Validation/ValidationFunction';
|
8
|
+
|
9
|
+
export type FieldArrayProps<
|
10
|
+
TForm extends object,
|
11
|
+
TProp extends keyof TForm
|
12
|
+
> = TForm[TProp] extends Array<any> | undefined | null
|
13
|
+
? {
|
14
|
+
/** Name of the field, used on submission. If using codegen this must be the provided dto. */
|
15
|
+
name: TProp;
|
16
|
+
/** Label of the field. */
|
17
|
+
label?: string;
|
18
|
+
/** Whether the field should be disabled. */
|
19
|
+
disabled?: boolean;
|
20
|
+
/** Function to validate the value. */
|
21
|
+
validate?:
|
22
|
+
| ValidationFunction<TForm[TProp]>
|
23
|
+
| ValidationFunction<TForm[TProp]>[];
|
24
|
+
children: (
|
25
|
+
formBuilder: ArrayFormBuilderProp<TForm[TProp]>
|
26
|
+
) => JSX.Element;
|
27
|
+
}
|
28
|
+
: never;
|
29
|
+
|
30
|
+
export type ArrayFormBuilderProp<TValue extends Array<any> | undefined | null> =
|
31
|
+
TValue extends Array<infer TForm> | undefined | null
|
32
|
+
? TForm extends object
|
33
|
+
? {
|
34
|
+
Field: <
|
35
|
+
TProp extends keyof TForm,
|
36
|
+
TRenderComponent extends ElementType
|
37
|
+
>(
|
38
|
+
props: FieldProps<TForm, TProp, TRenderComponent>
|
39
|
+
) => JSX.Element; // assumes this is never null - thought he final component may not render
|
40
|
+
|
41
|
+
FieldArray: <TProp extends keyof TForm>(
|
42
|
+
props: FieldArrayProps<TForm, TProp>
|
43
|
+
) => JSX.Element;
|
44
|
+
}
|
45
|
+
: never
|
46
|
+
: never;
|
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
|
+
*/
|
53
|
+
export default function FieldArray<
|
54
|
+
TForm extends object,
|
55
|
+
TProp extends keyof TForm
|
56
|
+
>({
|
57
|
+
name,
|
58
|
+
label,
|
59
|
+
validate,
|
60
|
+
disabled,
|
61
|
+
children,
|
62
|
+
...rest
|
63
|
+
}: FieldArrayProps<TForm, TProp>) {
|
64
|
+
const [input] = useStandardFormInput<TForm[TProp]>({
|
65
|
+
name: String(name),
|
66
|
+
validate: validate,
|
67
|
+
disabled: disabled,
|
68
|
+
});
|
69
|
+
|
70
|
+
const values: any[] = !input.value
|
71
|
+
? []
|
72
|
+
: Array.isArray(input.value)
|
73
|
+
? input.value
|
74
|
+
: [];
|
75
|
+
|
76
|
+
return (
|
77
|
+
<div className={FormDefaults.cssClassPrefix + 'field-array'}>
|
78
|
+
<div className={FormDefaults.cssClassPrefix + 'field-array-header'}>
|
79
|
+
<div className={FormDefaults.cssClassPrefix + 'field-array-title'}>
|
80
|
+
{label}
|
81
|
+
</div>
|
82
|
+
<button
|
83
|
+
className={clsx(
|
84
|
+
FormDefaults.cssClassPrefix + 'add-array-item-button',
|
85
|
+
{ [FormDefaults.cssClassPrefix + 'disabled']: disabled }
|
86
|
+
)}
|
87
|
+
title="Add Item"
|
88
|
+
type="button"
|
89
|
+
onClick={addItem}>
|
90
|
+
+
|
91
|
+
</button>
|
92
|
+
</div>
|
93
|
+
<div className={FormDefaults.cssClassPrefix + 'field-array-body'}>
|
94
|
+
{values.map((value, index) => {
|
95
|
+
const itemName = `${input.name}[${index}]`;
|
96
|
+
return (
|
97
|
+
<div
|
98
|
+
key={
|
99
|
+
(value && value['form-input-array-key']) ||
|
100
|
+
(value && value['id']) ||
|
101
|
+
itemName
|
102
|
+
}
|
103
|
+
className={clsx(
|
104
|
+
FormDefaults.cssClassPrefix + 'field-array-item',
|
105
|
+
{
|
106
|
+
[FormDefaults.cssClassPrefix + 'removed']: value.isDeleted,
|
107
|
+
}
|
108
|
+
)}
|
109
|
+
role="listitem">
|
110
|
+
<FieldNameContext.Provider value={itemName}>
|
111
|
+
{children({
|
112
|
+
Field: Field,
|
113
|
+
FieldArray: FieldArray,
|
114
|
+
} as any)}
|
115
|
+
</FieldNameContext.Provider>
|
116
|
+
<button
|
117
|
+
className={clsx(
|
118
|
+
FormDefaults.cssClassPrefix + 'remove-array-item-button',
|
119
|
+
{ [FormDefaults.cssClassPrefix + 'disabled']: disabled }
|
120
|
+
)}
|
121
|
+
type="button"
|
122
|
+
title="Remove Item"
|
123
|
+
onClick={() => removeItem(value)}>
|
124
|
+
-
|
125
|
+
</button>
|
126
|
+
</div>
|
127
|
+
);
|
128
|
+
})}
|
129
|
+
</div>
|
130
|
+
</div>
|
131
|
+
);
|
132
|
+
|
133
|
+
function addItem() {
|
134
|
+
if (disabled) {
|
135
|
+
return;
|
136
|
+
}
|
137
|
+
input.onChange([...values, {}] as any);
|
138
|
+
}
|
139
|
+
|
140
|
+
function removeItem(item: any) {
|
141
|
+
if (disabled) {
|
142
|
+
return;
|
143
|
+
}
|
144
|
+
// assumes anything from the server has an 'id' value sent
|
145
|
+
if (!item.id) {
|
146
|
+
input.onChange(values.filter((x) => x !== item) as any);
|
147
|
+
return;
|
148
|
+
}
|
149
|
+
const mapped = values.map((x) =>
|
150
|
+
x !== item ? x : Object.assign({}, x, { isDeleted: true })
|
151
|
+
);
|
152
|
+
input.onChange(mapped as any);
|
153
|
+
}
|
154
|
+
}
|