Advertisement
Guest User

Untitled

a guest
Jan 25th, 2017
246
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.10 KB | None | 0 0
  1. import Immutable from 'immutable';
  2. import React, { Component, PropTypes } from 'react';
  3. import { ContentBox, ContentBoxHeader, ContentBoxParagraph } from 'react-virtualized';
  4. import { LabeledInput, InputRow } from 'react-virtualized';
  5. import { connect } from 'react-redux';
  6. import { AutoSizer } from 'react-virtualized';
  7. import { Column } from 'react-virtualized';
  8. import { Table } from 'react-virtualized';
  9. import SortDirection from './SortDirection';
  10. import SortIndicator from './SortIndicator';
  11. import shallowCompare from 'react-addons-shallow-compare'
  12. import styles from 'styles.scss'
  13.  
  14. export default class DataTable extends Component {
  15.  
  16. static contextTypes = {
  17. list: PropTypes.instanceOf(Immutable.List).isRequired
  18. };
  19.  
  20. constructor (props, context) {
  21. super(props, context)
  22.  
  23. this.state = {
  24. disableHeader: false,
  25. headerHeight: 30,
  26. height: 270,
  27. list: {},
  28. hideIndexRow: false,
  29. overscanRowCount: 10,
  30. rowHeight: 40,
  31. rowCount: 1000,
  32. scrollToIndex: undefined,
  33. sortBy: 'index',
  34. sortDirection: SortDirection.ASC,
  35. useDynamicRowHeight: false
  36. }
  37.  
  38. this._getRowHeight = this._getRowHeight.bind(this)
  39. this._headerRenderer = this._headerRenderer.bind(this)
  40. this._noRowsRenderer = this._noRowsRenderer.bind(this)
  41. this._onRowCountChange = this._onRowCountChange.bind(this)
  42. this._onScrollToRowChange = this._onScrollToRowChange.bind(this)
  43. this._rowClassName = this._rowClassName.bind(this)
  44. this._sort = this._sort.bind(this)
  45. }
  46.  
  47. render () {
  48. const {
  49. disableHeader,
  50. headerHeight,
  51. height,
  52. hideIndexRow,
  53. overscanRowCount,
  54. rowHeight,
  55. rowCount,
  56. scrollToIndex,
  57. sortBy,
  58. sortDirection,
  59. useDynamicRowHeight
  60. } = this.state
  61.  
  62. const { list } = this.props
  63. const sortedList = this._isSortEnabled()
  64. ? list
  65. .sortBy(item => item[sortBy])
  66. .update(list =>
  67. sortDirection === SortDirection.DESC
  68. ? list.reverse()
  69. : list
  70. )
  71. : list
  72.  
  73. const rowGetter = ({ index }) => this._getDatum(sortedList, index)
  74.  
  75. return (
  76. <ContentBox>
  77. <ContentBoxHeader
  78. text='Table'
  79. sourceLink='https://github.com/bvaughn/react-virtualized/blob/master/source/Table/Table.example.js'
  80. docsLink='https://github.com/bvaughn/react-virtualized/blob/master/docs/Table.md'
  81. />
  82.  
  83. <ContentBoxParagraph>
  84. The table layout below is created with flexboxes.
  85. This allows it to have a fixed header and scrollable body content.
  86. It also makes use of <code>Grid</code> for windowing table content so that large lists are rendered efficiently.
  87. Adjust its configurable properties below to see how it reacts.
  88. </ContentBoxParagraph>
  89.  
  90. <ContentBoxParagraph>
  91. <label className={styles.checkboxLabel}>
  92. <input
  93. aria-label='Use dynamic row heights?'
  94. checked={useDynamicRowHeight}
  95. className={styles.checkbox}
  96. type='checkbox'
  97. onChange={event => this._updateUseDynamicRowHeight(event.target.checked)}
  98. />
  99. Use dynamic row heights?
  100. </label>
  101.  
  102. <label className={styles.checkboxLabel}>
  103. <input
  104. aria-label='Hide index?'
  105. checked={hideIndexRow}
  106. className={styles.checkbox}
  107. type='checkbox'
  108. onChange={event => this.setState({ hideIndexRow: event.target.checked })}
  109. />
  110. Hide index?
  111. </label>
  112.  
  113. <label className={styles.checkboxLabel}>
  114. <input
  115. aria-label='Hide header?'
  116. checked={disableHeader}
  117. className={styles.checkbox}
  118. type='checkbox'
  119. onChange={event => this.setState({ disableHeader: event.target.checked })}
  120. />
  121. Hide header?
  122. </label>
  123. </ContentBoxParagraph>
  124.  
  125. <InputRow>
  126. <LabeledInput
  127. label='Num rows'
  128. name='rowCount'
  129. onChange={this._onRowCountChange}
  130. value={rowCount}
  131. />
  132. <LabeledInput
  133. label='Scroll to'
  134. name='onScrollToRow'
  135. placeholder='Index...'
  136. onChange={this._onScrollToRowChange}
  137. value={scrollToIndex || ''}
  138. />
  139. <LabeledInput
  140. label='List height'
  141. name='height'
  142. onChange={event => this.setState({ height: parseInt(event.target.value, 10) || 1 })}
  143. value={height}
  144. />
  145. <LabeledInput
  146. disabled={useDynamicRowHeight}
  147. label='Row height'
  148. name='rowHeight'
  149. onChange={event => this.setState({ rowHeight: parseInt(event.target.value, 10) || 1 })}
  150. value={rowHeight}
  151. />
  152. <LabeledInput
  153. label='Header height'
  154. name='headerHeight'
  155. onChange={event => this.setState({ headerHeight: parseInt(event.target.value, 10) || 1 })}
  156. value={headerHeight}
  157. />
  158. <LabeledInput
  159. label='Overscan'
  160. name='overscanRowCount'
  161. onChange={event => this.setState({ overscanRowCount: parseInt(event.target.value, 10) || 0 })}
  162. value={overscanRowCount}
  163. />
  164. </InputRow>
  165.  
  166. <div>
  167. <AutoSizer disableHeight>
  168. {({ width }) => (
  169. <Table
  170. ref='Table'
  171. disableHeader={disableHeader}
  172. headerClassName={styles.headerColumn}
  173. headerHeight={headerHeight}
  174. height={height}
  175. noRowsRenderer={this._noRowsRenderer}
  176. overscanRowCount={overscanRowCount}
  177. rowClassName={this._rowClassName}
  178. rowHeight={useDynamicRowHeight ? this._getRowHeight : rowHeight}
  179. rowGetter={rowGetter}
  180. rowCount={rowCount}
  181. scrollToIndex={scrollToIndex}
  182. sort={this._sort}
  183. sortBy={sortBy}
  184. sortDirection={sortDirection}
  185. width={width}
  186. >
  187. {!hideIndexRow &&
  188. <Column
  189. label='Index'
  190. cellDataGetter={
  191. ({ columnData, dataKey, rowData }) => rowData.index
  192. }
  193. dataKey='index'
  194. disableSort={!this._isSortEnabled()}
  195. width={60}
  196. />
  197. }
  198. <Column
  199. dataKey='fcl_id'
  200. disableSort={!this._isSortEnabled()}
  201. headerRenderer={this._headerRenderer}
  202. width={90}
  203. />
  204. <Column
  205. width={210}
  206. disableSort
  207. label='The description label is really long so that it will be truncated'
  208. dataKey='propertyaddress'
  209. className={styles.exampleColumn}
  210. cellRenderer={
  211. ({ cellData, columnData, dataKey, rowData, rowIndex }) => cellData
  212. }
  213. flexGrow={1}
  214. />
  215. </Table>
  216. )}
  217. </AutoSizer>
  218. </div>
  219. </ContentBox>
  220. )
  221. }
  222.  
  223. shouldComponentUpdate (nextProps, nextState) {
  224. return shallowCompare(this, nextProps, nextState)
  225. }
  226.  
  227. _getDatum (list, index) {
  228. return list.get(index % list.size)
  229. }
  230.  
  231. _getRowHeight ({ index }) {
  232. const { list } = this.props
  233.  
  234. return this._getDatum(list, index).size
  235. }
  236.  
  237. _headerRenderer ({
  238. columnData,
  239. dataKey,
  240. disableSort,
  241. label,
  242. sortBy,
  243. sortDirection
  244. }) {
  245. return (
  246. <div>
  247. Full Name
  248. {sortBy === dataKey &&
  249. <SortIndicator sortDirection={sortDirection} />
  250. }
  251. </div>
  252. )
  253. }
  254.  
  255. _isSortEnabled () {
  256. const { list } = this.props
  257. console.log(this.props)
  258. const { rowCount } = this.state
  259.  
  260. return rowCount <= list.size
  261. }
  262.  
  263. _noRowsRenderer () {
  264. return (
  265. <div className={styles.noRows}>
  266. No rows
  267. </div>
  268. )
  269. }
  270.  
  271. _onRowCountChange (event) {
  272. const rowCount = parseInt(event.target.value, 10) || 0
  273.  
  274. this.setState({ rowCount })
  275. }
  276.  
  277. _onScrollToRowChange (event) {
  278. const { rowCount } = this.state
  279. let scrollToIndex = Math.min(rowCount - 1, parseInt(event.target.value, 10))
  280.  
  281. if (isNaN(scrollToIndex)) {
  282. scrollToIndex = undefined
  283. }
  284.  
  285. this.setState({ scrollToIndex })
  286. }
  287.  
  288. _rowClassName ({ index }) {
  289. if (index < 0) {
  290. return styles.headerRow
  291. } else {
  292. return index % 2 === 0 ? styles.evenRow : styles.oddRow
  293. }
  294. }
  295.  
  296. _sort ({ sortBy, sortDirection }) {
  297. this.setState({ sortBy, sortDirection })
  298. }
  299.  
  300. _updateUseDynamicRowHeight (value) {
  301. this.setState({
  302. useDynamicRowHeight: value
  303. })
  304. }
  305. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement