Table.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. parentId: null,
  23. show: true
  24. }
  25. static propTypes = {
  26. headerItems: PropTypes.array,
  27. listItems: PropTypes.array,
  28. customClassName: PropTypes.string,
  29. show: PropTypes.bool,
  30. parentId: PropTypes.string
  31. }
  32. constructor(props) {
  33. super(props)
  34. this.state = {
  35. openState: [],
  36. parentId: null
  37. }
  38. }
  39. componentWillReceiveProps(newProps) {
  40. let openState = []
  41. for (let i in newProps.listItems) {
  42. // Use the previous open state if the table's parent ID is the same
  43. // i.e. don't close the collapser if new props arrive
  44. let isSameParent = this.state.parentId === null || newProps.parentId === this.state.parentId
  45. let prevOpenState = isSameParent && this.state.openState[i]
  46. openState.push(newProps.listItems[i].openState || prevOpenState)
  47. }
  48. this.setState({ openState: openState, parentId: newProps.parentId })
  49. }
  50. toggleDrawer(index) {
  51. let newOpenState = this.state.openState
  52. let toggled = !newOpenState[index]
  53. for (let i in newOpenState) {
  54. newOpenState[i] = false
  55. }
  56. newOpenState[index] = toggled
  57. this.setState({ openState: newOpenState })
  58. }
  59. rowMouseDown(e) {
  60. this.dragStartPosition = { x: e.screenX, y: e.screenY };
  61. }
  62. rowMouseUp(e, index) {
  63. this.dragStartPosition = this.dragStartPosition || { x: e.screenX, y: e.screenY };
  64. // If a drag operation has been initiated (i.e. text selection), don't call toggleDrawer
  65. if (Math.abs(this.dragStartPosition.x - e.screenX) < 3 && Math.abs(this.dragStartPosition.y - e.screenY) < 3) {
  66. this.toggleDrawer(index);
  67. }
  68. this.dragStartPosition = null;
  69. }
  70. render() {
  71. let headerItems = this.props.headerItems.map((item, index) =>
  72. <div className={s.cell + " cell"} key={'headerItem_' + index}>{item.label}</div>
  73. )
  74. let listItems = (<div className="no-results">No results</div>)
  75. if (this.props.listItems) {
  76. listItems = this.props.listItems.map((listItem, index) => {
  77. let row = this.props.headerItems.map((headerItem) =>
  78. (
  79. <div className={s.cell + " cell"} key={headerItem.key + " " + index}>
  80. {listItem[headerItem.key]}
  81. </div>
  82. )
  83. )
  84. let detailView = null
  85. if (listItem.detailView) {
  86. detailView = (
  87. <div className={s.detailView}>
  88. <span className={s.caret}></span>
  89. <Collapse
  90. isOpened={typeof this.state.openState[index] == "undefined" ? false : this.state.openState[index]}
  91. key={"collapse_" + index}
  92. springConfig={{ stiffness: 100, damping: 20 }}
  93. >
  94. {listItem.detailView}
  95. </Collapse>
  96. </div>)
  97. }
  98. return (
  99. <div
  100. className={s.row + " row " + (this.state.openState[index] ? "isOpen" : "")}
  101. key={"row_" + index}
  102. onMouseDown={e => this.rowMouseDown(e)}
  103. onMouseUp={e => this.rowMouseUp(e, index)}
  104. >
  105. {row} {detailView}
  106. </div>)
  107. }, this)
  108. }
  109. return (
  110. <div className={s.root + " " + this.props.customClassName}>
  111. <div className={s.headerItems + " headerItems"}>
  112. {headerItems}
  113. </div>
  114. <div className={s.listItems + " listItems"}>
  115. {listItems}
  116. </div>
  117. </div>
  118. );
  119. }
  120. }
  121. export default withStyles(Table, s);