Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(Validation): Add new Validation components #351

Merged
merged 9 commits into from
Jul 31, 2020
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,16 @@
"@types/react": "^16.9.0",
"@types/react-dom": "^16.9.0",
"@types/react-redux": "^7.1.9",
"@types/yup": "^0.29.3",
"formik": "^2.1.5",
"react": "^16.13.1",
"react-dom": "^16.13.1",
"react-redux": "^7.2.0",
"react-router-dom": "^5.2.0",
"react-scripts": "3.4.1",
"redux": "^4.0.5",
"typescript": "~3.9.7"
"typescript": "~3.9.7",
"yup": "^0.29.1"
},
"scripts": {
"start": "SKIP_PREFLIGHT_CHECK=true react-app-rewired start",
Expand Down
17 changes: 6 additions & 11 deletions example/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,14 @@ import {
import HomePage from './pages/Home'
import ExamplePage from './pages/Example'
import ModalsPage from './pages/Modals'
import FormsPage from './pages/Forms'
import { Routes } from './routes'

import './App.css'

/* Routes */
const routes = {
HOME_PAGE: '/',
EXAMPLES_PAGE: '/examples',
MODALS_PAGE: '/modals',
}

const App = () => {
const [mobileNavOpen, setMobileNavOpen] = useState(false)
const { HOME_PAGE, EXAMPLES_PAGE, MODALS_PAGE } = routes
const { HOME_PAGE, EXAMPLES_PAGE, MODALS_PAGE, FORMS_PAGE } = Routes

const toggleMobileNav = (): void => {
setMobileNavOpen((prevOpen) => !prevOpen)
Expand All @@ -46,9 +41,6 @@ const App = () => {
<NavLink to={EXAMPLES_PAGE} activeClassName="usa-current">
Examples
</NavLink>,
<NavLink to={MODALS_PAGE} activeClassName="usa-current">
Modals
</NavLink>,
]

return (
Expand Down Expand Up @@ -85,6 +77,9 @@ const App = () => {
<Route path={MODALS_PAGE}>
<ModalsPage />
</Route>
<Route path={FORMS_PAGE}>
<FormsPage />
</Route>
<Route path={HOME_PAGE}>
<HomePage />
</Route>
Expand Down
28 changes: 19 additions & 9 deletions example/src/pages/Example.tsx
Original file line number Diff line number Diff line change
@@ -1,21 +1,31 @@
import React from 'react'
import { Link } from 'react-router-dom'
import { Link as USWDSLink } from '@trussworks/react-uswds'

const ExamplePage = (): React.ReactElement => (
<section>
<h1>Examples</h1>

<p className="usa-intro">
Right now there are no examples! Things that we could add include...
</p>
<p className="usa-intro">Example usage of react-uswds in real life!</p>

<ul className="usa-list">
<li>Modals</li>
<li>
Form libraries
<ul className="usa-list">
<li>Formik</li>
<li>React-hook-form</li>
</ul>
<USWDSLink
to="/forms"
asCustom={Link}
variant="external"
target="_blank">
Forms
</USWDSLink>
</li>
<li>
<USWDSLink
to="/modals"
asCustom={Link}
variant="external"
target="_blank">
Modals
</USWDSLink>
</li>
</ul>
</section>
Expand Down
100 changes: 100 additions & 0 deletions example/src/pages/Forms.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
import React, { useState } from 'react'
import { Field, Formik } from 'formik'
import * as Yup from 'yup'
import {
Alert,
Button,
Form,
Label,
TextInput,
ValidationChecklist,
ValidationItem,
} from '@trussworks/react-uswds'

type FormValues = {
email?: string
password?: string
}

const FormSchema = Yup.object().shape({
email: Yup.string().email().required(),
password: Yup.string().min(8).max(20).required(),
})

const FormsPage = (): React.ReactElement => {
return (
<>
<h1>Forms Examples</h1>
<section>
<h2>Formik</h2>
<Formik
initialValues={{ email: '', password: '' }}
validationSchema={FormSchema}
onSubmit={(values, { setSubmitting }) => {
setTimeout(() => {
alert(JSON.stringify(values, null, 2))

setSubmitting(false)
}, 400)
}}>
{({ values, errors, touched, handleSubmit, isSubmitting }) => (
<Form onSubmit={handleSubmit}>
{touched.email && errors.email && (
<Alert type="info" validation heading="Email Requirements">
<ValidationChecklist id="validate-email">
<ValidationItem
id="required"
isValid={values.email.length > 0}>
Include required field.
</ValidationItem>
<ValidationItem id="validFormat" isValid={!errors.email}>
Use only valid email formatting
</ValidationItem>
{errors.email}
</ValidationChecklist>
</Alert>
)}
<Label htmlFor="email">Email</Label>
<Field
as={TextInput}
type="email"
name="email"
id="email"
value={values.email}
/>

{touched.password && errors.password && (
<Alert type="info" validation heading="Password Requirements">
<ValidationChecklist id="validate-password">
<ValidationItem
id="required"
isValid={values.password.length > 0}>
Copy link
Contributor Author

@haworku haworku Jul 27, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ideally isValid would use different logic than this - just did something simple for the example. Open to other ways to do this. The alternative I found was writing a long validate function that digs inside yup's error object and manually updated bits of component state. It seemed like overkill for this example.

react hook form seems to handle this better than formik... couldn't find a similar formik util.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ahh I see, yeah I wouldn't worry too much about this since it's really a Formik implementation detail..

Include required field.
</ValidationItem>
<ValidationItem id="length" isValid={!errors.password}>
Length is between 8-20 characters
</ValidationItem>
</ValidationChecklist>
</Alert>
)}
<Label htmlFor="password">Password</Label>
<Field
as={TextInput}
type="password"
name="password"
id="password"
value={values.password}
/>

<Button type="submit" disabled={isSubmitting}>
Submit
</Button>
</Form>
)}
</Formik>
</section>
</>
)
}

export default FormsPage
6 changes: 1 addition & 5 deletions example/src/pages/Home.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,7 @@ const HomePage = (): React.ReactElement => (
<p className="usa-intro">
This is an example application that can be used to demonstrate and test
ReactUSWDS functionality. Here's a{' '}
<USWDSLink
to="/examples"
asCustom={Link}
variant="external"
target="_blank">
<USWDSLink to="/examples" asCustom={Link}>
link to the examples page!
</USWDSLink>
</p>
Expand Down
8 changes: 8 additions & 0 deletions example/src/routes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const Routes = {
HOME_PAGE: '/',
EXAMPLES_PAGE: '/examples',
FORMS_PAGE: '/forms',
MODALS_PAGE: '/modals',
}

export { Routes }
79 changes: 77 additions & 2 deletions example/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1067,7 +1067,7 @@
dependencies:
regenerator-runtime "^0.13.4"

"@babel/runtime@>=7.0.0", "@babel/runtime@^7.0.0", "@babel/runtime@^7.1.2", "@babel/runtime@^7.10.2", "@babel/runtime@^7.10.3", "@babel/runtime@^7.3.4", "@babel/runtime@^7.4.5", "@babel/runtime@^7.5.5", "@babel/runtime@^7.7.2", "@babel/runtime@^7.8.4", "@babel/runtime@^7.9.2":
"@babel/runtime@>=7.0.0", "@babel/runtime@^7.0.0", "@babel/runtime@^7.1.2", "@babel/runtime@^7.10.2", "@babel/runtime@^7.10.3", "@babel/runtime@^7.3.4", "@babel/runtime@^7.4.5", "@babel/runtime@^7.5.5", "@babel/runtime@^7.7.2", "@babel/runtime@^7.8.4", "@babel/runtime@^7.9.2", "@babel/runtime@^7.9.6":
version "7.10.5"
resolved "https://registry.yarnpkg.com/@babel/runtime/-/runtime-7.10.5.tgz#303d8bd440ecd5a491eae6117fd3367698674c5c"
integrity sha512-otddXKhdNn7d0ptoFRHtMLa8LqDxLYwTjB4nYgM1yy5N6gU/MUf8zqyyLltCH3yAVitBzmwK4us+DD0l/MauAg==
Expand Down Expand Up @@ -1704,6 +1704,11 @@
dependencies:
"@types/yargs-parser" "*"

"@types/yup@^0.29.3":
version "0.29.3"
resolved "https://registry.yarnpkg.com/@types/yup/-/yup-0.29.3.tgz#5a85024796bffe0eb01601bfc180fe218356dba4"
integrity sha512-XxZFKnxzTfm+DR8MMBA35UUXfUPmjPpi8HJ90VZg7q/LIbtiOhVGJ26gNnATcflcpnIyf2Qm9A+oEhswaqoDpA==

"@typescript-eslint/eslint-plugin@^2.10.0":
version "2.34.0"
resolved "https://registry.yarnpkg.com/@typescript-eslint/eslint-plugin/-/eslint-plugin-2.34.0.tgz#6f8ce8a46c7dea4a6f1d171d2bb8fbae6dac2be9"
Expand Down Expand Up @@ -3789,6 +3794,11 @@ deep-is@~0.1.3:
resolved "https://registry.yarnpkg.com/deep-is/-/deep-is-0.1.3.tgz#b369d6fb5dbc13eecf524f91b070feedc357cf34"
integrity sha1-s2nW+128E+7PUk+RsHD+7cNXzzQ=

deepmerge@^2.1.1:
version "2.2.1"
resolved "https://registry.yarnpkg.com/deepmerge/-/deepmerge-2.2.1.tgz#5d3ff22a01c00f645405a2fbc17d0778a1801170"
integrity sha512-R9hc1Xa/NOBi9WRVUWg19rl1UB7Tt4kuPd+thNJgFZoxXsTz7ncaPaeIm+40oSGuP33DfMb4sZt1QIGiJzC4EA==

default-gateway@^4.2.0:
version "4.2.0"
resolved "https://registry.yarnpkg.com/default-gateway/-/default-gateway-4.2.0.tgz#167104c7500c2115f6dd69b0a536bb8ed720552b"
Expand Down Expand Up @@ -4880,6 +4890,11 @@ flush-write-stream@^1.0.0:
inherits "^2.0.3"
readable-stream "^2.3.6"

fn-name@~3.0.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/fn-name/-/fn-name-3.0.0.tgz#0596707f635929634d791f452309ab41558e3c5c"
integrity sha512-eNMNr5exLoavuAMhIUVsOKF79SWd/zG104ef6sxBTSw+cZc6BXdQXDvYcGvp0VbxVVSp1XDUNoz7mg1xMtSznA==

follow-redirects@^1.0.0:
version "1.12.1"
resolved "https://registry.yarnpkg.com/follow-redirects/-/follow-redirects-1.12.1.tgz#de54a6205311b93d60398ebc01cf7015682312b6"
Expand Down Expand Up @@ -4930,6 +4945,20 @@ form-data@~2.3.2:
combined-stream "^1.0.6"
mime-types "^2.1.12"

formik@^2.1.5:
version "2.1.5"
resolved "https://registry.yarnpkg.com/formik/-/formik-2.1.5.tgz#de5bbbe35543fa6d049fe96b8ee329d6cd6892b8"
integrity sha512-bWpo3PiqVDYslvrRjTq0Isrm0mFXHiO33D8MS6t6dWcqSFGeYF52nlpCM2xwOJ6tRVRznDkL+zz/iHPL4LDuvQ==
dependencies:
deepmerge "^2.1.1"
hoist-non-react-statics "^3.3.0"
lodash "^4.17.14"
lodash-es "^4.17.14"
react-fast-compare "^2.0.1"
scheduler "^0.18.0"
tiny-warning "^1.0.2"
tslib "^1.10.0"

forwarded@~0.1.2:
version "0.1.2"
resolved "https://registry.yarnpkg.com/forwarded/-/forwarded-0.1.2.tgz#98c23dab1175657b8c0573e8ceccd91b0ff18c84"
Expand Down Expand Up @@ -6874,6 +6903,11 @@ locate-path@^5.0.0:
dependencies:
p-locate "^4.1.0"

lodash-es@^4.17.11, lodash-es@^4.17.14:
version "4.17.15"
resolved "https://registry.yarnpkg.com/lodash-es/-/lodash-es-4.17.15.tgz#21bd96839354412f23d7a10340e5eac6ee455d78"
integrity sha512-rlrc3yU3+JNOpZ9zj5pQtxnx2THmvRykwL4Xlxoa8I9lHBlVbbyPhgyPMioxVZ4NqyxaVVtaJnzsyOidQIhyyQ==

lodash._reinterpolate@^3.0.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/lodash._reinterpolate/-/lodash._reinterpolate-3.0.0.tgz#0ccf2d89166af03b3663c796538b75ac6e114d9d"
Expand Down Expand Up @@ -8846,6 +8880,11 @@ prop-types@^15.6.2, prop-types@^15.7.2:
object-assign "^4.1.1"
react-is "^16.8.1"

property-expr@^2.0.2:
version "2.0.2"
resolved "https://registry.yarnpkg.com/property-expr/-/property-expr-2.0.2.tgz#fff2a43919135553a3bc2fdd94bdb841965b2330"
integrity sha512-bc/5ggaYZxNkFKj374aLbEDqVADdYaLcFo8XBkishUWbaAdjlphaBFns9TvRA2pUseVL/wMFmui9X3IdNDU37g==

proxy-addr@~2.0.5:
version "2.0.6"
resolved "https://registry.yarnpkg.com/proxy-addr/-/proxy-addr-2.0.6.tgz#fdc2336505447d3f2f2c638ed272caf614bbb2bf"
Expand Down Expand Up @@ -9055,6 +9094,11 @@ react-error-overlay@^6.0.7:
resolved "https://registry.yarnpkg.com/react-error-overlay/-/react-error-overlay-6.0.7.tgz#1dcfb459ab671d53f660a991513cb2f0a0553108"
integrity sha512-TAv1KJFh3RhqxNvhzxj6LeT5NWklP6rDr2a0jaTfsZ5wSZWHOGeqQyejUp3xxLfPt2UpyJEcVQB/zyPcmonNFA==

react-fast-compare@^2.0.1:
version "2.0.4"
resolved "https://registry.yarnpkg.com/react-fast-compare/-/react-fast-compare-2.0.4.tgz#e84b4d455b0fec113e0402c329352715196f81f9"
integrity sha512-suNP+J1VU1MWFKcyt7RtjiSWUjvidmQSlqu+eHslq+342xCbGTYmC0mEhPCOHxlW0CywylOC1u2DFAT+bv4dBw==

react-is@^16.12.0, react-is@^16.6.0, react-is@^16.7.0, react-is@^16.8.1, react-is@^16.8.4, react-is@^16.9.0:
version "16.13.1"
resolved "https://registry.yarnpkg.com/react-is/-/react-is-16.13.1.tgz#789729a4dc36de2999dc156dd6c1d9c18cea56a4"
Expand Down Expand Up @@ -9694,6 +9738,14 @@ saxes@^3.1.9:
dependencies:
xmlchars "^2.1.1"

scheduler@^0.18.0:
version "0.18.0"
resolved "https://registry.yarnpkg.com/scheduler/-/scheduler-0.18.0.tgz#5901ad6659bc1d8f3fdaf36eb7a67b0d6746b1c4"
integrity sha512-agTSHR1Nbfi6ulI0kYNK0203joW2Y5W4po4l+v03tOoiJKpTBbxpNhWDvqc/4IcOw+KLmSiQLTasZ4cab2/UWQ==
dependencies:
loose-envify "^1.1.0"
object-assign "^4.1.1"

scheduler@^0.19.1:
version "0.19.1"
resolved "https://registry.yarnpkg.com/scheduler/-/scheduler-0.19.1.tgz#4f3e2ed2c1a7d65681f4c854fa8c5a1ccb40f196"
Expand Down Expand Up @@ -10441,6 +10493,11 @@ symbol-tree@^3.2.2:
resolved "https://registry.yarnpkg.com/symbol-tree/-/symbol-tree-3.2.4.tgz#430637d248ba77e078883951fb9aa0eed7c63fa2"
integrity sha512-9QNk5KwDF+Bvz+PyObkmSYjI5ksVUYtjW7AU22r2NKcfLJcXp96hkDWU3+XndOsUb+AQ9QhfzfCT2O+CNWT5Tw==

synchronous-promise@^2.0.10:
version "2.0.13"
resolved "https://registry.yarnpkg.com/synchronous-promise/-/synchronous-promise-2.0.13.tgz#9d8c165ddee69c5a6542862b405bc50095926702"
integrity sha512-R9N6uDkVsghHePKh1TEqbnLddO2IY25OcsksyFp/qBe7XYd0PVbKEWxhcdMhpLzE1I6skj5l4aEZ3CRxcbArlA==

table@^5.2.3:
version "5.4.6"
resolved "https://registry.yarnpkg.com/table/-/table-5.4.6.tgz#1292d19500ce3f86053b05f0e8e7e4a3bb21079e"
Expand Down Expand Up @@ -10561,7 +10618,7 @@ tiny-invariant@^1.0.2:
resolved "https://registry.yarnpkg.com/tiny-invariant/-/tiny-invariant-1.1.0.tgz#634c5f8efdc27714b7f386c35e6760991d230875"
integrity sha512-ytxQvrb1cPc9WBEI/HSeYYoGD0kWnGEOR8RY6KomWLBVhqz0RgTwVO9dLrGz7dC+nN9llyI7OKAgRq8Vq4ZBSw==

tiny-warning@^1.0.0, tiny-warning@^1.0.3:
tiny-warning@^1.0.0, tiny-warning@^1.0.2, tiny-warning@^1.0.3:
version "1.0.3"
resolved "https://registry.yarnpkg.com/tiny-warning/-/tiny-warning-1.0.3.tgz#94a30db453df4c643d0fd566060d60a875d84754"
integrity sha512-lBN9zLN/oAf68o3zNXYrdCt1kP8WsiGW8Oo2ka41b2IM5JL/S1CTyX1rW0mb/zSuJun0ZUrDxx4sqvYS2FWzPA==
Expand Down Expand Up @@ -10625,6 +10682,11 @@ toidentifier@1.0.0:
resolved "https://registry.yarnpkg.com/toidentifier/-/toidentifier-1.0.0.tgz#7e1be3470f1e77948bc43d94a3c8f4d7752ba553"
integrity sha512-yaOH/Pk/VEhBWWTlhI+qXxDFXlejDGcQipMlyxda9nthulaxLZUNcUqFxokp0vcYnvteJln5FNQDRrxj3YcbVw==

toposort@^2.0.2:
version "2.0.2"
resolved "https://registry.yarnpkg.com/toposort/-/toposort-2.0.2.tgz#ae21768175d1559d48bef35420b2f4962f09c330"
integrity sha1-riF2gXXRVZ1IvvNUILL0li8JwzA=

tough-cookie@^2.3.3, tough-cookie@^2.3.4, tough-cookie@^2.5.0, tough-cookie@~2.5.0:
version "2.5.0"
resolved "https://registry.yarnpkg.com/tough-cookie/-/tough-cookie-2.5.0.tgz#cd9fb2a0aa1d5a12b473bd9fb96fa3dcff65ade2"
Expand Down Expand Up @@ -11483,6 +11545,19 @@ yargs@^13.3.0:
y18n "^4.0.0"
yargs-parser "^13.1.2"

yup@^0.29.1:
version "0.29.1"
resolved "https://registry.yarnpkg.com/yup/-/yup-0.29.1.tgz#35d25aab470a0c3950f66040ba0ff4b1b6efe0d9"
integrity sha512-U7mPIbgfQWI6M3hZCJdGFrr+U0laG28FxMAKIgNvgl7OtyYuUoc4uy9qCWYHZjh49b8T7Ug8NNDdiMIEytcXrQ==
dependencies:
"@babel/runtime" "^7.9.6"
fn-name "~3.0.0"
lodash "^4.17.15"
lodash-es "^4.17.11"
property-expr "^2.0.2"
synchronous-promise "^2.0.10"
toposort "^2.0.2"

zip-stream@^2.1.2:
version "2.1.3"
resolved "https://registry.yarnpkg.com/zip-stream/-/zip-stream-2.1.3.tgz#26cc4bdb93641a8590dd07112e1f77af1758865b"
Expand Down
Loading