From 13d8829ab6284c48747842d3aa1370bfac26fda4 Mon Sep 17 00:00:00 2001 From: Josh Salisbury Date: Wed, 10 Feb 2021 15:36:05 -0600 Subject: [PATCH 1/7] Error messages/validations added to form Waiting for the recent updates to the review pages before moving forward more with this work --- frontend/package.json | 3 +- frontend/src/components/DatePicker.css | 6 + frontend/src/components/DatePicker.js | 11 +- frontend/src/components/FormItem.css | 0 frontend/src/components/FormItem.js | 42 +++ frontend/src/components/MultiSelect.js | 84 +++-- frontend/src/components/Navigator/index.js | 36 +- .../ActivityReport/Pages/ReviewSubmit.js | 16 +- .../Pages/SubmitterReviewPage.js | 92 ++++-- .../ActivityReport/Pages/activitySummary.js | 310 +++++++++++------- .../src/pages/ActivityReport/Pages/index.js | 4 +- .../ActivityReport/Pages/topicsResources.js | 23 +- frontend/yarn.lock | 13 +- 13 files changed, 404 insertions(+), 236 deletions(-) create mode 100644 frontend/src/components/FormItem.css create mode 100644 frontend/src/components/FormItem.js diff --git a/frontend/package.json b/frontend/package.json index 9269b1d928..13b13d0ad8 100644 --- a/frontend/package.json +++ b/frontend/package.json @@ -8,6 +8,7 @@ "@fortawesome/fontawesome-svg-core": "^1.2.32", "@fortawesome/free-solid-svg-icons": "^5.15.1", "@fortawesome/react-fontawesome": "^0.1.11", + "@hookform/error-message": "^0.0.5", "@testing-library/jest-dom": "^4.2.4", "@trussworks/react-uswds": "^1.9.1", "@use-it/interval": "^1.0.0", @@ -21,7 +22,7 @@ "react-dom": "^16.14.0", "react-dropzone": "^11.2.0", "react-helmet": "^6.1.0", - "react-hook-form": "^6.9.0", + "react-hook-form": "^6.15.0", "react-idle-timer": "^4.4.2", "react-input-autosize": "^3.0.0", "react-responsive": "^8.1.1", diff --git a/frontend/src/components/DatePicker.css b/frontend/src/components/DatePicker.css index 6539117efa..1586359b58 100644 --- a/frontend/src/components/DatePicker.css +++ b/frontend/src/components/DatePicker.css @@ -14,3 +14,9 @@ .DateInput { width: fit-content; } + +.usa-hint { + font-size: 14px; + margin-top: 5px; + margin-bottom: 10px; +} \ No newline at end of file diff --git a/frontend/src/components/DatePicker.js b/frontend/src/components/DatePicker.js index beab959f6c..65a499c64d 100644 --- a/frontend/src/components/DatePicker.js +++ b/frontend/src/components/DatePicker.js @@ -10,10 +10,8 @@ 2. react-dates had easily readable documentation and conveniences such as `maxDate` and `minDate`. I couldn't find great docs using the USWDS datepicker javascript */ - import React, { useState } from 'react'; import PropTypes from 'prop-types'; -import { Label } from '@trussworks/react-uswds'; import { SingleDatePicker } from 'react-dates'; import { OPEN_UP, OPEN_DOWN } from 'react-dates/constants'; import { Controller } from 'react-hook-form'; @@ -24,9 +22,8 @@ import './DatePicker.css'; const dateFmt = 'MM/DD/YYYY'; const DateInput = ({ - control, label, minDate, name, disabled, maxDate, openUp, required, + control, minDate, name, disabled, maxDate, openUp, required, }) => { - const labelId = `${name}-id`; const hintId = `${name}-hint`; const [isFocused, updateFocus] = useState(false); const openDirection = openUp ? OPEN_UP : OPEN_DOWN; @@ -40,8 +37,7 @@ const DateInput = ({ return ( <> - -
mm/dd/yyyy
+
mm/dd/yyyy
{ const date = value ? moment(value, dateFmt) : null; @@ -68,7 +64,7 @@ const DateInput = ({ disabled={disabled} defaultValue={null} rules={{ - required, + required: required ? 'Please select a date' : false, }} /> @@ -80,7 +76,6 @@ DateInput.propTypes = { // eslint-disable-next-line react/forbid-prop-types control: PropTypes.object.isRequired, name: PropTypes.string.isRequired, - label: PropTypes.string.isRequired, minDate: PropTypes.string, maxDate: PropTypes.string, openUp: PropTypes.bool, diff --git a/frontend/src/components/FormItem.css b/frontend/src/components/FormItem.css new file mode 100644 index 0000000000..e69de29bb2 diff --git a/frontend/src/components/FormItem.js b/frontend/src/components/FormItem.js new file mode 100644 index 0000000000..a1538943fa --- /dev/null +++ b/frontend/src/components/FormItem.js @@ -0,0 +1,42 @@ +import React from 'react'; +import PropTypes from 'prop-types'; +import { useFormContext } from 'react-hook-form'; +import { ErrorMessage as ReactHookFormError } from '@hookform/error-message'; +import { Label, FormGroup, ErrorMessage } from '@trussworks/react-uswds'; + +import './FormItem.css'; + +function FormItem({ + label, children, required, name, +}) { + const { formState: { errors } } = useFormContext(); + const fieldErrors = errors[name]; + + return ( + + + + ); +} + +FormItem.propTypes = { + label: PropTypes.string.isRequired, + children: PropTypes.node.isRequired, + name: PropTypes.isRequired, + required: PropTypes.bool, +}; + +FormItem.defaultProps = { + required: true, +}; + +export default FormItem; diff --git a/frontend/src/components/MultiSelect.js b/frontend/src/components/MultiSelect.js index 68c79d3717..69bc424515 100644 --- a/frontend/src/components/MultiSelect.js +++ b/frontend/src/components/MultiSelect.js @@ -23,7 +23,6 @@ import React from 'react'; import PropTypes from 'prop-types'; import Select, { components } from 'react-select'; -import { Label } from '@trussworks/react-uswds'; import { Controller } from 'react-hook-form'; import arrowBoth from '../images/arrow-both.svg'; @@ -73,7 +72,6 @@ const styles = { }; function MultiSelect({ - label, name, options, disabled, @@ -117,44 +115,45 @@ function MultiSelect({ }; return ( -