envoc-form 4.0.1-9 → 4.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +5117 -12
- package/es/AddressInput/AddressInput.d.ts +15 -5
- package/es/AddressInput/AddressInput.js +10 -0
- package/es/ConfirmBaseForm/ConfirmBaseForm.d.ts +14 -0
- package/es/ConfirmBaseForm/ConfirmBaseForm.js +6 -0
- package/es/ConfirmDeleteForm/ConfirmDeleteForm.d.ts +13 -0
- package/es/ConfirmDeleteForm/ConfirmDeleteForm.js +5 -0
- package/es/DatePicker/DatePickerGroup.d.ts +7 -2
- package/es/DatePicker/DatePickerGroup.js +16 -6
- package/es/DatePicker/StringDateOnlyPickerGroup.d.ts +6 -1
- package/es/DatePicker/StringDateOnlyPickerGroup.js +5 -0
- package/es/DatePicker/StringDatePickerGroup.d.ts +6 -1
- package/es/DatePicker/StringDatePickerGroup.js +5 -0
- package/es/Field/Field.d.ts +9 -3
- package/es/Field/Field.js +9 -4
- package/es/Field/FieldErrorScrollTarget.d.ts +1 -0
- package/es/Field/FieldErrorScrollTarget.js +1 -0
- package/es/Field/StandAloneInput.d.ts +4 -0
- package/es/Field/StandAloneInput.js +1 -0
- package/es/Field/useStandardField.d.ts +6 -0
- package/es/Field/useStandardField.js +8 -9
- package/es/FieldArray/FieldArray.d.ts +11 -2
- package/es/FieldArray/FieldArray.js +7 -2
- package/es/File/FileGroup.d.ts +3 -1
- package/es/File/FileGroup.js +4 -3
- package/es/File/FileList.d.ts +2 -2
- package/es/Form/FocusError.d.ts +2 -0
- package/es/Form/FocusError.js +1 -0
- package/es/Form/Form.d.ts +6 -0
- package/es/Form/Form.js +1 -0
- package/es/Form/FormBasedPreventNavigation.d.ts +3 -1
- package/es/Form/FormBasedPreventNavigation.js +12 -45
- package/es/Form/LegacyFormBasedPreventNavigation.d.ts +17 -0
- package/es/Form/LegacyFormBasedPreventNavigation.js +69 -0
- package/es/Form/NewFormBasedPreventNavigation.d.ts +14 -0
- package/es/Form/NewFormBasedPreventNavigation.js +39 -0
- package/es/Form/ServerErrorContext.d.ts +1 -0
- package/es/Form/ServerErrorContext.js +1 -0
- package/es/FormActions.d.ts +6 -0
- package/es/FormActions.js +1 -0
- package/es/FormDefaults.d.ts +1 -0
- package/es/FormDefaults.js +1 -0
- package/es/Group.d.ts +7 -4
- package/es/Group.js +4 -3
- package/es/Input/IconInputGroup.d.ts +4 -1
- package/es/Input/IconInputGroup.js +3 -1
- package/es/Input/InputGroup.d.ts +4 -4
- package/es/Input/InputGroup.js +4 -4
- package/es/Input/MoneyInputGroup.d.ts +3 -1
- package/es/Input/MoneyInputGroup.js +1 -0
- package/es/Input/NumberInputGroup.d.ts +3 -1
- package/es/Input/NumberInputGroup.js +1 -0
- package/es/Input/StringInputGroup.d.ts +2 -1
- package/es/Input/StringInputGroup.js +3 -1
- package/es/Normalization/normalizers.d.ts +4 -2
- package/es/Normalization/normalizers.js +2 -0
- package/es/Select/BooleanSelectGroup.d.ts +2 -1
- package/es/Select/BooleanSelectGroup.js +1 -0
- package/es/Select/NumberSelectGroup.d.ts +4 -2
- package/es/Select/NumberSelectGroup.js +2 -0
- package/es/Select/SelectGroup.d.ts +8 -2
- package/es/Select/SelectGroup.js +5 -4
- package/es/Select/StringSelectGroup.d.ts +2 -0
- package/es/Select/StringSelectGroup.js +2 -0
- package/es/StandardFormActions.d.ts +5 -0
- package/es/StandardFormActions.js +1 -0
- package/es/SubmitFormButton.d.ts +4 -1
- package/es/SubmitFormButton.js +1 -0
- package/es/TextArea/TextAreaGroup.d.ts +2 -1
- package/es/TextArea/TextAreaGroup.js +5 -4
- package/es/Validation/validators.d.ts +11 -8
- package/es/Validation/validators.js +6 -2
- package/es/index.d.ts +4 -4
- package/es/setupTests.d.ts +1 -0
- package/es/setupTests.js +1 -0
- package/lib/AddressInput/AddressInput.d.ts +15 -5
- package/lib/AddressInput/AddressInput.js +10 -0
- package/lib/ConfirmBaseForm/ConfirmBaseForm.d.ts +14 -0
- package/lib/ConfirmBaseForm/ConfirmBaseForm.js +6 -0
- package/lib/ConfirmDeleteForm/ConfirmDeleteForm.d.ts +13 -0
- package/lib/ConfirmDeleteForm/ConfirmDeleteForm.js +5 -0
- package/lib/DatePicker/DatePickerGroup.d.ts +7 -2
- package/lib/DatePicker/DatePickerGroup.js +16 -6
- package/lib/DatePicker/StringDateOnlyPickerGroup.d.ts +6 -1
- package/lib/DatePicker/StringDateOnlyPickerGroup.js +5 -0
- package/lib/DatePicker/StringDatePickerGroup.d.ts +6 -1
- package/lib/DatePicker/StringDatePickerGroup.js +5 -0
- package/lib/Field/Field.d.ts +9 -3
- package/lib/Field/Field.js +9 -4
- package/lib/Field/FieldErrorScrollTarget.d.ts +1 -0
- package/lib/Field/FieldErrorScrollTarget.js +1 -0
- package/lib/Field/StandAloneInput.d.ts +4 -0
- package/lib/Field/StandAloneInput.js +1 -0
- package/lib/Field/useStandardField.d.ts +6 -0
- package/lib/Field/useStandardField.js +8 -9
- package/lib/FieldArray/FieldArray.d.ts +11 -2
- package/lib/FieldArray/FieldArray.js +7 -2
- package/lib/File/FileGroup.d.ts +3 -1
- package/lib/File/FileGroup.js +4 -3
- package/lib/File/FileList.d.ts +2 -2
- package/lib/Form/FocusError.d.ts +2 -0
- package/lib/Form/FocusError.js +1 -0
- package/lib/Form/Form.d.ts +6 -0
- package/lib/Form/Form.js +1 -0
- package/lib/Form/FormBasedPreventNavigation.d.ts +3 -1
- package/lib/Form/FormBasedPreventNavigation.js +13 -43
- package/lib/Form/LegacyFormBasedPreventNavigation.d.ts +17 -0
- package/lib/Form/LegacyFormBasedPreventNavigation.js +72 -0
- package/lib/Form/NewFormBasedPreventNavigation.d.ts +14 -0
- package/lib/Form/NewFormBasedPreventNavigation.js +42 -0
- package/lib/Form/ServerErrorContext.d.ts +1 -0
- package/lib/Form/ServerErrorContext.js +1 -0
- package/lib/FormActions.d.ts +6 -0
- package/lib/FormActions.js +1 -0
- package/lib/FormDefaults.d.ts +1 -0
- package/lib/FormDefaults.js +1 -0
- package/lib/Group.d.ts +7 -4
- package/lib/Group.js +4 -3
- package/lib/Input/IconInputGroup.d.ts +4 -1
- package/lib/Input/IconInputGroup.js +3 -1
- package/lib/Input/InputGroup.d.ts +4 -4
- package/lib/Input/InputGroup.js +4 -4
- package/lib/Input/MoneyInputGroup.d.ts +3 -1
- package/lib/Input/MoneyInputGroup.js +1 -0
- package/lib/Input/NumberInputGroup.d.ts +3 -1
- package/lib/Input/NumberInputGroup.js +1 -0
- package/lib/Input/StringInputGroup.d.ts +2 -1
- package/lib/Input/StringInputGroup.js +3 -1
- package/lib/Normalization/normalizers.d.ts +4 -2
- package/lib/Normalization/normalizers.js +2 -0
- package/lib/Select/BooleanSelectGroup.d.ts +2 -1
- package/lib/Select/BooleanSelectGroup.js +1 -0
- package/lib/Select/NumberSelectGroup.d.ts +4 -2
- package/lib/Select/NumberSelectGroup.js +2 -0
- package/lib/Select/SelectGroup.d.ts +8 -2
- package/lib/Select/SelectGroup.js +5 -4
- package/lib/Select/StringSelectGroup.d.ts +2 -0
- package/lib/Select/StringSelectGroup.js +2 -0
- package/lib/StandardFormActions.d.ts +5 -0
- package/lib/StandardFormActions.js +1 -0
- package/lib/SubmitFormButton.d.ts +4 -1
- package/lib/SubmitFormButton.js +1 -0
- package/lib/TextArea/TextAreaGroup.d.ts +2 -1
- package/lib/TextArea/TextAreaGroup.js +5 -4
- package/lib/Validation/validators.d.ts +11 -8
- package/lib/Validation/validators.js +6 -2
- package/lib/index.d.ts +4 -4
- package/lib/setupTests.d.ts +1 -0
- package/lib/setupTests.js +3 -0
- package/package.json +9 -6
- package/src/AddressInput/AddressInput.tsx +15 -5
- package/src/AddressInput/__snapshots__/AddressInput.test.tsx.snap +8 -4
- 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/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/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
@@ -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
|
}
|
@@ -5,10 +5,15 @@ import SubmitFormButton from './SubmitFormButton';
|
|
5
5
|
import { FormDefaults } from './FormDefaults';
|
6
6
|
|
7
7
|
export interface StandardFormActionsProps {
|
8
|
+
/** Allow the form to be submitted without any changes. By default this is not allowed. */
|
8
9
|
allowPristineSubmit?: boolean;
|
10
|
+
/** The cancel button's `onClick`.
|
11
|
+
* @defaultValue `window.history.back()`
|
12
|
+
*/
|
9
13
|
handleCancel?: MouseEventHandler<HTMLButtonElement>;
|
10
14
|
}
|
11
15
|
|
16
|
+
/** Standard submit and cancel buttons. */
|
12
17
|
export default function StandardFormActions({
|
13
18
|
allowPristineSubmit,
|
14
19
|
handleCancel,
|
package/src/SubmitFormButton.tsx
CHANGED
@@ -5,9 +5,12 @@ import { FormDefaults } from './FormDefaults';
|
|
5
5
|
|
6
6
|
export interface SubmitFormButtonProps
|
7
7
|
extends Omit<React.HTMLProps<HTMLButtonElement>, 'type'> {
|
8
|
+
/** Allow the form to be submitted without any changes. By default this is not allowed. */
|
8
9
|
allowPristineSubmit?: boolean;
|
10
|
+
/** Text to display on the button. Default text is 'Submit'. */
|
9
11
|
children?: React.ReactNode;
|
10
12
|
}
|
13
|
+
|
11
14
|
function SubmitFormButton(
|
12
15
|
{
|
13
16
|
allowPristineSubmit,
|
@@ -45,6 +48,7 @@ function SubmitFormButton(
|
|
45
48
|
);
|
46
49
|
}
|
47
50
|
|
51
|
+
/** Generic submit button for forms. */
|
48
52
|
export default React.forwardRef(
|
49
53
|
SubmitFormButton
|
50
|
-
) as React.
|
54
|
+
) as React.FunctionComponent<SubmitFormButtonProps>;
|
@@ -1,11 +1,11 @@
|
|
1
1
|
import React, { 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 TextAreaGroupProps
|
8
|
-
extends InjectedFieldProps<string | undefined>,
|
8
|
+
extends InjectedFieldProps<string | undefined | null>,
|
9
9
|
Omit<
|
10
10
|
React.HTMLProps<HTMLTextAreaElement>,
|
11
11
|
keyof InjectedFieldProps<any> | 'children' | 'className' | 'label'
|
@@ -14,7 +14,6 @@ export interface TextAreaGroupProps
|
|
14
14
|
icon?: React.ReactNode;
|
15
15
|
}
|
16
16
|
|
17
|
-
/** generic controlled <textarea/> wrapped by a Group */
|
18
17
|
function TextAreaGroup(
|
19
18
|
{
|
20
19
|
input,
|
@@ -22,6 +21,7 @@ function TextAreaGroup(
|
|
22
21
|
label,
|
23
22
|
helpText,
|
24
23
|
className,
|
24
|
+
required,
|
25
25
|
disabled,
|
26
26
|
icon,
|
27
27
|
...rest
|
@@ -32,6 +32,7 @@ function TextAreaGroup(
|
|
32
32
|
<Group
|
33
33
|
input={input}
|
34
34
|
meta={meta}
|
35
|
+
required={required}
|
35
36
|
disabled={disabled}
|
36
37
|
label={label}
|
37
38
|
helpText={helpText}
|
@@ -43,7 +44,7 @@ function TextAreaGroup(
|
|
43
44
|
<textarea
|
44
45
|
{...input}
|
45
46
|
{...rest}
|
46
|
-
value={input.value}
|
47
|
+
value={input.value ?? ''}
|
47
48
|
onChange={(val) => input.onChange(val.target.value)}
|
48
49
|
ref={ref}
|
49
50
|
className={classNames(
|
@@ -55,6 +56,7 @@ function TextAreaGroup(
|
|
55
56
|
);
|
56
57
|
}
|
57
58
|
|
59
|
+
/** Generic controlled `<textarea/>` wrapped by a `<Group/>` */
|
58
60
|
const TextAreaGroupWithRef = React.forwardRef(
|
59
61
|
TextAreaGroup
|
60
62
|
) as React.ComponentType<TextAreaGroupProps>;
|
@@ -9,13 +9,14 @@ export const required = (value: any) =>
|
|
9
9
|
? undefined
|
10
10
|
: 'Required';
|
11
11
|
|
12
|
-
|
13
|
-
export const length =
|
14
|
-
|
15
|
-
|
16
|
-
}
|
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
|
+
};
|
17
18
|
|
18
|
-
export const integer = (val: string | number | undefined) => {
|
19
|
+
export const integer = (val: string | number | undefined | null) => {
|
19
20
|
if (!val) {
|
20
21
|
return undefined;
|
21
22
|
}
|
@@ -25,41 +26,42 @@ export const integer = (val: string | number | undefined) => {
|
|
25
26
|
};
|
26
27
|
|
27
28
|
export const maxLength =
|
28
|
-
(len: number) => (value: string | number | undefined) => {
|
29
|
+
(len: number) => (value: string | number | undefined | null) => {
|
29
30
|
const hasError = getLengthOfValue(value) > len;
|
30
31
|
return !hasError ? undefined : `Maximum length ${len} exceeded`;
|
31
32
|
};
|
32
33
|
|
33
34
|
export const maxCount =
|
34
|
-
(count: number) => (value: { isDeleted?: boolean }[] | undefined) => {
|
35
|
+
(count: number) => (value: { isDeleted?: boolean }[] | undefined | null) => {
|
35
36
|
const hasError =
|
36
37
|
!!value && value.filter((x) => !x.isDeleted).length > count;
|
37
38
|
return !hasError ? undefined : `Should not have more than ${count}`;
|
38
39
|
};
|
39
40
|
|
40
41
|
export const minCount =
|
41
|
-
(count: number) => (value: { isDeleted?: boolean }[] | undefined) => {
|
42
|
+
(count: number) => (value: { isDeleted?: boolean }[] | undefined | null) => {
|
42
43
|
const hasError = !value || value.filter((x) => !x.isDeleted).length < count;
|
43
44
|
return !hasError ? undefined : `Should have at least ${count}`;
|
44
45
|
};
|
45
46
|
|
46
|
-
export const maxValue = (max: number) => (value: number | undefined) => {
|
47
|
+
export const maxValue = (max: number) => (value: number | undefined | null) => {
|
47
48
|
const hasError = !!value && value > max;
|
48
49
|
return !hasError ? undefined : `Maximum value ${max} exceeded`;
|
49
50
|
};
|
50
51
|
|
51
|
-
export const minValue = (min: number) => (value: number | undefined) => {
|
52
|
+
export const minValue = (min: number) => (value: number | undefined | null) => {
|
52
53
|
const hasError = !value || value < min;
|
53
54
|
return !hasError ? undefined : `Minimum value ${min} not met`;
|
54
55
|
};
|
55
56
|
|
56
|
-
|
57
|
+
/** Validate for a ZIP Code. Accepts formats: ##### and #####-#### */
|
58
|
+
export const zipCode = (value: string | undefined | null) => {
|
57
59
|
return value && !/^[0-9]{5}(?:-[0-9]{4})?$/.test(value)
|
58
60
|
? 'Invalid ZIP Code'
|
59
61
|
: undefined;
|
60
62
|
};
|
61
63
|
|
62
|
-
|
64
|
+
/** One of the validators provided must be true. */
|
63
65
|
export const any =
|
64
66
|
<TValue>(validatorList: ValidationFunction<TValue>[]) =>
|
65
67
|
(value: TValue) => {
|
@@ -73,7 +75,10 @@ export const any =
|
|
73
75
|
);
|
74
76
|
};
|
75
77
|
|
76
|
-
function getLengthOfValue(value: string | number | undefined) {
|
78
|
+
function getLengthOfValue(value: string | number | undefined | null) {
|
79
|
+
if (value === null) {
|
80
|
+
return 0;
|
81
|
+
}
|
77
82
|
switch (typeof value) {
|
78
83
|
case 'undefined':
|
79
84
|
return 0;
|
@@ -1,28 +1,29 @@
|
|
1
|
-
import '@testing-library/jest-dom/extend-expect';
|
2
1
|
import { HashRouter } from 'react-router-dom';
|
3
|
-
import {
|
4
|
-
import {
|
2
|
+
import { Form, FormBuilderProp } from '../';
|
3
|
+
import { ValidatedApiResult } from '../Validation/ValidatedApiResult';
|
5
4
|
|
6
5
|
interface FormTestBaseProps<TForm extends object> {
|
7
6
|
children: (formBuilder: FormBuilderProp<TForm>) => JSX.Element;
|
8
7
|
initialValues?: TForm;
|
8
|
+
handleSubmit?: (data: TForm | FormData) => Promise<ValidatedApiResult>;
|
9
9
|
}
|
10
10
|
|
11
11
|
export default function FormTestBase<TForm extends PersonDto>({
|
12
12
|
children,
|
13
13
|
initialValues,
|
14
|
+
handleSubmit,
|
14
15
|
}: FormTestBaseProps<TForm>) {
|
15
16
|
return (
|
16
17
|
<HashRouter>
|
17
|
-
<Form
|
18
|
+
<Form
|
19
|
+
onSubmit={handleSubmit ? handleSubmit : onSubmit}
|
20
|
+
initialValues={initialValues}>
|
18
21
|
{children}
|
19
22
|
</Form>
|
20
23
|
</HashRouter>
|
21
24
|
);
|
22
25
|
|
23
|
-
async function onSubmit(
|
24
|
-
data: PersonDto | FormData
|
25
|
-
): Promise<ValidatedApiResult> {
|
26
|
+
async function onSubmit(data: TForm | FormData): Promise<ValidatedApiResult> {
|
26
27
|
const isFormData = data instanceof FormData;
|
27
28
|
const result = await fetch('/api/submit', {
|
28
29
|
body: isFormData ? data : JSON.stringify(data),
|
@@ -43,7 +44,7 @@ export default function FormTestBase<TForm extends PersonDto>({
|
|
43
44
|
}
|
44
45
|
}
|
45
46
|
|
46
|
-
interface PersonDto {
|
47
|
+
export interface PersonDto {
|
47
48
|
name?: string | undefined;
|
48
49
|
nameWithIcon?: string | undefined;
|
49
50
|
numberOfArms?: number;
|
@@ -0,0 +1,82 @@
|
|
1
|
+
import { render, screen, waitFor } from '@testing-library/react';
|
2
|
+
import user from '@testing-library/user-event';
|
3
|
+
import FormTestBase from './FormTestBase';
|
4
|
+
import { Form, StringInputGroup, ValidatedApiResult } from '../';
|
5
|
+
import { PersonDto } from '../__Tests__/FormTestBase';
|
6
|
+
import StandardFormActions from '../StandardFormActions';
|
7
|
+
import { validators } from '../';
|
8
|
+
|
9
|
+
const handleSubmitMock = jest.fn((value: PersonDto | FormData) => {
|
10
|
+
//return value;
|
11
|
+
return new Promise<ValidatedApiResult>((res, rej) => {
|
12
|
+
setTimeout(() => {
|
13
|
+
res({});
|
14
|
+
}, 15);
|
15
|
+
});
|
16
|
+
});
|
17
|
+
|
18
|
+
describe('Realistic Form', () => {
|
19
|
+
it('Validates, resolves validation, submits values', async () => {
|
20
|
+
global.scrollTo = jest.fn();
|
21
|
+
const fullForm = (
|
22
|
+
<FormTestBase handleSubmit={handleSubmitMock}>
|
23
|
+
{({ Field }) => (
|
24
|
+
<>
|
25
|
+
<Field name="name" label="name" Component={StringInputGroup} />
|
26
|
+
<Field
|
27
|
+
name="nameWithIcon"
|
28
|
+
label="Name with Icon"
|
29
|
+
Component={StringInputGroup}
|
30
|
+
validate={validators.required}
|
31
|
+
/>
|
32
|
+
<StandardFormActions />
|
33
|
+
<Form.DisplayFormState />
|
34
|
+
</>
|
35
|
+
)}
|
36
|
+
</FormTestBase>
|
37
|
+
);
|
38
|
+
|
39
|
+
render(fullForm);
|
40
|
+
const nameInput = screen.getByLabelText('name');
|
41
|
+
const formSubmitButton = screen.getByRole('button', {
|
42
|
+
name: 'Submit',
|
43
|
+
});
|
44
|
+
//The form should start off with the submit button disabled
|
45
|
+
expect(formSubmitButton).toBeDisabled();
|
46
|
+
|
47
|
+
//Enter a name into the name input
|
48
|
+
user.click(nameInput);
|
49
|
+
user.type(nameInput, 'Travis Overmier');
|
50
|
+
expect(nameInput).toHaveValue('Travis Overmier');
|
51
|
+
|
52
|
+
//Submit should be enabled, click submit
|
53
|
+
expect(formSubmitButton).toBeEnabled();
|
54
|
+
user.click(formSubmitButton);
|
55
|
+
|
56
|
+
//Submit should disable while 'submitting' then re-enable
|
57
|
+
expect(formSubmitButton).toBeDisabled();
|
58
|
+
await waitFor(() => {
|
59
|
+
expect(formSubmitButton).toBeEnabled();
|
60
|
+
});
|
61
|
+
|
62
|
+
//Validation message should appear
|
63
|
+
const nameWithIconInput = screen.getByLabelText('Name with Icon');
|
64
|
+
expect(nameWithIconInput).toHaveErrorMessage('Required');
|
65
|
+
|
66
|
+
//Fill in field, error message goes away
|
67
|
+
user.type(nameWithIconInput, 'Kyle Joiner');
|
68
|
+
user.tab();
|
69
|
+
await waitFor(() => {
|
70
|
+
expect(nameWithIconInput).not.toHaveErrorMessage();
|
71
|
+
});
|
72
|
+
|
73
|
+
//Click the submit button
|
74
|
+
user.click(formSubmitButton);
|
75
|
+
await waitFor(() => {
|
76
|
+
expect(handleSubmitMock).toHaveBeenCalled();
|
77
|
+
expect(JSON.stringify(handleSubmitMock.mock.calls[0][0])).toBe(
|
78
|
+
`{"name":"Travis Overmier","nameWithIcon":"Kyle Joiner"}`
|
79
|
+
);
|
80
|
+
});
|
81
|
+
});
|
82
|
+
});
|
package/src/index.ts
CHANGED
@@ -16,7 +16,7 @@ export type { ConfirmDeleteFormProps } from './ConfirmDeleteForm/ConfirmDeleteFo
|
|
16
16
|
|
17
17
|
// Date
|
18
18
|
export { default as DatePickerGroup } from './DatePicker/DatePickerGroup';
|
19
|
-
export type { DatePickerGroupProps
|
19
|
+
export type { DatePickerGroupProps } from './DatePicker/DatePickerGroup';
|
20
20
|
export { convertToTimeZoneInsensitiveISOString } from './DatePicker/DatePickerGroup';
|
21
21
|
|
22
22
|
export type { DatePickerHelper } from './DatePicker/DatePickerHelper';
|
@@ -68,17 +68,17 @@ export type {
|
|
68
68
|
} from './Form/ServerErrorContext';
|
69
69
|
|
70
70
|
export { default as FormActions } from './FormActions';
|
71
|
-
export type { FormActionsProps
|
71
|
+
export type { FormActionsProps } from './FormActions';
|
72
72
|
|
73
73
|
export { default as StandardFormActions } from './StandardFormActions';
|
74
|
-
export type { StandardFormActionsProps
|
74
|
+
export type { StandardFormActionsProps } from './StandardFormActions';
|
75
75
|
|
76
76
|
// FormDefaults
|
77
77
|
export { FormDefaults } from './FormDefaults';
|
78
78
|
|
79
79
|
// Input
|
80
80
|
export { default as Group } from './Group';
|
81
|
-
export type { GroupProps
|
81
|
+
export type { GroupProps } from './Group';
|
82
82
|
|
83
83
|
export { default as IconInputGroup } from './Input/IconInputGroup';
|
84
84
|
export type { IconInputGroupProps } from './Input/IconInputGroup';
|
@@ -0,0 +1 @@
|
|
1
|
+
import '@testing-library/jest-dom/extend-expect';
|