Advertisement
beelzebielsk

variable-length-list.js

Dec 15th, 2017
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import React, { Component } from 'react';
  2.  
  3. function CheckMark(props) {
  4.     return (
  5.         <input
  6.             type="checkbox"
  7.             checked={props.checked}
  8.             onChange={props.onChange}/>
  9.     );
  10. }
  11.  
  12. /* The list will take two properties:
  13.  * entries: An object where:
  14.  * - The key of each entry is a label of a list item.
  15.  * - The value of each entry is the text for the corresponding list
  16.  *   item.
  17.  * none: The text for the "none of the other options" entry. For now,
  18.  * I'm still treating this specially.
  19.  */
  20. class List extends Component {
  21.     constructor(props) {
  22.         super(props);
  23.  
  24.         /*
  25.             let state = {}
  26.         for (let label in props.entries) {
  27.             state[label] = false;
  28.         }
  29.         state.none = false;
  30.         this.state = state;
  31.         */
  32.  
  33.         this.state = {};
  34.         for (let label in props.entries) {
  35.             this.state[label] = false;
  36.         }
  37.         this.state.none = false;
  38.  
  39.         this.checkEntry = this.checkEntry.bind(this);
  40.         this.checkNone = this.checkNone.bind(this);
  41.     }
  42.  
  43.     checkEntry(label) {
  44.         console.log(`Check ${label}.`);
  45.         /* I do this because I want to change the state of several
  46.          * things, but change the overall state of the component once.
  47.          * I can't use an object literal because I don't know the
  48.          * names of all of the keys ahead of time. Though, if I
  49.          * remember correctly, it might be possible in ES6 right now
  50.          * to do computed field names in an objet literal.
  51.          */
  52.         let state = {}
  53.         state[label] = !this.state[label];
  54.         state.none = false;
  55.         this.setState(state);
  56.     }
  57.  
  58.     checkNone() {
  59.         console.log("Check none.");
  60.         let state = {};
  61.         for (let label in this.props.entries) {
  62.             state[label] = false;
  63.         }
  64.         state.none = true;
  65.         this.setState(state);
  66.     }
  67.  
  68.     render() {
  69.         console.log(this.state);
  70.         return (
  71.             <ul>
  72.             {
  73.                 /* First, render the animal choices. To do this, I map
  74.                  * over a list of labels for each choice, and pretty
  75.                  * similarly to the basic version, each animal choice
  76.                  * is represented by a list item (li) that contains a
  77.                  * checkmark. I set the state of the checkmark based
  78.                  * on the state of the list.
  79.                  */
  80.                 Object.keys(this.props.entries).map(label => {
  81.                     return (
  82.                         <li key={label}>
  83.                             {this.props.entries[label]}
  84.                             <CheckMark
  85.                                 checked={this.state[label]}
  86.                                 onChange={() => this.checkEntry(label)}/>
  87.                         </li>
  88.                     );
  89.                 })
  90.             }
  91.             <li key="none">
  92.                 {this.props.none}
  93.                 <CheckMark
  94.                     checked={this.state.none}
  95.                     onChange={this.checkNone}/>
  96.             </li>
  97.             </ul>
  98.         );
  99.     }
  100. }
  101.  
  102. let entries = {
  103.     cat : "I like cats (fuck that).",
  104.     dog : "I like dogs (I'm a compulsive liar).",
  105.     iguana : "Iguanas are okay.",
  106.     'komodo dragon' : "Komodo dragons? Where!?",
  107. }
  108.  
  109. class App extends Component {
  110.   render() {
  111.       return <List entries={entries} none="I hate them all."/>
  112.   }
  113. }
  114.  
  115. export default App;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement