Skip to content

Commit

Permalink
Do not reset the sort state when props are updated [#154374720]
Browse files Browse the repository at this point in the history
Signed-off-by: Ming Xiao <mxiao@pivotal.io>
  • Loading branch information
sjolicoeur authored and Ming Xiao committed Jan 16, 2018
1 parent f1b19ab commit 5a41266
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 11 deletions.
31 changes: 28 additions & 3 deletions spec/pivotal-ui-react/table/plugins/sorting_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,45 @@ describe('withSorting', () => {
});

describe('with columns', () => {
let subject;

beforeEach(() => {
const columns = [{
attribute: 'attr1'
attribute: 'attr1', sortable: true
}, {
attribute: 'attr2', displayName: 'Display2'
}];

ReactDOM.render(<ComposedTable {...{columns, data, className: 'sorting-table'}}/>, root);
subject = ReactDOM.render(<ComposedTable {...{columns, data, className: 'sorting-table'}}/>, root);
});

it('renders', () => {
expect('.sorting-table').toExist();
});

describe('when sorted in descending order', () => {
beforeEach(() => {
$('.sortable:eq(0)').click();
});

it('sorts in descending order', () => {
expect('.sortable:eq(0)').toHaveClass('sorted-desc');
});

describe('when new data is added', () => {
beforeEach(() => {
data.push({
attr1: 'row3-value1', attr2: 'row3-value2'
});
subject::setProps({data});
});

it('retains the sort order', () => {
expect('.sortable:eq(0)').toHaveClass('sorted-desc');
expect('tbody tr:eq(0) td:eq(0)').toHaveText('row3-value1');
});
});
});
});

describe('without columns', () => {
Expand All @@ -37,5 +63,4 @@ describe('withSorting', () => {
expect('.sorting-table').toExist();
});
});

});
8 changes: 0 additions & 8 deletions src/react/table/plugins/sorting.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,6 @@ export function withSorting(Table) {
};
}

componentWillReceiveProps({columns, defaultSort}) {
if (!columns) return;
const sortColumn = find(columns, ({sortable, attribute}) =>
defaultSort ? attribute === defaultSort : sortable
);
this.setState({sortColumn, sortOrder: SORT_ORDER.asc});
}

updateSort(column) {
const {sortColumn} = this.state;
const isSortColumn = column === sortColumn;
Expand Down

0 comments on commit 5a41266

Please sign in to comment.