Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(react-grid): extract the Table plugin components #766

Merged
merged 21 commits into from
Mar 7, 2018
Merged
Show file tree
Hide file tree
Changes from 10 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import * as React from 'react';
import {
Grid,
Table,
TableHeaderRow,
} from '@devexpress/dx-react-grid-bootstrap3';

import {
generateRows,
globalSalesValues,
} from '../../../demo-data/generator';

const TableComponent = ({ ...restProps }) => (
<Table.Container
Copy link
Contributor

Choose a reason for hiding this comment

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

Container? Why?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Table name was better?

{...restProps}
className="table-striped"
/>
);

export default class Demo extends React.PureComponent {
constructor(props) {
super(props);

this.state = {
columns: [
{ name: 'region', title: 'Region' },
{ name: 'sector', title: 'Sector' },
{ name: 'channel', title: 'Channel' },
{ name: 'customer', title: 'Customer' },
{ name: 'product', title: 'Product' },
{ name: 'amount', title: 'Sale Amount' },
],
rows: generateRows({ columnValues: globalSalesValues, length: 8 }),
};
}
render() {
const { rows, columns } = this.state;

return (
<div>
<Grid
rows={rows}
columns={columns}
>
<Table
tableComponent={TableComponent}
/>
<TableHeaderRow />
</Grid>
</div>
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import * as React from 'react';
import Paper from 'material-ui/Paper';
import {
Grid,
Table,
TableHeaderRow,
} from '@devexpress/dx-react-grid-material-ui';
import { withStyles } from 'material-ui/styles';
import {
generateRows,
globalSalesValues,
} from '../../../demo-data/generator';

const styles = theme => ({
tableStriped: {
'& tbody tr:nth-of-type(odd)': {
backgroundColor: theme.palette.primary[50],
},
},
});

const TableComponentBase = ({ classes, ...restProps }) => (
<Table.Container
{...restProps}
className={classes.tableStriped}
/>
);

export const TableComponent = withStyles(styles, { name: 'TableComponent' })(TableComponentBase);

export default class Demo extends React.PureComponent {
constructor(props) {
super(props);

this.state = {
columns: [
{ name: 'region', title: 'Region' },
{ name: 'sector', title: 'Sector' },
{ name: 'channel', title: 'Channel' },
{ name: 'customer', title: 'Customer' },
{ name: 'product', title: 'Product' },
{ name: 'amount', title: 'Sale Amount' },
],
rows: generateRows({ columnValues: globalSalesValues, length: 8 }),
};
}
render() {
const { rows, columns } = this.state;

return (
<Paper>
<Grid
rows={rows}
columns={columns}
>
<Table
tableComponent={TableComponent}
/>
<TableHeaderRow />
</Grid>
</Paper>
);
}
}
10 changes: 10 additions & 0 deletions packages/dx-react-grid-bootstrap3/src/plugins/table.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ import { TableStubCell } from '../templates/table-stub-cell';
import { TableStubHeaderCell } from '../templates/table-stub-header-cell';
import { TableNoDataCell } from '../templates/table-no-data-cell';
import { TableRow } from '../templates/table-row';
import { Table as TableComponent } from '../templates/table';

const TableHead = props => <thead {...props} />;
const TableBody = props => <tbody {...props} />;

const defaultMessages = {
noData: 'No data',
Expand All @@ -21,6 +25,9 @@ export class Table extends React.PureComponent {

return (
<TableBase
tableComponent={TableComponent}
headComponent={TableHead}
bodyComponent={TableBody}
layoutComponent={TableLayout}
rowComponent={TableRow}
cellComponent={TableCell}
Expand All @@ -41,6 +48,9 @@ Table.NoDataCell = TableNoDataCell;
Table.NoDataRow = TableRow;
Table.StubCell = TableStubCell;
Table.StubHeaderCell = TableStubCell;
Table.Container = TableComponent;
Table.TableHead = TableHead;
Table.TableBody = TableBody;

Table.propTypes = {
messages: PropTypes.shape({
Expand Down
13 changes: 13 additions & 0 deletions packages/dx-react-grid-bootstrap3/src/plugins/virtual-table.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,18 @@ import * as React from 'react';
import * as PropTypes from 'prop-types';
import { createRenderComponent } from '@devexpress/dx-react-core';
import { Table as TableBase } from '@devexpress/dx-react-grid';
import { Table as TableComponent } from '../templates/table';
import { VirtualTableLayout } from '../templates/virtual-table-layout';
import { TableCell } from '../templates/table-cell';
import { TableRow } from '../templates/table-row';
import { TableNoDataCell } from '../templates/table-no-data-cell';
import { TableStubCell } from '../templates/table-stub-cell';
import { TableStubHeaderCell } from '../templates/table-stub-header-cell';

const StickyTable = props => <TableComponent use="head" {...props} />;
const TableHead = props => <thead {...props} />;
const TableBody = props => <tbody {...props} />;

const defaultMessages = {
noData: 'No data',
};
Expand All @@ -35,6 +40,10 @@ export class VirtualTable extends React.PureComponent {
return (
<TableBase
layoutComponent={this.layoutRenderComponent.component}
tableComponent={TableComponent}
headComponent={TableHead}
bodyComponent={TableBody}
stickyTableComponent={StickyTable}
rowComponent={TableRow}
cellComponent={TableCell}
noDataRowComponent={TableRow}
Expand All @@ -54,6 +63,10 @@ VirtualTable.NoDataCell = TableNoDataCell;
VirtualTable.NoDataRow = TableRow;
VirtualTable.StubCell = TableStubCell;
VirtualTable.StubHeaderCell = TableStubCell;
VirtualTable.Container = TableComponent;
VirtualTable.TableHead = TableHead;
VirtualTable.TableBody = TableBody;
VirtualTable.StickyTable = StickyTable;

VirtualTable.propTypes = {
estimatedRowHeight: PropTypes.number,
Expand Down
16 changes: 9 additions & 7 deletions packages/dx-react-grid-bootstrap3/src/templates/table-layout.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,18 @@ import {
StaticTableLayout,
} from '@devexpress/dx-react-grid';
import { TableContainer } from './table-container';
import { Table } from './table';

const MINIMAL_COLUMN_WIDTH = 120;

const TableHead = props => <thead {...props} />;
const TableBody = props => <tbody {...props} />;

export const TableLayout = ({
headerRows,
bodyRows,
columns,
cellComponent,
rowComponent,
tableComponent,
headComponent,
bodyComponent,
}) => (
<TableLayoutCore
layoutComponent={StaticTableLayout}
Expand All @@ -26,9 +25,9 @@ export const TableLayout = ({
columns={columns}
minColumnWidth={MINIMAL_COLUMN_WIDTH}
containerComponent={TableContainer}
Copy link
Contributor

Choose a reason for hiding this comment

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

Here is the Container/Root component. Should we publish it too?

tableComponent={Table}
headComponent={TableHead}
bodyComponent={TableBody}
tableComponent={tableComponent}
headComponent={headComponent}
bodyComponent={bodyComponent}
rowComponent={rowComponent}
cellComponent={cellComponent}
/>
Expand All @@ -40,4 +39,7 @@ TableLayout.propTypes = {
columns: PropTypes.array.isRequired,
cellComponent: PropTypes.func.isRequired,
rowComponent: PropTypes.func.isRequired,
tableComponent: PropTypes.func.isRequired,
headComponent: PropTypes.func.isRequired,
bodyComponent: PropTypes.func.isRequired,
};
8 changes: 5 additions & 3 deletions packages/dx-react-grid-bootstrap3/src/templates/table.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import * as React from 'react';
import * as PropTypes from 'prop-types';
import classNames from 'classnames';

let globalStickyProp;
const testCSSProp = (property, value, noPrefixes) => {
Expand Down Expand Up @@ -45,14 +46,13 @@ export class Table extends React.Component {
}
render() {
const {
children, use, style,
...restProps
children, use, style, className, ...restProps
} = this.props;
const { stickyProp, backgroundColor } = this.state;
return (
<table
ref={(node) => { this.node = node; }}
className="table"
className={classNames('table', className)}
style={{
tableLayout: 'fixed',
overflow: 'hidden',
Expand All @@ -76,9 +76,11 @@ Table.propTypes = {
use: PropTypes.oneOf(['head']),
children: PropTypes.node.isRequired,
style: PropTypes.object,
className: PropTypes.string,
};

Table.defaultProps = {
use: undefined,
style: null,
className: undefined,
};
33 changes: 33 additions & 0 deletions packages/dx-react-grid-bootstrap3/src/templates/table.test.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import * as React from 'react';
import { mount } from 'enzyme';
import { Table } from './table';

describe('Table', () => {
it('should pass class to the root element', () => {
const tree = mount((
<div className="panel">
<Table className="custom-class">
<tbody />
</Table>
</div>
));

expect(tree.find('table').is('.custom-class'))
.toBeTruthy();
expect(tree.find('table').is('.table'))
.toBeTruthy();
});

it('should pass rest props to the root element', () => {
const tree = mount((
<div className="panel">
<Table data={{ a: 1 }}>
<tbody />
</Table>
</div>
));

expect(tree.find('table').props().data)
.toMatchObject({ a: 1 });
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,9 @@ import {
VirtualTableLayout as VirtualTableLayoutCore,
} from '@devexpress/dx-react-grid';
import { TableContainer } from './table-container';
import { Table } from './table';

const MINIMAL_COLUMN_WIDTH = 120;

const HeadTable = props => <Table use="head" {...props} />;
const TableHead = props => <thead {...props} />;
const TableBody = props => <tbody {...props} />;

export const VirtualTableLayout = ({
headerRows,
bodyRows,
Expand All @@ -21,6 +16,7 @@ export const VirtualTableLayout = ({
rowComponent,
height,
estimatedRowHeight,
tableComponent, headComponent, bodyComponent, headTableComponent,
}) => (
<TableLayout
layoutComponent={VirtualTableLayoutCore}
Expand All @@ -29,10 +25,10 @@ export const VirtualTableLayout = ({
columns={columns}
cellComponent={cellComponent}
rowComponent={rowComponent}
headComponent={TableHead}
bodyComponent={TableBody}
tableComponent={Table}
headTableComponent={HeadTable}
tableComponent={tableComponent}
headComponent={headComponent}
bodyComponent={bodyComponent}
headTableComponent={headTableComponent}
containerComponent={TableContainer}
estimatedRowHeight={estimatedRowHeight}
minColumnWidth={MINIMAL_COLUMN_WIDTH}
Expand All @@ -48,4 +44,8 @@ VirtualTableLayout.propTypes = {
rowComponent: PropTypes.func.isRequired,
height: PropTypes.number.isRequired,
estimatedRowHeight: PropTypes.number.isRequired,
tableComponent: PropTypes.func.isRequired,
headComponent: PropTypes.func.isRequired,
bodyComponent: PropTypes.func.isRequired,
headTableComponent: PropTypes.func.isRequired,
};
8 changes: 8 additions & 0 deletions packages/dx-react-grid-material-ui/src/plugins/table.jsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import * as React from 'react';
import * as PropTypes from 'prop-types';
import { Table as TableBase } from '@devexpress/dx-react-grid';
import { TableBody, TableHead } from 'material-ui/Table';
import { Table as TableComponent } from '../templates/table';
import { TableRow } from '../templates/table-row';
import { TableLayout } from '../templates/table-layout';
import { TableCell } from '../templates/table-cell';
Expand All @@ -20,6 +22,9 @@ export class Table extends React.PureComponent {

return (
<TableBase
tableComponent={TableComponent}
headComponent={TableHead}
bodyComponent={TableBody}
layoutComponent={TableLayout}
rowComponent={TableRow}
cellComponent={TableCell}
Expand All @@ -40,6 +45,9 @@ Table.NoDataCell = TableNoDataCell;
Table.NoDataRow = TableRow;
Table.StubCell = TableStubCell;
Table.StubHeaderCell = TableStubCell;
Table.Container = TableComponent;
Table.TableHead = TableHead;
Table.TableBody = TableBody;

Table.propTypes = {
messages: PropTypes.shape({
Expand Down
Loading