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

Fix bug in EuiInMemoryTable to not mutate the items prop #1057

Merged
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,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