Guest User

Untitled

a guest
Jun 21st, 2018
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.23 KB | None | 0 0
  1. import React from 'react';
  2. import classNames from 'classnames';
  3. import PropTypes from 'prop-types';
  4. import { withStyles } from '@material-ui/core/styles';
  5. import Table from '@material-ui/core/Table';
  6. import TableBody from '@material-ui/core/TableBody';
  7. import TableCell from '@material-ui/core/TableCell';
  8. import TableHead from '@material-ui/core/TableHead';
  9. import TablePagination from '@material-ui/core/TablePagination';
  10. import TableRow from '@material-ui/core/TableRow';
  11. import TableSortLabel from '@material-ui/core/TableSortLabel';
  12. import Toolbar from '@material-ui/core/Toolbar';
  13. import Typography from '@material-ui/core/Typography';
  14. import Paper from '@material-ui/core/Paper';
  15. import Checkbox from '@material-ui/core/Checkbox';
  16. import IconButton from '@material-ui/core/IconButton';
  17. import Tooltip from '@material-ui/core/Tooltip';
  18. import DeleteIcon from '@material-ui/icons/Delete';
  19. import FilterListIcon from '@material-ui/icons/FilterList';
  20. import { lighten } from '@material-ui/core/styles/colorManipulator';
  21. import InfoIcon from '@material-ui/icons/Info';
  22. import EditIcon from '@material-ui/icons/Edit';
  23. import VisibilityIcon from '@material-ui/icons/Visibility';
  24. import Select from "@material-ui/core/Select";
  25. import MenuItem from "@material-ui/core/MenuItem";
  26.  
  27. const selectStyles = theme => ({
  28. selectRoot : {
  29. color: theme.palette.text.secondary,
  30. fontSize: '0.75rem',
  31. fontWeidht: '500',
  32. }
  33. });
  34.  
  35. let StyledSelect = ({classes, children,...otherProps}) => (
  36. <Select
  37. {...otherProps}
  38. classes = {{
  39. root: classes.selectRoot,
  40. }}
  41. >
  42. {children}
  43. </Select>
  44. );
  45.  
  46. StyledSelect = withStyles(selectStyles)(StyledSelect);
  47.  
  48. class EnhancedTableHead extends React.Component {
  49. createSortHandler = property => event => {
  50. this.props.onRequestSort(event, property);
  51. };
  52.  
  53. render() {
  54. const {
  55. onSelectAllClick,
  56. order,
  57. orderBy,
  58. numSelected,
  59. rowCount,
  60. columnData,
  61. columnConfig,
  62. columnChanger,
  63. showActions,
  64. } = this.props;
  65.  
  66. return (
  67. <TableHead>
  68. <TableRow>
  69. <TableCell padding="checkbox">
  70. <Checkbox
  71. indeterminate={numSelected > 0 && numSelected < rowCount}
  72. checked={numSelected === rowCount}
  73. onChange={onSelectAllClick}
  74. />
  75. </TableCell>
  76. {columnData.map(column => {
  77. let columnOptions = columnConfig && columnConfig[column.id];
  78. if(columnOptions && columnOptions.length) {
  79. return (
  80. <TableCell
  81. key={column.id}
  82. numeric={ column.numeric === undefined ? true : column.numeric}
  83. padding={ column.disablePadding ? 'none' : 'default'}
  84. >
  85. <StyledSelect
  86. value={columnConfig[column.id][0].id}
  87. onChange={(event)=>{columnChanger(column.id,event.target.value)}}
  88. disableUnderline
  89. name="Scope"
  90. >
  91. {
  92. columnOptions.map((option)=>
  93. <MenuItem key={option.id} value={option.id}>{option.label}</MenuItem>
  94. )
  95. }
  96. </StyledSelect>
  97. </TableCell>
  98. )
  99. }
  100. return (
  101. <TableCell
  102. key={column.id}
  103. numeric={ column.numeric === undefined ? true : column.numeric}
  104. padding={ column.disablePadding ? 'none' : 'default'}
  105. sortDirection={orderBy === column.id ? order : false}
  106. >
  107. <Tooltip
  108. title="Sort"
  109. placement={column.numeric ? 'bottom-end' : 'bottom-start'}
  110. enterDelay={300}
  111. >
  112. <TableSortLabel
  113. active={orderBy === column.id}
  114. direction={order}
  115. onClick={this.createSortHandler(column.id)}
  116. >
  117. {column.label}
  118. </TableSortLabel>
  119. </Tooltip>
  120. </TableCell>
  121. );
  122. })
  123. }
  124. {showActions &&
  125. <TableCell padding = 'none'>
  126. Actions
  127. </TableCell>
  128. }
  129. </TableRow>
  130. </TableHead>
  131. );
  132. }
  133. }
  134.  
  135. EnhancedTableHead.propTypes = {
  136. numSelected: PropTypes.number.isRequired,
  137. classes: PropTypes.object.isRequired,
  138. onRequestSort: PropTypes.func.isRequired,
  139. onSelectAllClick: PropTypes.func.isRequired,
  140. order: PropTypes.string.isRequired,
  141. orderBy: PropTypes.string.isRequired,
  142. rowCount: PropTypes.number.isRequired
  143. };
  144.  
  145. const toolbarStyles = theme => ({
  146. root: {
  147. paddingRight: theme.spacing.unit
  148. },
  149. highlight:
  150. theme.palette.type === 'light'
  151. ? {
  152. color: theme.palette.secondary.main,
  153. backgroundColor: lighten(theme.palette.secondary.light, 0.85)
  154. }
  155. : {
  156. color: theme.palette.text.primary,
  157. backgroundColor: theme.palette.secondary.dark
  158. },
  159. spacer: {
  160. flex: '1 1 100%'
  161. },
  162. actions: {
  163. color: theme.palette.text.secondary
  164. },
  165. title: {
  166. flex: '0 0 auto'
  167. }
  168. });
  169.  
  170. const styles = theme => ({
  171. root: {
  172. width: '100%',
  173. marginTop: theme.spacing.unit * 3
  174. },
  175. table: {
  176. minWidth: 1020
  177. },
  178. tableWrapper: {
  179. overflowX: 'auto'
  180. }
  181. });
  182.  
  183. class EnhancedTable extends React.Component {
  184. constructor(props) {
  185. super(props);
  186.  
  187. this.state = {
  188. order: 'asc',
  189. orderBy: 'calories',
  190. selected: [],
  191. page: 0,
  192. rowsPerPage: 5
  193. };
  194. }
  195.  
  196. handleRequestSort = (event, property) => {
  197. const orderBy = property;
  198. let order = 'desc';
  199.  
  200. if (this.state.orderBy === property && this.state.order === 'desc') {
  201. order = 'asc';
  202. }
  203.  
  204. const data =
  205. order === 'desc'
  206. ? this.state.data.sort((a, b) => (b[orderBy] < a[orderBy] ? -1 : 1))
  207. : this.state.data.sort((a, b) => (a[orderBy] < b[orderBy] ? -1 : 1));
  208.  
  209. this.setState({ data, order, orderBy });
  210. };
  211.  
  212. handleSelectAllClick = (event, checked) => {
  213. if (checked) {
  214. this.setState({ selected: this.state.data.map(n => n.id) });
  215. return;
  216. }
  217. this.setState({ selected: [] });
  218. };
  219.  
  220. handleInfoClick = (event, id) => {
  221. console.log(id);
  222. event.stopPropagation();
  223. }
  224. handleClick = (event, id) => {
  225. const { selected } = this.state;
  226. const selectedIndex = selected.indexOf(id);
  227. let newSelected = [];
  228.  
  229. if (selectedIndex === -1) {
  230. newSelected = newSelected.concat(selected, id);
  231. } else if (selectedIndex === 0) {
  232. newSelected = newSelected.concat(selected.slice(1));
  233. } else if (selectedIndex === selected.length - 1) {
  234. newSelected = newSelected.concat(selected.slice(0, -1));
  235. } else if (selectedIndex > 0) {
  236. newSelected = newSelected.concat(
  237. selected.slice(0, selectedIndex),
  238. selected.slice(selectedIndex + 1)
  239. );
  240. }
  241.  
  242. this.setState({ selected: newSelected });
  243. };
  244.  
  245. handleChangePage = (event, page) => {
  246. this.setState({ page });
  247. };
  248.  
  249. handleChangeRowsPerPage = event => {
  250. this.setState({ rowsPerPage: event.target.value });
  251. };
  252.  
  253. isSelected = id => this.state.selected.indexOf(id) !== -1;
  254.  
  255. render() {
  256. const { classes, data, columnData, columnConfig, rowAction, showActions } = this.props; //columnData, columnConfig
  257.  
  258. //this had data
  259. const { order, orderBy, selected, rowsPerPage, page } = this.state;
  260. const emptyRows =
  261. rowsPerPage - Math.min(rowsPerPage, data.length - page * rowsPerPage);
  262.  
  263. return (
  264. <Paper className={classes.root}>
  265. {/* <EnhancedTableToolbar numSelected={selected.length} /> */}
  266. <div className={classes.tableWrapper}>
  267. <Table className={classes.table} aria-labelledby="tableTitle">
  268. <EnhancedTableHead
  269. //columnData = {columnData}
  270. //columnConfig = {columnConfig}
  271. {...this.props}
  272. numSelected={selected.length}
  273. order={order}
  274. orderBy={orderBy}
  275. onSelectAllClick={this.handleSelectAllClick}
  276. onRequestSort={this.handleRequestSort}
  277. rowCount={data.length}
  278. />
  279. <TableBody>
  280. {data
  281. .slice(page * rowsPerPage, page * rowsPerPage + rowsPerPage)
  282. .map((n,index) => {
  283. const isSelected = this.isSelected(n.id);
  284. return (
  285. <TableRow
  286. hover
  287. //onClick={event => this.handleClick(event, n.id)}
  288. role="checkbox"
  289. aria-checked={isSelected}
  290. tabIndex={-1}
  291. key={n.id}
  292. selected={isSelected}
  293. >
  294. <TableCell padding="checkbox">
  295. <Checkbox checked={isSelected} />
  296. </TableCell>
  297. {columnData.map((colItem,index)=>{
  298. return (
  299. index === 0 ?
  300. <TableCell
  301. scope="row"
  302. padding="none"
  303. key={colItem.id}
  304. numeric={ colItem.numeric === undefined ? true : colItem.numeric}
  305. >
  306. {n[colItem.id]}
  307. </TableCell> :
  308. <TableCell
  309. key={colItem.id}
  310. numeric
  311. numeric={ colItem.numeric === undefined ? true : colItem.numeric}
  312. >
  313. {n[colItem.id]}
  314. </TableCell>
  315. );
  316. })
  317. }
  318. {showActions &&
  319. <TableCell >
  320. <IconButton onClick={ event => rowAction('edit',n.id,index)}>
  321. <EditIcon />
  322. </IconButton>
  323. <IconButton onClick={ event => rowAction('info',n.id,index)}>
  324. <InfoIcon />
  325. </IconButton>
  326. <IconButton onClick={ event => event => rowAction('visibility',n.id,index)}>
  327. <VisibilityIcon />
  328. </IconButton>
  329. </TableCell>
  330. }
  331. </TableRow>
  332. );
  333. })}
  334. {emptyRows > 0 && (
  335. <TableRow style={{ height: 49 * emptyRows }}>
  336. <TableCell colSpan={6} />
  337. </TableRow>
  338. )}
  339. </TableBody>
  340. </Table>
  341. </div>
  342. <TablePagination
  343. component="div"
  344. count={data.length}
  345. rowsPerPage={rowsPerPage}
  346. page={page}
  347. backIconButtonProps={{
  348. 'aria-label': 'Previous Page'
  349. }}
  350. nextIconButtonProps={{
  351. 'aria-label': 'Next Page'
  352. }}
  353. onChangePage={this.handleChangePage}
  354. onChangeRowsPerPage={this.handleChangeRowsPerPage}
  355. />
  356. </Paper>
  357. );
  358. }
  359. }
  360.  
  361. EnhancedTable.propTypes = {
  362. classes: PropTypes.object.isRequired
  363. };
  364.  
  365. export default withStyles(styles)(EnhancedTable);
Add Comment
Please, Sign In to add comment