From 072fe0e0df45d5351f83c07013462242381fe796 Mon Sep 17 00:00:00 2001 From: Asabeneh Date: Sun, 11 Oct 2020 20:34:07 +0300 Subject: [PATCH 1/5] telegram vote has been added --- readMe.md | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/readMe.md b/readMe.md index 3428445..594aaa9 100644 --- a/readMe.md +++ b/readMe.md @@ -38,12 +38,16 @@ |13|[Controlled and Uncondrolled Component 😞]()| |13|[Component Life Cycles😞]()| - 🧡🧡🧡 HAPPY CODING 🧡🧡🧡 -
-Support [**Asabeneh**](https://www.patreon.com/asabeneh?fan_landing=true) to create more educational materials -[become-asabeneh-patreon](https://www.patreon.com/asabeneh?fan_landing=true) + + + + +Support [*Asabeneh*](https://www.patreon.com/asabeneh?fan_landing=true) to create more educational materials + +[become-asabeneh-patreon](https://www.patreon.com/asabeneh?fan_landing=true) +
--- @@ -86,6 +90,8 @@ I believe you will learn quite a lot in the next 30 days and your programming an I will use conversational English and less jargons to write this challenge. The content will be continuously updated. If you find a typo or grammar mistakes don't be surprised because I don't do any proof read before I publish it. I would recommend you to focus on the main message of the challenge instead of the English and some minor mistakes. I really appreciate if you send me pull requests for improvement and remember to pull first from master before you send pull requests. Most of the images I have used in this challenge came from 30DaysOfJavaScript challenge therefore you may need to rename file names and folders 30DaysOfReact. If you are good at arrays, loops, functions, objects, functional programming, destructuring and spreading and class then you will be able to follow the challenge properly. Otherwise, I strongly recommend you to check [30DaysOfJavaScript](https://github.com/Asabeneh/30-Days-Of-JavaScript). +Before you dive into this course, you may check the [review] of 30 Days Of React. + ## Requirements To get along with the challenge you need to have the following: From 92e3e69e4f570926616d807a36647718d109d458 Mon Sep 17 00:00:00 2001 From: Asabeneh Date: Sun, 11 Oct 2020 20:37:14 +0300 Subject: [PATCH 2/5] telegram vote has been added --- readMe.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/readMe.md b/readMe.md index 594aaa9..3cacc70 100644 --- a/readMe.md +++ b/readMe.md @@ -90,7 +90,7 @@ I believe you will learn quite a lot in the next 30 days and your programming an I will use conversational English and less jargons to write this challenge. The content will be continuously updated. If you find a typo or grammar mistakes don't be surprised because I don't do any proof read before I publish it. I would recommend you to focus on the main message of the challenge instead of the English and some minor mistakes. I really appreciate if you send me pull requests for improvement and remember to pull first from master before you send pull requests. Most of the images I have used in this challenge came from 30DaysOfJavaScript challenge therefore you may need to rename file names and folders 30DaysOfReact. If you are good at arrays, loops, functions, objects, functional programming, destructuring and spreading and class then you will be able to follow the challenge properly. Otherwise, I strongly recommend you to check [30DaysOfJavaScript](https://github.com/Asabeneh/30-Days-Of-JavaScript). -Before you dive into this course, you may check the [review] of 30 Days Of React. +Before you dive into this course, you may check the [review](https://t.me/thirtydaysofreact/992) of 30 Days Of React. ## Requirements From 1e26e7f0436778876011df9acb0454e1252a7a48 Mon Sep 17 00:00:00 2001 From: Asabeneh Date: Mon, 12 Oct 2020 01:20:28 +0300 Subject: [PATCH 3/5] Day_12 has been added --- .gitignore | 2 +- 12_Day_Forms/11_forms.md | 874 ++ 12_Day_Forms/11_forms_boilerplate/.gitignore | 23 + 12_Day_Forms/11_forms_boilerplate/README.md | 5 + .../11_forms_boilerplate/package.json | 34 + .../11_forms_boilerplate/public/index.html | 111 + .../src/data/countries.js | 2013 +++ .../src/data/ten_most_highest_populations.js | 13 + .../src/images/asabeneh.jpg | Bin 0 -> 20817 bytes .../src/images/css_logo.png | Bin 0 -> 84176 bytes .../src/images/html_logo.png | Bin 0 -> 18060 bytes .../src/images/js_logo.png | Bin 0 -> 11783 bytes .../src/images/react_logo.png | Bin 0 -> 41477 bytes .../11_forms_boilerplate/src/index.js | 318 + 12_Day_Forms/11_forms_boilerplate/yarn.lock | 10849 ++++++++++++++++ readMe.md | 4 +- 16 files changed, 14244 insertions(+), 2 deletions(-) create mode 100644 12_Day_Forms/11_forms.md create mode 100644 12_Day_Forms/11_forms_boilerplate/.gitignore create mode 100644 12_Day_Forms/11_forms_boilerplate/README.md create mode 100644 12_Day_Forms/11_forms_boilerplate/package.json create mode 100644 12_Day_Forms/11_forms_boilerplate/public/index.html create mode 100644 12_Day_Forms/11_forms_boilerplate/src/data/countries.js create mode 100644 12_Day_Forms/11_forms_boilerplate/src/data/ten_most_highest_populations.js create mode 100644 12_Day_Forms/11_forms_boilerplate/src/images/asabeneh.jpg create mode 100644 12_Day_Forms/11_forms_boilerplate/src/images/css_logo.png create mode 100644 12_Day_Forms/11_forms_boilerplate/src/images/html_logo.png create mode 100644 12_Day_Forms/11_forms_boilerplate/src/images/js_logo.png create mode 100644 12_Day_Forms/11_forms_boilerplate/src/images/react_logo.png create mode 100644 12_Day_Forms/11_forms_boilerplate/src/index.js create mode 100644 12_Day_Forms/11_forms_boilerplate/yarn.lock diff --git a/.gitignore b/.gitignore index d90ebd4..fa79e16 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,5 @@ draft.md react-for-everyone.md component.md -12_Day_Forms + diff --git a/12_Day_Forms/11_forms.md b/12_Day_Forms/11_forms.md new file mode 100644 index 0000000..a8c54d8 --- /dev/null +++ b/12_Day_Forms/11_forms.md @@ -0,0 +1,874 @@ +
+

30 Days Of React: Forms

+ + + + + Twitter Follow + + +Author: +Asabeneh Yetayeh
+ October, 2020 +
+ +
+ +[<< Day 11](../11_Day_Events/11_events.md) | [Day 13 >>]() + +![30 Days of React banner](../images/30_days_of_react_banner_day_12.jpg) + +- [Forms](#forms) + - [Getting data from input field](#getting-data-from-input-field) + - [Getting multiple input data from form](#getting-multiple-input-data-from-form) + - [Get data from different input field types](#get-data-from-different-input-field-types) + - [Form Validation](#form-validation) + - [What is validation?](#what-is-validation) + - [What is the purpose of validation](#what-is-the-purpose-of-validation) + - [Validation Types](#validation-types) +- [Exercises](#exercises) + - [Exercises: Level 1](#exercises-level-1) + - [Exercises: Level 2](#exercises-level-2) + - [Exercises: Level 3](#exercises-level-3) + +# Forms + +Form is used to collect data from user. Once in a while we use form to fill our information on a paper or on a website. Either to sign up, sign in or to apply for a job we fill different form fields to submit data to remote database. We encounter different form fields when we fill a form such as simple text, email, password, telephone, date, checkbox, radio button, option selection and text area field. Currently, HTML5 has provide quite a lot of field types. You may have + +```html + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +``` + +Another HTML fields to get data from form are textarea and options. + +```html + + + +``` + +Now, you know most of fields we need to get data from a form. Let's start with an input of type text field. In the previous day, we saw different types of events and today we will focus on more of _onChange_ event type which triggers whenever an input field data changes. Input field has by default a memory to store input data but in this section we control that using state and we implement a controlled input. Today we will implement a controlled input. We will cover uncontrolled input in a separate section. + +## Getting data from input field + +So far we did not get any data from input field. Now, it is time to learn how to get data from an input field. We need on input field, event listener (onChange) and state to get data from a controlled input. See the example below. The h1 element below the input tag display what we write on the input. Check live [demo](https://codepen.io/Asabeneh/full/OJVpyqm). + +The input element has many attributes such as value, name, id, placeholder, type and event handler. In addition, we can link a label and an input field using an id of input field and htmlFor of the label.If label and input are linked it will focus the input when we click on label. Look at the example give below. + +```js +class App extends React.Component { + // declaring state + // initial state + state = { + firstName: '', + } + handleChange = (e) => { + const value = e.target.value + this.setState({ firstName: value }) + } + + render() { + // accessing the state value and this value will injected to the input in the value attribute + const firstName = this.state.firstName + return ( +
+
+ ) + } +} +``` + +We usually use form to handle use information. Let us move to form section and make use the form element. + +## Getting multiple input data from form + +In this section we will develop a small form which collect user information. Our user is a student. We use a parent form element and certain number of input elements to collect user information. In addition to that we will have event listener for the form (onSubmit) and for the inputs (onChange). See the following example try to see the commonts too. You can also check the live [demo](https://codepen.io/Asabeneh/full/eYNvJda). + +```js +class App extends React.Component { + // declaring state + state = { + firstName: '', + lastName: '', + country: '', + title: '', + } + handleChange = (e) => { + /* + // we can get the name and value like this but we can also destructure it from e.target + const name = e.target.name + const value = e.target.value + */ + const { name, value } = e.target + // [variablename] this we can make a value stored in a certain variable could be a key for an object, in this case a key for the state + this.setState({ [name]: value }) + } + handleSubmit = (e) => { + // stops the default behavior of form element specifically refreshing of page + e.preventDefault() + console.log(this.state) + } + + render() { + // accessing the state value by destrutcturing the state + const { firstName, lastName, title, country } = this.state + return ( +
+

Add Student

+
+
+ +
+
+ +
+
+ +
+
+ +
+ + +
+
+ ) + } +} +``` + +The above form handles only text types but can handle different input field types. Let's do another form which handle all the different input field types. + +## Get data from different input field types + +```js +// index.js +import React, { Component } from 'react' +import ReactDOM from 'react-dom' + +const options = [ + { + value: '', + label: '-- Select Country--', + }, + { + value: 'Finland', + label: 'Finland', + }, + { + value: 'Sweden', + label: 'Sweden', + }, + { + value: 'Norway', + label: 'Norway', + }, + { + value: 'Denmark', + label: 'Denmark', + }, +] + +// mapping the options to list(array) of JSX options + +const selectOptions = options.map(({ value, label }) => ( + +)) + +class App extends React.Component { + // declaring state + state = { + firstName: '', + lastName: '', + email: '', + country: '', + tel: '', + dateOfBirth: '', + favoriteColor: '', + weight: '', + gender: '', + file: '', + bio: '', + skills: { + html: false, + css: false, + javascript: false, + }, + } + handleChange = (e) => { + /* + // we can get the name and value like this but we can also destructure it from e.target + const name = e.target.name + const value = e.target.value + */ + const { name, value, type, checked } = e.target + // [variablename] this we can make a value stored in a certain variable could be a key for an object, in this case a key for the state + + if (type === 'checkbox') { + this.setState({ + skills: { ...this.state.skills, [name]: checked }, + }) + } else if (type === 'file') { + console.log(type, 'cehck here') + this.setState({ [name]: e.target.files[0] }) + } else { + this.setState({ [name]: value }) + } + } + handleSubmit = (e) => { + // stops the default behavior of form element specifically refreshing of page + e.preventDefault() + e.preventDefault() + const { + firstName, + lastName, + email, + country, + gender, + bio, + file, + skills, + } = this.state + + const formattedSkills = [] + for (const key in skills) { + console.log(key) + if (skills[key]) { + formattedSkills.push(key.toUpperCase()) + } + } + const data = { + firstName, + lastName, + email, + country, + gender, + bio, + file, + skills: formattedSkills, + } + console.log(data) + } + + render() { + // accessing the state value by destrutcturing the state + const { firstName, lastName, title, country } = this.state + return ( +
+

Add Student

+
+
+ React Form and Form Validation +
+
+ + +
+
+ + +
+
+ + +
+
+ +
+ + +
+ +
+ + +
+
+ + +
+
+ + +
+
+
+ +
+ +
+

Gender

+
+ + +
+
+ + +
+
+ + +
+
+ +
+

Select your skills

+
+ + +
+
+ + +
+
+ + +
+
+
+
+