Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import Immutable from 'immutable';
- import React, { Component, PropTypes } from 'react';
- import { ContentBox, ContentBoxHeader, ContentBoxParagraph } from 'react-virtualized';
- import { LabeledInput, InputRow } from 'react-virtualized';
- import { connect } from 'react-redux';
- import { AutoSizer } from 'react-virtualized';
- import { Column } from 'react-virtualized';
- import { Table } from 'react-virtualized';
- import SortDirection from './SortDirection';
- import SortIndicator from './SortIndicator';
- import shallowCompare from 'react-addons-shallow-compare'
- import styles from 'styles.scss'
- export default class DataTable extends Component {
- static contextTypes = {
- list: PropTypes.instanceOf(Immutable.List).isRequired
- };
- constructor (props, context) {
- super(props, context)
- this.state = {
- disableHeader: false,
- headerHeight: 30,
- height: 270,
- list: {},
- hideIndexRow: false,
- overscanRowCount: 10,
- rowHeight: 40,
- rowCount: 1000,
- scrollToIndex: undefined,
- sortBy: 'index',
- sortDirection: SortDirection.ASC,
- useDynamicRowHeight: false
- }
- this._getRowHeight = this._getRowHeight.bind(this)
- this._headerRenderer = this._headerRenderer.bind(this)
- this._noRowsRenderer = this._noRowsRenderer.bind(this)
- this._onRowCountChange = this._onRowCountChange.bind(this)
- this._onScrollToRowChange = this._onScrollToRowChange.bind(this)
- this._rowClassName = this._rowClassName.bind(this)
- this._sort = this._sort.bind(this)
- }
- render () {
- const {
- disableHeader,
- headerHeight,
- height,
- hideIndexRow,
- overscanRowCount,
- rowHeight,
- rowCount,
- scrollToIndex,
- sortBy,
- sortDirection,
- useDynamicRowHeight
- } = this.state
- const { list } = this.props
- const sortedList = this._isSortEnabled()
- ? list
- .sortBy(item => item[sortBy])
- .update(list =>
- sortDirection === SortDirection.DESC
- ? list.reverse()
- : list
- )
- : list
- const rowGetter = ({ index }) => this._getDatum(sortedList, index)
- return (
- <ContentBox>
- <ContentBoxHeader
- text='Table'
- sourceLink='https://github.com/bvaughn/react-virtualized/blob/master/source/Table/Table.example.js'
- docsLink='https://github.com/bvaughn/react-virtualized/blob/master/docs/Table.md'
- />
- <ContentBoxParagraph>
- The table layout below is created with flexboxes.
- This allows it to have a fixed header and scrollable body content.
- It also makes use of <code>Grid</code> for windowing table content so that large lists are rendered efficiently.
- Adjust its configurable properties below to see how it reacts.
- </ContentBoxParagraph>
- <ContentBoxParagraph>
- <label className={styles.checkboxLabel}>
- <input
- aria-label='Use dynamic row heights?'
- checked={useDynamicRowHeight}
- className={styles.checkbox}
- type='checkbox'
- onChange={event => this._updateUseDynamicRowHeight(event.target.checked)}
- />
- Use dynamic row heights?
- </label>
- <label className={styles.checkboxLabel}>
- <input
- aria-label='Hide index?'
- checked={hideIndexRow}
- className={styles.checkbox}
- type='checkbox'
- onChange={event => this.setState({ hideIndexRow: event.target.checked })}
- />
- Hide index?
- </label>
- <label className={styles.checkboxLabel}>
- <input
- aria-label='Hide header?'
- checked={disableHeader}
- className={styles.checkbox}
- type='checkbox'
- onChange={event => this.setState({ disableHeader: event.target.checked })}
- />
- Hide header?
- </label>
- </ContentBoxParagraph>
- <InputRow>
- <LabeledInput
- label='Num rows'
- name='rowCount'
- onChange={this._onRowCountChange}
- value={rowCount}
- />
- <LabeledInput
- label='Scroll to'
- name='onScrollToRow'
- placeholder='Index...'
- onChange={this._onScrollToRowChange}
- value={scrollToIndex || ''}
- />
- <LabeledInput
- label='List height'
- name='height'
- onChange={event => this.setState({ height: parseInt(event.target.value, 10) || 1 })}
- value={height}
- />
- <LabeledInput
- disabled={useDynamicRowHeight}
- label='Row height'
- name='rowHeight'
- onChange={event => this.setState({ rowHeight: parseInt(event.target.value, 10) || 1 })}
- value={rowHeight}
- />
- <LabeledInput
- label='Header height'
- name='headerHeight'
- onChange={event => this.setState({ headerHeight: parseInt(event.target.value, 10) || 1 })}
- value={headerHeight}
- />
- <LabeledInput
- label='Overscan'
- name='overscanRowCount'
- onChange={event => this.setState({ overscanRowCount: parseInt(event.target.value, 10) || 0 })}
- value={overscanRowCount}
- />
- </InputRow>
- <div>
- <AutoSizer disableHeight>
- {({ width }) => (
- <Table
- ref='Table'
- disableHeader={disableHeader}
- headerClassName={styles.headerColumn}
- headerHeight={headerHeight}
- height={height}
- noRowsRenderer={this._noRowsRenderer}
- overscanRowCount={overscanRowCount}
- rowClassName={this._rowClassName}
- rowHeight={useDynamicRowHeight ? this._getRowHeight : rowHeight}
- rowGetter={rowGetter}
- rowCount={rowCount}
- scrollToIndex={scrollToIndex}
- sort={this._sort}
- sortBy={sortBy}
- sortDirection={sortDirection}
- width={width}
- >
- {!hideIndexRow &&
- <Column
- label='Index'
- cellDataGetter={
- ({ columnData, dataKey, rowData }) => rowData.index
- }
- dataKey='index'
- disableSort={!this._isSortEnabled()}
- width={60}
- />
- }
- <Column
- dataKey='fcl_id'
- disableSort={!this._isSortEnabled()}
- headerRenderer={this._headerRenderer}
- width={90}
- />
- <Column
- width={210}
- disableSort
- label='The description label is really long so that it will be truncated'
- dataKey='propertyaddress'
- className={styles.exampleColumn}
- cellRenderer={
- ({ cellData, columnData, dataKey, rowData, rowIndex }) => cellData
- }
- flexGrow={1}
- />
- </Table>
- )}
- </AutoSizer>
- </div>
- </ContentBox>
- )
- }
- shouldComponentUpdate (nextProps, nextState) {
- return shallowCompare(this, nextProps, nextState)
- }
- _getDatum (list, index) {
- return list.get(index % list.size)
- }
- _getRowHeight ({ index }) {
- const { list } = this.props
- return this._getDatum(list, index).size
- }
- _headerRenderer ({
- columnData,
- dataKey,
- disableSort,
- label,
- sortBy,
- sortDirection
- }) {
- return (
- <div>
- Full Name
- {sortBy === dataKey &&
- <SortIndicator sortDirection={sortDirection} />
- }
- </div>
- )
- }
- _isSortEnabled () {
- const { list } = this.props
- console.log(this.props)
- const { rowCount } = this.state
- return rowCount <= list.size
- }
- _noRowsRenderer () {
- return (
- <div className={styles.noRows}>
- No rows
- </div>
- )
- }
- _onRowCountChange (event) {
- const rowCount = parseInt(event.target.value, 10) || 0
- this.setState({ rowCount })
- }
- _onScrollToRowChange (event) {
- const { rowCount } = this.state
- let scrollToIndex = Math.min(rowCount - 1, parseInt(event.target.value, 10))
- if (isNaN(scrollToIndex)) {
- scrollToIndex = undefined
- }
- this.setState({ scrollToIndex })
- }
- _rowClassName ({ index }) {
- if (index < 0) {
- return styles.headerRow
- } else {
- return index % 2 === 0 ? styles.evenRow : styles.oddRow
- }
- }
- _sort ({ sortBy, sortDirection }) {
- this.setState({ sortBy, sortDirection })
- }
- _updateUseDynamicRowHeight (value) {
- this.setState({
- useDynamicRowHeight: value
- })
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement