Advertisement
Guest User

Untitled

a guest
Jun 16th, 2019
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.55 KB | None | 0 0
  1. import React, {Component} from "react";
  2. import PropTypes from "prop-types";
  3. import {
  4. Table,
  5. TableBody,
  6. TableHeader,
  7. TableHeaderColumn,
  8. TableRow,
  9. TableRowColumn
  10. } from "material-ui/Table";
  11. import Dialog from "@material-ui/core/Dialog";
  12.  
  13. export const RowParser = row => {
  14. const reg = /"(.*?)"/g;
  15. const matches = row.match(reg);
  16. if (matches) {
  17. return matches.map(value => {
  18. return value.substr(1, value.length - 2);
  19. });
  20. }
  21. return row.split(",");
  22. };
  23.  
  24. class CSVTable extends Component {
  25. constructor(props) {
  26. super(props);
  27. const {data, header} = this.props;
  28. const rows = [...data.split("n")];
  29. const contentRows = header ? rows.slice(1,rows.length) : rows;
  30. this.state = {
  31. open: false,
  32. title: '',
  33. contentRows: contentRows
  34. };
  35.  
  36. }
  37.  
  38. handleClickOpen=(rowIndex)=> {
  39. // Do whatever you want with the row data
  40. const rowData = this.state.contentRows[rowIndex];
  41. console.log(rowData);
  42. this.setState({
  43. open:true
  44.  
  45. })
  46. };
  47.  
  48. handleClose = () => {
  49. this.setState({open: false});
  50. };
  51.  
  52. static propTypes = {
  53. data: PropTypes.string.isRequired,
  54. header: PropTypes.bool
  55. };
  56. static defaultProps = {
  57. data: "",
  58. header: false,
  59.  
  60. };
  61.  
  62. rowParser = row => {
  63. return RowParser(row);
  64. };
  65.  
  66. render() {
  67. const {data, header} = this.props;
  68. const rows = [...data.split("n")];
  69. const contentRows = header ? rows.slice(1,rows.length) : rows;
  70.  
  71. return (
  72. <div>
  73.  
  74. <Table>
  75. {header && (
  76. <TableHeader adjustForCheckbox={false} displaySelectAll={false}>
  77. <TableRow>
  78. {this.rowParser(rows[0]).map((value, index) => (
  79. <TableHeaderColumn key={index}>{value}</TableHeaderColumn>
  80. ))}
  81. </TableRow>
  82. </TableHeader>
  83. )}
  84.  
  85. <TableBody displayRowCheckbox={false}>
  86. {contentRows.map((row, rowIndex) => (
  87.  
  88. <TableRow key={rowIndex} onDoubleClick={this.handleClickOpen}>
  89. {this.rowParser(row).map((value, colIndex) => (
  90. <TableRowColumn key={colIndex} data={value}
  91. style={{cursor: 'pointer'}}>{value}
  92. </TableRowColumn>
  93. ))}
  94. <button type="button" id ={rowIndex}
  95. onClick={() => this.handleClickOpen(rowIndex)}>
  96. Edit
  97. </button>
  98. </TableRow>
  99.  
  100. ))}
  101.  
  102. <Dialog open={this.state.open}
  103. onClose={this.handleClose} maxWidth={"md"} maxHeight={"md"}>
  104. <div style={{ width: '400px',
  105. height: '500px',
  106. margin: '55px'}}>
  107. {header &&(
  108. <TableHeader adjustForCheckbox={false} displaySelectAll={false}>
  109.  
  110. <TableRow>
  111. {this.rowParser(rows[0]).map((value, index) => (
  112. <tr key={index}>{value}</tr>
  113.  
  114. ))}
  115. </TableRow>
  116. </TableHeader>
  117. )}
  118. </div>
  119. <div style={{cursor: 'pointer', margin: '-287px 150px 200px'}}>
  120. <TableRow>
  121. {this.rowParser(rows[1]).map((value, colIndex) => (
  122. <tr key={colIndex}>
  123. <input value={value}/>
  124.  
  125. </tr>
  126. ))}
  127. </TableRow>
  128. </div>
  129. </Dialog>
  130.  
  131. </TableBody>
  132. </Table>
  133. </div>
  134. );
  135. }
  136. }
  137. CSVTable.propTypes = {
  138. fullScreen: PropTypes.bool.isRequired,
  139. };
  140.  
  141.  
  142. export default CSVTable;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement