Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions src/DataTable/DataTable.stories.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import React from 'react'
import { storiesOf } from '@storybook/react'
import { withKnobs, boolean } from '@storybook/addon-knobs'

import { defaultTemplate } from '../../storybook/decorators/storyTemplates'
// import { AutoSizer, Table, Column } from 'react-virtualized'
import { DataTable, Column } from './index.js'

const stories = storiesOf('DataTable', module)
stories.addDecorator(withKnobs)
stories.addDecorator(
defaultTemplate({
title: 'Data Table',
documentationLink:
'http://www.patternfly.org/pattern-library/content-views/table-view/'
})
)

stories.addWithInfo('DataTable', `update this with better description`, () => {
const items = {
item1: { title: 'Item 1', description: 'This is item 1' },
item2: { title: 'Item 2', description: 'This is item 2' }
}

return (
<DataTable rowGetter={key => items[key]} rowKeys={Object.keys(items)}>
<Column label="Title" dataKey="title" />
<Column label="Description" dataKey="description" />
</DataTable>
)
})
42 changes: 42 additions & 0 deletions src/DataTable/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import PropTypes from 'prop-types'
import React from 'react'

export const DataTable = ({ children, className, rowGetter, rowKeys }) => (
<table className="table table-striped table-bordered table-hover">
<thead>
<tr>
{React.Children.map(children, child => {
return <th>{child.props.label}</th>
})}
</tr>
</thead>
<tbody>
{rowKeys.map(rowKey => (
<tr key={rowKey}>
{React.Children.map(children, child => {
const { columnData, cellDataGetter, dataKey } = child.props
return (
<td key={dataKey}>
{cellDataGetter({
columnData,
dataKey,
rowData: rowGetter(rowKey)
})}
</td>
)
})}
</tr>
))}
</tbody>
</table>
)
DataTable.propTypes = { rowGetter: PropTypes.func.isRequired }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we also include rowKeys: PropTypes.array in the propTypes check?


const defaultCellDataGetter = ({ columnData, dataKey, rowData }) =>
rowData[dataKey]

export class Column extends React.Component {}
Column.propTypes = { cellDataGetter: PropTypes.func.isRequired }
Column.defaultProps = {
cellDataGetter: defaultCellDataGetter
}