Advertisement
Guest User

Untitled

a guest
Oct 19th, 2019
258
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //App.js
  2. import React, { Component } from 'react';
  3. import Paper from '@material-ui/core/Paper';
  4. import { withStyles } from '@material-ui/styles';
  5. import Grid from '@material-ui/core/Grid';
  6. import FeatureInputFields from './FeatureInputFields';
  7. import FeatureHistory from './FeatureHistory';
  8.  
  9.  
  10. const styles = theme => ({
  11.   root: {
  12.     margin: '10px',
  13.     height: 400,
  14.     width: 300,
  15.     padding: '0 30px',
  16.     overflowY: 'scroll',
  17.   },
  18. });
  19.  
  20. class App extends Component {
  21.  
  22.   constructor(props) {
  23.     super(props)
  24.     this.state = {
  25.       field1: '',
  26.       field2: '',
  27.       field3: '',
  28.       fieldHistory: [["", "", "", ""]]
  29.     }
  30.     this.handleSubmit = this.handleSubmit.bind(this);
  31.     this.updateField = this.updateField.bind(this);
  32.     this.restoreState = this.restoreState.bind(this);
  33.   }
  34.  
  35.   updateField(fieldName, newValue) {
  36.     console.log('Updating field data in store');
  37.     this.setState({
  38.         [fieldName]: newValue
  39.     });
  40.   }
  41.  
  42.   handleSubmit() {
  43.     console.log(this.state)
  44.     this.setState({
  45.       fieldHistory: [...this.state.fieldHistory, [this.state.field1, this.state.field2, this.state.field3, Date()]]
  46.     });
  47.   }
  48.  
  49.   restoreState(index) {
  50.     this.setState({
  51.       field1: this.state.fieldHistory[index][0],
  52.       field2: this.state.fieldHistory[index][1],
  53.       field3: this.state.fieldHistory[index][2]
  54.     })
  55.   }
  56.  
  57.   render () {
  58.     const { classes } = this.props;
  59.     return (
  60.         <Grid container spacing={0}>
  61.           <Grid item>
  62.             <Paper className={classes.root}>
  63.               <FeatureInputFields fieldValues={ this.state } handleChange={ (fieldName, fieldValue) => this.updateField(fieldName, fieldValue) } handleSubmit={this.handleSubmit}/>
  64.             </Paper>
  65.           </Grid>
  66.           <Grid item>
  67.             <Paper className={classes.root}>
  68.               <FeatureHistory fieldHistory={ this.state.fieldHistory } restoreState={ this.restoreState }/>
  69.             </Paper>
  70.           </Grid>          
  71.         </Grid>
  72.     );
  73.   }
  74. }
  75. export default withStyles(styles)(App);
  76.  
  77. //FeatureInputFields.js
  78. import React from 'react';
  79. import { withStyles } from '@material-ui/styles';
  80. import Grid from '@material-ui/core/Grid';
  81. import TextField from '@material-ui/core/TextField';
  82.  
  83. const styles = theme => ({
  84.   textField: {
  85.     //marginLeft: theme.spacing(1),
  86.     // marginRight: theme.spacing(1),
  87.     width: 200,
  88.   }
  89. });
  90.  
  91. function FeatureInputFields(props) {
  92.     const { classes } = props;
  93.     return (
  94.       <div>
  95.           <h3>Input Fields</h3>
  96.           <Grid container spacing={0}>
  97.             <Grid item xs={12}>
  98.               <TextField
  99.               id="standard-name"
  100.               label="Field1"
  101.               test-class='field1input'
  102.               className={classes.textField}
  103.               value={props.fieldValues.field1}
  104.               onChange={(event) => props.handleChange('field1', event.target.value)}
  105.               onBlur={(props.fieldValues.fieldHistory[props.fieldValues.fieldHistory.length-1][0] != props.fieldValues.field1 ? props.handleSubmit : null)}
  106.               margin="normal"
  107.             />
  108.             </Grid>
  109.             <Grid item xs={12}>
  110.               <TextField
  111.               id="standard-name"
  112.               label="Field2"
  113.               test-class='field2input'
  114.               className={classes.textField}
  115.               value={props.fieldValues.field2}
  116.               onChange={(event) => props.handleChange('field2', event.target.value)}
  117.               onBlur={(props.fieldValues.fieldHistory[props.fieldValues.fieldHistory.length-1][1] != props.fieldValues.field2 ? props.handleSubmit : null)}
  118.               margin="normal"
  119.             />
  120.             </Grid>
  121.             <Grid item xs={12}>
  122.               <TextField
  123.               id="standard-name"
  124.               label="Field3"
  125.               test-class='field3input'
  126.               className={classes.textField}
  127.               value={props.fieldValues.field3}
  128.               onChange={(event) => props.handleChange('field3', event.target.value)}
  129.               onBlur={(props.fieldValues.fieldHistory[props.fieldValues.fieldHistory.length-1][2] != props.fieldValues.field3 ? props.handleSubmit : null)}
  130.               margin="normal"
  131.             />
  132.             </Grid>
  133.           </Grid>
  134.       </div>
  135.     )
  136. };
  137.  
  138. export default withStyles(styles)(FeatureInputFields);
  139.  
  140. //FeatureHistory.js
  141. import React from 'react';
  142. import List from '@material-ui/core/List';
  143. import ListItem from '@material-ui/core/ListItem';
  144. import ListItemText from '@material-ui/core/ListItemText';
  145. import IconButton from '@material-ui/core/IconButton';
  146. import ListItemSecondaryAction from '@material-ui/core/ListItemSecondaryAction';
  147. import ReloadIcon from '@material-ui/icons/Update'
  148.  
  149.  
  150. const list = function(nestedArray, props) {
  151.   return nestedArray.slice(1, ).map(
  152. (el, index) =>
  153.   <ListItem button alignItems="flex-start" key={index}>
  154.     <ListItemText
  155.     primary={el.slice(0,3).map((value, index) => <div>{"Field".concat(index+1, ": ", value)}</div>)}/>
  156.     <ListItemSecondaryAction onClick={() => props.restoreState(index+1)}>
  157.       <IconButton edge="end" aria-label="reload">
  158.       <ReloadIcon />
  159.       </IconButton>
  160.       </ListItemSecondaryAction>
  161.   </ListItem>
  162.   ).reverse()
  163. }
  164.  
  165. function FeatureHistory(props) {
  166.     const fields = list(props.fieldHistory, props)
  167.     return (
  168.      <div>
  169.       <h3>Feature History</h3>
  170.       <List component="nav" aria-label="main mailbox folders">
  171.         {fields}
  172.       </List>
  173.     </div>
  174.     )
  175. };
  176.  
  177. export default FeatureHistory;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement