Created
          July 16, 2016 21:00 
        
      - 
      
- 
        Save timwis/d300e895b69b8f72e8650d5707dd9ed5 to your computer and use it in GitHub Desktop. 
    choo form component example
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | const getFormData = require('get-form-data') | |
| const html = require('choo/html') | |
| module.exports = function formComponent (opts) { | |
| return ` | |
| <form> | |
| <input name="woof" type="text" placeholder="type here" | |
| value=${opts.values.woof} oninput=${onInput}> | |
| <input type="submit" onsubmit=${onSubmit}> | |
| </form> | |
| ` | |
| function onInput (e) { | |
| const data = e.value | |
| opts.onInput && opts.onInput(data) // allows onInput callback to be optional | |
| } | |
| function onSubmit (e) { | |
| const data = getFormData(e.target) | |
| opts.onSubmit && opts.onSubmit(data) | |
| e.preventDefault() // not called if above lines throw error, degrading to default form functionality | |
| } | |
| } | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | const html = require('choo/html') | |
| const Form = require('../components/form') | |
| module.exports = function mainView (state, prev, send) { | |
| const form = Form({ | |
| values: state.myFormModel, | |
| onInput: (data) => send('myFormModel:validate', data), | |
| onSubmit: (data) => send('myFormModel:submit', data) | |
| }) | |
| return html` | |
| <main> | |
| ${form} | |
| </main> | |
| ` | |
| } | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment