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

[EuiBasicTable] Fix sorting bug #453

Merged
merged 1 commit into from
Mar 19, 2018
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 @@ -5,6 +5,7 @@
**Bug fixes**

- Made `EuiIconTip` screen reader accessible ([#534](https://github.com/elastic/eui/pull/534))
- Fixed a sorting issue in `EuiInMemoryTable` ([#453](https://github.com/elastic/eui/pull/453))

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

Expand Down
78 changes: 62 additions & 16 deletions src/components/basic_table/__snapshots__/basic_table.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ exports[`EuiBasicTable basic - empty - custom message 1`] = `
<EuiTableHeader>
<EuiTableHeaderCell
align="left"
isSortAscending={false}
isSorted={false}
key="_data_h_name_0"
scope="col"
>
Expand Down Expand Up @@ -39,8 +37,6 @@ exports[`EuiBasicTable basic - empty - custom message as node 1`] = `
<EuiTableHeader>
<EuiTableHeaderCell
align="left"
isSortAscending={false}
isSorted={false}
key="_data_h_name_0"
scope="col"
>
Expand Down Expand Up @@ -78,8 +74,6 @@ exports[`EuiBasicTable basic - empty 1`] = `
<EuiTableHeader>
<EuiTableHeaderCell
align="left"
isSortAscending={false}
isSorted={false}
key="_data_h_name_0"
scope="col"
>
Expand Down Expand Up @@ -109,8 +103,6 @@ exports[`EuiBasicTable basic - with items 1`] = `
<EuiTableHeader>
<EuiTableHeaderCell
align="left"
isSortAscending={false}
isSorted={false}
key="_data_h_name_0"
scope="col"
>
Expand Down Expand Up @@ -173,8 +165,6 @@ exports[`EuiBasicTable with pagination - 2nd page 1`] = `
<EuiTableHeader>
<EuiTableHeaderCell
align="left"
isSortAscending={false}
isSorted={false}
key="_data_h_name_0"
scope="col"
>
Expand Down Expand Up @@ -234,8 +224,6 @@ exports[`EuiBasicTable with pagination 1`] = `
<EuiTableHeader>
<EuiTableHeaderCell
align="left"
isSortAscending={false}
isSorted={false}
key="_data_h_name_0"
scope="col"
>
Expand Down Expand Up @@ -309,8 +297,6 @@ exports[`EuiBasicTable with pagination and error 1`] = `
<EuiTableHeader>
<EuiTableHeaderCell
align="left"
isSortAscending={false}
isSorted={false}
key="_data_h_name_0"
scope="col"
>
Expand Down Expand Up @@ -360,8 +346,6 @@ exports[`EuiBasicTable with pagination and selection 1`] = `
</EuiTableHeaderCellCheckbox>
<EuiTableHeaderCell
align="left"
isSortAscending={false}
isSorted={false}
key="_data_h_name_0"
scope="col"
>
Expand Down Expand Up @@ -1389,6 +1373,68 @@ exports[`EuiBasicTable with pagination, selection, sorting, column renderer and
</div>
`;

exports[`EuiBasicTable with sortable columns and sorting disabled 1`] = `
<div
className="euiBasicTable testClass1 testClass2"
>
<EuiTable>
<EuiTableHeader>
<EuiTableHeaderCell
align="left"
key="_data_h_name_0"
scope="col"
>
Name
</EuiTableHeaderCell>
</EuiTableHeader>
<EuiTableBody>
<EuiTableRow
isSelected={false}
key="row_0_0"
onMouseOut={[Function]}
onMouseOver={[Function]}
>
<EuiTableRowCell
align="left"
key="_data_column_name_0_0"
textOnly={true}
>
name1
</EuiTableRowCell>
</EuiTableRow>
<EuiTableRow
isSelected={false}
key="row_1_1"
onMouseOut={[Function]}
onMouseOver={[Function]}
>
<EuiTableRowCell
align="left"
key="_data_column_name_1_0"
textOnly={true}
>
name2
</EuiTableRowCell>
</EuiTableRow>
<EuiTableRow
isSelected={false}
key="row_2_2"
onMouseOut={[Function]}
onMouseOver={[Function]}
>
<EuiTableRowCell
align="left"
key="_data_column_name_2_0"
textOnly={true}
>
name3
</EuiTableRowCell>
</EuiTableRow>
</EuiTableBody>
</EuiTable>
</div>
`;

exports[`EuiBasicTable with sorting 1`] = `
<div
className="euiBasicTable testClass1 testClass2"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,11 @@ exports[`EuiInMemoryTable with pagination, selection and sorting 1`] = `
"onSelectionChanged": [Function],
}
}
sorting={
Object {
"sort": undefined,
}
}
/>
`;

Expand Down Expand Up @@ -404,6 +409,11 @@ exports[`EuiInMemoryTable with pagination, selection, sorting and simple search
"onSelectionChanged": [Function],
}
}
sorting={
Object {
"sort": undefined,
}
}
/>
</div>
`;
Expand Down Expand Up @@ -467,6 +477,11 @@ exports[`EuiInMemoryTable with pagination, selection, sorting and a single recor
"onSelectionChanged": [Function],
}
}
sorting={
Object {
"sort": undefined,
}
}
/>
`;

Expand Down Expand Up @@ -515,6 +530,11 @@ exports[`EuiInMemoryTable with pagination, selection, sorting and column rendere
"onSelectionChanged": [Function],
}
}
sorting={
Object {
"sort": undefined,
}
}
/>
`;

Expand Down Expand Up @@ -596,6 +616,11 @@ exports[`EuiInMemoryTable with pagination, selection, sorting and configured sea
"onSelectionChanged": [Function],
}
}
sorting={
Object {
"sort": undefined,
}
}
/>
</div>
`;
Expand Down Expand Up @@ -630,5 +655,10 @@ exports[`EuiInMemoryTable with sorting 1`] = `
}
noItemsMessage="No items found"
onChange={[Function]}
sorting={
Object {
"sort": undefined,
}
}
/>
`;
27 changes: 15 additions & 12 deletions src/components/basic_table/basic_table.js
Original file line number Diff line number Diff line change
Expand Up @@ -368,18 +368,19 @@ export class EuiBasicTable extends Component {
}

// field data column
const sortDirection = this.resolveColumnSortDirection(column);
const onSort = this.resolveColumnOnSort(column);
const isSorted = !!sortDirection;
const isSortAscending = SortDirection.isAsc(sortDirection);
const sorting = {};
if (this.props.sorting && column.sortable) {
const sortDirection = this.resolveColumnSortDirection(column);
sorting.isSorted = !!sortDirection;
sorting.isSortAscending = sortDirection ? SortDirection.isAsc(sortDirection) : undefined;
sorting.onSort = this.resolveColumnOnSort(column);
}
headers.push(
<EuiTableHeaderCell
key={`_data_h_${column.field}_${index}`}
align={align}
isSorted={isSorted}
isSortAscending={isSortAscending}
onSort={onSort}
width={column.width}
{...sorting}
>
{column.name}
</EuiTableHeaderCell>
Expand Down Expand Up @@ -604,13 +605,15 @@ export class EuiBasicTable extends Component {
}

resolveColumnOnSort(column) {
if (column.sortable) {
if (!this.props.onChange) {
throw new Error(`BasicTable is configured to be sortable on column [${column.field}] but
const { sorting } = this.props;
if (!sorting || !column.sortable) {
return;
}
if (!this.props.onChange) {
throw new Error(`BasicTable is configured to be sortable on column [${column.field}] but
[onChange] is not configured. This callback must be implemented to handle the sort requests`);
}
return () => this.onColumnSortChange(column);
}
return () => this.onColumnSortChange(column);
}

resolveContentRenderer(column) {
Expand Down
26 changes: 26 additions & 0 deletions src/components/basic_table/basic_table.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,32 @@ describe('EuiBasicTable', () => {
expect(component).toMatchSnapshot();
});

test('with sortable columns and sorting disabled', () => {

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
}
],
onChange: () => {}
};
const component = shallow(
<EuiBasicTable {...props} />
);

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

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

const props = {
Expand Down
6 changes: 3 additions & 3 deletions src/components/basic_table/in_memory_table.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ export class EuiInMemoryTable extends Component {
constructor(props) {
super(props);

const { search, pagination, sorting, columns } = props;
const { search, pagination, sorting } = props;
const { pageIndex, pageSize, pageSizeOptions } = getInitialPagination(pagination);
const { sortField, sortDirection } = getInitialSorting(sorting);

Expand Down Expand Up @@ -238,8 +238,8 @@ export class EuiInMemoryTable extends Component {
// user, but can't be reproduced with client-side sort logic. So we allow the table to display
// rows in the order in which they're initially loaded by providing an undefined sorting prop.
// Once a user sorts a column, this will become a fully-defined sorting prop.
const sorting = !hasSorting || (!sortField && !sortDirection) ? undefined : {
sort: {
const sorting = !hasSorting ? undefined : {
sort: (!sortField && !sortDirection) ? undefined : {
field: sortField,
direction: sortDirection,
},
Expand Down