DemoEditPrimary.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. import React from 'react'
  2. import DemoEditSecondary from './DemoEditSecondary'
  3. class DemoEditPrimary extends React.Component {
  4. constructor () {
  5. super()
  6. this.handleChange = this.handleChange.bind(this)
  7. this.handleSubmit = this.handleSubmit.bind(this)
  8. }
  9. handleChange (event) {
  10. const { actions } = this.props
  11. const { id, name, value } = event.target
  12. const passedValue = (name === 'active') ? (value === 'active') : value
  13. actions.updatePrimary(id, {[name]: passedValue})
  14. }
  15. handleSubmit (event) {
  16. event.preventDefault()
  17. // Save values in database.
  18. }
  19. render () {
  20. const { primary } = this.props
  21. return (
  22. <div>
  23. <form onSubmit={this.handleSubmit}>
  24. <input type='text' id={primary.id} name='name' ref={input => { this.name = input }} value={primary.name} onChange={this.handleChange} />
  25. <select id={primary.id} name='active' ref={input => { this.active = input }} value={primary.active ? 'active' : 'inactive'} onChange={this.handleChange}>
  26. <option value='active'>active</option>
  27. <option value='inactive'>inactive</option>
  28. </select>
  29. <button type='submit'>Save</button>
  30. </form>
  31. {primary.secondary.map((secondary, key) => (
  32. <DemoEditSecondary key={key} primaryKey={primary.id} secondary={secondary} updateSecondary={this.props.actions.updateSecondary} />
  33. ))}
  34. </div>
  35. )
  36. }
  37. }
  38. export default DemoEditPrimary