Skip to content

Commit

Permalink
Add ability to specify initial sorting to EuiInMemoryTable (#547)
Browse files Browse the repository at this point in the history
* Add ability to specify initial sorting to EuiInMemoryTable
  • Loading branch information
jen-huang authored Mar 21, 2018
1 parent aac6c20 commit 769bb30
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 9 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# [`master`](https://github.com/elastic/eui/tree/master)

No public interface changes since `0.0.32`.
- Added initial sorting option to `EuiInMemoryTable` ([#547](https://github.com/elastic/eui/pull/547))

# [`0.0.32`](https://github.com/elastic/eui/tree/v0.0.32)

Expand Down
9 changes: 8 additions & 1 deletion src-docs/src/views/tables/in_memory/in_memory.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,19 @@ export const Table = () => {
sortable: true
}];

const sorting = {
sort: {
field: 'dateOfBirth',
direction: 'desc',
}
};

return (
<EuiInMemoryTable
items={store.users}
columns={columns}
pagination={true}
sorting={true}
sorting={sorting}
/>
);
};
6 changes: 3 additions & 3 deletions src-docs/src/views/tables/in_memory/props_info.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { omit } from '../../../../../src/services/objects';
import { propsInfo as basicPropsInfo } from '../basic/props_info';
import { propsInfo as searchBarPropsInfo } from '../../search_bar/props_info';

const basicTableProps = omit(basicPropsInfo, [ 'EuiBasicTable', 'Pagination', 'Sorting' ]);
const basicTableProps = omit(basicPropsInfo, [ 'EuiBasicTable', 'Pagination' ]);
const searchBarProps = omit(searchBarPropsInfo, [ 'EuiSearchBar' ]);

export const propsInfo = {
Expand Down Expand Up @@ -39,9 +39,9 @@ export const propsInfo = {
type: { name: 'boolean | #Pagination' }
},
sorting: {
description: 'Enables/disables sorting',
description: 'Enables/disables sorting. Can be an object that configures initial sorting when enabled',
required: false,
type: { name: 'boolean' }
type: { name: 'boolean | #Sorting' }
},
search: {
description: 'Configures a search bar for the table',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,47 @@ exports[`EuiInMemoryTable empty array 1`] = `
/>
`;

exports[`EuiInMemoryTable with initial sorting 1`] = `
<EuiBasicTable
columns={
Array [
Object {
"description": "description",
"field": "name",
"name": "Name",
"sortable": true,
},
]
}
items={
Array [
Object {
"id": "3",
"name": "name3",
},
Object {
"id": "2",
"name": "name2",
},
Object {
"id": "1",
"name": "name1",
},
]
}
noItemsMessage="No items found"
onChange={[Function]}
sorting={
Object {
"sort": Object {
"direction": "desc",
"field": "name",
},
}
}
/>
`;

exports[`EuiInMemoryTable with items 1`] = `
<EuiBasicTable
columns={
Expand Down
13 changes: 9 additions & 4 deletions src/components/basic_table/in_memory_table.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
defaults as paginationBarDefaults
} from './pagination_bar';
import { isBoolean, isString } from '../../services/predicate';
import { Comparators } from '../../services/sort';
import { Comparators, PropertySortType } from '../../services/sort';
import {
Query,
QueryType,
Expand Down Expand Up @@ -40,7 +40,12 @@ const InMemoryTablePropTypes = {
pageSizeOptions: PropTypes.arrayOf(PropTypes.number)
})
]),
sorting: PropTypes.bool,
sorting: PropTypes.oneOfType([
PropTypes.bool,
PropTypes.shape({
sort: PropertySortType
})
]),
selection: SelectionType
};

Expand Down Expand Up @@ -81,7 +86,7 @@ const getInitialPagination = (pagination) => {
};

const getInitialSorting = (sorting) => {
if (!sorting) {
if (!sorting || !sorting.sort) {
return {
sortField: undefined,
sortDirection: undefined,
Expand All @@ -91,7 +96,7 @@ const getInitialSorting = (sorting) => {
const {
field: sortField,
direction: sortDirection,
} = sorting;
} = sorting.sort;

return {
sortField,
Expand Down
31 changes: 31 additions & 0 deletions src/components/basic_table/in_memory_table.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,37 @@ describe('EuiInMemoryTable', () => {
expect(component).toMatchSnapshot();
});

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

const props = {
...requiredProps,
items: [
{ id: '1', name: 'name1' },
{ id: '2', name: 'name2' },
{ id: '3', name: 'name3' }
],
columns: [
{
field: 'name',
name: 'Name',
description: 'description',
sortable: true
}
],
sorting: {
sort: {
field: 'name',
direction: 'desc'
}
}
};
const component = shallow(
<EuiInMemoryTable {...props} />
);

expect(component).toMatchSnapshot();
});

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

const props = {
Expand Down

0 comments on commit 769bb30

Please sign in to comment.