envoc-form 5.0.3 → 5.0.5
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 +158 -15
- 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/index.d.ts +2 -0
- package/es/index.js +1 -0
- 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/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 +115 -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 +85 -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 +237 -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 +72 -72
- package/src/Input/MoneyInputGroup.tsx +50 -50
- package/src/Input/NumberInputGroup.tsx +48 -48
- package/src/Input/PhoneNumberInputGroup.tsx +45 -45
- package/src/Input/StringInputGroup.tsx +53 -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,64 +1,64 @@
|
|
1
|
-
import React, { LegacyRef } from 'react';
|
2
|
-
import classNames from 'classnames';
|
3
|
-
import { InjectedFieldProps } from '../Field/InjectedFieldProps';
|
4
|
-
import { FormDefaults } from '../FormDefaults';
|
5
|
-
import Group, { GroupProps } from '../Group';
|
6
|
-
|
7
|
-
export interface TextAreaGroupProps
|
8
|
-
extends InjectedFieldProps<string | undefined | null>,
|
9
|
-
Omit<
|
10
|
-
React.HTMLProps<HTMLTextAreaElement>,
|
11
|
-
keyof InjectedFieldProps<any> | 'children' | 'className' | 'label'
|
12
|
-
>,
|
13
|
-
Omit<GroupProps, keyof InjectedFieldProps<any> | 'children'> {
|
14
|
-
icon?: React.ReactNode;
|
15
|
-
}
|
16
|
-
|
17
|
-
function TextAreaGroup(
|
18
|
-
{
|
19
|
-
input,
|
20
|
-
meta,
|
21
|
-
label,
|
22
|
-
helpText,
|
23
|
-
className,
|
24
|
-
required,
|
25
|
-
disabled,
|
26
|
-
icon,
|
27
|
-
...rest
|
28
|
-
}: TextAreaGroupProps,
|
29
|
-
ref: LegacyRef<HTMLTextAreaElement>
|
30
|
-
) {
|
31
|
-
return (
|
32
|
-
<Group
|
33
|
-
input={input}
|
34
|
-
meta={meta}
|
35
|
-
required={required}
|
36
|
-
disabled={disabled}
|
37
|
-
label={label}
|
38
|
-
helpText={helpText}
|
39
|
-
className={classNames(
|
40
|
-
className,
|
41
|
-
FormDefaults.cssClassPrefix + 'text-area-group'
|
42
|
-
)}>
|
43
|
-
{icon}
|
44
|
-
<textarea
|
45
|
-
{...input}
|
46
|
-
{...rest}
|
47
|
-
value={input.value ?? ''}
|
48
|
-
onChange={(val) => input.onChange(val.target.value)}
|
49
|
-
ref={ref}
|
50
|
-
className={classNames(
|
51
|
-
className,
|
52
|
-
FormDefaults.cssClassPrefix + 'text-area-group'
|
53
|
-
)}
|
54
|
-
/>
|
55
|
-
</Group>
|
56
|
-
);
|
57
|
-
}
|
58
|
-
|
59
|
-
/** Generic controlled `<textarea/>` wrapped by a `<Group/>` */
|
60
|
-
const TextAreaGroupWithRef = React.forwardRef(
|
61
|
-
TextAreaGroup
|
62
|
-
) as React.ComponentType<TextAreaGroupProps>;
|
63
|
-
|
64
|
-
export default TextAreaGroupWithRef;
|
1
|
+
import React, { LegacyRef } from 'react';
|
2
|
+
import classNames from 'classnames';
|
3
|
+
import { InjectedFieldProps } from '../Field/InjectedFieldProps';
|
4
|
+
import { FormDefaults } from '../FormDefaults';
|
5
|
+
import Group, { GroupProps } from '../Group';
|
6
|
+
|
7
|
+
export interface TextAreaGroupProps
|
8
|
+
extends InjectedFieldProps<string | undefined | null>,
|
9
|
+
Omit<
|
10
|
+
React.HTMLProps<HTMLTextAreaElement>,
|
11
|
+
keyof InjectedFieldProps<any> | 'children' | 'className' | 'label'
|
12
|
+
>,
|
13
|
+
Omit<GroupProps, keyof InjectedFieldProps<any> | 'children'> {
|
14
|
+
icon?: React.ReactNode;
|
15
|
+
}
|
16
|
+
|
17
|
+
function TextAreaGroup(
|
18
|
+
{
|
19
|
+
input,
|
20
|
+
meta,
|
21
|
+
label,
|
22
|
+
helpText,
|
23
|
+
className,
|
24
|
+
required,
|
25
|
+
disabled,
|
26
|
+
icon,
|
27
|
+
...rest
|
28
|
+
}: TextAreaGroupProps,
|
29
|
+
ref: LegacyRef<HTMLTextAreaElement>
|
30
|
+
) {
|
31
|
+
return (
|
32
|
+
<Group
|
33
|
+
input={input}
|
34
|
+
meta={meta}
|
35
|
+
required={required}
|
36
|
+
disabled={disabled}
|
37
|
+
label={label}
|
38
|
+
helpText={helpText}
|
39
|
+
className={classNames(
|
40
|
+
className,
|
41
|
+
FormDefaults.cssClassPrefix + 'text-area-group'
|
42
|
+
)}>
|
43
|
+
{icon}
|
44
|
+
<textarea
|
45
|
+
{...input}
|
46
|
+
{...rest}
|
47
|
+
value={input.value ?? ''}
|
48
|
+
onChange={(val) => input.onChange(val.target.value)}
|
49
|
+
ref={ref}
|
50
|
+
className={classNames(
|
51
|
+
className,
|
52
|
+
FormDefaults.cssClassPrefix + 'text-area-group'
|
53
|
+
)}
|
54
|
+
/>
|
55
|
+
</Group>
|
56
|
+
);
|
57
|
+
}
|
58
|
+
|
59
|
+
/** Generic controlled `<textarea/>` wrapped by a `<Group/>` */
|
60
|
+
const TextAreaGroupWithRef = React.forwardRef(
|
61
|
+
TextAreaGroup
|
62
|
+
) as React.ComponentType<TextAreaGroupProps>;
|
63
|
+
|
64
|
+
export default TextAreaGroupWithRef;
|
@@ -1,8 +1,8 @@
|
|
1
|
-
import { ValidationError } from './ValidationError';
|
2
|
-
|
3
|
-
// TODO: this means that all template projects MUST have this API shape - thoughts?
|
4
|
-
/** The root result from a typical envoc API call when forms are returned */
|
5
|
-
export interface ValidatedApiResult {
|
6
|
-
validationFailures?: ValidationError[];
|
7
|
-
hasErrors?: boolean;
|
8
|
-
}
|
1
|
+
import { ValidationError } from './ValidationError';
|
2
|
+
|
3
|
+
// TODO: this means that all template projects MUST have this API shape - thoughts?
|
4
|
+
/** The root result from a typical envoc API call when forms are returned */
|
5
|
+
export interface ValidatedApiResult {
|
6
|
+
validationFailures?: ValidationError[];
|
7
|
+
hasErrors?: boolean;
|
8
|
+
}
|
@@ -1,6 +1,6 @@
|
|
1
|
-
// TODO: this means that all template projects MUST have this API shape - thoughts?
|
2
|
-
/** Represents a server side validation failure - only 'standard' for envoc */
|
3
|
-
export interface ValidationError {
|
4
|
-
propertyName?: string;
|
5
|
-
errorMessage?: string;
|
6
|
-
}
|
1
|
+
// TODO: this means that all template projects MUST have this API shape - thoughts?
|
2
|
+
/** Represents a server side validation failure - only 'standard' for envoc */
|
3
|
+
export interface ValidationError {
|
4
|
+
propertyName?: string;
|
5
|
+
errorMessage?: string;
|
6
|
+
}
|
@@ -1,4 +1,4 @@
|
|
1
|
-
/** Client side validation function */
|
2
|
-
export interface ValidationFunction<TValue> {
|
3
|
-
(value: TValue): string | undefined;
|
4
|
-
}
|
1
|
+
/** Client side validation function */
|
2
|
+
export interface ValidationFunction<TValue> {
|
3
|
+
(value: TValue): string | undefined;
|
4
|
+
}
|
@@ -1,81 +1,81 @@
|
|
1
|
-
import { integer, length, maxLength, maxValue, minValue } from './validators';
|
2
|
-
|
3
|
-
describe('integer validator', () => {
|
4
|
-
it('accepts an integer', () => {
|
5
|
-
const returned = integer('123');
|
6
|
-
expect(returned).toBe(undefined);
|
7
|
-
});
|
8
|
-
|
9
|
-
it("doesn't accept a float", () => {
|
10
|
-
const returned = integer('123.5569');
|
11
|
-
expect(returned).toBe('Must be a whole number');
|
12
|
-
});
|
13
|
-
});
|
14
|
-
|
15
|
-
describe('length validator', () => {
|
16
|
-
it('fails for invalid Length', () => {
|
17
|
-
const length10 = length(10);
|
18
|
-
const tooLong = length10('01234567890');
|
19
|
-
const tooShort = length10('aaa');
|
20
|
-
expect(tooLong).toBe('Length must be 10');
|
21
|
-
expect(tooShort).toBe('Length must be 10');
|
22
|
-
});
|
23
|
-
|
24
|
-
it('succeeds for correct length', () => {
|
25
|
-
const length10 = length(10);
|
26
|
-
const validString = length10('0123456789');
|
27
|
-
const validNumber = length10(1234567890);
|
28
|
-
expect(validString).toBe(undefined);
|
29
|
-
expect(validNumber).toBe(undefined);
|
30
|
-
});
|
31
|
-
});
|
32
|
-
|
33
|
-
describe('maxValue validator', () => {
|
34
|
-
it('fails for invalid value', () => {
|
35
|
-
const maxValue10 = maxValue(10);
|
36
|
-
const tooBig = maxValue10(11);
|
37
|
-
expect(tooBig).toBe('Maximum value 10 exceeded');
|
38
|
-
});
|
39
|
-
|
40
|
-
it('succeeds for valid value', () => {
|
41
|
-
const maxValue10 = maxValue(10);
|
42
|
-
const maxAllowable = maxValue10(10);
|
43
|
-
const lowAllowable = maxValue10(1);
|
44
|
-
expect(maxAllowable).toBe(undefined);
|
45
|
-
expect(lowAllowable).toBe(undefined);
|
46
|
-
});
|
47
|
-
});
|
48
|
-
|
49
|
-
describe('minValue validator', () => {
|
50
|
-
it('fails for invalid value', () => {
|
51
|
-
const minValue10 = minValue(10);
|
52
|
-
const tooSmall = minValue10(9);
|
53
|
-
expect(tooSmall).toBe('Minimum value 10 not met');
|
54
|
-
});
|
55
|
-
|
56
|
-
it('succeeds for valid value', () => {
|
57
|
-
const minValue10 = minValue(10);
|
58
|
-
const minAllowable = minValue10(10);
|
59
|
-
const highAllowable = minValue10(15);
|
60
|
-
expect(minAllowable).toBe(undefined);
|
61
|
-
expect(highAllowable).toBe(undefined);
|
62
|
-
});
|
63
|
-
});
|
64
|
-
|
65
|
-
describe('maxLength validator', () => {
|
66
|
-
it('fails for invalid value', () => {
|
67
|
-
const maxLength10 = maxLength(10);
|
68
|
-
const stringTooLong = maxLength10('012345678910');
|
69
|
-
const numberTooLong = maxLength10(1234567890123);
|
70
|
-
expect(stringTooLong).toBe('Maximum length 10 exceeded');
|
71
|
-
expect(numberTooLong).toBe('Maximum length 10 exceeded');
|
72
|
-
});
|
73
|
-
|
74
|
-
it('succeeds for valid value', () => {
|
75
|
-
const maxLength10 = maxLength(10);
|
76
|
-
const exactMatch = maxLength10('0123456789');
|
77
|
-
const shortValid = maxLength10('0123');
|
78
|
-
expect(exactMatch).toBe(undefined);
|
79
|
-
expect(shortValid).toBe(undefined);
|
80
|
-
});
|
81
|
-
});
|
1
|
+
import { integer, length, maxLength, maxValue, minValue } from './validators';
|
2
|
+
|
3
|
+
describe('integer validator', () => {
|
4
|
+
it('accepts an integer', () => {
|
5
|
+
const returned = integer('123');
|
6
|
+
expect(returned).toBe(undefined);
|
7
|
+
});
|
8
|
+
|
9
|
+
it("doesn't accept a float", () => {
|
10
|
+
const returned = integer('123.5569');
|
11
|
+
expect(returned).toBe('Must be a whole number');
|
12
|
+
});
|
13
|
+
});
|
14
|
+
|
15
|
+
describe('length validator', () => {
|
16
|
+
it('fails for invalid Length', () => {
|
17
|
+
const length10 = length(10);
|
18
|
+
const tooLong = length10('01234567890');
|
19
|
+
const tooShort = length10('aaa');
|
20
|
+
expect(tooLong).toBe('Length must be 10');
|
21
|
+
expect(tooShort).toBe('Length must be 10');
|
22
|
+
});
|
23
|
+
|
24
|
+
it('succeeds for correct length', () => {
|
25
|
+
const length10 = length(10);
|
26
|
+
const validString = length10('0123456789');
|
27
|
+
const validNumber = length10(1234567890);
|
28
|
+
expect(validString).toBe(undefined);
|
29
|
+
expect(validNumber).toBe(undefined);
|
30
|
+
});
|
31
|
+
});
|
32
|
+
|
33
|
+
describe('maxValue validator', () => {
|
34
|
+
it('fails for invalid value', () => {
|
35
|
+
const maxValue10 = maxValue(10);
|
36
|
+
const tooBig = maxValue10(11);
|
37
|
+
expect(tooBig).toBe('Maximum value 10 exceeded');
|
38
|
+
});
|
39
|
+
|
40
|
+
it('succeeds for valid value', () => {
|
41
|
+
const maxValue10 = maxValue(10);
|
42
|
+
const maxAllowable = maxValue10(10);
|
43
|
+
const lowAllowable = maxValue10(1);
|
44
|
+
expect(maxAllowable).toBe(undefined);
|
45
|
+
expect(lowAllowable).toBe(undefined);
|
46
|
+
});
|
47
|
+
});
|
48
|
+
|
49
|
+
describe('minValue validator', () => {
|
50
|
+
it('fails for invalid value', () => {
|
51
|
+
const minValue10 = minValue(10);
|
52
|
+
const tooSmall = minValue10(9);
|
53
|
+
expect(tooSmall).toBe('Minimum value 10 not met');
|
54
|
+
});
|
55
|
+
|
56
|
+
it('succeeds for valid value', () => {
|
57
|
+
const minValue10 = minValue(10);
|
58
|
+
const minAllowable = minValue10(10);
|
59
|
+
const highAllowable = minValue10(15);
|
60
|
+
expect(minAllowable).toBe(undefined);
|
61
|
+
expect(highAllowable).toBe(undefined);
|
62
|
+
});
|
63
|
+
});
|
64
|
+
|
65
|
+
describe('maxLength validator', () => {
|
66
|
+
it('fails for invalid value', () => {
|
67
|
+
const maxLength10 = maxLength(10);
|
68
|
+
const stringTooLong = maxLength10('012345678910');
|
69
|
+
const numberTooLong = maxLength10(1234567890123);
|
70
|
+
expect(stringTooLong).toBe('Maximum length 10 exceeded');
|
71
|
+
expect(numberTooLong).toBe('Maximum length 10 exceeded');
|
72
|
+
});
|
73
|
+
|
74
|
+
it('succeeds for valid value', () => {
|
75
|
+
const maxLength10 = maxLength(10);
|
76
|
+
const exactMatch = maxLength10('0123456789');
|
77
|
+
const shortValid = maxLength10('0123');
|
78
|
+
expect(exactMatch).toBe(undefined);
|
79
|
+
expect(shortValid).toBe(undefined);
|
80
|
+
});
|
81
|
+
});
|
@@ -1,97 +1,97 @@
|
|
1
|
-
import { ValidationFunction } from './ValidationFunction';
|
2
|
-
|
3
|
-
export const required = (value: any) =>
|
4
|
-
Array.isArray(value)
|
5
|
-
? value.length > 0
|
6
|
-
? undefined
|
7
|
-
: 'Required'
|
8
|
-
: value || value === false || value === 0
|
9
|
-
? undefined
|
10
|
-
: 'Required';
|
11
|
-
|
12
|
-
/** Asserts that the value is a certain number of characters. numbers are coerced to a string */
|
13
|
-
export const length =
|
14
|
-
(len: number) => (value: string | number | undefined | null) => {
|
15
|
-
const hasError = getLengthOfValue(value) !== len;
|
16
|
-
return !hasError ? undefined : `Length must be ${len}`;
|
17
|
-
};
|
18
|
-
|
19
|
-
export const integer = (val: string | number | undefined | null) => {
|
20
|
-
if (!val) {
|
21
|
-
return undefined;
|
22
|
-
}
|
23
|
-
if (!Number.isInteger(typeof val === 'number' ? val : parseFloat(val))) {
|
24
|
-
return 'Must be a whole number';
|
25
|
-
}
|
26
|
-
};
|
27
|
-
|
28
|
-
export const maxLength =
|
29
|
-
(len: number) => (value: string | number | undefined | null) => {
|
30
|
-
const hasError = getLengthOfValue(value) > len;
|
31
|
-
return !hasError ? undefined : `Maximum length ${len} exceeded`;
|
32
|
-
};
|
33
|
-
|
34
|
-
export const maxCount =
|
35
|
-
(count: number) => (value: { isDeleted?: boolean }[] | undefined | null) => {
|
36
|
-
const hasError =
|
37
|
-
!!value && value.filter((x) => !x.isDeleted).length > count;
|
38
|
-
return !hasError ? undefined : `Should not have more than ${count}`;
|
39
|
-
};
|
40
|
-
|
41
|
-
export const minCount =
|
42
|
-
(count: number) => (value: { isDeleted?: boolean }[] | undefined | null) => {
|
43
|
-
const hasError = !value || value.filter((x) => !x.isDeleted).length < count;
|
44
|
-
return !hasError ? undefined : `Should have at least ${count}`;
|
45
|
-
};
|
46
|
-
|
47
|
-
export const maxValue = (max: number) => (value: number | undefined | null) => {
|
48
|
-
const hasError = !!value && value > max;
|
49
|
-
return !hasError ? undefined : `Maximum value ${max} exceeded`;
|
50
|
-
};
|
51
|
-
|
52
|
-
export const minValue = (min: number) => (value: number | undefined | null) => {
|
53
|
-
const hasError = !value || value < min;
|
54
|
-
return !hasError ? undefined : `Minimum value ${min} not met`;
|
55
|
-
};
|
56
|
-
|
57
|
-
/** Validate for a ZIP Code. Accepts formats: ##### and #####-#### */
|
58
|
-
export const zipCode = (value: string | undefined | null) => {
|
59
|
-
return value && !/^[0-9]{5}(?:-[0-9]{4})?$/.test(value)
|
60
|
-
? 'Invalid ZIP Code'
|
61
|
-
: undefined;
|
62
|
-
};
|
63
|
-
|
64
|
-
/** One of the validators provided must be true. */
|
65
|
-
export const any =
|
66
|
-
<TValue>(validatorList: ValidationFunction<TValue>[]) =>
|
67
|
-
(value: TValue) => {
|
68
|
-
if (validatorList.length === 0 || !value) {
|
69
|
-
return;
|
70
|
-
}
|
71
|
-
|
72
|
-
return validatorList.reduce(
|
73
|
-
(isAnyTrue, x) => (isAnyTrue ? isAnyTrue : x(value)),
|
74
|
-
undefined as string | undefined
|
75
|
-
);
|
76
|
-
};
|
77
|
-
|
78
|
-
function getLengthOfValue(value: string | number | undefined | null) {
|
79
|
-
if (value === null) {
|
80
|
-
return 0;
|
81
|
-
}
|
82
|
-
switch (typeof value) {
|
83
|
-
case 'undefined':
|
84
|
-
return 0;
|
85
|
-
case 'string':
|
86
|
-
return value.length;
|
87
|
-
case 'number':
|
88
|
-
// TODO: this seems iffy to me - what if the actual visible value is a fixed length or some special format (e.g. with commas?)
|
89
|
-
return value.toString().length;
|
90
|
-
default:
|
91
|
-
assertUnreachable(value);
|
92
|
-
return 0;
|
93
|
-
}
|
94
|
-
}
|
95
|
-
function assertUnreachable(x: never) {
|
96
|
-
return null;
|
97
|
-
}
|
1
|
+
import { ValidationFunction } from './ValidationFunction';
|
2
|
+
|
3
|
+
export const required = (value: any) =>
|
4
|
+
Array.isArray(value)
|
5
|
+
? value.length > 0
|
6
|
+
? undefined
|
7
|
+
: 'Required'
|
8
|
+
: value || value === false || value === 0
|
9
|
+
? undefined
|
10
|
+
: 'Required';
|
11
|
+
|
12
|
+
/** Asserts that the value is a certain number of characters. numbers are coerced to a string */
|
13
|
+
export const length =
|
14
|
+
(len: number) => (value: string | number | undefined | null) => {
|
15
|
+
const hasError = getLengthOfValue(value) !== len;
|
16
|
+
return !hasError ? undefined : `Length must be ${len}`;
|
17
|
+
};
|
18
|
+
|
19
|
+
export const integer = (val: string | number | undefined | null) => {
|
20
|
+
if (!val) {
|
21
|
+
return undefined;
|
22
|
+
}
|
23
|
+
if (!Number.isInteger(typeof val === 'number' ? val : parseFloat(val))) {
|
24
|
+
return 'Must be a whole number';
|
25
|
+
}
|
26
|
+
};
|
27
|
+
|
28
|
+
export const maxLength =
|
29
|
+
(len: number) => (value: string | number | undefined | null) => {
|
30
|
+
const hasError = getLengthOfValue(value) > len;
|
31
|
+
return !hasError ? undefined : `Maximum length ${len} exceeded`;
|
32
|
+
};
|
33
|
+
|
34
|
+
export const maxCount =
|
35
|
+
(count: number) => (value: { isDeleted?: boolean }[] | undefined | null) => {
|
36
|
+
const hasError =
|
37
|
+
!!value && value.filter((x) => !x.isDeleted).length > count;
|
38
|
+
return !hasError ? undefined : `Should not have more than ${count}`;
|
39
|
+
};
|
40
|
+
|
41
|
+
export const minCount =
|
42
|
+
(count: number) => (value: { isDeleted?: boolean }[] | undefined | null) => {
|
43
|
+
const hasError = !value || value.filter((x) => !x.isDeleted).length < count;
|
44
|
+
return !hasError ? undefined : `Should have at least ${count}`;
|
45
|
+
};
|
46
|
+
|
47
|
+
export const maxValue = (max: number) => (value: number | undefined | null) => {
|
48
|
+
const hasError = !!value && value > max;
|
49
|
+
return !hasError ? undefined : `Maximum value ${max} exceeded`;
|
50
|
+
};
|
51
|
+
|
52
|
+
export const minValue = (min: number) => (value: number | undefined | null) => {
|
53
|
+
const hasError = !value || value < min;
|
54
|
+
return !hasError ? undefined : `Minimum value ${min} not met`;
|
55
|
+
};
|
56
|
+
|
57
|
+
/** Validate for a ZIP Code. Accepts formats: ##### and #####-#### */
|
58
|
+
export const zipCode = (value: string | undefined | null) => {
|
59
|
+
return value && !/^[0-9]{5}(?:-[0-9]{4})?$/.test(value)
|
60
|
+
? 'Invalid ZIP Code'
|
61
|
+
: undefined;
|
62
|
+
};
|
63
|
+
|
64
|
+
/** One of the validators provided must be true. */
|
65
|
+
export const any =
|
66
|
+
<TValue>(validatorList: ValidationFunction<TValue>[]) =>
|
67
|
+
(value: TValue) => {
|
68
|
+
if (validatorList.length === 0 || !value) {
|
69
|
+
return;
|
70
|
+
}
|
71
|
+
|
72
|
+
return validatorList.reduce(
|
73
|
+
(isAnyTrue, x) => (isAnyTrue ? isAnyTrue : x(value)),
|
74
|
+
undefined as string | undefined
|
75
|
+
);
|
76
|
+
};
|
77
|
+
|
78
|
+
function getLengthOfValue(value: string | number | undefined | null) {
|
79
|
+
if (value === null) {
|
80
|
+
return 0;
|
81
|
+
}
|
82
|
+
switch (typeof value) {
|
83
|
+
case 'undefined':
|
84
|
+
return 0;
|
85
|
+
case 'string':
|
86
|
+
return value.length;
|
87
|
+
case 'number':
|
88
|
+
// TODO: this seems iffy to me - what if the actual visible value is a fixed length or some special format (e.g. with commas?)
|
89
|
+
return value.toString().length;
|
90
|
+
default:
|
91
|
+
assertUnreachable(value);
|
92
|
+
return 0;
|
93
|
+
}
|
94
|
+
}
|
95
|
+
function assertUnreachable(x: never) {
|
96
|
+
return null;
|
97
|
+
}
|
@@ -1,64 +1,65 @@
|
|
1
|
-
import { HashRouter } from 'react-router-dom';
|
2
|
-
import { Form, FormBuilderProp } from '../';
|
3
|
-
import { ValidatedApiResult } from '../Validation/ValidatedApiResult';
|
4
|
-
|
5
|
-
interface FormTestBaseProps<TForm extends object> {
|
6
|
-
children: (formBuilder: FormBuilderProp<TForm>) => JSX.Element;
|
7
|
-
initialValues?: TForm;
|
8
|
-
handleSubmit?: (data: TForm | FormData) => Promise<ValidatedApiResult>;
|
9
|
-
}
|
10
|
-
|
11
|
-
export default function FormTestBase<TForm extends PersonDto>({
|
12
|
-
children,
|
13
|
-
initialValues,
|
14
|
-
handleSubmit,
|
15
|
-
}: FormTestBaseProps<TForm>) {
|
16
|
-
return (
|
17
|
-
<HashRouter>
|
18
|
-
<Form
|
19
|
-
onSubmit={handleSubmit ? handleSubmit : onSubmit}
|
20
|
-
initialValues={initialValues}>
|
21
|
-
{children}
|
22
|
-
</Form>
|
23
|
-
</HashRouter>
|
24
|
-
);
|
25
|
-
|
26
|
-
async function onSubmit(data: TForm | FormData): Promise<ValidatedApiResult> {
|
27
|
-
const isFormData = data instanceof FormData;
|
28
|
-
const result = await fetch('/api/submit', {
|
29
|
-
body: isFormData ? data : JSON.stringify(data),
|
30
|
-
method: 'POST',
|
31
|
-
headers: {
|
32
|
-
'Content-Type': isFormData ? 'multipart/form-data' : 'application/json',
|
33
|
-
'X-Show-Errors': `Errors go here`,
|
34
|
-
},
|
35
|
-
});
|
36
|
-
|
37
|
-
const apiResult = await (result.json() as Promise<ValidatedApiResult>);
|
38
|
-
|
39
|
-
if (!apiResult.hasErrors) {
|
40
|
-
alert('submitted');
|
41
|
-
}
|
42
|
-
|
43
|
-
return apiResult;
|
44
|
-
}
|
45
|
-
}
|
46
|
-
|
47
|
-
export interface PersonDto {
|
48
|
-
name?: string | undefined;
|
49
|
-
nameWithIcon?: string | undefined;
|
50
|
-
numberOfArms?: number;
|
51
|
-
phoneNumber?: string;
|
52
|
-
yearlySalaryUSD?: number;
|
53
|
-
isCool?: boolean;
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
1
|
+
import { HashRouter } from 'react-router-dom';
|
2
|
+
import { Form, FormBuilderProp } from '../';
|
3
|
+
import { ValidatedApiResult } from '../Validation/ValidatedApiResult';
|
4
|
+
|
5
|
+
interface FormTestBaseProps<TForm extends object> {
|
6
|
+
children: (formBuilder: FormBuilderProp<TForm>) => JSX.Element;
|
7
|
+
initialValues?: TForm;
|
8
|
+
handleSubmit?: (data: TForm | FormData) => Promise<ValidatedApiResult>;
|
9
|
+
}
|
10
|
+
|
11
|
+
export default function FormTestBase<TForm extends PersonDto>({
|
12
|
+
children,
|
13
|
+
initialValues,
|
14
|
+
handleSubmit,
|
15
|
+
}: FormTestBaseProps<TForm>) {
|
16
|
+
return (
|
17
|
+
<HashRouter>
|
18
|
+
<Form
|
19
|
+
onSubmit={handleSubmit ? handleSubmit : onSubmit}
|
20
|
+
initialValues={initialValues}>
|
21
|
+
{children}
|
22
|
+
</Form>
|
23
|
+
</HashRouter>
|
24
|
+
);
|
25
|
+
|
26
|
+
async function onSubmit(data: TForm | FormData): Promise<ValidatedApiResult> {
|
27
|
+
const isFormData = data instanceof FormData;
|
28
|
+
const result = await fetch('/api/submit', {
|
29
|
+
body: isFormData ? data : JSON.stringify(data),
|
30
|
+
method: 'POST',
|
31
|
+
headers: {
|
32
|
+
'Content-Type': isFormData ? 'multipart/form-data' : 'application/json',
|
33
|
+
'X-Show-Errors': `Errors go here`,
|
34
|
+
},
|
35
|
+
});
|
36
|
+
|
37
|
+
const apiResult = await (result.json() as Promise<ValidatedApiResult>);
|
38
|
+
|
39
|
+
if (!apiResult.hasErrors) {
|
40
|
+
alert('submitted');
|
41
|
+
}
|
42
|
+
|
43
|
+
return apiResult;
|
44
|
+
}
|
45
|
+
}
|
46
|
+
|
47
|
+
export interface PersonDto {
|
48
|
+
name?: string | undefined;
|
49
|
+
nameWithIcon?: string | undefined;
|
50
|
+
numberOfArms?: number;
|
51
|
+
phoneNumber?: string;
|
52
|
+
yearlySalaryUSD?: number;
|
53
|
+
isCool?: boolean;
|
54
|
+
isMusicEnjoyer?: boolean;
|
55
|
+
gradDate?: string;
|
56
|
+
favoriteDate?: string;
|
57
|
+
favoriteNumber?: number;
|
58
|
+
favoriteColor?: string;
|
59
|
+
favoriteColors?: string[];
|
60
|
+
favoriteNames?: string[];
|
61
|
+
allowLogin?: boolean;
|
62
|
+
userRoles?: number[];
|
63
|
+
profileImage?: any;
|
64
|
+
favoritePictures?: any[];
|
65
|
+
}
|