envoc-form 3.3.0 → 4.0.1-2
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 +10 -1
- package/dist/css/envoc-form-styles.css +0 -113
- package/dist/css/envoc-form-styles.css.map +1 -1
- package/es/AddressInput/AddressInput.d.ts +15 -0
- package/es/AddressInput/AddressInput.js +21 -97
- package/es/AddressInput/UsStates.d.ts +3 -0
- package/es/AddressInput/UsStates.js +53 -152
- package/es/ConfirmBaseForm/ConfirmBaseForm.d.ts +10 -0
- package/es/ConfirmBaseForm/ConfirmBaseForm.js +35 -43
- package/es/ConfirmDeleteForm/ConfirmDeleteForm.d.ts +11 -0
- package/es/ConfirmDeleteForm/ConfirmDeleteForm.js +54 -40
- package/es/DatePicker/DateOnlyDatePickerGroup.d.ts +10 -0
- package/es/DatePicker/DateOnlyDatePickerGroup.js +23 -0
- package/es/DatePicker/DatePickerGroup.d.ts +8 -0
- package/es/DatePicker/DatePickerGroup.js +60 -0
- package/es/DatePicker/DatePickerHelper.d.ts +3 -0
- package/es/DatePicker/DatePickerHelper.js +1 -0
- package/es/DatePicker/StringDatePickerGroup.d.ts +5 -0
- package/es/DatePicker/StringDatePickerGroup.js +19 -0
- package/es/Field/CustomFieldInputProps.d.ts +7 -0
- package/es/Field/CustomFieldInputProps.js +1 -0
- package/es/Field/CustomFieldMetaProps.d.ts +4 -0
- package/es/Field/CustomFieldMetaProps.js +1 -0
- package/es/Field/Field.d.ts +22 -0
- package/es/Field/Field.js +46 -0
- package/es/Field/FieldErrorScrollTarget.d.ts +2 -0
- package/es/Field/FieldErrorScrollTarget.js +11 -0
- package/es/Field/FieldNameContext.d.ts +3 -0
- package/es/Field/FieldNameContext.js +3 -0
- package/es/Field/FieldSection.d.ts +7 -0
- package/es/Field/FieldSection.js +20 -0
- package/es/Field/InjectedFieldProps.d.ts +7 -0
- package/es/Field/InjectedFieldProps.js +1 -0
- package/es/Field/StandAloneInput.d.ts +9 -0
- package/es/Field/StandAloneInput.js +49 -0
- package/es/Field/useStandardField.d.ts +15 -0
- package/es/Field/useStandardField.js +93 -0
- package/es/FieldArray/FieldArray.d.ts +15 -0
- package/es/FieldArray/FieldArray.js +80 -0
- package/es/File/FileGroup.d.ts +8 -0
- package/es/File/FileGroup.js +50 -0
- package/es/File/FileList.d.ts +6 -0
- package/es/File/FileList.js +20 -0
- package/es/File/humanFileSize.d.ts +1 -0
- package/es/File/humanFileSize.js +6 -0
- package/es/Form/FocusError.d.ts +5 -0
- package/es/Form/FocusError.js +41 -53
- package/es/Form/Form.d.ts +22 -0
- package/es/Form/Form.js +111 -125
- package/es/Form/FormBasedPreventNavigation.d.ts +5 -0
- package/es/Form/FormBasedPreventNavigation.js +53 -30
- package/es/Form/ServerErrorContext.d.ts +10 -0
- package/es/Form/ServerErrorContext.js +8 -6
- package/es/FormActions.d.ts +7 -0
- package/es/FormActions.js +22 -0
- package/es/Group.d.ts +13 -0
- package/es/Group.js +22 -0
- package/es/Input/IconInputGroup.d.ts +9 -0
- package/es/Input/IconInputGroup.js +40 -0
- package/es/Input/InputGroup.d.ts +12 -0
- package/es/Input/InputGroup.js +33 -0
- package/es/Input/MoneyInputGroup.d.ts +7 -0
- package/es/Input/MoneyInputGroup.js +41 -0
- package/es/Input/NumberInputGroup.d.ts +7 -0
- package/es/Input/NumberInputGroup.js +41 -0
- package/es/Input/StringInputGroup.d.ts +7 -0
- package/es/Input/StringInputGroup.js +40 -0
- package/es/Normalization/NormalizationFunction.d.ts +4 -0
- package/es/Normalization/NormalizationFunction.js +1 -0
- package/es/Normalization/normalizers.d.ts +2 -0
- package/es/Normalization/normalizers.js +30 -0
- package/es/Select/BooleanSelectGroup.d.ts +7 -0
- package/es/Select/BooleanSelectGroup.js +27 -0
- package/es/Select/NumberSelectGroup.d.ts +8 -0
- package/es/Select/NumberSelectGroup.js +19 -0
- package/es/Select/SelectGroup.d.ts +22 -0
- package/es/Select/SelectGroup.js +59 -0
- package/es/Select/SelectGroupPropsHelper.d.ts +3 -0
- package/es/Select/SelectGroupPropsHelper.js +1 -0
- package/es/Select/StringSelectGroup.d.ts +8 -0
- package/es/Select/StringSelectGroup.js +19 -0
- package/es/StandardFormActions.d.ts +6 -0
- package/es/StandardFormActions.js +23 -24
- package/es/SubmitFormButton.d.ts +7 -0
- package/es/SubmitFormButton.js +37 -23
- package/es/TextArea/TextAreaGroup.d.ts +8 -0
- package/es/TextArea/TextAreaGroup.js +33 -0
- package/es/Validation/ValidatedApiResult.d.ts +6 -0
- package/es/Validation/ValidatedApiResult.js +1 -0
- package/es/Validation/ValidationError.d.ts +5 -0
- package/es/Validation/ValidationError.js +1 -0
- package/es/Validation/ValidationFunction.d.ts +4 -0
- package/es/Validation/ValidationFunction.js +1 -0
- package/es/Validation/validators.d.ts +15 -0
- package/es/Validation/validators.js +73 -0
- package/es/__Tests__/FormTestBase.d.ts +27 -0
- package/es/__Tests__/FormTestBase.js +83 -12
- package/{src/__Tests__/index.js → es/__Tests__/index.d.ts} +0 -0
- package/es/__Tests__/index.js +2 -2
- package/es/index.d.ts +71 -0
- package/es/index.js +47 -25
- package/es/utils/objectContainsNonSerializableProperty.d.ts +1 -0
- package/es/utils/objectContainsNonSerializableProperty.js +14 -16
- package/es/utils/objectToFormData.d.ts +10 -0
- package/es/utils/objectToFormData.js +77 -65
- package/es/utils/typeChecks.d.ts +8 -0
- package/es/utils/typeChecks.js +18 -25
- package/lib/AddressInput/AddressInput.d.ts +15 -0
- package/lib/AddressInput/AddressInput.js +27 -123
- package/lib/AddressInput/UsStates.d.ts +3 -0
- package/lib/AddressInput/UsStates.js +55 -159
- package/lib/ConfirmBaseForm/ConfirmBaseForm.d.ts +10 -0
- package/lib/ConfirmBaseForm/ConfirmBaseForm.js +41 -57
- package/lib/ConfirmDeleteForm/ConfirmDeleteForm.d.ts +11 -0
- package/lib/ConfirmDeleteForm/ConfirmDeleteForm.js +60 -56
- package/lib/DatePicker/DateOnlyDatePickerGroup.d.ts +10 -0
- package/lib/DatePicker/DateOnlyDatePickerGroup.js +29 -0
- package/lib/DatePicker/DatePickerGroup.d.ts +8 -0
- package/lib/DatePicker/DatePickerGroup.js +66 -0
- package/lib/DatePicker/DatePickerHelper.d.ts +3 -0
- package/lib/DatePicker/DatePickerHelper.js +2 -0
- package/lib/DatePicker/StringDatePickerGroup.d.ts +5 -0
- package/lib/DatePicker/StringDatePickerGroup.js +25 -0
- package/lib/Field/CustomFieldInputProps.d.ts +7 -0
- package/lib/Field/CustomFieldInputProps.js +2 -0
- package/lib/Field/CustomFieldMetaProps.d.ts +4 -0
- package/lib/Field/CustomFieldMetaProps.js +2 -0
- package/lib/Field/Field.d.ts +22 -0
- package/lib/Field/Field.js +51 -0
- package/lib/Field/FieldErrorScrollTarget.d.ts +2 -0
- package/lib/Field/FieldErrorScrollTarget.js +14 -0
- package/lib/Field/FieldNameContext.d.ts +3 -0
- package/lib/Field/FieldNameContext.js +9 -0
- package/lib/Field/FieldSection.d.ts +7 -0
- package/lib/Field/FieldSection.js +23 -0
- package/lib/Field/InjectedFieldProps.d.ts +7 -0
- package/lib/Field/InjectedFieldProps.js +2 -0
- package/lib/Field/StandAloneInput.d.ts +9 -0
- package/lib/Field/StandAloneInput.js +75 -0
- package/lib/Field/useStandardField.d.ts +15 -0
- package/lib/Field/useStandardField.js +96 -0
- package/lib/FieldArray/FieldArray.d.ts +15 -0
- package/lib/FieldArray/FieldArray.js +86 -0
- package/lib/File/FileGroup.d.ts +8 -0
- package/lib/File/FileGroup.js +55 -0
- package/lib/File/FileList.d.ts +6 -0
- package/lib/File/FileList.js +23 -0
- package/lib/File/humanFileSize.d.ts +1 -0
- package/lib/File/humanFileSize.js +10 -0
- package/lib/Form/FocusError.d.ts +5 -0
- package/lib/Form/FocusError.js +47 -66
- package/lib/Form/Form.d.ts +22 -0
- package/lib/Form/Form.js +117 -153
- package/lib/Form/FormBasedPreventNavigation.d.ts +5 -0
- package/lib/Form/FormBasedPreventNavigation.js +56 -39
- package/lib/Form/ServerErrorContext.d.ts +10 -0
- package/lib/Form/ServerErrorContext.js +14 -18
- package/lib/FormActions.d.ts +7 -0
- package/lib/FormActions.js +28 -0
- package/lib/Group.d.ts +13 -0
- package/lib/Group.js +28 -0
- package/lib/Input/IconInputGroup.d.ts +9 -0
- package/lib/Input/IconInputGroup.js +45 -0
- package/lib/Input/InputGroup.d.ts +12 -0
- package/lib/Input/InputGroup.js +38 -0
- package/lib/Input/MoneyInputGroup.d.ts +7 -0
- package/lib/Input/MoneyInputGroup.js +46 -0
- package/lib/Input/NumberInputGroup.d.ts +7 -0
- package/lib/Input/NumberInputGroup.js +46 -0
- package/lib/Input/StringInputGroup.d.ts +7 -0
- package/lib/Input/StringInputGroup.js +45 -0
- package/lib/Normalization/NormalizationFunction.d.ts +4 -0
- package/lib/Normalization/NormalizationFunction.js +2 -0
- package/lib/Normalization/normalizers.d.ts +2 -0
- package/lib/Normalization/normalizers.js +35 -0
- package/lib/Select/BooleanSelectGroup.d.ts +7 -0
- package/lib/Select/BooleanSelectGroup.js +34 -0
- package/lib/Select/NumberSelectGroup.d.ts +8 -0
- package/lib/Select/NumberSelectGroup.js +27 -0
- package/lib/Select/SelectGroup.d.ts +22 -0
- package/lib/Select/SelectGroup.js +65 -0
- package/lib/Select/SelectGroupPropsHelper.d.ts +3 -0
- package/lib/Select/SelectGroupPropsHelper.js +2 -0
- package/lib/Select/StringSelectGroup.d.ts +8 -0
- package/lib/Select/StringSelectGroup.js +27 -0
- package/lib/StandardFormActions.d.ts +6 -0
- package/lib/StandardFormActions.js +29 -37
- package/lib/SubmitFormButton.d.ts +7 -0
- package/lib/SubmitFormButton.js +42 -38
- package/lib/TextArea/TextAreaGroup.d.ts +8 -0
- package/lib/TextArea/TextAreaGroup.js +38 -0
- package/lib/Validation/ValidatedApiResult.d.ts +6 -0
- package/lib/Validation/ValidatedApiResult.js +2 -0
- package/lib/Validation/ValidationError.d.ts +5 -0
- package/lib/Validation/ValidationError.js +2 -0
- package/lib/Validation/ValidationFunction.d.ts +4 -0
- package/lib/Validation/ValidationFunction.js +2 -0
- package/lib/Validation/validators.d.ts +15 -0
- package/lib/Validation/validators.js +86 -0
- package/lib/__Tests__/FormTestBase.d.ts +27 -0
- package/lib/__Tests__/FormTestBase.js +86 -25
- package/lib/__Tests__/index.d.ts +2 -0
- package/lib/__Tests__/index.js +8 -15
- package/lib/index.d.ts +71 -0
- package/lib/index.js +112 -262
- package/lib/utils/objectContainsNonSerializableProperty.d.ts +1 -0
- package/lib/utils/objectContainsNonSerializableProperty.js +17 -24
- package/lib/utils/objectToFormData.d.ts +10 -0
- package/lib/utils/objectToFormData.js +79 -73
- package/lib/utils/typeChecks.d.ts +8 -0
- package/lib/utils/typeChecks.js +29 -58
- package/package.json +106 -101
- package/src/AddressInput/{AddesssInput.test.js → AddressInput.test.tsx} +7 -3
- package/src/AddressInput/AddressInput.tsx +72 -0
- package/src/AddressInput/{UsStates.js → UsStates.ts} +3 -1
- package/src/AddressInput/__snapshots__/AddressInput.test.tsx.snap +179 -0
- package/src/ConfirmBaseForm/ConfirmBaseForm.test.tsx +24 -0
- package/src/ConfirmBaseForm/ConfirmBaseForm.tsx +50 -0
- package/src/ConfirmBaseForm/__snapshots__/{ConfirmBaseForm.test.js.snap → ConfirmBaseForm.test.tsx.snap} +2 -2
- package/src/ConfirmDeleteForm/{ConfirmDeleteForm.test.js → ConfirmDeleteForm.test.tsx} +3 -3
- package/src/ConfirmDeleteForm/ConfirmDeleteForm.tsx +67 -0
- package/src/ConfirmDeleteForm/__snapshots__/{ConfirmDeleteForm.test.js.snap → ConfirmDeleteForm.test.tsx.snap} +2 -2
- package/src/DatePicker/DateOnlyDatePickerGroup.tsx +24 -0
- package/src/DatePicker/DatePicker.test.tsx +48 -0
- package/src/DatePicker/DatePickerGroup.tsx +75 -0
- package/src/DatePicker/DatePickerHelper.ts +4 -0
- package/src/DatePicker/StringDatePickerGroup.tsx +14 -0
- package/src/DatePicker/__snapshots__/DatePicker.test.tsx.snap +151 -0
- package/src/Field/CustomFieldInputProps.ts +10 -0
- package/src/Field/CustomFieldMetaProps.ts +5 -0
- package/src/Field/Field.tsx +93 -0
- package/src/Field/FieldErrorScrollTarget.tsx +11 -0
- package/src/Field/FieldNameContext.ts +6 -0
- package/src/Field/FieldSection.tsx +18 -0
- package/src/Field/InjectedFieldProps.ts +8 -0
- package/src/Field/StandAloneInput.tsx +51 -0
- package/src/Field/useStandardField.ts +121 -0
- package/src/FieldArray/FieldArray.tsx +133 -0
- package/src/File/FileGroup.test.tsx +35 -0
- package/src/File/FileGroup.tsx +71 -0
- package/src/File/FileList.tsx +19 -0
- package/src/File/__snapshots__/FileGroup.test.tsx.snap +33 -0
- package/src/File/humanFileSize.ts +8 -0
- package/src/Form/{FocusError.js → FocusError.tsx} +10 -6
- package/src/Form/Form.test.tsx +14 -0
- package/src/Form/{Form.js → Form.tsx} +71 -31
- package/src/Form/FormBasedPreventNavigation.tsx +74 -0
- package/src/Form/ServerErrorContext.ts +17 -0
- package/src/Form/__snapshots__/{Form.test.js.snap → Form.test.tsx.snap} +1 -1
- package/src/FormActions.tsx +34 -0
- package/src/Group.tsx +43 -0
- package/src/Input/IconInputGroup.tsx +48 -0
- package/src/Input/InputGroup.tsx +57 -0
- package/src/Input/MoneyInputGroup.tsx +44 -0
- package/src/Input/NumberInputGroup.tsx +42 -0
- package/src/Input/StringInputGroup.tsx +48 -0
- package/src/Input/__Tests__/IconInputGroup.test.tsx +35 -0
- package/src/Input/__Tests__/MoneyInputGroup.test.tsx +37 -0
- package/src/Input/__Tests__/NumberInputGroup.test.tsx +35 -0
- package/src/Input/__Tests__/StringInputGroup.test.tsx +27 -0
- package/src/Input/__Tests__/__snapshots__/IconInputGroup.test.tsx.snap +30 -0
- package/src/Input/__Tests__/__snapshots__/MoneyInputGroup.test.tsx.snap +32 -0
- package/src/Input/__Tests__/__snapshots__/NumberInputGroup.test.tsx.snap +30 -0
- package/src/Input/__Tests__/__snapshots__/StringInputGroup.test.tsx.snap +29 -0
- package/src/Normalization/NormalizationFunction.ts +4 -0
- package/src/{normalizers.js → Normalization/normalizers.ts} +5 -5
- package/src/Select/BooleanSelectGroup.tsx +27 -0
- package/src/Select/NumberSelectGroup.tsx +14 -0
- package/src/Select/SelectGroup.tsx +106 -0
- package/src/Select/SelectGroupPropsHelper.ts +4 -0
- package/src/Select/StringSelectGroup.tsx +14 -0
- package/src/Select/__tests__/BooleanSelectGroup.test.tsx +35 -0
- package/src/Select/__tests__/NumberSelectGroup.test.tsx +87 -0
- package/src/Select/__tests__/StringSelectGroup.test.tsx +89 -0
- package/src/Select/__tests__/__snapshots__/BooleanSelectGroup.test.tsx.snap +99 -0
- package/src/Select/__tests__/__snapshots__/NumberSelectGroup.test.tsx.snap +197 -0
- package/src/Select/__tests__/__snapshots__/StringSelectGroup.test.tsx.snap +197 -0
- package/src/{StandardFormActions.js → StandardFormActions.tsx} +12 -6
- package/src/SubmitFormButton.tsx +44 -0
- package/src/TextArea/TextAreaGroup.tsx +55 -0
- package/src/Validation/ValidatedApiResult.ts +8 -0
- package/src/Validation/ValidationError.ts +6 -0
- package/src/Validation/ValidationFunction.ts +4 -0
- package/src/{validators/validators.test.js → Validation/validators.test.tsx} +6 -4
- package/src/Validation/validators.ts +92 -0
- package/src/__Tests__/FormTestBase.tsx +63 -0
- package/src/__Tests__/{StandardFormActions.test.js → StandardFormActions.test.tsx} +2 -8
- package/src/__Tests__/{SubmitFormButton.test.js → SubmitFormButton.test.tsx} +2 -8
- package/src/__Tests__/__snapshots__/{StandardFormActions.test.js.snap → StandardFormActions.test.tsx.snap} +2 -2
- package/src/__Tests__/__snapshots__/{SubmitFormButton.test.js.snap → SubmitFormButton.test.tsx.snap} +1 -1
- package/src/__Tests__/index.ts +3 -0
- package/src/index.ts +149 -0
- package/src/react-app-env.d.ts +1 -0
- package/src/styles.scss +0 -7
- package/src/utils/{objectContainsNonSerializableProperty.test.js → objectContainsNonSerializableProperty.test.tsx} +0 -0
- package/src/utils/{objectContainsNonSerializableProperty.js → objectContainsNonSerializableProperty.ts} +4 -2
- package/src/utils/{objectToFormData.test.js → objectToFormData.test.tsx} +0 -0
- package/src/utils/{objectToFormData.js → objectToFormData.ts} +29 -13
- package/src/utils/typeChecks.ts +18 -0
- package/es/AddressInput/index.js +0 -3
- package/es/BoolInput/BoolInput.js +0 -10
- package/es/BoolInput/InlineBoolInput.js +0 -10
- package/es/BoolInput/boolOptions.js +0 -8
- package/es/BoolInput/index.js +0 -3
- package/es/ConfirmBaseForm/index.js +0 -2
- package/es/ConfirmDeleteForm/index.js +0 -2
- package/es/DatePickerInput/DatePickerInput.js +0 -95
- package/es/DatePickerInput/index.js +0 -2
- package/es/ErrorScrollTarget.js +0 -10
- package/es/FileInput/DefaultFileList.js +0 -36
- package/es/FileInput/DropzoneFileInput.js +0 -58
- package/es/FileInput/FileInput.js +0 -62
- package/es/FileInput/index.js +0 -4
- package/es/Form/index.js +0 -5
- package/es/FormGroup.js +0 -22
- package/es/FormGroupWrapper.js +0 -22
- package/es/FormInput/FormInput.js +0 -212
- package/es/FormInput/index.js +0 -2
- package/es/FormInputArray/FormInputArray.js +0 -225
- package/es/FormInputArray/index.js +0 -2
- package/es/FormSection.js +0 -11
- package/es/IconInput.js +0 -33
- package/es/InlineFormInput/InlineFormInput.js +0 -8
- package/es/InlineFormInput/index.js +0 -2
- package/es/MoneyFormattedInput/MoneyFormattedInput.js +0 -106
- package/es/MoneyFormattedInput/index.js +0 -2
- package/es/MoneyInput/InlineMoneyInput.js +0 -7
- package/es/MoneyInput/MoneyInput.js +0 -7
- package/es/MoneyInput/index.js +0 -3
- package/es/MoneyInput/moneyInputProps.js +0 -13
- package/es/NestedFormFieldContext.js +0 -4
- package/es/ReactSelectField/ReactSelectField.js +0 -162
- package/es/ReactSelectField/index.js +0 -2
- package/es/normalizers.js +0 -39
- package/es/selectors.js +0 -6
- package/es/useStandardFormInput.js +0 -119
- package/es/utils/index.js +0 -3
- package/es/validators/index.js +0 -2
- package/es/validators/validators.js +0 -94
- package/lib/AddressInput/index.js +0 -23
- package/lib/BoolInput/BoolInput.js +0 -23
- package/lib/BoolInput/InlineBoolInput.js +0 -23
- package/lib/BoolInput/boolOptions.js +0 -15
- package/lib/BoolInput/index.js +0 -23
- package/lib/ConfirmBaseForm/index.js +0 -13
- package/lib/ConfirmDeleteForm/index.js +0 -13
- package/lib/DatePickerInput/DatePickerInput.js +0 -113
- package/lib/DatePickerInput/index.js +0 -13
- package/lib/ErrorScrollTarget.js +0 -20
- package/lib/FileInput/DefaultFileList.js +0 -47
- package/lib/FileInput/DropzoneFileInput.js +0 -75
- package/lib/FileInput/FileInput.js +0 -82
- package/lib/FileInput/index.js +0 -31
- package/lib/Form/index.js +0 -39
- package/lib/FormGroup.js +0 -33
- package/lib/FormGroupWrapper.js +0 -37
- package/lib/FormInput/FormInput.js +0 -236
- package/lib/FormInput/index.js +0 -13
- package/lib/FormInputArray/FormInputArray.js +0 -248
- package/lib/FormInputArray/index.js +0 -13
- package/lib/FormSection.js +0 -28
- package/lib/IconInput.js +0 -51
- package/lib/InlineFormInput/InlineFormInput.js +0 -20
- package/lib/InlineFormInput/index.js +0 -13
- package/lib/MoneyFormattedInput/MoneyFormattedInput.js +0 -126
- package/lib/MoneyFormattedInput/index.js +0 -15
- package/lib/MoneyInput/InlineMoneyInput.js +0 -20
- package/lib/MoneyInput/MoneyInput.js +0 -20
- package/lib/MoneyInput/index.js +0 -23
- package/lib/MoneyInput/moneyInputProps.js +0 -26
- package/lib/NestedFormFieldContext.js +0 -16
- package/lib/ReactSelectField/ReactSelectField.js +0 -187
- package/lib/ReactSelectField/index.js +0 -31
- package/lib/normalizers.js +0 -51
- package/lib/selectors.js +0 -18
- package/lib/useStandardFormInput.js +0 -135
- package/lib/utils/index.js +0 -23
- package/lib/validators/index.js +0 -17
- package/lib/validators/validators.js +0 -130
- package/src/AddressInput/AddressInput.js +0 -73
- package/src/AddressInput/__snapshots__/AddesssInput.test.js.snap +0 -207
- package/src/AddressInput/index.js +0 -3
- package/src/BoolInput/BoolInput.js +0 -7
- package/src/BoolInput/BoolInput.test.js +0 -23
- package/src/BoolInput/InlineBoolInput.js +0 -7
- package/src/BoolInput/__snapshots__/BoolInput.test.js.snap +0 -89
- package/src/BoolInput/boolOptions.js +0 -6
- package/src/BoolInput/index.js +0 -4
- package/src/ConfirmBaseForm/ConfirmBaseForm.js +0 -37
- package/src/ConfirmBaseForm/ConfirmBaseForm.test.js +0 -14
- package/src/ConfirmBaseForm/index.js +0 -2
- package/src/ConfirmDeleteForm/ConfirmDeleteForm.js +0 -39
- package/src/ConfirmDeleteForm/index.js +0 -2
- package/src/DatePickerInput/DatePickerInput.js +0 -49
- package/src/DatePickerInput/DatePickerInput.test.js +0 -74
- package/src/DatePickerInput/__snapshots__/DatePickerInput.test.js.snap +0 -134
- package/src/DatePickerInput/date-picker-input.scss +0 -42
- package/src/DatePickerInput/index.js +0 -3
- package/src/ErrorScrollTarget.js +0 -6
- package/src/FileInput/DefaultFileList.js +0 -39
- package/src/FileInput/DropzoneFileInput.js +0 -56
- package/src/FileInput/DropzoneFileInput.test.js +0 -24
- package/src/FileInput/FileInput.js +0 -77
- package/src/FileInput/FileInput.test.js +0 -24
- package/src/FileInput/__snapshots__/DropzoneFileInput.test.js.snap +0 -57
- package/src/FileInput/__snapshots__/FileInput.test.js.snap +0 -58
- package/src/FileInput/file-input.scss +0 -58
- package/src/FileInput/index.js +0 -5
- package/src/Form/Form.test.js +0 -23
- package/src/Form/FormBasedPreventNavigation.js +0 -25
- package/src/Form/ServerErrorContext.js +0 -7
- package/src/Form/index.js +0 -11
- package/src/FormGroup.js +0 -30
- package/src/FormGroupWrapper.js +0 -28
- package/src/FormInput/FormInput.js +0 -148
- package/src/FormInput/FormInput.test.js +0 -66
- package/src/FormInput/__snapshots__/FormInput.test.js.snap +0 -323
- package/src/FormInput/form-input.scss +0 -9
- package/src/FormInput/index.js +0 -2
- package/src/FormInputArray/FormInputArray.js +0 -224
- package/src/FormInputArray/FormInputArray.test.js +0 -108
- package/src/FormInputArray/__snapshots__/FormInputArray.test.js.snap +0 -52
- package/src/FormInputArray/form-input-array.scss +0 -13
- package/src/FormInputArray/index.js +0 -2
- package/src/FormSection.js +0 -13
- package/src/IconInput.js +0 -31
- package/src/InlineFormInput/InlineFormInput.js +0 -6
- package/src/InlineFormInput/InlineFormInput.test.js +0 -23
- package/src/InlineFormInput/__snapshots__/InlineFormInput.test.js.snap +0 -26
- package/src/InlineFormInput/index.js +0 -3
- package/src/InlineFormInput/inline-form-input.scss +0 -3
- package/src/MoneyFormattedInput/MoneyFormattedInput.js +0 -115
- package/src/MoneyFormattedInput/index.js +0 -3
- package/src/MoneyInput/InlineMoneyInput.js +0 -7
- package/src/MoneyInput/MoneyInput.js +0 -7
- package/src/MoneyInput/MoneyInputs.test.js +0 -43
- package/src/MoneyInput/__snapshots__/MoneyInputs.test.js.snap +0 -81
- package/src/MoneyInput/index.js +0 -4
- package/src/MoneyInput/money-input.scss +0 -3
- package/src/MoneyInput/moneyInputProps.js +0 -12
- package/src/NestedFormFieldContext.js +0 -6
- package/src/ReactSelectField/ReactSelectField.js +0 -155
- package/src/ReactSelectField/index.js +0 -6
- package/src/ReactSelectField/react-select-field.scss +0 -5
- package/src/SubmitFormButton.js +0 -28
- package/src/__Tests__/FormTestBase.js +0 -14
- package/src/__Tests__/IconInput.test.js +0 -23
- package/src/__Tests__/__snapshots__/IconInput.test.js.snap +0 -38
- package/src/index.js +0 -70
- package/src/selectors.js +0 -3
- package/src/useStandardFormInput.js +0 -118
- package/src/utils/index.js +0 -3
- package/src/utils/typeChecks.js +0 -18
- package/src/validators/index.js +0 -2
- package/src/validators/validators.js +0 -93
@@ -0,0 +1,121 @@
|
|
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?: string;
|
12
|
+
name: string;
|
13
|
+
disabled?: boolean;
|
14
|
+
validate?: ValidationFunction<TValue> | ValidationFunction<TValue>[];
|
15
|
+
normalize?: NormalizationFunction<TValue>;
|
16
|
+
}
|
17
|
+
|
18
|
+
// provides a consistent way to deal with all form fields (non array)
|
19
|
+
export default function useStandardField<TValue>({
|
20
|
+
id: providedId,
|
21
|
+
name: providedName,
|
22
|
+
disabled,
|
23
|
+
validate,
|
24
|
+
normalize,
|
25
|
+
}: useStandardFieldProps<TValue>): [
|
26
|
+
CustomFieldInputProps<TValue>,
|
27
|
+
CustomFieldMetaProps<TValue>
|
28
|
+
] {
|
29
|
+
// because the formik errors are evaluated all at the same time we need to keep server errors separate
|
30
|
+
const { getError: getServerError, setError: setServerError } =
|
31
|
+
useContext(ServerErrorContext);
|
32
|
+
|
33
|
+
// ensure that form section values are obeyed, e.g. homeAddress.zipCode
|
34
|
+
const fieldNameContextValue = useContext(FieldNameContext);
|
35
|
+
const name = fieldNameContextValue
|
36
|
+
? `${fieldNameContextValue}.${providedName}`
|
37
|
+
: providedName;
|
38
|
+
|
39
|
+
// ensure that nested contexts don't have duplicate id issues when an id is specified
|
40
|
+
const id = providedId
|
41
|
+
? fieldNameContextValue
|
42
|
+
? `${fieldNameContextValue}.${providedId}`
|
43
|
+
: providedId
|
44
|
+
: name;
|
45
|
+
|
46
|
+
// ensure that our custom validation rules are handled
|
47
|
+
// e.g. we allow arrays of validators
|
48
|
+
const [formikInput, formikMeta] = useField<TValue>({
|
49
|
+
name,
|
50
|
+
id: id ? id : name,
|
51
|
+
disabled: disabled,
|
52
|
+
validate: callAllValidators,
|
53
|
+
});
|
54
|
+
const { setFieldValue, isSubmitting } = useFormikContext();
|
55
|
+
|
56
|
+
const touched =
|
57
|
+
formikMeta.touched !== false && formikMeta.touched !== undefined;
|
58
|
+
useEffect(() => {
|
59
|
+
if (!touched && isSubmitting) {
|
60
|
+
// because we do not always register all fields up front.
|
61
|
+
// e.g. formik expects even a 'create' form to have all fields given, at least, blank values
|
62
|
+
// that seems to be how it touches on submit:
|
63
|
+
// see: https://codesandbox.io/s/formik-example-4n7n8 vs https://codesandbox.io/s/formik-example-kttk5
|
64
|
+
// note in particular the change in line 24
|
65
|
+
// thus, we manually touch all fields here by calling onBlur
|
66
|
+
// there is a "setFieldTouched" from useFormikContext but it doesn't appear to work on field arrays
|
67
|
+
handleBlur();
|
68
|
+
}
|
69
|
+
});
|
70
|
+
|
71
|
+
// these are the props we expect consumers of this hook to pass directly to the input (or other control)
|
72
|
+
const resultInput: CustomFieldInputProps<TValue> = {
|
73
|
+
name: formikInput.name,
|
74
|
+
// pass any direct from server props through normalize without making the form dirty (e.g. phone number)
|
75
|
+
value: normalize ? normalize(formikInput.value) : formikInput.value,
|
76
|
+
onChange: handleChange,
|
77
|
+
onBlur: handleBlur,
|
78
|
+
// extensions to formik
|
79
|
+
id: id,
|
80
|
+
};
|
81
|
+
|
82
|
+
const resultMeta: CustomFieldMetaProps<TValue> = {
|
83
|
+
...formikMeta,
|
84
|
+
error: getServerError(name) || (touched ? formikMeta.error : undefined),
|
85
|
+
// extensions to formik
|
86
|
+
warning: undefined, // TODO - did this never work?
|
87
|
+
touched: touched,
|
88
|
+
};
|
89
|
+
|
90
|
+
return [resultInput, resultMeta];
|
91
|
+
|
92
|
+
function handleBlur() {
|
93
|
+
formikInput.onBlur({ target: { name: name } });
|
94
|
+
}
|
95
|
+
|
96
|
+
function handleChange(value: TValue) {
|
97
|
+
if (disabled) {
|
98
|
+
return;
|
99
|
+
}
|
100
|
+
const normalized = normalize ? normalize(value) : value;
|
101
|
+
setFieldValue(name, normalized);
|
102
|
+
setServerError(name, undefined);
|
103
|
+
}
|
104
|
+
|
105
|
+
function callAllValidators(value: TValue) {
|
106
|
+
if (disabled || !validate) {
|
107
|
+
return;
|
108
|
+
}
|
109
|
+
|
110
|
+
if (!Array.isArray(validate)) {
|
111
|
+
return validate(value);
|
112
|
+
}
|
113
|
+
|
114
|
+
for (let i = 0; i < validate.length; i++) {
|
115
|
+
const result = validate[i](value);
|
116
|
+
if (result) {
|
117
|
+
return result;
|
118
|
+
}
|
119
|
+
}
|
120
|
+
}
|
121
|
+
}
|
@@ -0,0 +1,133 @@
|
|
1
|
+
import { ElementType } from 'react';
|
2
|
+
import classNames from 'classnames';
|
3
|
+
import Field, { FieldProps } from '../Field/Field';
|
4
|
+
import { FieldNameContext } from '../Field/FieldNameContext';
|
5
|
+
import useStandardFormInput from '../Field/useStandardField';
|
6
|
+
import { ValidationFunction } from '../Validation/ValidationFunction';
|
7
|
+
|
8
|
+
export type FieldArrayProps<
|
9
|
+
TForm extends object,
|
10
|
+
TProp extends keyof TForm
|
11
|
+
> = TForm[TProp] extends Array<any> | undefined
|
12
|
+
? {
|
13
|
+
name: TProp;
|
14
|
+
label?: string;
|
15
|
+
disabled?: boolean;
|
16
|
+
validate?:
|
17
|
+
| ValidationFunction<TForm[TProp]>
|
18
|
+
| ValidationFunction<TForm[TProp]>[];
|
19
|
+
children: (
|
20
|
+
formBuilder: ArrayFormBuilderProp<TForm[TProp]>
|
21
|
+
) => JSX.Element;
|
22
|
+
}
|
23
|
+
: never;
|
24
|
+
|
25
|
+
export type ArrayFormBuilderProp<TValue extends Array<any> | undefined> =
|
26
|
+
TValue extends Array<infer TForm> | undefined
|
27
|
+
? TForm extends object
|
28
|
+
? {
|
29
|
+
Field: <
|
30
|
+
TProp extends keyof TForm,
|
31
|
+
TRenderComponent extends ElementType
|
32
|
+
>(
|
33
|
+
props: FieldProps<TForm, TProp, TRenderComponent>
|
34
|
+
) => JSX.Element; // assumes this is never null - thought he final component may not render
|
35
|
+
|
36
|
+
FieldArray: <TProp extends keyof TForm>(
|
37
|
+
props: FieldArrayProps<TForm, TProp>
|
38
|
+
) => JSX.Element;
|
39
|
+
}
|
40
|
+
: never
|
41
|
+
: never;
|
42
|
+
|
43
|
+
export default function FieldArray<
|
44
|
+
TForm extends object,
|
45
|
+
TProp extends keyof TForm
|
46
|
+
>({
|
47
|
+
name,
|
48
|
+
label,
|
49
|
+
validate,
|
50
|
+
disabled,
|
51
|
+
children,
|
52
|
+
...rest
|
53
|
+
}: FieldArrayProps<TForm, TProp>) {
|
54
|
+
const [input, meta] = useStandardFormInput<TForm[TProp]>({
|
55
|
+
name: String(name),
|
56
|
+
validate: validate,
|
57
|
+
disabled: disabled,
|
58
|
+
});
|
59
|
+
|
60
|
+
const values: any[] = !input.value
|
61
|
+
? []
|
62
|
+
: Array.isArray(input.value)
|
63
|
+
? input.value
|
64
|
+
: [];
|
65
|
+
|
66
|
+
return (
|
67
|
+
<div className="field-array">
|
68
|
+
<div className="field-array-header">
|
69
|
+
<div className="field-array-title">{label}</div>
|
70
|
+
<button
|
71
|
+
className={classNames('add-array-item-button', { disabled })}
|
72
|
+
title="Add Item"
|
73
|
+
type="button"
|
74
|
+
onClick={addItem}>
|
75
|
+
+
|
76
|
+
</button>
|
77
|
+
</div>
|
78
|
+
<div className="field-array-body">
|
79
|
+
{values.map((value, index) => {
|
80
|
+
const itemName = `${input.name}[${index}]`;
|
81
|
+
return (
|
82
|
+
<div
|
83
|
+
key={
|
84
|
+
(value && value['form-input-array-key']) ||
|
85
|
+
(value && value['id']) ||
|
86
|
+
itemName
|
87
|
+
}
|
88
|
+
className={classNames('field-array-item', {
|
89
|
+
removed: value.isDeleted,
|
90
|
+
})}
|
91
|
+
role="listitem">
|
92
|
+
<FieldNameContext.Provider value={itemName}>
|
93
|
+
{children({
|
94
|
+
Field: Field,
|
95
|
+
FieldArray: FieldArray,
|
96
|
+
} as any)}
|
97
|
+
</FieldNameContext.Provider>
|
98
|
+
<button
|
99
|
+
className={classNames('remove-array-item-button', { disabled })}
|
100
|
+
type="button"
|
101
|
+
title="Remove Item"
|
102
|
+
onClick={() => removeItem(value)}>
|
103
|
+
-
|
104
|
+
</button>
|
105
|
+
</div>
|
106
|
+
);
|
107
|
+
})}
|
108
|
+
</div>
|
109
|
+
</div>
|
110
|
+
);
|
111
|
+
|
112
|
+
function addItem() {
|
113
|
+
if (disabled) {
|
114
|
+
return;
|
115
|
+
}
|
116
|
+
input.onChange([...values, {}] as any);
|
117
|
+
}
|
118
|
+
|
119
|
+
function removeItem(item: any) {
|
120
|
+
if (disabled) {
|
121
|
+
return;
|
122
|
+
}
|
123
|
+
// assumes anything from the server has an 'id' value sent
|
124
|
+
if (!item.id) {
|
125
|
+
input.onChange(values.filter((x) => x !== item) as any);
|
126
|
+
return;
|
127
|
+
}
|
128
|
+
const mapped = values.map((x) =>
|
129
|
+
x !== item ? x : Object.assign({}, x, { isDeleted: true })
|
130
|
+
);
|
131
|
+
input.onChange(mapped as any);
|
132
|
+
}
|
133
|
+
}
|
@@ -0,0 +1,35 @@
|
|
1
|
+
import React from 'react';
|
2
|
+
import { render } from '@testing-library/react';
|
3
|
+
import { FileGroup } from '../';
|
4
|
+
import FormTestBase from '../__Tests__/FormTestBase';
|
5
|
+
|
6
|
+
describe('FileGroup', () => {
|
7
|
+
it('renders without crashing', () => {
|
8
|
+
render(
|
9
|
+
<FormTestBase>
|
10
|
+
{({ Field }) => (
|
11
|
+
<Field
|
12
|
+
name="profileImage"
|
13
|
+
Component={FileGroup}
|
14
|
+
label="Profile Image"
|
15
|
+
/>
|
16
|
+
)}
|
17
|
+
</FormTestBase>
|
18
|
+
);
|
19
|
+
});
|
20
|
+
|
21
|
+
it('has matching snapshot', () => {
|
22
|
+
const renderResult = render(
|
23
|
+
<FormTestBase>
|
24
|
+
{({ Field }) => (
|
25
|
+
<Field
|
26
|
+
name="profileImage"
|
27
|
+
Component={FileGroup}
|
28
|
+
label="Profile Image"
|
29
|
+
/>
|
30
|
+
)}
|
31
|
+
</FormTestBase>
|
32
|
+
);
|
33
|
+
expect(renderResult.asFragment()).toMatchSnapshot();
|
34
|
+
});
|
35
|
+
});
|
@@ -0,0 +1,71 @@
|
|
1
|
+
import React, { ComponentType, LegacyRef } from 'react';
|
2
|
+
import classNames from 'classnames';
|
3
|
+
import FileList from './FileList';
|
4
|
+
import { InjectedFieldProps } from '../Field/InjectedFieldProps';
|
5
|
+
import Group, { GroupProps } from '../Group';
|
6
|
+
|
7
|
+
export interface FileGroupProps
|
8
|
+
// note: file props are of type "any" with the current type generation
|
9
|
+
extends InjectedFieldProps<any | undefined>,
|
10
|
+
Omit<GroupProps, keyof InjectedFieldProps<any> | 'children'>,
|
11
|
+
Omit<
|
12
|
+
React.HTMLProps<HTMLInputElement>,
|
13
|
+
keyof InjectedFieldProps<any> | 'children' | 'className' | 'label'
|
14
|
+
> {
|
15
|
+
multiple?: boolean | undefined;
|
16
|
+
}
|
17
|
+
|
18
|
+
function FileGroup(
|
19
|
+
{
|
20
|
+
input,
|
21
|
+
meta,
|
22
|
+
label,
|
23
|
+
helpText,
|
24
|
+
className,
|
25
|
+
multiple,
|
26
|
+
...rest
|
27
|
+
}: FileGroupProps,
|
28
|
+
ref: LegacyRef<HTMLInputElement>
|
29
|
+
) {
|
30
|
+
return (
|
31
|
+
<Group
|
32
|
+
input={input}
|
33
|
+
meta={meta}
|
34
|
+
label={label}
|
35
|
+
helpText={helpText}
|
36
|
+
className={classNames(className, { multiple }, 'file-group')}>
|
37
|
+
<input
|
38
|
+
{...input}
|
39
|
+
{...rest}
|
40
|
+
multiple={multiple}
|
41
|
+
onChange={(e) => {
|
42
|
+
if (!e?.target?.files?.length) {
|
43
|
+
input.onChange(undefined);
|
44
|
+
} else {
|
45
|
+
const files: File[] = [];
|
46
|
+
for (let i = 0; i < e.target.files.length; i++) {
|
47
|
+
files.push(e.target.files[i]);
|
48
|
+
}
|
49
|
+
if (!multiple) {
|
50
|
+
input.onChange(files[0]);
|
51
|
+
} else {
|
52
|
+
input.onChange(files);
|
53
|
+
}
|
54
|
+
}
|
55
|
+
}}
|
56
|
+
value={undefined}
|
57
|
+
ref={ref}
|
58
|
+
type="file"
|
59
|
+
className={classNames(className, 'file-group')}
|
60
|
+
/>
|
61
|
+
{/* Note: because input.value is any - due to how files are currently handled - type safeness isn't great here */}
|
62
|
+
<FileList files={input.value} />
|
63
|
+
</Group>
|
64
|
+
);
|
65
|
+
}
|
66
|
+
|
67
|
+
const FileGroupWithRef = React.forwardRef(
|
68
|
+
FileGroup
|
69
|
+
) as ComponentType<FileGroupProps>;
|
70
|
+
|
71
|
+
export default FileGroupWithRef;
|
@@ -0,0 +1,19 @@
|
|
1
|
+
export interface FileListProps {
|
2
|
+
files?: File | File[] | undefined;
|
3
|
+
rejectedFiles?: File | File[] | undefined;
|
4
|
+
}
|
5
|
+
export default function FileList({ files, rejectedFiles }: FileListProps) {
|
6
|
+
return (
|
7
|
+
<div className="file-list">
|
8
|
+
{!files ? null : Array.isArray(files) ? (
|
9
|
+
files.map((x, i) => <File file={x} key={i} />)
|
10
|
+
) : (
|
11
|
+
<File file={files} />
|
12
|
+
)}
|
13
|
+
</div>
|
14
|
+
);
|
15
|
+
}
|
16
|
+
|
17
|
+
function File({ file }: { file: File }) {
|
18
|
+
return null;
|
19
|
+
}
|
@@ -0,0 +1,33 @@
|
|
1
|
+
// Jest Snapshot v1, https://goo.gl/fbAQLP
|
2
|
+
|
3
|
+
exports[`FileGroup has matching snapshot 1`] = `
|
4
|
+
<DocumentFragment>
|
5
|
+
<form
|
6
|
+
action="#"
|
7
|
+
>
|
8
|
+
<div
|
9
|
+
class="file-group group"
|
10
|
+
>
|
11
|
+
<div
|
12
|
+
id="profileimage-error-scroll-target"
|
13
|
+
style="display: none;"
|
14
|
+
/>
|
15
|
+
<label
|
16
|
+
for="profileImage"
|
17
|
+
>
|
18
|
+
Profile Image
|
19
|
+
</label>
|
20
|
+
<input
|
21
|
+
class="file-group"
|
22
|
+
id="profileImage"
|
23
|
+
name="profileImage"
|
24
|
+
type="file"
|
25
|
+
value=""
|
26
|
+
/>
|
27
|
+
<div
|
28
|
+
class="file-list"
|
29
|
+
/>
|
30
|
+
</div>
|
31
|
+
</form>
|
32
|
+
</DocumentFragment>
|
33
|
+
`;
|
@@ -1,8 +1,12 @@
|
|
1
1
|
import { useEffect } from 'react';
|
2
2
|
import { useFormikContext } from 'formik';
|
3
3
|
import smoothscroll from 'smoothscroll-polyfill';
|
4
|
+
import { ServerErrorContextProps } from './ServerErrorContext';
|
4
5
|
|
5
|
-
export
|
6
|
+
export interface FocusErrorProps {
|
7
|
+
serverErrors: ServerErrorContextProps;
|
8
|
+
}
|
9
|
+
export default function FocusError(props: FocusErrorProps) {
|
6
10
|
const { errors, isSubmitting, isValidating } = useFormikContext();
|
7
11
|
smoothscroll.polyfill();
|
8
12
|
useEffect(() => {
|
@@ -28,14 +32,14 @@ export default function FocusError(props) {
|
|
28
32
|
return null;
|
29
33
|
}
|
30
34
|
|
31
|
-
const scrollToErrorElement = (keys) => {
|
32
|
-
let firstErrorElement = document.getElementById(
|
35
|
+
const scrollToErrorElement = (keys: string[]) => {
|
36
|
+
let firstErrorElement: HTMLElement | null = document.getElementById(
|
33
37
|
`${keys[0].toLowerCase()}-error-scroll-target`
|
34
38
|
);
|
35
|
-
if (firstErrorElement
|
39
|
+
if (!firstErrorElement || !firstErrorElement.parentNode) {
|
36
40
|
return;
|
37
41
|
}
|
38
|
-
firstErrorElement = firstErrorElement.parentNode;
|
42
|
+
firstErrorElement = firstErrorElement.parentNode as HTMLElement;
|
39
43
|
const headerOffset = -110;
|
40
44
|
const y =
|
41
45
|
firstErrorElement.getBoundingClientRect().top +
|
@@ -43,6 +47,6 @@ const scrollToErrorElement = (keys) => {
|
|
43
47
|
headerOffset;
|
44
48
|
window.scrollTo({ top: y, behavior: 'smooth' });
|
45
49
|
setTimeout(() => {
|
46
|
-
firstErrorElement
|
50
|
+
firstErrorElement?.focus();
|
47
51
|
}, 500);
|
48
52
|
};
|
@@ -0,0 +1,14 @@
|
|
1
|
+
import React from 'react';
|
2
|
+
import { render } from '@testing-library/react';
|
3
|
+
import FormTestBase from '../__Tests__/FormTestBase';
|
4
|
+
|
5
|
+
describe('FormTestBase', () => {
|
6
|
+
it('renders without crashing', () => {
|
7
|
+
render(<FormTestBase>{() => <></>}</FormTestBase>);
|
8
|
+
});
|
9
|
+
|
10
|
+
it('has matching snapshot', () => {
|
11
|
+
const renderResult = render(<FormTestBase>{() => <></>}</FormTestBase>);
|
12
|
+
expect(renderResult.asFragment()).toMatchSnapshot();
|
13
|
+
});
|
14
|
+
});
|
@@ -1,15 +1,56 @@
|
|
1
|
-
import
|
2
|
-
|
1
|
+
import {
|
2
|
+
CSSProperties,
|
3
|
+
ElementType,
|
4
|
+
useContext,
|
5
|
+
useMemo,
|
6
|
+
useState,
|
7
|
+
} from 'react';
|
8
|
+
import {
|
9
|
+
Form as FormikFormWrapper,
|
10
|
+
Formik,
|
11
|
+
FormikHelpers,
|
12
|
+
useFormikContext,
|
13
|
+
} from 'formik';
|
3
14
|
import FocusError from './FocusError';
|
4
|
-
import { Formik, Form as FormikFormWrapper, useFormikContext } from 'formik';
|
5
15
|
import FormBasedPreventNavigation from './FormBasedPreventNavigation';
|
6
|
-
import ServerErrorContext from './ServerErrorContext';
|
7
16
|
import {
|
8
|
-
|
9
|
-
|
10
|
-
|
17
|
+
ServerErrorContext,
|
18
|
+
ServerErrorContextProps,
|
19
|
+
ServerErrors,
|
20
|
+
} from './ServerErrorContext';
|
21
|
+
import Field, { FieldProps } from '../Field/Field';
|
22
|
+
import FieldArray, { FieldArrayProps } from '../FieldArray/FieldArray';
|
23
|
+
import objectContainsNonSerializableProperty from '../utils/objectContainsNonSerializableProperty';
|
24
|
+
import objectToFormData from '../utils/objectToFormData';
|
25
|
+
import { ValidatedApiResult } from '../Validation/ValidatedApiResult';
|
26
|
+
import { ValidationError } from '../Validation/ValidationError';
|
27
|
+
|
28
|
+
// This exposes the builder that ensures only "name" values on the given TForm can be used
|
29
|
+
// Further, each Field can then infer the proper type given the name
|
30
|
+
export type FormBuilderProp<TForm extends object> = {
|
31
|
+
// the first set of <> is a generic method signature - we don't split this off because we need the TForm closure
|
32
|
+
Field: <TProp extends keyof TForm, TRenderComponent extends ElementType>(
|
33
|
+
props: FieldProps<TForm, TProp, TRenderComponent>
|
34
|
+
) => JSX.Element; // assumes this is never null - thought he final component may not render
|
35
|
+
|
36
|
+
FieldArray: <TProp extends keyof TForm>(
|
37
|
+
props: FieldArrayProps<TForm, TProp>
|
38
|
+
) => JSX.Element;
|
39
|
+
};
|
11
40
|
|
12
|
-
export
|
41
|
+
export interface FormProps<TForm extends object> {
|
42
|
+
children: (formBuilder: FormBuilderProp<TForm>) => JSX.Element;
|
43
|
+
onSubmit: (
|
44
|
+
formValues: FormData | TForm,
|
45
|
+
formikBag: FormikHelpers<TForm>
|
46
|
+
) => Promise<ValidatedApiResult>;
|
47
|
+
className?: string;
|
48
|
+
style?: CSSProperties;
|
49
|
+
ignoreLostChanges?: boolean;
|
50
|
+
initialValues?: TForm;
|
51
|
+
}
|
52
|
+
|
53
|
+
export default function Form<TForm extends object>({
|
13
54
|
children,
|
14
55
|
className,
|
15
56
|
style,
|
@@ -17,13 +58,13 @@ export default function Form({
|
|
17
58
|
onSubmit,
|
18
59
|
initialValues,
|
19
60
|
...props
|
20
|
-
}) {
|
61
|
+
}: FormProps<TForm>) {
|
21
62
|
// formik resets all error on each blur (with our settings)
|
22
63
|
// this means that ALL errors from the server disappear when any one field is blurred
|
23
64
|
// So, we have to store server errors ourselves
|
24
65
|
// Since we only use useStandardFormInput, that means there is only one consumer
|
25
|
-
const [serverErrors, setServerErrors] = useState(
|
26
|
-
const serverErrorContextValue = useMemo(
|
66
|
+
const [serverErrors, setServerErrors] = useState<ServerErrors>({});
|
67
|
+
const serverErrorContextValue = useMemo<ServerErrorContextProps>(
|
27
68
|
() => ({
|
28
69
|
errors: serverErrors,
|
29
70
|
getError: (path) => {
|
@@ -47,21 +88,25 @@ export default function Form({
|
|
47
88
|
validateOnChange={false}
|
48
89
|
validateOnBlur={true}
|
49
90
|
validateOnMount={false}
|
50
|
-
initialValues={initialValues || {}}
|
91
|
+
initialValues={initialValues || ({} as TForm)}
|
51
92
|
onSubmit={handleSubmit}
|
52
93
|
{...props}>
|
53
94
|
<ServerErrorContext.Provider value={serverErrorContextValue}>
|
54
95
|
<FormikFormWrapper className={className} style={style}>
|
55
96
|
<FocusError serverErrors={serverErrorContextValue} />
|
56
97
|
<FormBasedPreventNavigation ignoreLostChanges={ignoreLostChanges} />
|
57
|
-
{
|
98
|
+
{children({
|
99
|
+
// hack for ref forwarding
|
100
|
+
Field: Field as any,
|
101
|
+
FieldArray: FieldArray as any,
|
102
|
+
})}
|
58
103
|
</FormikFormWrapper>
|
59
104
|
</ServerErrorContext.Provider>
|
60
105
|
</Formik>
|
61
106
|
);
|
62
107
|
|
63
|
-
function handleSubmit(values, formikBag) {
|
64
|
-
let formData =
|
108
|
+
function handleSubmit(values: TForm, formikBag: FormikHelpers<TForm>) {
|
109
|
+
let formData: FormData | undefined = undefined;
|
65
110
|
if (objectContainsNonSerializableProperty(values)) {
|
66
111
|
formData = objectToFormData(values, {
|
67
112
|
indices: true,
|
@@ -71,7 +116,7 @@ export default function Form({
|
|
71
116
|
});
|
72
117
|
}
|
73
118
|
|
74
|
-
return
|
119
|
+
return onSubmit(formData ?? values, formikBag)
|
75
120
|
.then((response) => {
|
76
121
|
return response;
|
77
122
|
})
|
@@ -84,17 +129,18 @@ export default function Form({
|
|
84
129
|
err.response.data.validationFailures
|
85
130
|
) {
|
86
131
|
try {
|
87
|
-
const serverErrors = err.response.data.validationFailures
|
88
|
-
|
89
|
-
// case insensitive path
|
90
|
-
path: error.propertyName.toLowerCase(),
|
91
|
-
errorMessage: error.errorMessage,
|
92
|
-
}))
|
93
|
-
.reduce((acc, value) => {
|
132
|
+
const serverErrors = err.response.data.validationFailures.reduce(
|
133
|
+
(acc: ServerErrors, value: ValidationError) => {
|
94
134
|
// for simplicity, just keep it to one server error at a time per path
|
95
|
-
|
135
|
+
// don't care of the property name case changes
|
136
|
+
const path = value.propertyName?.toLowerCase();
|
137
|
+
if (!!path && !!value.errorMessage) {
|
138
|
+
acc[path] = value.errorMessage;
|
139
|
+
}
|
96
140
|
return acc;
|
97
|
-
},
|
141
|
+
},
|
142
|
+
{} as ServerErrors
|
143
|
+
);
|
98
144
|
setServerErrors(serverErrors);
|
99
145
|
} catch (err) {
|
100
146
|
console.error('Failure to getErrorObject');
|
@@ -131,9 +177,3 @@ function DisplayFormState() {
|
|
131
177
|
</div>
|
132
178
|
);
|
133
179
|
}
|
134
|
-
|
135
|
-
Form.propTypes = {
|
136
|
-
ignoreLostChanges: PropTypes.bool,
|
137
|
-
onSubmit: PropTypes.func.isRequired,
|
138
|
-
initialValues: PropTypes.object,
|
139
|
-
};
|