├── formik-controls-demo
├── .gitignore
├── README.md
├── package.json
├── public
│ ├── favicon.ico
│ ├── index.html
│ ├── logo192.png
│ ├── logo512.png
│ ├── manifest.json
│ └── robots.txt
├── src
│ ├── App.css
│ ├── App.js
│ ├── App.test.js
│ ├── components
│ │ ├── ChakraInput.js
│ │ ├── CheckboxGroup.js
│ │ ├── DatePicker.js
│ │ ├── EnrollmentForm.js
│ │ ├── FormikContainer.js
│ │ ├── FormikControl.js
│ │ ├── Input.js
│ │ ├── LoginForm.js
│ │ ├── RadioButtons.js
│ │ ├── RegistrationForm.js
│ │ ├── Select.js
│ │ ├── TextError.js
│ │ └── Textarea.js
│ ├── index.css
│ ├── index.js
│ ├── logo.svg
│ ├── serviceWorker.js
│ └── setupTests.js
└── yarn.lock
└── react-formik-demo
├── .gitignore
├── README.md
├── package.json
├── public
├── favicon.ico
├── index.html
├── logo192.png
├── logo512.png
├── manifest.json
└── robots.txt
├── src
├── App.css
├── App.js
├── App.test.js
├── components
│ ├── OldYoutubeForm.js
│ ├── TextError.js
│ └── YoutubeForm.js
├── index.css
├── index.js
├── logo.svg
├── serviceWorker.js
└── setupTests.js
└── yarn.lock
/formik-controls-demo/.gitignore:
--------------------------------------------------------------------------------
1 | # See https://help.github.com/articles/ignoring-files/ for more about ignoring files.
2 |
3 | # dependencies
4 | /node_modules
5 | /.pnp
6 | .pnp.js
7 |
8 | # testing
9 | /coverage
10 |
11 | # production
12 | /build
13 |
14 | # misc
15 | .DS_Store
16 | .env.local
17 | .env.development.local
18 | .env.test.local
19 | .env.production.local
20 |
21 | npm-debug.log*
22 | yarn-debug.log*
23 | yarn-error.log*
24 |
--------------------------------------------------------------------------------
/formik-controls-demo/README.md:
--------------------------------------------------------------------------------
1 | This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app).
2 |
3 | ## Available Scripts
4 |
5 | In the project directory, you can run:
6 |
7 | ### `yarn start`
8 |
9 | Runs the app in the development mode.
10 | Open [http://localhost:3000](http://localhost:3000) to view it in the browser.
11 |
12 | The page will reload if you make edits.
13 | You will also see any lint errors in the console.
14 |
15 | ### `yarn test`
16 |
17 | Launches the test runner in the interactive watch mode.
18 | See the section about [running tests](https://facebook.github.io/create-react-app/docs/running-tests) for more information.
19 |
20 | ### `yarn build`
21 |
22 | Builds the app for production to the `build` folder.
23 | It correctly bundles React in production mode and optimizes the build for the best performance.
24 |
25 | The build is minified and the filenames include the hashes.
26 | Your app is ready to be deployed!
27 |
28 | See the section about [deployment](https://facebook.github.io/create-react-app/docs/deployment) for more information.
29 |
30 | ### `yarn eject`
31 |
32 | **Note: this is a one-way operation. Once you `eject`, you can’t go back!**
33 |
34 | If you aren’t satisfied with the build tool and configuration choices, you can `eject` at any time. This command will remove the single build dependency from your project.
35 |
36 | Instead, it will copy all the configuration files and the transitive dependencies (webpack, Babel, ESLint, etc) right into your project so you have full control over them. All of the commands except `eject` will still work, but they will point to the copied scripts so you can tweak them. At this point you’re on your own.
37 |
38 | You don’t have to ever use `eject`. The curated feature set is suitable for small and middle deployments, and you shouldn’t feel obligated to use this feature. However we understand that this tool wouldn’t be useful if you couldn’t customize it when you are ready for it.
39 |
40 | ## Learn More
41 |
42 | You can learn more in the [Create React App documentation](https://facebook.github.io/create-react-app/docs/getting-started).
43 |
44 | To learn React, check out the [React documentation](https://reactjs.org/).
45 |
46 | ### Code Splitting
47 |
48 | This section has moved here: https://facebook.github.io/create-react-app/docs/code-splitting
49 |
50 | ### Analyzing the Bundle Size
51 |
52 | This section has moved here: https://facebook.github.io/create-react-app/docs/analyzing-the-bundle-size
53 |
54 | ### Making a Progressive Web App
55 |
56 | This section has moved here: https://facebook.github.io/create-react-app/docs/making-a-progressive-web-app
57 |
58 | ### Advanced Configuration
59 |
60 | This section has moved here: https://facebook.github.io/create-react-app/docs/advanced-configuration
61 |
62 | ### Deployment
63 |
64 | This section has moved here: https://facebook.github.io/create-react-app/docs/deployment
65 |
66 | ### `yarn build` fails to minify
67 |
68 | This section has moved here: https://facebook.github.io/create-react-app/docs/troubleshooting#npm-run-build-fails-to-minify
69 |
--------------------------------------------------------------------------------
/formik-controls-demo/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "formik-controls-demo",
3 | "version": "0.1.0",
4 | "private": true,
5 | "dependencies": {
6 | "@chakra-ui/core": "^0.7.0",
7 | "@emotion/core": "^10.0.28",
8 | "@emotion/styled": "^10.0.27",
9 | "@testing-library/jest-dom": "^4.2.4",
10 | "@testing-library/react": "^9.3.2",
11 | "@testing-library/user-event": "^7.1.2",
12 | "emotion-theming": "^10.0.27",
13 | "formik": "^2.1.4",
14 | "react": "^16.13.1",
15 | "react-datepicker": "^2.14.1",
16 | "react-dom": "^16.13.1",
17 | "react-scripts": "3.4.1",
18 | "yup": "^0.28.4"
19 | },
20 | "scripts": {
21 | "start": "react-scripts start",
22 | "build": "react-scripts build",
23 | "test": "react-scripts test",
24 | "eject": "react-scripts eject"
25 | },
26 | "eslintConfig": {
27 | "extends": "react-app"
28 | },
29 | "browserslist": {
30 | "production": [
31 | ">0.2%",
32 | "not dead",
33 | "not op_mini all"
34 | ],
35 | "development": [
36 | "last 1 chrome version",
37 | "last 1 firefox version",
38 | "last 1 safari version"
39 | ]
40 | }
41 | }
42 |
--------------------------------------------------------------------------------
/formik-controls-demo/public/favicon.ico:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/gopinav/React-Formik-Tutorials/1c96b3be68a65fb05e0b8488a37c4c662a036498/formik-controls-demo/public/favicon.ico
--------------------------------------------------------------------------------
/formik-controls-demo/public/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
12 |
13 |
17 |
18 |
27 | React App
28 |
29 |
30 | You need to enable JavaScript to run this app.
31 |
32 |
42 |
43 |
44 |
--------------------------------------------------------------------------------
/formik-controls-demo/public/logo192.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/gopinav/React-Formik-Tutorials/1c96b3be68a65fb05e0b8488a37c4c662a036498/formik-controls-demo/public/logo192.png
--------------------------------------------------------------------------------
/formik-controls-demo/public/logo512.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/gopinav/React-Formik-Tutorials/1c96b3be68a65fb05e0b8488a37c4c662a036498/formik-controls-demo/public/logo512.png
--------------------------------------------------------------------------------
/formik-controls-demo/public/manifest.json:
--------------------------------------------------------------------------------
1 | {
2 | "short_name": "React App",
3 | "name": "Create React App Sample",
4 | "icons": [
5 | {
6 | "src": "favicon.ico",
7 | "sizes": "64x64 32x32 24x24 16x16",
8 | "type": "image/x-icon"
9 | },
10 | {
11 | "src": "logo192.png",
12 | "type": "image/png",
13 | "sizes": "192x192"
14 | },
15 | {
16 | "src": "logo512.png",
17 | "type": "image/png",
18 | "sizes": "512x512"
19 | }
20 | ],
21 | "start_url": ".",
22 | "display": "standalone",
23 | "theme_color": "#000000",
24 | "background_color": "#ffffff"
25 | }
26 |
--------------------------------------------------------------------------------
/formik-controls-demo/public/robots.txt:
--------------------------------------------------------------------------------
1 | # https://www.robotstxt.org/robotstxt.html
2 | User-agent: *
3 | Disallow:
4 |
--------------------------------------------------------------------------------
/formik-controls-demo/src/App.css:
--------------------------------------------------------------------------------
1 | .App {
2 | display: flex;
3 | justify-content: center;
4 | }
5 |
6 | label {
7 | font-weight: bold;
8 | display: flex;
9 | margin-bottom: 5px;
10 | }
11 |
12 | input + label {
13 | font-weight: bold;
14 | display: inline-flex;
15 | margin-right: 20px;
16 | }
17 |
18 | input[type='text'],
19 | input[type='email'],
20 | input[type='password'],
21 | textarea,
22 | select {
23 | display: block;
24 | width: 400px;
25 | padding: 6px 12px;
26 | font-size: 14px;
27 | line-height: 1.42857143;
28 | color: #555;
29 | background-color: #fff;
30 | background-image: none;
31 | border: 1px solid #ccc;
32 | border-radius: 4px;
33 | /* margin-bottom: 20px; */
34 | }
35 |
36 | .form-control {
37 | margin-bottom: 20px;
38 | }
39 |
40 | .error {
41 | color: red;
42 | }
--------------------------------------------------------------------------------
/formik-controls-demo/src/App.js:
--------------------------------------------------------------------------------
1 | import React from 'react'
2 | import './App.css'
3 | import FormikContainer from './components/FormikContainer'
4 | import LoginForm from './components/LoginForm'
5 | import RegistrationForm from './components/RegistrationForm'
6 | import EnrollmentForm from './components/EnrollmentForm'
7 | import { theme, ThemeProvider } from '@chakra-ui/core'
8 |
9 | function App () {
10 | return (
11 |
12 |
13 | {/* */}
14 |
15 | {/* */}
16 | {/* */}
17 |
18 |
19 | )
20 | }
21 |
22 | export default App
23 |
--------------------------------------------------------------------------------
/formik-controls-demo/src/App.test.js:
--------------------------------------------------------------------------------
1 | import React from 'react';
2 | import { render } from '@testing-library/react';
3 | import App from './App';
4 |
5 | test('renders learn react link', () => {
6 | const { getByText } = render( );
7 | const linkElement = getByText(/learn react/i);
8 | expect(linkElement).toBeInTheDocument();
9 | });
10 |
--------------------------------------------------------------------------------
/formik-controls-demo/src/components/ChakraInput.js:
--------------------------------------------------------------------------------
1 | import React from 'react'
2 | import { Field } from 'formik'
3 | import {
4 | Input,
5 | FormControl,
6 | FormLabel,
7 | FormErrorMessage
8 | } from '@chakra-ui/core'
9 |
10 | function ChakraInput (props) {
11 | const { label, name, ...rest } = props
12 | return (
13 |
14 | {({ field, form }) => (
15 |
16 | {label}
17 |
18 | {form.errors[name]}
19 |
20 | )}
21 |
22 | )
23 | }
24 |
25 | export default ChakraInput
26 |
--------------------------------------------------------------------------------
/formik-controls-demo/src/components/CheckboxGroup.js:
--------------------------------------------------------------------------------
1 | import React from 'react'
2 | import { Field, ErrorMessage } from 'formik'
3 | import TextError from './TextError'
4 |
5 | function CheckboxGroup (props) {
6 | const { label, name, options, ...rest } = props
7 | return (
8 |
9 | {label}
10 |
11 | {({ field }) => {
12 | return options.map(option => {
13 | return (
14 |
15 |
23 | {option.key}
24 |
25 | )
26 | })
27 | }}
28 |
29 |
30 |
31 | )
32 | }
33 |
34 | export default CheckboxGroup
35 |
--------------------------------------------------------------------------------
/formik-controls-demo/src/components/DatePicker.js:
--------------------------------------------------------------------------------
1 | import React from 'react'
2 | import DateView from 'react-datepicker'
3 | import { Field, ErrorMessage } from 'formik'
4 | import TextError from './TextError'
5 | import 'react-datepicker/dist/react-datepicker.css'
6 |
7 | function DatePicker (props) {
8 | const { label, name, ...rest } = props
9 | return (
10 |
11 | {label}
12 |
13 | {({ form, field }) => {
14 | const { setFieldValue } = form
15 | const { value } = field
16 | return (
17 | setFieldValue(name, val)}
23 | />
24 | )
25 | }}
26 |
27 |
28 |
29 | )
30 | }
31 |
32 | export default DatePicker
33 |
--------------------------------------------------------------------------------
/formik-controls-demo/src/components/EnrollmentForm.js:
--------------------------------------------------------------------------------
1 | import React from 'react'
2 | import { Formik, Form } from 'formik'
3 | import * as Yup from 'yup'
4 | import FormikControl from './FormikControl'
5 |
6 | function EnrollmentForm () {
7 | const dropdownOptions = [
8 | { key: 'Select your course', value: '' },
9 | { key: 'React', value: 'react' },
10 | { key: 'Angular', value: 'angular' },
11 | { key: 'Vue', value: 'vue' }
12 | ]
13 |
14 | const checkboxOptions = [
15 | { key: 'HTML', value: 'html' },
16 | { key: 'CSS', value: 'css' },
17 | { key: 'JavaScript', value: 'javascript' }
18 | ]
19 |
20 | const initialValues = {
21 | email: '',
22 | bio: '',
23 | course: '',
24 | skills: [],
25 | courseDate: null
26 | }
27 |
28 | const validationSchema = Yup.object({
29 | email: Yup.string()
30 | .email('Invalid email format')
31 | .required('Required'),
32 | bio: Yup.string().required('Required'),
33 | course: Yup.string().required('Required'),
34 | courseDate: Yup.date()
35 | .required('Required')
36 | .nullable()
37 | })
38 |
39 | const onSubmit = values => {
40 | console.log('Form data', values)
41 | }
42 |
43 | return (
44 |
49 | {formik => {
50 | return (
51 |
84 | )
85 | }}
86 |
87 | )
88 | }
89 |
90 | export default EnrollmentForm
91 |
--------------------------------------------------------------------------------
/formik-controls-demo/src/components/FormikContainer.js:
--------------------------------------------------------------------------------
1 | import React from 'react'
2 | import { Formik, Form } from 'formik'
3 | import * as Yup from 'yup'
4 | import FormikControl from './FormikControl'
5 |
6 | function FormikContainer () {
7 | const dropdownOptions = [
8 | { key: 'Select an option', value: '' },
9 | { key: 'Option 1', value: 'option1' },
10 | { key: 'Option 2', value: 'option2' },
11 | { key: 'Option 3', value: 'option3' }
12 | ]
13 | const radioOptions = [
14 | { key: 'Option 1', value: 'rOption1' },
15 | { key: 'Option 2', value: 'rOption2' },
16 | { key: 'Option 3', value: 'rOption3' }
17 | ]
18 | const checkboxOptions = [
19 | { key: 'Option 1', value: 'cOption1' },
20 | { key: 'Option 2', value: 'cOption2' },
21 | { key: 'Option 3', value: 'cOption3' }
22 | ]
23 | const initialValues = {
24 | email: '',
25 | description: '',
26 | selectOption: '',
27 | radioOption: '',
28 | checkboxOption: [],
29 | birthDate: null
30 | }
31 | const validationSchema = Yup.object({
32 | email: Yup.string().required('Required'),
33 | description: Yup.string().required('Required'),
34 | selectOption: Yup.string().required('Required'),
35 | radioOption: Yup.string().required('Required'),
36 | checkboxOption: Yup.array().required('Required'),
37 | birthDate: Yup.date()
38 | .required('Required')
39 | .nullable()
40 | })
41 | const onSubmit = values => {
42 | console.log('Form data', values)
43 | console.log('Saved data', JSON.parse(JSON.stringify(values)))
44 | }
45 |
46 | return (
47 |
52 | {formik => (
53 |
90 | )}
91 |
92 | )
93 | }
94 |
95 | export default FormikContainer
96 |
--------------------------------------------------------------------------------
/formik-controls-demo/src/components/FormikControl.js:
--------------------------------------------------------------------------------
1 | import React from 'react'
2 | import Input from './Input'
3 | import Textarea from './Textarea'
4 | import Select from './Select'
5 | import RadioButtons from './RadioButtons'
6 | import CheckboxGroup from './CheckboxGroup'
7 | import DatePicker from './DatePicker'
8 | import ChakraInput from './ChakraInput'
9 |
10 | function FormikControl (props) {
11 | const { control, ...rest } = props
12 | switch (control) {
13 | case 'input':
14 | return
15 | case 'textarea':
16 | return
17 | case 'select':
18 | return
19 | case 'radio':
20 | return
21 | case 'checkbox':
22 | return
23 | case 'date':
24 | return
25 | case 'chakraInput':
26 | return
27 | default:
28 | return null
29 | }
30 | }
31 |
32 | export default FormikControl
33 |
--------------------------------------------------------------------------------
/formik-controls-demo/src/components/Input.js:
--------------------------------------------------------------------------------
1 | import React from 'react'
2 | import { Field, ErrorMessage } from 'formik'
3 | import TextError from './TextError'
4 |
5 | function Input (props) {
6 | const { label, name, ...rest } = props
7 | return (
8 |
9 | {label}
10 |
11 |
12 |
13 | )
14 | }
15 |
16 | export default Input
17 |
--------------------------------------------------------------------------------
/formik-controls-demo/src/components/LoginForm.js:
--------------------------------------------------------------------------------
1 | import React from 'react'
2 | import { Formik, Form } from 'formik'
3 | import * as Yup from 'yup'
4 | import FormikControl from './FormikControl'
5 |
6 | function LoginForm () {
7 | const initialValues = {
8 | email: '',
9 | password: ''
10 | }
11 |
12 | const validationSchema = Yup.object({
13 | email: Yup.string()
14 | .email('Invalid email format')
15 | .required('Required'),
16 | password: Yup.string().required('Required')
17 | })
18 |
19 | const onSubmit = values => {
20 | console.log('Form data', values)
21 | }
22 |
23 | return (
24 |
29 | {formik => {
30 | return (
31 |
47 | )
48 | }}
49 |
50 | )
51 | }
52 |
53 | export default LoginForm
54 |
--------------------------------------------------------------------------------
/formik-controls-demo/src/components/RadioButtons.js:
--------------------------------------------------------------------------------
1 | import React from 'react'
2 | import { Field, ErrorMessage } from 'formik'
3 | import TextError from './TextError'
4 |
5 | function RadioButtons (props) {
6 | const { label, name, options, ...rest } = props
7 | return (
8 |
9 | {label}
10 |
11 | {({ field }) => {
12 | return options.map(option => {
13 | return (
14 |
15 |
23 | {option.key}
24 |
25 | )
26 | })
27 | }}
28 |
29 |
30 |
31 | )
32 | }
33 |
34 | export default RadioButtons
35 |
--------------------------------------------------------------------------------
/formik-controls-demo/src/components/RegistrationForm.js:
--------------------------------------------------------------------------------
1 | import React from 'react'
2 | import { Formik, Form } from 'formik'
3 | import * as Yup from 'yup'
4 | import FormikControl from './FormikControl'
5 |
6 | function RegistrationForm () {
7 | const options = [
8 | { key: 'Email', value: 'emailmoc' },
9 | { key: 'Telephone', value: 'telephonemoc' }
10 | ]
11 | const initialValues = {
12 | email: '',
13 | password: '',
14 | confirmPassword: '',
15 | modeOfContact: '',
16 | phone: ''
17 | }
18 |
19 | const validationSchema = Yup.object({
20 | email: Yup.string()
21 | .email('Invalid email format')
22 | .required('Required'),
23 | password: Yup.string().required('Required'),
24 | confirmPassword: Yup.string()
25 | .oneOf([Yup.ref('password'), ''], 'Passwords must match')
26 | .required('Required'),
27 | modeOfContact: Yup.string().required('Required'),
28 | phone: Yup.string().when('modeOfContact', {
29 | is: 'telephonemoc',
30 | then: Yup.string().required('Required')
31 | })
32 | })
33 |
34 | const onSubmit = values => {
35 | console.log('Form data', values)
36 | }
37 |
38 | return (
39 |
44 | {formik => {
45 | return (
46 |
81 | )
82 | }}
83 |
84 | )
85 | }
86 |
87 | export default RegistrationForm
88 |
--------------------------------------------------------------------------------
/formik-controls-demo/src/components/Select.js:
--------------------------------------------------------------------------------
1 | import React from 'react'
2 | import { Field, ErrorMessage } from 'formik'
3 | import TextError from './TextError'
4 |
5 | function Select (props) {
6 | const { label, name, options, ...rest } = props
7 | return (
8 |
9 | {label}
10 |
11 | {options.map(option => {
12 | return (
13 |
14 | {option.key}
15 |
16 | )
17 | })}
18 |
19 |
20 |
21 | )
22 | }
23 |
24 | export default Select
25 |
--------------------------------------------------------------------------------
/formik-controls-demo/src/components/TextError.js:
--------------------------------------------------------------------------------
1 | import React from 'react'
2 |
3 | function TextError (props) {
4 | return {props.children}
5 | }
6 |
7 | export default TextError
8 |
--------------------------------------------------------------------------------
/formik-controls-demo/src/components/Textarea.js:
--------------------------------------------------------------------------------
1 | import React from 'react'
2 | import { Field, ErrorMessage } from 'formik'
3 | import TextError from './TextError'
4 |
5 | function Textarea (props) {
6 | const { label, name, ...rest } = props
7 | return (
8 |
9 | {label}
10 |
11 |
12 |
13 | )
14 | }
15 |
16 | export default Textarea
17 |
--------------------------------------------------------------------------------
/formik-controls-demo/src/index.css:
--------------------------------------------------------------------------------
1 | body {
2 | margin: 0;
3 | font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen',
4 | 'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue',
5 | sans-serif;
6 | -webkit-font-smoothing: antialiased;
7 | -moz-osx-font-smoothing: grayscale;
8 | }
9 |
10 | code {
11 | font-family: source-code-pro, Menlo, Monaco, Consolas, 'Courier New',
12 | monospace;
13 | }
14 |
--------------------------------------------------------------------------------
/formik-controls-demo/src/index.js:
--------------------------------------------------------------------------------
1 | import React from 'react';
2 | import ReactDOM from 'react-dom';
3 | import './index.css';
4 | import App from './App';
5 | import * as serviceWorker from './serviceWorker';
6 |
7 | ReactDOM.render(
8 |
9 |
10 | ,
11 | document.getElementById('root')
12 | );
13 |
14 | // If you want your app to work offline and load faster, you can change
15 | // unregister() to register() below. Note this comes with some pitfalls.
16 | // Learn more about service workers: https://bit.ly/CRA-PWA
17 | serviceWorker.unregister();
18 |
--------------------------------------------------------------------------------
/formik-controls-demo/src/logo.svg:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/formik-controls-demo/src/serviceWorker.js:
--------------------------------------------------------------------------------
1 | // This optional code is used to register a service worker.
2 | // register() is not called by default.
3 |
4 | // This lets the app load faster on subsequent visits in production, and gives
5 | // it offline capabilities. However, it also means that developers (and users)
6 | // will only see deployed updates on subsequent visits to a page, after all the
7 | // existing tabs open on the page have been closed, since previously cached
8 | // resources are updated in the background.
9 |
10 | // To learn more about the benefits of this model and instructions on how to
11 | // opt-in, read https://bit.ly/CRA-PWA
12 |
13 | const isLocalhost = Boolean(
14 | window.location.hostname === 'localhost' ||
15 | // [::1] is the IPv6 localhost address.
16 | window.location.hostname === '[::1]' ||
17 | // 127.0.0.0/8 are considered localhost for IPv4.
18 | window.location.hostname.match(
19 | /^127(?:\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}$/
20 | )
21 | );
22 |
23 | export function register(config) {
24 | if (process.env.NODE_ENV === 'production' && 'serviceWorker' in navigator) {
25 | // The URL constructor is available in all browsers that support SW.
26 | const publicUrl = new URL(process.env.PUBLIC_URL, window.location.href);
27 | if (publicUrl.origin !== window.location.origin) {
28 | // Our service worker won't work if PUBLIC_URL is on a different origin
29 | // from what our page is served on. This might happen if a CDN is used to
30 | // serve assets; see https://github.com/facebook/create-react-app/issues/2374
31 | return;
32 | }
33 |
34 | window.addEventListener('load', () => {
35 | const swUrl = `${process.env.PUBLIC_URL}/service-worker.js`;
36 |
37 | if (isLocalhost) {
38 | // This is running on localhost. Let's check if a service worker still exists or not.
39 | checkValidServiceWorker(swUrl, config);
40 |
41 | // Add some additional logging to localhost, pointing developers to the
42 | // service worker/PWA documentation.
43 | navigator.serviceWorker.ready.then(() => {
44 | console.log(
45 | 'This web app is being served cache-first by a service ' +
46 | 'worker. To learn more, visit https://bit.ly/CRA-PWA'
47 | );
48 | });
49 | } else {
50 | // Is not localhost. Just register service worker
51 | registerValidSW(swUrl, config);
52 | }
53 | });
54 | }
55 | }
56 |
57 | function registerValidSW(swUrl, config) {
58 | navigator.serviceWorker
59 | .register(swUrl)
60 | .then(registration => {
61 | registration.onupdatefound = () => {
62 | const installingWorker = registration.installing;
63 | if (installingWorker == null) {
64 | return;
65 | }
66 | installingWorker.onstatechange = () => {
67 | if (installingWorker.state === 'installed') {
68 | if (navigator.serviceWorker.controller) {
69 | // At this point, the updated precached content has been fetched,
70 | // but the previous service worker will still serve the older
71 | // content until all client tabs are closed.
72 | console.log(
73 | 'New content is available and will be used when all ' +
74 | 'tabs for this page are closed. See https://bit.ly/CRA-PWA.'
75 | );
76 |
77 | // Execute callback
78 | if (config && config.onUpdate) {
79 | config.onUpdate(registration);
80 | }
81 | } else {
82 | // At this point, everything has been precached.
83 | // It's the perfect time to display a
84 | // "Content is cached for offline use." message.
85 | console.log('Content is cached for offline use.');
86 |
87 | // Execute callback
88 | if (config && config.onSuccess) {
89 | config.onSuccess(registration);
90 | }
91 | }
92 | }
93 | };
94 | };
95 | })
96 | .catch(error => {
97 | console.error('Error during service worker registration:', error);
98 | });
99 | }
100 |
101 | function checkValidServiceWorker(swUrl, config) {
102 | // Check if the service worker can be found. If it can't reload the page.
103 | fetch(swUrl, {
104 | headers: { 'Service-Worker': 'script' },
105 | })
106 | .then(response => {
107 | // Ensure service worker exists, and that we really are getting a JS file.
108 | const contentType = response.headers.get('content-type');
109 | if (
110 | response.status === 404 ||
111 | (contentType != null && contentType.indexOf('javascript') === -1)
112 | ) {
113 | // No service worker found. Probably a different app. Reload the page.
114 | navigator.serviceWorker.ready.then(registration => {
115 | registration.unregister().then(() => {
116 | window.location.reload();
117 | });
118 | });
119 | } else {
120 | // Service worker found. Proceed as normal.
121 | registerValidSW(swUrl, config);
122 | }
123 | })
124 | .catch(() => {
125 | console.log(
126 | 'No internet connection found. App is running in offline mode.'
127 | );
128 | });
129 | }
130 |
131 | export function unregister() {
132 | if ('serviceWorker' in navigator) {
133 | navigator.serviceWorker.ready
134 | .then(registration => {
135 | registration.unregister();
136 | })
137 | .catch(error => {
138 | console.error(error.message);
139 | });
140 | }
141 | }
142 |
--------------------------------------------------------------------------------
/formik-controls-demo/src/setupTests.js:
--------------------------------------------------------------------------------
1 | // jest-dom adds custom jest matchers for asserting on DOM nodes.
2 | // allows you to do things like:
3 | // expect(element).toHaveTextContent(/react/i)
4 | // learn more: https://github.com/testing-library/jest-dom
5 | import '@testing-library/jest-dom/extend-expect';
6 |
--------------------------------------------------------------------------------
/react-formik-demo/.gitignore:
--------------------------------------------------------------------------------
1 | # See https://help.github.com/articles/ignoring-files/ for more about ignoring files.
2 |
3 | # dependencies
4 | /node_modules
5 | /.pnp
6 | .pnp.js
7 |
8 | # testing
9 | /coverage
10 |
11 | # production
12 | /build
13 |
14 | # misc
15 | .DS_Store
16 | .env.local
17 | .env.development.local
18 | .env.test.local
19 | .env.production.local
20 |
21 | npm-debug.log*
22 | yarn-debug.log*
23 | yarn-error.log*
24 |
--------------------------------------------------------------------------------
/react-formik-demo/README.md:
--------------------------------------------------------------------------------
1 | This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app).
2 |
3 | ## Available Scripts
4 |
5 | In the project directory, you can run:
6 |
7 | ### `yarn start`
8 |
9 | Runs the app in the development mode.
10 | Open [http://localhost:3000](http://localhost:3000) to view it in the browser.
11 |
12 | The page will reload if you make edits.
13 | You will also see any lint errors in the console.
14 |
15 | ### `yarn test`
16 |
17 | Launches the test runner in the interactive watch mode.
18 | See the section about [running tests](https://facebook.github.io/create-react-app/docs/running-tests) for more information.
19 |
20 | ### `yarn build`
21 |
22 | Builds the app for production to the `build` folder.
23 | It correctly bundles React in production mode and optimizes the build for the best performance.
24 |
25 | The build is minified and the filenames include the hashes.
26 | Your app is ready to be deployed!
27 |
28 | See the section about [deployment](https://facebook.github.io/create-react-app/docs/deployment) for more information.
29 |
30 | ### `yarn eject`
31 |
32 | **Note: this is a one-way operation. Once you `eject`, you can’t go back!**
33 |
34 | If you aren’t satisfied with the build tool and configuration choices, you can `eject` at any time. This command will remove the single build dependency from your project.
35 |
36 | Instead, it will copy all the configuration files and the transitive dependencies (webpack, Babel, ESLint, etc) right into your project so you have full control over them. All of the commands except `eject` will still work, but they will point to the copied scripts so you can tweak them. At this point you’re on your own.
37 |
38 | You don’t have to ever use `eject`. The curated feature set is suitable for small and middle deployments, and you shouldn’t feel obligated to use this feature. However we understand that this tool wouldn’t be useful if you couldn’t customize it when you are ready for it.
39 |
40 | ## Learn More
41 |
42 | You can learn more in the [Create React App documentation](https://facebook.github.io/create-react-app/docs/getting-started).
43 |
44 | To learn React, check out the [React documentation](https://reactjs.org/).
45 |
46 | ### Code Splitting
47 |
48 | This section has moved here: https://facebook.github.io/create-react-app/docs/code-splitting
49 |
50 | ### Analyzing the Bundle Size
51 |
52 | This section has moved here: https://facebook.github.io/create-react-app/docs/analyzing-the-bundle-size
53 |
54 | ### Making a Progressive Web App
55 |
56 | This section has moved here: https://facebook.github.io/create-react-app/docs/making-a-progressive-web-app
57 |
58 | ### Advanced Configuration
59 |
60 | This section has moved here: https://facebook.github.io/create-react-app/docs/advanced-configuration
61 |
62 | ### Deployment
63 |
64 | This section has moved here: https://facebook.github.io/create-react-app/docs/deployment
65 |
66 | ### `yarn build` fails to minify
67 |
68 | This section has moved here: https://facebook.github.io/create-react-app/docs/troubleshooting#npm-run-build-fails-to-minify
69 |
--------------------------------------------------------------------------------
/react-formik-demo/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "react-formik-demo",
3 | "version": "0.1.0",
4 | "private": true,
5 | "dependencies": {
6 | "@testing-library/jest-dom": "^4.2.4",
7 | "@testing-library/react": "^9.3.2",
8 | "@testing-library/user-event": "^7.1.2",
9 | "formik": "^2.1.4",
10 | "react": "^16.13.1",
11 | "react-dom": "^16.13.1",
12 | "react-scripts": "3.4.1",
13 | "yup": "^0.28.3"
14 | },
15 | "scripts": {
16 | "start": "react-scripts start",
17 | "build": "react-scripts build",
18 | "test": "react-scripts test",
19 | "eject": "react-scripts eject"
20 | },
21 | "eslintConfig": {
22 | "extends": "react-app"
23 | },
24 | "browserslist": {
25 | "production": [
26 | ">0.2%",
27 | "not dead",
28 | "not op_mini all"
29 | ],
30 | "development": [
31 | "last 1 chrome version",
32 | "last 1 firefox version",
33 | "last 1 safari version"
34 | ]
35 | }
36 | }
37 |
--------------------------------------------------------------------------------
/react-formik-demo/public/favicon.ico:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/gopinav/React-Formik-Tutorials/1c96b3be68a65fb05e0b8488a37c4c662a036498/react-formik-demo/public/favicon.ico
--------------------------------------------------------------------------------
/react-formik-demo/public/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
12 |
13 |
17 |
18 |
27 | React App
28 |
29 |
30 | You need to enable JavaScript to run this app.
31 |
32 |
42 |
43 |
44 |
--------------------------------------------------------------------------------
/react-formik-demo/public/logo192.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/gopinav/React-Formik-Tutorials/1c96b3be68a65fb05e0b8488a37c4c662a036498/react-formik-demo/public/logo192.png
--------------------------------------------------------------------------------
/react-formik-demo/public/logo512.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/gopinav/React-Formik-Tutorials/1c96b3be68a65fb05e0b8488a37c4c662a036498/react-formik-demo/public/logo512.png
--------------------------------------------------------------------------------
/react-formik-demo/public/manifest.json:
--------------------------------------------------------------------------------
1 | {
2 | "short_name": "React App",
3 | "name": "Create React App Sample",
4 | "icons": [
5 | {
6 | "src": "favicon.ico",
7 | "sizes": "64x64 32x32 24x24 16x16",
8 | "type": "image/x-icon"
9 | },
10 | {
11 | "src": "logo192.png",
12 | "type": "image/png",
13 | "sizes": "192x192"
14 | },
15 | {
16 | "src": "logo512.png",
17 | "type": "image/png",
18 | "sizes": "512x512"
19 | }
20 | ],
21 | "start_url": ".",
22 | "display": "standalone",
23 | "theme_color": "#000000",
24 | "background_color": "#ffffff"
25 | }
26 |
--------------------------------------------------------------------------------
/react-formik-demo/public/robots.txt:
--------------------------------------------------------------------------------
1 | # https://www.robotstxt.org/robotstxt.html
2 | User-agent: *
3 | Disallow:
4 |
--------------------------------------------------------------------------------
/react-formik-demo/src/App.css:
--------------------------------------------------------------------------------
1 | .App {
2 | display: flex;
3 | justify-content: center;
4 | }
5 |
6 | label {
7 | font-weight: bold;
8 | display: flex;
9 | margin-bottom: 5px;
10 | }
11 |
12 | input[type='text'],
13 | input[type='email'],
14 | textarea {
15 | display: block;
16 | width: 400px;
17 | padding: 6px 12px;
18 | font-size: 14px;
19 | line-height: 1.42857143;
20 | color: #555;
21 | background-color: #fff;
22 | background-image: none;
23 | border: 1px solid #ccc;
24 | border-radius: 4px;
25 | /* margin-bottom: 20px; */
26 | }
27 |
28 | .form-control {
29 | margin-bottom: 20px;
30 | }
31 |
32 | .error {
33 | color: red;
34 | }
--------------------------------------------------------------------------------
/react-formik-demo/src/App.js:
--------------------------------------------------------------------------------
1 | import React from 'react'
2 | import './App.css'
3 | import YoutubeForm from './components/YoutubeForm'
4 |
5 | function App () {
6 | return (
7 |
8 |
9 |
10 | )
11 | }
12 |
13 | export default App
14 |
--------------------------------------------------------------------------------
/react-formik-demo/src/App.test.js:
--------------------------------------------------------------------------------
1 | import React from 'react';
2 | import { render } from '@testing-library/react';
3 | import App from './App';
4 |
5 | test('renders learn react link', () => {
6 | const { getByText } = render( );
7 | const linkElement = getByText(/learn react/i);
8 | expect(linkElement).toBeInTheDocument();
9 | });
10 |
--------------------------------------------------------------------------------
/react-formik-demo/src/components/OldYoutubeForm.js:
--------------------------------------------------------------------------------
1 | import React from 'react'
2 | import { useFormik } from 'formik'
3 | import * as Yup from 'yup'
4 |
5 | const initialValues = {
6 | name: 'Vishwas',
7 | email: '',
8 | channel: ''
9 | }
10 |
11 | const onSubmit = values => {
12 | console.log('Form data', values)
13 | }
14 |
15 | // const validate = values => {
16 | // const errors = {}
17 |
18 | // if (!values.name) {
19 | // errors.name = 'Required'
20 | // }
21 |
22 | // if (!values.email) {
23 | // errors.email = 'Required'
24 | // } else if (!/^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i.test(values.email)) {
25 | // errors.email = 'Invalid email format'
26 | // }
27 |
28 | // if (!values.channel) {
29 | // errors.channel = 'Required'
30 | // }
31 |
32 | // return errors
33 | // }
34 |
35 | const validationSchema = Yup.object({
36 | name: Yup.string().required('Required'),
37 | email: Yup.string()
38 | .email('Invalid email format')
39 | .required('Required'),
40 | channel: Yup.string().required('Required')
41 | })
42 |
43 | function OldYoutubeForm () {
44 | const formik = useFormik({
45 | initialValues,
46 | onSubmit,
47 | // validate,
48 | validationSchema
49 | })
50 |
51 | console.log('formik.touched', formik.touched)
52 |
53 | return (
54 |
102 | )
103 | }
104 |
105 | export default OldYoutubeForm
106 |
--------------------------------------------------------------------------------
/react-formik-demo/src/components/TextError.js:
--------------------------------------------------------------------------------
1 | import React from 'react'
2 |
3 | function TextError (props) {
4 | return {props.children}
5 | }
6 |
7 | export default TextError
8 |
--------------------------------------------------------------------------------
/react-formik-demo/src/components/YoutubeForm.js:
--------------------------------------------------------------------------------
1 | import React, { useState } from 'react'
2 | import {
3 | Formik,
4 | Form,
5 | Field,
6 | ErrorMessage,
7 | FieldArray,
8 | FastField
9 | } from 'formik'
10 | import * as Yup from 'yup'
11 | import TextError from './TextError'
12 |
13 | const initialValues = {
14 | name: 'Vishwas',
15 | email: '',
16 | channel: '',
17 | comments: '',
18 | address: '',
19 | social: {
20 | facebook: '',
21 | twitter: ''
22 | },
23 | phoneNumbers: ['', ''],
24 | phNumbers: ['']
25 | }
26 |
27 | const savedValues = {
28 | name: 'Vishwas',
29 | email: 'v@example.com',
30 | channel: 'codevolution',
31 | comments: 'Welcome to Formik',
32 | address: '221B Baker Street',
33 | social: {
34 | facebook: '',
35 | twitter: ''
36 | },
37 | phoneNumbers: ['', ''],
38 | phNumbers: ['']
39 | }
40 |
41 | const onSubmit = (values, submitProps) => {
42 | console.log('Form data', values)
43 | console.log('submitProps', submitProps)
44 | submitProps.setSubmitting(false)
45 | submitProps.resetForm()
46 | }
47 |
48 | const validationSchema = Yup.object({
49 | name: Yup.string().required('Required'),
50 | email: Yup.string()
51 | .email('Invalid email format')
52 | .required('Required'),
53 | channel: Yup.string().required('Required'),
54 | comments: Yup.string().required('Required')
55 | })
56 |
57 | const validateComments = value => {
58 | let error
59 | if (!value) {
60 | error = 'Required'
61 | }
62 | return error
63 | }
64 |
65 | function YoutubeForm () {
66 | const [formValues, setFormValues] = useState(null)
67 | return (
68 |
77 | {formik => {
78 | console.log('Formik props', formik)
79 | return (
80 |
222 | )
223 | }}
224 |
225 | )
226 | }
227 |
228 | export default YoutubeForm
229 |
--------------------------------------------------------------------------------
/react-formik-demo/src/index.css:
--------------------------------------------------------------------------------
1 | body {
2 | margin: 0;
3 | font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen',
4 | 'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue',
5 | sans-serif;
6 | -webkit-font-smoothing: antialiased;
7 | -moz-osx-font-smoothing: grayscale;
8 | }
9 |
10 | code {
11 | font-family: source-code-pro, Menlo, Monaco, Consolas, 'Courier New',
12 | monospace;
13 | }
14 |
--------------------------------------------------------------------------------
/react-formik-demo/src/index.js:
--------------------------------------------------------------------------------
1 | import React from 'react'
2 | import ReactDOM from 'react-dom'
3 | import './index.css'
4 | import App from './App'
5 | import * as serviceWorker from './serviceWorker'
6 |
7 | ReactDOM.render(
8 |
9 |
10 | ,
11 | document.getElementById('root')
12 | )
13 |
14 | // If you want your app to work offline and load faster, you can change
15 | // unregister() to register() below. Note this comes with some pitfalls.
16 | // Learn more about service workers: https://bit.ly/CRA-PWA
17 | serviceWorker.unregister()
18 |
--------------------------------------------------------------------------------
/react-formik-demo/src/logo.svg:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/react-formik-demo/src/serviceWorker.js:
--------------------------------------------------------------------------------
1 | // This optional code is used to register a service worker.
2 | // register() is not called by default.
3 |
4 | // This lets the app load faster on subsequent visits in production, and gives
5 | // it offline capabilities. However, it also means that developers (and users)
6 | // will only see deployed updates on subsequent visits to a page, after all the
7 | // existing tabs open on the page have been closed, since previously cached
8 | // resources are updated in the background.
9 |
10 | // To learn more about the benefits of this model and instructions on how to
11 | // opt-in, read https://bit.ly/CRA-PWA
12 |
13 | const isLocalhost = Boolean(
14 | window.location.hostname === 'localhost' ||
15 | // [::1] is the IPv6 localhost address.
16 | window.location.hostname === '[::1]' ||
17 | // 127.0.0.0/8 are considered localhost for IPv4.
18 | window.location.hostname.match(
19 | /^127(?:\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}$/
20 | )
21 | );
22 |
23 | export function register(config) {
24 | if (process.env.NODE_ENV === 'production' && 'serviceWorker' in navigator) {
25 | // The URL constructor is available in all browsers that support SW.
26 | const publicUrl = new URL(process.env.PUBLIC_URL, window.location.href);
27 | if (publicUrl.origin !== window.location.origin) {
28 | // Our service worker won't work if PUBLIC_URL is on a different origin
29 | // from what our page is served on. This might happen if a CDN is used to
30 | // serve assets; see https://github.com/facebook/create-react-app/issues/2374
31 | return;
32 | }
33 |
34 | window.addEventListener('load', () => {
35 | const swUrl = `${process.env.PUBLIC_URL}/service-worker.js`;
36 |
37 | if (isLocalhost) {
38 | // This is running on localhost. Let's check if a service worker still exists or not.
39 | checkValidServiceWorker(swUrl, config);
40 |
41 | // Add some additional logging to localhost, pointing developers to the
42 | // service worker/PWA documentation.
43 | navigator.serviceWorker.ready.then(() => {
44 | console.log(
45 | 'This web app is being served cache-first by a service ' +
46 | 'worker. To learn more, visit https://bit.ly/CRA-PWA'
47 | );
48 | });
49 | } else {
50 | // Is not localhost. Just register service worker
51 | registerValidSW(swUrl, config);
52 | }
53 | });
54 | }
55 | }
56 |
57 | function registerValidSW(swUrl, config) {
58 | navigator.serviceWorker
59 | .register(swUrl)
60 | .then(registration => {
61 | registration.onupdatefound = () => {
62 | const installingWorker = registration.installing;
63 | if (installingWorker == null) {
64 | return;
65 | }
66 | installingWorker.onstatechange = () => {
67 | if (installingWorker.state === 'installed') {
68 | if (navigator.serviceWorker.controller) {
69 | // At this point, the updated precached content has been fetched,
70 | // but the previous service worker will still serve the older
71 | // content until all client tabs are closed.
72 | console.log(
73 | 'New content is available and will be used when all ' +
74 | 'tabs for this page are closed. See https://bit.ly/CRA-PWA.'
75 | );
76 |
77 | // Execute callback
78 | if (config && config.onUpdate) {
79 | config.onUpdate(registration);
80 | }
81 | } else {
82 | // At this point, everything has been precached.
83 | // It's the perfect time to display a
84 | // "Content is cached for offline use." message.
85 | console.log('Content is cached for offline use.');
86 |
87 | // Execute callback
88 | if (config && config.onSuccess) {
89 | config.onSuccess(registration);
90 | }
91 | }
92 | }
93 | };
94 | };
95 | })
96 | .catch(error => {
97 | console.error('Error during service worker registration:', error);
98 | });
99 | }
100 |
101 | function checkValidServiceWorker(swUrl, config) {
102 | // Check if the service worker can be found. If it can't reload the page.
103 | fetch(swUrl, {
104 | headers: { 'Service-Worker': 'script' },
105 | })
106 | .then(response => {
107 | // Ensure service worker exists, and that we really are getting a JS file.
108 | const contentType = response.headers.get('content-type');
109 | if (
110 | response.status === 404 ||
111 | (contentType != null && contentType.indexOf('javascript') === -1)
112 | ) {
113 | // No service worker found. Probably a different app. Reload the page.
114 | navigator.serviceWorker.ready.then(registration => {
115 | registration.unregister().then(() => {
116 | window.location.reload();
117 | });
118 | });
119 | } else {
120 | // Service worker found. Proceed as normal.
121 | registerValidSW(swUrl, config);
122 | }
123 | })
124 | .catch(() => {
125 | console.log(
126 | 'No internet connection found. App is running in offline mode.'
127 | );
128 | });
129 | }
130 |
131 | export function unregister() {
132 | if ('serviceWorker' in navigator) {
133 | navigator.serviceWorker.ready
134 | .then(registration => {
135 | registration.unregister();
136 | })
137 | .catch(error => {
138 | console.error(error.message);
139 | });
140 | }
141 | }
142 |
--------------------------------------------------------------------------------
/react-formik-demo/src/setupTests.js:
--------------------------------------------------------------------------------
1 | // jest-dom adds custom jest matchers for asserting on DOM nodes.
2 | // allows you to do things like:
3 | // expect(element).toHaveTextContent(/react/i)
4 | // learn more: https://github.com/testing-library/jest-dom
5 | import '@testing-library/jest-dom/extend-expect';
6 |
--------------------------------------------------------------------------------