form.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. import React from 'react';
  2. import PropTypes from 'prop-types';
  3. import { withStyles } from '@material-ui/core/styles';
  4. import MenuItem from '@material-ui/core/MenuItem';
  5. import TextField from '@material-ui/core/TextField';
  6. import Select from '@material-ui/core/Select';
  7. import { FormControl, Input, InputLabel, FormHelperText } from '@material-ui/core'
  8. const styles = theme => ({
  9. container: {
  10. display: 'block',
  11. },
  12. textField: {
  13. marginLeft: theme.spacing.unit,
  14. marginRight: theme.spacing.unit,
  15. width: 200,
  16. },
  17. dense: {
  18. marginTop: 19,
  19. },
  20. menu: {
  21. width: 200,
  22. },
  23. fullWidth: {
  24. width: '100%'
  25. }
  26. });
  27. const foodItems = [
  28. 'Cake',
  29. 'Turkey',
  30. 'Roasted Dog',
  31. 'Turnip',
  32. 'Ratatouille'
  33. ]
  34. class TextFields extends React.Component {
  35. state = {
  36. name: 'The Grinch',
  37. foodItem: '',
  38. significantOther: false
  39. };
  40. handleChange = name => event => {
  41. this.setState({
  42. [name]: event.target.value,
  43. });
  44. };
  45. handleFoodSelect = (event) => {
  46. console.log('FOOD', event.target.value)
  47. this.setState({foodItem: event.target.value})
  48. }
  49. handleSignificantOther = () => {
  50. this.setState({significantOther: !this.state.significantOther})
  51. }
  52. FoodItems = () => {
  53. const items = []
  54. for (let i = 0; i < foodItems.length; i++) {
  55. items.push(<MenuItem key={i} value={foodItems[i]}>{foodItems[i]}</MenuItem>)
  56. }
  57. return items
  58. }
  59. render() {
  60. const { classes } = this.props;
  61. return (
  62. <form className={classes.container} noValidate autoComplete="off">
  63. <div style={styles.fullWidth}>
  64. <TextField
  65. id="standard-name"
  66. label="Name"
  67. className={classes.textField}
  68. value={this.state.name}
  69. onChange={this.handleChange('name')}
  70. margin="normal"
  71. />
  72. </div>
  73. <div style={styles.fullWidth}>
  74. <InputLabel htmlFor="significant-other">Bringing Someone?</InputLabel>
  75. <input
  76. id="significant-other"
  77. name="Significant Other"
  78. type="checkbox"
  79. checked={this.state.significantOther}
  80. onChange={this.handleSignificantOther} />
  81. </div>
  82. <FormControl className='food-select' style={styles.fullWidth}>
  83. <InputLabel htmlFor="food-helper">What You Got, Gangster?</InputLabel>
  84. <Select style={styles.fullWidth}
  85. value={this.state.foodItem}
  86. onChange={this.handleFoodSelect}
  87. input={<Input name="food" id="food-helper" value={this.state.foodItem}/>}
  88. >
  89. <MenuItem value="">
  90. <em>Not a damn thing!</em>
  91. </MenuItem>
  92. {this.FoodItems()}
  93. </Select>
  94. <FormHelperText>Bring Us Food</FormHelperText>
  95. </FormControl>
  96. </form>
  97. );
  98. }
  99. }
  100. TextFields.propTypes = {
  101. classes: PropTypes.object.isRequired,
  102. };
  103. export default withStyles(styles)(TextFields);