Table.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /*
  2. Copyright (C) 2017 Cloudbase Solutions SRL
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU Affero General Public License as
  5. published by the Free Software Foundation, either version 3 of the
  6. License, or (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Affero General Public License for more details.
  11. You should have received a copy of the GNU Affero General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. import React, { Component, PropTypes } from 'react';
  15. import Collapse from 'react-collapse';
  16. import withStyles from 'isomorphic-style-loader/lib/withStyles';
  17. import s from './Table.scss';
  18. class Table extends Component {
  19. static defaultProps = {
  20. headerItems: [],
  21. listItems: [],
  22. show: true
  23. }
  24. static propTypes = {
  25. headerItems: PropTypes.array,
  26. listItems: PropTypes.array,
  27. customClassName: PropTypes.string,
  28. show: PropTypes.bool
  29. }
  30. constructor(props) {
  31. super(props)
  32. this.state = {
  33. openState: []
  34. }
  35. }
  36. componentWillReceiveProps(newProps) {
  37. let openState = []
  38. for (let i in newProps.listItems) {
  39. openState.push(newProps.listItems[i].openState)
  40. }
  41. this.setState({ openState: openState })
  42. }
  43. toggleDrawer(index) {
  44. let newOpenState = this.state.openState
  45. let toggled = !newOpenState[index]
  46. for (let i in newOpenState) {
  47. newOpenState[i] = false
  48. }
  49. newOpenState[index] = toggled
  50. this.setState({ openState: newOpenState })
  51. }
  52. rowMouseDown(e) {
  53. this.dragStartPosition = {x: e.screenX, y: e.screenY};
  54. }
  55. rowMouseUp(e, index) {
  56. this.dragStartPosition = this.dragStartPosition || {x: e.screenX, y: e.screenY};
  57. // If a drag operation has been initiated (i.e. text selection), don't call toggleDrawer
  58. if (Math.abs(this.dragStartPosition.x - e.screenX) < 3 && Math.abs(this.dragStartPosition.y - e.screenY) < 3) {
  59. this.toggleDrawer(index);
  60. }
  61. this.dragStartPosition = null;
  62. }
  63. render() {
  64. let headerItems = this.props.headerItems.map((item, index) =>
  65. <div className={s.cell + " cell"} key={'headerItem_' + index}>{item.label}</div>
  66. )
  67. let listItems = (<div className="no-results">No results</div>)
  68. if (this.props.listItems) {
  69. listItems = this.props.listItems.map((listItem, index) => {
  70. let row = this.props.headerItems.map((headerItem) =>
  71. (
  72. <div className={s.cell + " cell"} key={headerItem.key + " " + index}>
  73. {listItem[headerItem.key]}
  74. </div>
  75. )
  76. )
  77. let detailView = null
  78. if (listItem.detailView) {
  79. detailView = (
  80. <div className={s.detailView}>
  81. <span className={s.caret}></span>
  82. <Collapse
  83. isOpened={typeof this.state.openState[index] == "undefined" ? false : this.state.openState[index]}
  84. key={"collapse_" + index}
  85. springConfig={{ stiffness: 100, damping: 20 }}
  86. >
  87. {listItem.detailView}
  88. </Collapse>
  89. </div>)
  90. }
  91. return (
  92. <div
  93. className={s.row + " row " + (this.state.openState[index] ? "isOpen" : "")}
  94. key={"row_" + index}
  95. onMouseDown={e => this.rowMouseDown(e)}
  96. onMouseUp={e => this.rowMouseUp(e, index)}
  97. >
  98. {row} {detailView}
  99. </div>)
  100. }, this)
  101. }
  102. return (
  103. <div className={s.root + " " + this.props.customClassName}>
  104. <div className={s.headerItems + " headerItems"}>
  105. {headerItems}
  106. </div>
  107. <div className={s.listItems + " listItems"}>
  108. {listItems}
  109. </div>
  110. </div>
  111. );
  112. }
  113. }
  114. export default withStyles(Table, s);