From 5a412667289feed7f5204991f9911645db4e8dad Mon Sep 17 00:00:00 2001 From: Stephane Jolicoeur Date: Tue, 16 Jan 2018 14:55:52 -0800 Subject: [PATCH] Do not reset the sort state when props are updated [#154374720] Signed-off-by: Ming Xiao --- .../table/plugins/sorting_spec.js | 31 +++++++++++++++++-- src/react/table/plugins/sorting.js | 8 ----- 2 files changed, 28 insertions(+), 11 deletions(-) diff --git a/spec/pivotal-ui-react/table/plugins/sorting_spec.js b/spec/pivotal-ui-react/table/plugins/sorting_spec.js index 45cd5036b..6bb43c2fb 100644 --- a/spec/pivotal-ui-react/table/plugins/sorting_spec.js +++ b/spec/pivotal-ui-react/table/plugins/sorting_spec.js @@ -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(, root); + subject = ReactDOM.render(, 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', () => { @@ -37,5 +63,4 @@ describe('withSorting', () => { expect('.sorting-table').toExist(); }); }); - }); \ No newline at end of file diff --git a/src/react/table/plugins/sorting.js b/src/react/table/plugins/sorting.js index f65e96fcf..9e0e361ab 100644 --- a/src/react/table/plugins/sorting.js +++ b/src/react/table/plugins/sorting.js @@ -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;