Skip to content

Commit

Permalink
Validate React Forms with Formik and Yup
Browse files Browse the repository at this point in the history
  • Loading branch information
DevanB committed Mar 27, 2019
1 parent 6237776 commit 2efd354
Show file tree
Hide file tree
Showing 11 changed files with 10,518 additions and 0 deletions.
1 change: 1 addition & 0 deletions validate-react-forms-with-formik-and-yup/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
17 changes: 17 additions & 0 deletions validate-react-forms-with-formik-and-yup/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Validate React Forms with Formik and Yup

[Watch the lesson](https://egghead.io/lessons/react-simplify-and-convert-a-traditional-react-form-to-formik)

# Running

1. Install dependencies

```sh
yarn
```

2. Run the develop script

```sh
yarn start
```
34 changes: 34 additions & 0 deletions validate-react-forms-with-formik-and-yup/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{
"name": "validate-react-forms-with-formik-and-yup",
"version": "1.0.0",
"description": "An example form with validations built with Formik, Yup, and React",
"license": "MIT",
"keywords": [
"formik",
"yup",
"react"
],
"homepage": "https://codesandbox.io/s/",
"main": "index.js",
"dependencies": {
"formik": "1.5.1",
"react": "16.8.5",
"react-dom": "16.8.5",
"yup": "0.27.0"
},
"devDependencies": {
"react-scripts": "2.1.8"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test --env=jsdom",
"eject": "react-scripts eject"
},
"browserslist": [
">0.2%",
"not dead",
"not ie <= 11",
"not op_mini all"
]
}
Binary file not shown.
38 changes: 38 additions & 0 deletions validate-react-forms-with-formik-and-yup/public/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<meta name="theme-color" content="#000000">
<!--
manifest.json provides metadata used when your web app is added to the
homescreen on Android. See https://developers.google.com/web/fundamentals/engage-and-retain/web-app-manifest/
-->
<link rel="manifest" href="%PUBLIC_URL%/manifest.json">
<link rel="shortcut icon" href="%PUBLIC_URL%/favicon.ico">
<!--
Notice the use of %PUBLIC_URL% in the tags above.
It will be replaced with the URL of the `public` folder during the build.
Only files inside the `public` folder can be referenced from the HTML.
Unlike "/favicon.ico" or "favicon.ico", "%PUBLIC_URL%/favicon.ico" will
work correctly both with client-side routing and a non-root public URL.
Learn how to configure a non-root public URL by running `npm run build`.
-->
<title>React App</title>

</head>
<body>
<div id="root"></div>
<!--
This HTML file is a template.
If you open it directly in the browser, you will see an empty page.
You can add webfonts, meta tags, or analytics to this file.
The build step will place the bundled scripts into the <body> tag.
To begin the development, run `npm start` or `yarn start`.
To create a production bundle, use `npm run build` or `yarn build`.
-->
</body>
</html>
15 changes: 15 additions & 0 deletions validate-react-forms-with-formik-and-yup/public/manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"short_name": "React App",
"name": "Create React App Sample",
"icons": [
{
"src": "favicon.ico",
"sizes": "192x192",
"type": "image/png"
}
],
"start_url": "./index.html",
"display": "standalone",
"theme_color": "#000000",
"background_color": "#ffffff"
}
11 changes: 11 additions & 0 deletions validate-react-forms-with-formik-and-yup/src/ItemList.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import React from "react";

const ItemList = ({ items }) => (
<ul>
{items.map(item => (
<li key={item}>{item}</li>
))}
</ul>
);

export default ItemList;
47 changes: 47 additions & 0 deletions validate-react-forms-with-formik-and-yup/src/index.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
body {
-moz-osx-font-smoothing: grayscale;
-webkit-font-smoothing: antialiased;
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Roboto", "Oxygen",
"Ubuntu", "Cantarell", "Fira Sans", "Droid Sans", "Helvetica Neue",
sans-serif;
margin: 0 auto;
max-width: 25em;
padding: 0 2em;
}
button,
input[type="submit"] {
background: white;
border-radius: 0.25em;
border: 1px solid black;
cursor: pointer;
display: inline-block;
font-size: 1em;
margin-top: 2em;
padding: 0.5em 1em;
transition: all 250ms ease;
}
button:hover,
button:focus,
input[type="submit"]:hover,
input[type="submit"]:focus {
background: hsl(0, 0%, 90%);
}
form {
margin-top: 2em;
width: 100%;
}
form * {
display: block;
}
input[type="text"] {
font-size: 1em;
margin-bottom: 1em;
padding: 1em 0.5em;
}
label {
margin-bottom: 0.25em;
}
ul {
background: hsl(0, 0%, 90%);
padding: 1em 1em 1em 2em;
}
43 changes: 43 additions & 0 deletions validate-react-forms-with-formik-and-yup/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { ErrorMessage, Field, Form, Formik } from 'formik';
import React from 'react';
import { render } from 'react-dom';
import './index.css';
import ItemList from './ItemList';
import * as Yup from 'yup';

const initialValues = {
item: '',
};

const validationSchema = Yup.object().shape({
item: Yup.string().required('Item name is required'),
});

const App = () => {
const [items, setItems] = React.useState([]);

return (
<React.Fragment>
<h2>Regular Maintenance:</h2>
<ItemList items={items} />
<Formik
initialValues={initialValues}
validationSchema={validationSchema}
onSubmit={values => {
setItems([...items, values.item]);
}}
>
<Form>
<label htmlFor="item">Item:</label>
<Field type="text" name="item" />
<ErrorMessage name="item" />
<button type="submit">Add Item</button>
</Form>
</Formik>
</React.Fragment>
);
};

export default App;

render(<App />, document.getElementById('root'));
31 changes: 31 additions & 0 deletions validate-react-forms-with-formik-and-yup/src/index.old.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { Field, Form, Formik } from "formik";
import React from "react";
import { render } from "react-dom";
import "./index.css";
import ItemList from "./ItemList";

const App = () => {
const [items, setItems] = React.useState([]);

return (
<React.Fragment>
<h2>Regular Maintenance:</h2>
<ItemList items={items} />
<Formik
onSubmit={values => {
setItems([...items, values.item]);
}}
>
<Form>
<label htmlFor="item">Item:</label>
<Field type="text" name="item" />
<button type="submit">Add Item</button>
</Form>
</Formik>
</React.Fragment>
);
};

export default App;

render(<App />, document.getElementById("root"));
Loading

0 comments on commit 2efd354

Please sign in to comment.