Skip to content

Commit

Permalink
Fix bug in EuiInMemoryTable to not mutate the items prop (#1057)
Browse files Browse the repository at this point in the history
* Fix EuiInMemoryTable to not mutate the items prop when sorting

* changelog
  • Loading branch information
chandlerprall authored Jul 31, 2018
1 parent 35f77ba commit 34d064c
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
**Bug fixes**

- Fixed `EuiXYChart` responsive resize in a flexbox layout ([#1041](https://github.com/elastic/eui/pull/1041))
- `EuiInMemoryTable` no longer mutates the `items` prop array when sorting, adding deterministic sorting ([#1057](https://github.com/elastic/eui/pull/1057))

## [`3.2.1`](https://github.com/elastic/eui/tree/v3.2.1)

Expand Down
6 changes: 5 additions & 1 deletion src/components/basic_table/in_memory_table.js
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,11 @@ export class EuiInMemoryTable extends Component {
const matchingItems = query ? EuiSearchBar.Query.execute(query, items) : items;

const sortedItems =
sortField ? matchingItems.sort(this.getItemSorter()) : matchingItems;
sortField
? matchingItems
.slice(0) // avoid mutating the source array
.sort(this.getItemSorter()) // sort, causes mutation
: matchingItems;

const visibleItems = pageSize ? (() => {
const startIndex = pageIndex * pageSize;
Expand Down
16 changes: 11 additions & 5 deletions src/components/basic_table/in_memory_table.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -257,13 +257,18 @@ describe('EuiInMemoryTable', () => {

test('with initial sorting', () => {

const items = [
{ id: '1', name: 'name1' },
{ id: '2', name: 'name2' },
{ id: '3', name: 'name3' }
];

// copy the array to ensure the `items` prop doesn't mutate
const itemsProp = items.slice(0);

const props = {
...requiredProps,
items: [
{ id: '1', name: 'name1' },
{ id: '2', name: 'name2' },
{ id: '3', name: 'name3' }
],
items: itemsProp,
columns: [
{
field: 'name',
Expand All @@ -284,6 +289,7 @@ describe('EuiInMemoryTable', () => {
);

expect(component).toMatchSnapshot();
expect(itemsProp).toEqual(items);
});

test('with pagination and selection', () => {
Expand Down

0 comments on commit 34d064c

Please sign in to comment.