[
  {
    "path": ".gitignore",
    "content": "# See http://help.github.com/ignore-files/ for more about ignoring files.\n\n# dependencies\nnode_modules\n\n# testing\ncoverage\n\n# production\nbuild\n\n# misc\n.DS_Store\n.idea/\n.env\nnpm-debug.log\n"
  },
  {
    "path": ".nvmrc",
    "content": "6.2.1"
  },
  {
    "path": "README.md",
    "content": "### Tutorial: React.js Controlled Form Components\n\nThis repo is the companion to my blog post, [React.js Controlled Form Components](http://lorenstewart.me/2016/10/31/react-js-forms-controlled-components/).\n\n[DEMO](http://lorenstewart.me/react-controlled-form-components/)\n\nTo get started:\n 1. Make sure you're using Node 6 or higher (4 and higher will work, though)\n 2. `npm install create-react-app -g` (if you don't have it installed already)\n 3. `npm install`\n 4. `npm run start`\n 5. Open [http://localhost:3000/](http://localhost:3000/) in your browser\n"
  },
  {
    "path": "package.json",
    "content": "{\n  \"name\": \"react-form-components\",\n  \"version\": \"0.1.0\",\n  \"private\": true,\n  \"devDependencies\": {\n    \"react-scripts\": \"0.6.1\"\n  },\n  \"dependencies\": {\n    \"react\": \"^15.3.2\",\n    \"react-dom\": \"^15.3.2\",\n    \"spectre.css\": \"^0.1.27\"\n  },\n  \"scripts\": {\n    \"start\": \"react-scripts start\",\n    \"build\": \"react-scripts build\",\n    \"test\": \"react-scripts test --env=jsdom\",\n    \"eject\": \"react-scripts eject\"\n  }\n}\n"
  },
  {
    "path": "public/fake_db.json",
    "content": "{\n  \"ownerName\": \"\",\n  \"petSelections\": [\"dog\", \"cat\", \"rabbit\", \"iguana\", \"pony\", \"ferret\", \"fish\", \"bird\"],\n  \"selectedPets\": [\"dog\", \"cat\", \"ferret\"],\n  \"ageOptions\": [\"18 - 25\", \"26 - 59\", \"60 or older\"],\n  \"ownerAgeRangeSelection\": \"\",\n  \"siblingOptions\": [\"yes\", \"no\"],\n  \"siblingSelection\": [\"yes\"],\n  \"currentPetCount\": 0,\n  \"description\": \"\"\n}"
  },
  {
    "path": "public/index.html",
    "content": "<!doctype html>\n<html lang=\"en\">\n  <head>\n    <meta charset=\"utf-8\">\n    <meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">\n    <link rel=\"shortcut icon\" href=\"%PUBLIC_URL%/favicon.ico\">\n    <!--\n      Notice the use of %PUBLIC_URL% in the tag above.\n      It will be replaced with the URL of the `public` folder during the build.\n      Only files inside the `public` folder can be referenced from the HTML.\n\n      Unlike \"/favicon.ico\" or \"favico.ico\", \"%PUBLIC_URL%/favicon.ico\" will\n      work correctly both with client-side routing and a non-root public URL.\n      Learn how to configure a non-root public URL by running `npm run build`.\n    -->\n    <title>React App</title>\n  </head>\n  <body>\n    <div id=\"root\"></div>\n    <!--\n      This HTML file is a template.\n      If you open it directly in the browser, you will see an empty page.\n\n      You can add webfonts, meta tags, or analytics to this file.\n      The build step will place the bundled scripts into the <body> tag.\n\n      To begin the development, run `npm start`.\n      To create a production bundle, use `npm run build`.\n    -->\n  </body>\n</html>\n"
  },
  {
    "path": "src/App.js",
    "content": "import React, { Component } from 'react';\nimport '../node_modules/spectre.css/dist/spectre.min.css';\nimport './styles.css';\nimport FormContainer from './containers/FormContainer';\n\nclass App extends Component {\n  render() {\n    return (\n      <div className=\"container\">\n        <div className=\"columns\">\n          <div className=\"col-md-9 centered\">\n            <h3>React.js Controlled Form Components</h3>\n            <FormContainer />\n          </div>\n        </div>\n      </div>\n    );\n  }\n}\n\nexport default App;\n"
  },
  {
    "path": "src/components/CheckboxOrRadioGroup.js",
    "content": "import React from 'react';\n\nconst CheckboxOrRadioGroup = (props) => (\n\t<div>\n\t\t<label className=\"form-label\">{props.title}</label>\n\t\t<div className=\"checkbox-group\">\n\t\t\t{props.options.map(option => {\n\t\t\t\treturn (\n\t\t\t\t\t<label key={option} className=\"form-label capitalize\">\n\t\t\t\t\t\t<input\n\t\t\t\t\t\t\tclassName=\"form-checkbox\"\n\t\t\t\t\t\t\tname={props.setName}\n\t\t\t\t\t\t\tonChange={props.controlFunc}\n\t\t\t\t\t\t\tvalue={option}\n\t\t\t\t\t\t\tchecked={props.selectedOptions.indexOf(option) > -1}\n\t\t\t\t\t\t\ttype={props.type} /> {option}\n\t\t\t\t\t</label>\n\t\t\t\t);\n\t\t\t})}\n\t\t</div>\n\t</div>\n);\n\nCheckboxOrRadioGroup.propTypes = {\n\ttitle: React.PropTypes.string.isRequired,\n\ttype: React.PropTypes.oneOf(['checkbox', 'radio']).isRequired,\n\tsetName: React.PropTypes.string.isRequired,\n\toptions: React.PropTypes.array.isRequired,\n\tselectedOptions: React.PropTypes.array,\n\tcontrolFunc: React.PropTypes.func.isRequired\n};\n\nexport default CheckboxOrRadioGroup;\n"
  },
  {
    "path": "src/components/Select.js",
    "content": "import React from 'react';\n\nconst Select = (props) => (\n\t<div className=\"form-group\">\n\t\t<select\n\t\t\tname={props.name}\n\t\t\tvalue={props.selectedOption}\n\t\t\tonChange={props.controlFunc}\n\t\t\tclassName=\"form-select\">\n\t\t\t<option value=\"\">{props.placeholder}</option>\n\t\t\t{props.options.map(opt => {\n\t\t\t\treturn (\n\t\t\t\t\t<option\n\t\t\t\t\t\tkey={opt}\n\t\t\t\t\t\tvalue={opt}>{opt}</option>\n\t\t\t\t);\n\t\t\t})}\n\t\t</select>\n\t</div>\n);\n\nSelect.propTypes = {\n\tname: React.PropTypes.string.isRequired,\n\toptions: React.PropTypes.array.isRequired,\n\tselectedOption: React.PropTypes.string,\n\tcontrolFunc: React.PropTypes.func.isRequired,\n\tplaceholder: React.PropTypes.string\n};\n\nexport default Select;"
  },
  {
    "path": "src/components/SingleInput.js",
    "content": "import React from 'react';\n\nconst SingleInput = (props) => (\n\t<div className=\"form-group\">\n\t\t<label className=\"form-label\">{props.title}</label>\n\t\t<input\n\t\t\tclassName=\"form-input\"\n\t\t\tname={props.name}\n\t\t\ttype={props.inputType}\n\t\t\tvalue={props.content}\n\t\t\tonChange={props.controlFunc}\n\t\t\tplaceholder={props.placeholder} />\n\t</div>\n);\n\nSingleInput.propTypes = {\n\tinputType: React.PropTypes.oneOf(['text', 'number']).isRequired,\n\ttitle: React.PropTypes.string.isRequired,\n\tname: React.PropTypes.string.isRequired,\n\tcontrolFunc: React.PropTypes.func.isRequired,\n\tcontent: React.PropTypes.oneOfType([\n\t\tReact.PropTypes.string,\n\t\tReact.PropTypes.number,\n\t]).isRequired,\n\tplaceholder: React.PropTypes.string,\n};\n\nexport default SingleInput;"
  },
  {
    "path": "src/components/TextArea.js",
    "content": "import React from 'react';\n\nconst TextArea = (props) => (\n\t<div className=\"form-group\">\n\t\t<label className=\"form-label\">{props.title}</label>\n\t\t<textarea\n\t\t\tclassName=\"form-input\"\n\t\t\tstyle={props.resize ? null : {resize: 'none'}}\n\t\t\tname={props.name}\n\t\t\trows={props.rows}\n\t\t\tvalue={props.content}\n\t\t\tonChange={props.controlFunc}\n\t\t\tplaceholder={props.placeholder} />\n\t</div>\n);\n\nTextArea.propTypes = {\n\ttitle: React.PropTypes.string.isRequired,\n\trows: React.PropTypes.number.isRequired,\n\tname: React.PropTypes.string.isRequired,\n\tcontent: React.PropTypes.string.isRequired,\n\tresize: React.PropTypes.bool,\n\tplaceholder: React.PropTypes.string,\n\tcontrolFunc: React.PropTypes.func.isRequired\n};\n\nexport default TextArea;"
  },
  {
    "path": "src/containers/FormContainer.js",
    "content": "import React, {Component} from 'react';\nimport CheckboxOrRadioGroup from '../components/CheckboxOrRadioGroup';\nimport SingleInput from '../components/SingleInput';\nimport TextArea from '../components/TextArea';\nimport Select from '../components/Select';\n\nclass FormContainer extends Component {\n\tconstructor(props) {\n\t\tsuper(props);\n\t\tthis.state = {\n\t\t\townerName: '',\n\t\t\tpetSelections: [],\n\t\t\tselectedPets: [],\n\t\t\tageOptions: [],\n\t\t\townerAgeRangeSelection: '',\n\t\t\tsiblingOptions: [],\n\t\t\tsiblingSelection: [],\n\t\t\tcurrentPetCount: 0,\n\t\t\tdescription: ''\n\t\t};\n\t\tthis.handleFormSubmit = this.handleFormSubmit.bind(this);\n\t\tthis.handleClearForm = this.handleClearForm.bind(this);\n\t\tthis.handleFullNameChange = this.handleFullNameChange.bind(this);\n\t\tthis.handleCurrentPetCountChange = this.handleCurrentPetCountChange.bind(this);\n\t\tthis.handleAgeRangeSelect = this.handleAgeRangeSelect.bind(this);\n\t\tthis.handlePetSelection = this.handlePetSelection.bind(this);\n\t\tthis.handleSiblingsSelection = this.handleSiblingsSelection.bind(this);\n\t\tthis.handleDescriptionChange = this.handleDescriptionChange.bind(this);\n\t}\n\tcomponentDidMount() {\n\t\tfetch('./fake_db.json')\n\t\t\t.then(res => res.json())\n\t\t\t.then(data => {\n\t\t\t\tthis.setState({\n\t\t\t\t\townerName: data.ownerName,\n\t\t\t\t\tpetSelections: data.petSelections,\n\t\t\t\t\tselectedPets: data.selectedPets,\n\t\t\t\t\tageOptions: data.ageOptions,\n\t\t\t\t\townerAgeRangeSelection: data.ownerAgeRangeSelection,\n\t\t\t\t\tsiblingOptions: data.siblingOptions,\n\t\t\t\t\tsiblingSelection: data.siblingSelection,\n\t\t\t\t\tcurrentPetCount: data.currentPetCount,\n\t\t\t\t\tdescription: data.description\n\t\t\t\t});\n\t\t\t});\n\t}\n\thandleFullNameChange(e) {\n\t\tthis.setState({ ownerName: e.target.value }, () => console.log('name:', this.state.ownerName));\n\t}\n\thandleCurrentPetCountChange(e) {\n\t\tthis.setState({ currentPetCount: e.target.value }, () => console.log('pet count', this.state.currentPetCount));\n\t}\n\thandleAgeRangeSelect(e) {\n\t\tthis.setState({ ownerAgeRangeSelection: e.target.value }, () => console.log('age range', this.state.ownerAgeRangeSelection));\n\t}\n\thandlePetSelection(e) {\n\t\tconst newSelection = e.target.value;\n\t\tlet newSelectionArray;\n\t\tif(this.state.selectedPets.indexOf(newSelection) > -1) {\n\t\t\tnewSelectionArray = this.state.selectedPets.filter(s => s !== newSelection)\n\t\t} else {\n\t\t\tnewSelectionArray = [...this.state.selectedPets, newSelection];\n\t\t}\n\t\tthis.setState({ selectedPets: newSelectionArray }, () => console.log('pet selection', this.state.selectedPets));\n\t}\n\thandleSiblingsSelection(e) {\n\t\tthis.setState({ siblingSelection: [e.target.value] }, () => console.log('siblingz', this.state.siblingSelection));\n\t}\n\thandleDescriptionChange(e) {\n\t\t// const textArray = e.target.value.split('').filter(x => x !== 'e');\n\t\t// console.log('string split into array of letters',textArray);\n\t\t// const filteredText = textArray.join('');\n\t\t// this.setState({ description: filteredText }, () => console.log('description', this.state.description));\n\t\tthis.setState({ description: e.target.value }, () => console.log('description', this.state.description));\n\t}\n\thandleClearForm(e) {\n\t\te.preventDefault();\n\t\tthis.setState({\n\t\t\townerName: '',\n\t\t\tselectedPets: [],\n\t\t\townerAgeRangeSelection: '',\n\t\t\tsiblingSelection: [],\n\t\t\tcurrentPetCount: 0,\n\t\t\tdescription: ''\n\t\t});\n\t}\n\thandleFormSubmit(e) {\n\t\te.preventDefault();\n\n\t\tconst formPayload = {\n\t\t\townerName: this.state.ownerName,\n\t\t\tselectedPets: this.state.selectedPets,\n\t\t\townerAgeRangeSelection: this.state.ownerAgeRangeSelection,\n\t\t\tsiblingSelection: this.state.siblingSelection,\n\t\t\tcurrentPetCount: this.state.currentPetCount,\n\t\t\tdescription: this.state.description\n\t\t};\n\n\t\tconsole.log('Send this in a POST request:', formPayload);\n\t\tthis.handleClearForm(e);\n\t}\n\trender() {\n\t\treturn (\n\t\t\t<form className=\"container\" onSubmit={this.handleFormSubmit}>\n\t\t\t\t<h5>Pet Adoption Form</h5>\n\t\t\t\t<SingleInput\n\t\t\t\t\tinputType={'text'}\n\t\t\t\t\ttitle={'Full name'}\n\t\t\t\t\tname={'name'}\n\t\t\t\t\tcontrolFunc={this.handleFullNameChange}\n\t\t\t\t\tcontent={this.state.ownerName}\n\t\t\t\t\tplaceholder={'Type first and last name here'} />\n\t\t\t\t<Select\n\t\t\t\t\tname={'ageRange'}\n\t\t\t\t\tplaceholder={'Choose your age range'}\n\t\t\t\t\tcontrolFunc={this.handleAgeRangeSelect}\n\t\t\t\t\toptions={this.state.ageOptions}\n\t\t\t\t\tselectedOption={this.state.ownerAgeRangeSelection} />\n\t\t\t\t<CheckboxOrRadioGroup\n\t\t\t\t\ttitle={'Which kinds of pets would you like to adopt?'}\n\t\t\t\t\tsetName={'pets'}\n\t\t\t\t\ttype={'checkbox'}\n\t\t\t\t\tcontrolFunc={this.handlePetSelection}\n\t\t\t\t\toptions={this.state.petSelections}\n\t\t\t\t\tselectedOptions={this.state.selectedPets} />\n\t\t\t\t<CheckboxOrRadioGroup\n\t\t\t\t\ttitle={'Are you willing to adopt more than one pet if we have siblings for adoption?'}\n\t\t\t\t\tsetName={'siblings'}\n\t\t\t\t\tcontrolFunc={this.handleSiblingsSelection}\n\t\t\t\t\ttype={'radio'}\n\t\t\t\t\toptions={this.state.siblingOptions}\n\t\t\t\t\tselectedOptions={this.state.siblingSelection} />\n\t\t\t\t<SingleInput\n\t\t\t\t\tinputType={'number'}\n\t\t\t\t\ttitle={'How many pets do you currently own?'}\n\t\t\t\t\tname={'currentPetCount'}\n\t\t\t\t\tcontrolFunc={this.handleCurrentPetCountChange}\n\t\t\t\t\tcontent={this.state.currentPetCount}\n\t\t\t\t\tplaceholder={'Enter number of current pets'} />\n\t\t\t\t<TextArea\n\t\t\t\t\ttitle={'If you currently own pets, please write their names, breeds, and an outline of their personalities.'}\n\t\t\t\t\trows={5}\n\t\t\t\t\tresize={false}\n\t\t\t\t\tcontent={this.state.description}\n\t\t\t\t\tname={'currentPetInfo'}\n\t\t\t\t\tcontrolFunc={this.handleDescriptionChange}\n\t\t\t\t\tplaceholder={'Please be thorough in your descriptions'} />\n\t\t\t\t<input\n\t\t\t\t\ttype=\"submit\"\n\t\t\t\t\tclassName=\"btn btn-primary float-right\"\n\t\t\t\t\tvalue=\"Submit\"/>\n\t\t\t\t<button\n\t\t\t\t\tclassName=\"btn btn-link float-left\"\n\t\t\t\t\tonClick={this.handleClearForm}>Clear form</button>\n\t\t\t</form>\n\t\t);\n\t}\n}\n\nexport default FormContainer;\n"
  },
  {
    "path": "src/index.js",
    "content": "import React from 'react';\nimport ReactDOM from 'react-dom';\nimport App from './App';\n\nReactDOM.render(\n  <App />,\n  document.getElementById('root')\n);\n"
  },
  {
    "path": "src/styles.css",
    "content": "body {\n    padding-bottom: 80px;\n}\nform {\n    border: 3px solid #ccc;\n    padding: 0 20px 25px 20px;\n    border-radius: 10px;\n}\ninput[type=checkbox],\ninput[type=radio] {\n    margin-right: 8px;\n}\n.form-label.capitalize {\n    text-transform: capitalize;\n}\n.checkbox-group {\n    display: flex;\n    justify-content: space-between;\n    flex-wrap: wrap;\n}\n.checkbox-group .form-label {\n    width: 50%;\n}\ninput[type=submit],\n.btn-link {\n    margin-top: 20px;\n}"
  }
]