Advertisement
beelzebielsk

react-form.js

Dec 18th, 2017
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import React, { Component } from 'react';
  2. import {render} from 'react-dom';
  3.  
  4. function Input(props) {
  5.   return (
  6.     <label>
  7.       {`${props.label}: `}
  8.       <input
  9.         type="text"
  10.         name={props.name}
  11.         value={props.value}
  12.         onChange={props.onChange} />
  13.     </label>
  14.   );
  15. }
  16.  
  17.  
  18. function RadioButtons(props) {
  19.   let options = props.options.map(option => {
  20.     console.log(option, props.checkedOption,
  21.       option === props.checkedOption);
  22.     return (
  23.       <label>
  24.         {option}
  25.         <input
  26.           type="radio"
  27.           name={props.name}
  28.           value={option}
  29.           checked={option === props.checkedOption}
  30.           onChange={props.onChange} />
  31.       </label>
  32.     );
  33.   });
  34.   return <div>{options}</div>
  35. }
  36.  
  37. /* An example of properties for HTML selects:
  38.  * https://www.w3schools.com/TAGS/tryit.asp?filename=tryhtml5_select_form
  39.  */
  40. function Select(props) {
  41.   let options = props.options.map(option => {
  42.     return (
  43.       <option value={option}>
  44.         {option}
  45.       </option>
  46.     );
  47.   });
  48.   /* I figured out which attributes to provide by looking here:
  49.    *      https://reactjs.org/docs/forms.html
  50.    * This was a helpful reference, and it turns out that React
  51.    * treats these elements a little differently than HTML does.
  52.    */
  53.   return <select
  54.     name={props.name}
  55.     onChange={props.onChange}
  56.     value={props.checkedOption}>
  57.     {options}
  58.   </select>
  59. }
  60.  
  61. function validEmail(email) {
  62.   let emailExpression = /[\w-]+@[\w-]+\.\w+/;
  63.   let ldh = "[\\w-]";
  64.   /* I'm just using the strings to give each "piece" of the
  65.    * regular expression a name. Makes them easier to build and
  66.    * reason through. I build them like this in my head, anyway.
  67.    */
  68.   let name = `${ldh}+`;
  69.   let domain = `${ldh}\.\w+`;
  70.  
  71.   //let emailExpresion = RegExp(`^${name}@${domain}$$`);
  72.   return emailExpression.test(email);
  73. }
  74.  
  75. class Form extends React.Component {
  76.   constructor() {
  77.     super();
  78.     this.state = {
  79.       email: "",
  80.       confirm: "",
  81.       rating: "",
  82.       identity: "",
  83.       submitted: false
  84.     }
  85.   }
  86.  
  87.   onFormChange = (event, field) => {
  88.     console.log(event);
  89.     console.log(event.target);
  90.     console.log(event.target.value);
  91.     console.log(event.target.name);
  92.     this.setState({
  93.       [field]: event.target.value,
  94.     });
  95.   }
  96.  
  97.   incompleteSubmit = () => {
  98.     alert("Please finish the form by following the directions!");
  99.   }
  100.  
  101.   onSubmit = () => {
  102.     console.log("Submitting...");
  103.     this.setState({ submitted: true });
  104.   }
  105.  
  106.   render() {
  107.     let ratings = ["1", "2", "3", "4", "5"];
  108.     let identities = ["yes", "no", "maybe"];
  109.     let instructions = [];
  110.     console.log(this.state);
  111.     let formState = this.state;
  112.     if (formState.email === "") {
  113.       instructions.push("Type in an email.");
  114.     }
  115.     if (formState.confirm !== formState.email) {
  116.       instructions.push(
  117.         "Your confirmation email must match your email!");
  118.     }
  119.     if (!validEmail(formState.email)) {
  120.       instructions.push("You have not typed in a valid email.");
  121.     }
  122.     if (!formState.rating) {
  123.       instructions.push("Choose a rating for liking cats!");
  124.     }
  125.     if (!formState.identity) {
  126.       instructions.push("Decide on your identity in drop-down menu.");
  127.     }
  128.  
  129.     let renderedInstructions = instructions.map(instruction => {
  130.       return <li>{instruction}</li>
  131.     });
  132.  
  133.     let submitFunction = (instructions.length === 0 ?
  134.       this.onSubmit :
  135.       this.incompleteSubmit);
  136.  
  137.  
  138.     let recap = (
  139.       <ul>
  140.         <li>Your email is: {this.state.email}</li>
  141.         <li>On a scale of 1 to 5, you rate your love of cats at
  142.                     {" "} {formState.rating}. </li>
  143.         <li>
  144.           {formState.identity === "yes" ? "You are a cat." :
  145.             formState.identity === "maybe" ? "You may be a cat." :
  146.               formState.identity === "no" ? "You are not a cat." :
  147.                 "You are... something else. Something not of this list."}
  148.         </li>
  149.       </ul>
  150.     );
  151.  
  152.     return (
  153.       <div>
  154.         <Input
  155.           label="Email"
  156.           name="email"
  157.           value={this.state.email}
  158.           onChange={(event) => this.onFormChange(event, "email")} />
  159.         <Input
  160.           label="Confirm Email"
  161.           name="confirm"
  162.           value={this.state.confirm}
  163.           onChange={(event) => this.onFormChange(event, "confirm")} />
  164.         <RadioButtons
  165.           options={ratings}
  166.           name="rating"
  167.           checkedOption={this.state.rating}
  168.           onChange={(event) => this.onFormChange(event, "rating")} />
  169.         <Select
  170.           name="identity"
  171.           options={identities}
  172.           checkedOption={this.state.identity}
  173.           onChange={(event) => this.onFormChange(event, "identity")} />
  174.         <button
  175.           name="submitted"
  176.           onClick={submitFunction}>
  177.           Submit
  178.                 </button>
  179.  
  180.  
  181.         <ul>{renderedInstructions}</ul>
  182.         {this.state.submitted ? recap : ""}
  183.       </div>
  184.     );
  185.   }
  186. }
  187.  
  188. render(<Form />, document.getElementById("root"));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement