Skip to content

Commit

Permalink
feat(Table): Rename SortableTable React component to Table
Browse files Browse the repository at this point in the history
* We only have one Table component
* Fix styling when table has no sortable columns

[#115065983]

BREAKING CHANGE: SortableTable React component now called Table

Signed-off-by: Elena Sharma <esharma@pivotal.io>
  • Loading branch information
Adam Berkovec authored and pivotal committed Mar 14, 2016
1 parent 241f882 commit b2ccf2a
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 40 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
require('../spec_helper');
import {SortableTable, TableCell, TableHeader, TableRow} from '../../../src/pivotal-ui-react/sortable-table/sortable-table';
import {Table, TableCell, TableHeader, TableRow} from '../../../src/pivotal-ui-react/table/table';

describe('SortableTable', function() {
describe('Table', function() {
let clickSpy;
describe('with multiple columns', function() {
clickSpy = jasmine.createSpy('click');
Expand Down Expand Up @@ -55,7 +55,7 @@ describe('SortableTable', function() {

function renderSortableTable(data, props = {}) {
ReactDOM.render((
<SortableTable columns={columns} data={data} {...props}/>
<Table columns={columns} data={data} {...props}/>
),
root
);
Expand Down Expand Up @@ -240,7 +240,7 @@ describe('SortableTable', function() {
const data = [{title: 'foo'}, { title: 'sup'}, { title: 'yee'}];

ReactDOM.render((
<SortableTable columns={columns} data={data} />
<Table columns={columns} data={data} />
),
root
);
Expand Down Expand Up @@ -282,7 +282,7 @@ describe('SortableTable', function() {
];

ReactDOM.render(
<SortableTable columns={columns} data={data}/>,
<Table columns={columns} data={data}/>,
root
);
});
Expand Down Expand Up @@ -326,7 +326,7 @@ describe('SortableTable', function() {
];

ReactDOM.render(
<SortableTable columns={columns} data={data}/>,
<Table columns={columns} data={data}/>,
root
);
});
Expand Down Expand Up @@ -368,7 +368,7 @@ describe('SortableTable', function() {
];

ReactDOM.render((
<SortableTable columns={columns} data={data} CustomRow={CustomRow}/>
<Table columns={columns} data={data} CustomRow={CustomRow}/>
),
root
);
Expand All @@ -390,6 +390,29 @@ describe('SortableTable', function() {
});
});

describe('with no sortable columns', function() {
it('does not assign sorted-asc or sorted-des to a column', function() {
const columns = [
{
attribute: 'title',
displayName: 'Title',
sortable: false
}
];

const data = [{title: 'foo'}, { title: 'sup'}, { title: 'yee'}];

ReactDOM.render((
<Table columns={columns} data={data} />
),
root
);

expect('th').not.toHaveClass('sorted-asc');
expect('th').not.toHaveClass('sorted-desc')
});
});

it('respects default sort', function() {
const columns = [
{
Expand All @@ -416,7 +439,7 @@ describe('SortableTable', function() {
];

ReactDOM.render((
<SortableTable columns={columns} data={data} defaultSort='theDefault'/>
<Table columns={columns} data={data} defaultSort='theDefault'/>
),
root
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export const TableCell = (props) => <td {...props}/>;

export const TableRow = (props) => <tr {...props}/>;

export class SortableTable extends React.Component {
export class Table extends React.Component {
constructor(props, context) {
super(props, context);
const {columns, defaultSort} = this.props;
Expand All @@ -47,8 +47,7 @@ export class SortableTable extends React.Component {
return defaultSort ? attribute === defaultSort : sortable;
});

// If none of the columns are sortable we default to the 0th column
this.state = {sortColumn: sortCol === -1 ? 0 : sortCol, sortAscending: true};
this.state = {sortColumn: sortCol, sortAscending: true};
}

static propTypes = {
Expand All @@ -64,7 +63,7 @@ export class SortableTable extends React.Component {

sortedRows() {
const {sortColumn, sortAscending} = this.state;
const {columns, data, CustomRow} = this.props;
const {columns, data} = this.props;

const sortedData = sortBy(data, (datum) => {
const column = columns[sortColumn];
Expand All @@ -73,7 +72,14 @@ export class SortableTable extends React.Component {
});

if(!sortAscending) sortedData.reverse();
return sortedData.map((datum, rowKey) => {

return this.rows(sortedData);
}

rows(data) {
const {columns, CustomRow} = this.props;

return data.map((datum, rowKey) => {
const cells = columns.map(({attribute, CustomCell}, key) => {
const Cell = CustomCell || TableCell;
return <Cell key={key} index={rowKey} value={datum[attribute]} rowDatum={datum}>{datum[attribute]}</Cell>;
Expand Down Expand Up @@ -109,15 +115,19 @@ export class SortableTable extends React.Component {
}

render() {
const {sortColumn} = this.state;
const {data} = this.props;
const props = mergeProps(this.props, {className: ['table', 'table-sortable', 'table-data']});

const rows = (sortColumn === -1) ? this.rows(data) : this.sortedRows();

return (
<table {...props} >
<thead>
<tr>{this.renderHeaders()}</tr>
</thead>
<tbody>
{this.sortedRows()}
{rows}
</tbody>
</table>
);
Expand Down
8 changes: 4 additions & 4 deletions library/src/pivotal-ui/javascripts/components.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,10 @@ module.exports = {
PortalDestination: require('pui-react-portals').PortalDestination,
StreamList: require('pui-react-stream-list').StreamList,
StreamListItem: require('pui-react-stream-list').StreamListItem,
SortableTable: require('pui-react-sortable-table').SortableTable,
TableHeader: require('pui-react-sortable-table').TableHeader,
TableCell: require('pui-react-sortable-table').TableCell,
TableRow: require('pui-react-sortable-table').TableRow,
Table: require('pui-react-table').Table,
TableHeader: require('pui-react-table').TableHeader,
TableCell: require('pui-react-table').TableCell,
TableRow: require('pui-react-table').TableRow,
...autocomplete,
...selectFancy,
TileLayout: TileLayout};
Original file line number Diff line number Diff line change
Expand Up @@ -6,35 +6,26 @@ categories:
- react_base_tables
- react_all
---
*/

/*doc
<code class="pam">
<i class="fa fa-download" alt="Install the Component">
npm install pui-react-sortable-table --save
npm install pui-react-table --save
</i>
</code>
Require the subcomponents:
```
var SortableTable = require('pui-react-sortable-table').SortableTable;
var TableHeader = require('pui-react-sortable-table').TableHeader;
var TableRow = require('pui-react-sortable-table').TableRow;
var TableCell = require('pui-react-sortable-table').TableCell;
var Table = require('pui-react-table').Table;
var TableHeader = require('pui-react-table').TableHeader;
var TableRow = require('pui-react-table').TableRow;
var TableCell = require('pui-react-table').TableCell;
```
---
title: Sortable
name: table_sortable_react
parent: table_react
---
The `SortableTable` component is a robust component that offers a styled table with fully
The `Table` component is a robust component that offers a styled table with fully
functioning sort. If the rows change, the content on the page will update.
The `SortableTable` expects the following properties:
The `Table` expects the following properties:
Property | Required? | Type | Description
-------------| ----------| -----------------| --------------------------------------------------------------------------
Expand Down Expand Up @@ -105,11 +96,11 @@ var data = [
```
```react_example
<SortableTable columns={columns} data={data} defaultSort='instances'/>
<Table columns={columns} data={data} defaultSort='instances'/>
```
The `TableRow` component is provided for users who wish to customize their rows
with the `CustomRow` prop to `SortableTable`. If a custom row is provided, the table will use that
with the `CustomRow` prop to `Table`. If a custom row is provided, the table will use that
component to render each row, giving it a `children` prop representing the cells for that row and `index`
representing the (zero-indexed) row number.
Expand All @@ -129,7 +120,7 @@ var CustomRow = React.createClass({
```
```react_example
<SortableTable columns={columns} data={data} CustomRow={CustomRow}/>
<Table columns={columns} data={data} CustomRow={CustomRow}/>
```
The `TableCell` component is provided for users who wish to customize their cells
Expand Down Expand Up @@ -171,7 +162,7 @@ var CustomRow = React.createClass({
```
```react_example
<SortableTable columns={customCellColumns} data={data}/>
<Table columns={customCellColumns} data={data}/>
```
*/
2 changes: 1 addition & 1 deletion styleguide/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@
"pui-react-portals": "file:../library/dist/react/portals",
"pui-react-radio": "file:../library/dist/react/radio",
"pui-react-ribbons": "file:../library/dist/react/ribbons",
"pui-react-sortable-table": "file:../library/dist/react/sortable-table",
"pui-react-table": "file:../library/dist/react/table",
"pui-react-stream-list": "file:../library/dist/react/stream-list",
"pui-react-tabs": "file:../library/dist/react/tabs",
"pui-react-tile-layout": "file:../library/dist/react/tile-layout",
Expand Down
2 changes: 1 addition & 1 deletion styleguide/src/pivotal-ui-components.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ assignToGlobal([
require('pui-react-portals'),
require('pui-react-radio'),
require('pui-react-ribbons'),
require('pui-react-sortable-table'),
require('pui-react-table'),
require('pui-react-stream-list'),
require('pui-react-tabs'),
require('pui-react-tile-layout'),
Expand Down

0 comments on commit b2ccf2a

Please sign in to comment.