-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #166 from adhocteam/js-6-frontend-validations
Add validations and error messages to the activity report
- Loading branch information
Showing
29 changed files
with
624 additions
and
340 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,3 +14,9 @@ | |
.DateInput { | ||
width: fit-content; | ||
} | ||
|
||
.usa-hint { | ||
font-size: 14px; | ||
margin-top: 5px; | ||
margin-bottom: 10px; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
.smart-hub--form-required { | ||
font-family: SourceSansPro; | ||
font-size: 16px; | ||
color: #d42240; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
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, Fieldset, | ||
} from '@trussworks/react-uswds'; | ||
|
||
import './FormItem.css'; | ||
|
||
const labelPropTypes = { | ||
label: PropTypes.node.isRequired, | ||
children: PropTypes.node.isRequired, | ||
}; | ||
|
||
function Checkbox({ label, children }) { | ||
return ( | ||
<Fieldset unstyled> | ||
<legend>{label}</legend> | ||
{children} | ||
</Fieldset> | ||
); | ||
} | ||
|
||
Checkbox.propTypes = labelPropTypes; | ||
|
||
function Field({ label, children }) { | ||
return ( | ||
<Label> | ||
{label} | ||
{children} | ||
</Label> | ||
); | ||
} | ||
|
||
Field.propTypes = labelPropTypes; | ||
|
||
function FormItem({ | ||
label, children, required, name, isCheckbox, | ||
}) { | ||
const { formState: { errors } } = useFormContext(); | ||
const fieldErrors = errors[name]; | ||
const labelWithRequiredTag = ( | ||
<> | ||
{label} | ||
{required && (<span className="smart-hub--form-required"> (Required)</span>)} | ||
</> | ||
); | ||
|
||
const LabelType = isCheckbox ? Checkbox : Field; | ||
|
||
return ( | ||
<FormGroup error={fieldErrors}> | ||
<LabelType label={labelWithRequiredTag}> | ||
<ReactHookFormError | ||
errors={errors} | ||
name={name} | ||
render={({ message }) => <ErrorMessage>{message}</ErrorMessage>} | ||
/> | ||
{children} | ||
</LabelType> | ||
</FormGroup> | ||
); | ||
} | ||
|
||
FormItem.propTypes = { | ||
label: PropTypes.string.isRequired, | ||
children: PropTypes.node.isRequired, | ||
name: PropTypes.string.isRequired, | ||
isCheckbox: PropTypes.bool, | ||
required: PropTypes.bool, | ||
}; | ||
|
||
FormItem.defaultProps = { | ||
required: true, | ||
isCheckbox: false, | ||
}; | ||
|
||
export default FormItem; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.