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

change default InMemoryTable sorting behavior #1591

Merged
merged 13 commits into from
Feb 28, 2019
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
## [`master`](https://github.com/elastic/eui/tree/master)

- Added `allowNeutralSort` prop to `EuiInMemoryTable` to support unsorting table columns ([#1591](https://github.com/elastic/eui/pull/1591))
- Added `mobileOptions` object prop for handling of all the mobile specific options of `EuiBasicTable` ([#1462](https://github.com/elastic/eui/pull/1462))
- Table headers now accept `React.node` types ([#1462](https://github.com/elastic/eui/pull/1462))
- Added `displayOnly` prop to `EuiFormRow` ([#1582](https://github.com/elastic/eui/pull/1582))
Expand Down
5 changes: 5 additions & 0 deletions src-docs/src/views/tables/basic/props_info.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,11 @@ export const propsInfo = {
description: 'Indicates the property/field to sort on',
required: false,
type: { name: '{ field: string, direction: "asc" | "desc" }' }
},
allowNeutralSort: {
description: 'Enables/disables unsorting of table columns. Supported by EuiInMemoryTable.',
required: false,
type: { name: 'bool' }
}
}
}
Expand Down
5 changes: 5 additions & 0 deletions src-docs/src/views/tables/in_memory/props_info.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ export const propsInfo = {
required: false,
type: { name: 'boolean | #Sorting' }
},
allowNeutralSort: {
description: 'Enables/disables unsorting of table columns. Defaults to true.',
required: false,
type: { name: 'boolean' }
},
search: {
description: 'Configures a search bar for the table',
required: false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -792,6 +792,7 @@ exports[`EuiInMemoryTable with initial sorting 1`] = `
responsive={true}
sorting={
Object {
"allowNeutralSort": true,
"sort": Object {
"direction": "desc",
"field": "name",
Expand Down Expand Up @@ -1265,6 +1266,7 @@ exports[`EuiInMemoryTable with pagination, selection and sorting 1`] = `
}
sorting={
Object {
"allowNeutralSort": true,
"sort": undefined,
}
}
Expand Down Expand Up @@ -1345,6 +1347,7 @@ exports[`EuiInMemoryTable with pagination, selection, sorting and simple search
}
sorting={
Object {
"allowNeutralSort": true,
"sort": undefined,
}
}
Expand Down Expand Up @@ -1419,6 +1422,7 @@ exports[`EuiInMemoryTable with pagination, selection, sorting and a single recor
}
sorting={
Object {
"allowNeutralSort": true,
"sort": undefined,
}
}
Expand Down Expand Up @@ -1477,6 +1481,7 @@ exports[`EuiInMemoryTable with pagination, selection, sorting and column rendere
}
sorting={
Object {
"allowNeutralSort": true,
"sort": undefined,
}
}
Expand Down Expand Up @@ -1569,6 +1574,7 @@ exports[`EuiInMemoryTable with pagination, selection, sorting and configured sea
}
sorting={
Object {
"allowNeutralSort": true,
"sort": undefined,
}
}
Expand Down Expand Up @@ -1612,6 +1618,7 @@ exports[`EuiInMemoryTable with sorting 1`] = `
responsive={true}
sorting={
Object {
"allowNeutralSort": true,
"sort": undefined,
}
}
Expand Down
4 changes: 3 additions & 1 deletion src/components/basic_table/basic_table.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,8 @@ export const SelectionType = PropTypes.shape({
});

const SortingType = PropTypes.shape({
sort: PropertySortType
sort: PropertySortType,
allowNeutralSort: PropTypes.bool,
lizozom marked this conversation as resolved.
Show resolved Hide resolved
});

const BasicTablePropTypes = {
Expand Down Expand Up @@ -550,6 +551,7 @@ export class EuiBasicTable extends Component {
sorting.isSorted = !!sortDirection;
sorting.isSortAscending = sortDirection ? SortDirection.isAsc(sortDirection) : undefined;
sorting.onSort = this.resolveColumnOnSort(column);
sorting.allowNeutralSort = this.props.sorting.allowNeutralSort;
}
headers.push(
<EuiTableHeaderCell
Expand Down
21 changes: 18 additions & 3 deletions src/components/basic_table/in_memory_table.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ const InMemoryTablePropTypes = {
sort: PropertySortType
})
]),
/**
* Set `allowNeutralSort` to false to force column sorting. Defaults to true.
*/
allowNeutralSort: PropTypes.bool,
lizozom marked this conversation as resolved.
Show resolved Hide resolved
selection: SelectionType,
itemId: ItemIdType,
rowProps: PropTypes.oneOfType([PropTypes.func, PropTypes.object]),
Expand Down Expand Up @@ -163,7 +167,7 @@ export class EuiInMemoryTable extends Component {
constructor(props) {
super(props);

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

Expand All @@ -177,6 +181,7 @@ export class EuiInMemoryTable extends Component {
pageSizeOptions,
sortField,
sortDirection,
allowNeutralSort: (allowNeutralSort === false) ? false : true,
hidePerPageOptions
};
}
Expand All @@ -191,11 +196,20 @@ export class EuiInMemoryTable extends Component {
size: pageSize
} = page;

const {
let {
field: sortField,
direction: sortDirection
} = sort;

// Allow going back to 'neutral' sorting
if (this.state.allowNeutralSort &&
this.state.sortField === sortField
&& this.state.sortDirection === 'desc'
&& sortDirection === 'asc') {
sortField = '';
sortDirection = '';
lizozom marked this conversation as resolved.
Show resolved Hide resolved
}

this.setState({
pageIndex,
pageSize,
Expand Down Expand Up @@ -327,6 +341,7 @@ export class EuiInMemoryTable extends Component {
search, // eslint-disable-line no-unused-vars
onTableChange, // eslint-disable-line no-unused-vars
executeQueryOptions, // eslint-disable-line no-unused-vars
allowNeutralSort, // eslint-disable-line no-unused-vars
...rest
} = this.props;

Expand All @@ -352,12 +367,12 @@ export class EuiInMemoryTable extends Component {
// Data loaded from a server can have a default sort order which is meaningful to the
// 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 ? undefined : {
sort: (!sortField && !sortDirection) ? undefined : {
field: sortField,
direction: sortDirection,
},
allowNeutralSort: this.state.allowNeutralSort,
};

const searchBar = this.renderSearchBar();
Expand Down
18 changes: 17 additions & 1 deletion src/components/table/table_header_cell.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export const EuiTableHeaderCell = ({
onSort,
isSorted,
isSortAscending,
allowNeutralSort,
className,
scope,
mobileOptions,
Expand Down Expand Up @@ -56,6 +57,16 @@ export const EuiTableHeaderCell = ({
ariaSortValue = isSortAscending ? 'ascending' : 'descending';
}

function getScreenCasterDirection() {
if (ariaSortValue === 'ascending') {
return 'Click to sort in descending order';
} else if (allowNeutralSort && ariaSortValue === 'descending') {
return 'Click to unsort';
} else {
return 'Click to sort in ascending order';
lizozom marked this conversation as resolved.
Show resolved Hide resolved
}
}

return (
<th
className={classes}
Expand All @@ -82,7 +93,7 @@ export const EuiTableHeaderCell = ({
/>
)}
<EuiScreenReaderOnly>
<span>{`Click to sort in ${(ariaSortValue === 'descending' || 'none') ? 'ascending' : 'descending'} order`}</span>
<span>{getScreenCasterDirection()}</span>
</EuiScreenReaderOnly>
</span>
</button>
Expand Down Expand Up @@ -111,6 +122,11 @@ EuiTableHeaderCell.propTypes = {
onSort: PropTypes.func,
isSorted: PropTypes.bool,
isSortAscending: PropTypes.bool,
/**
* Set `allowNeutralSort` on EuiInMemoryTable to false to force column sorting.
* EuiBasicTable always forces column sorting.
*/
allowNeutralSort: PropTypes.bool,
lizozom marked this conversation as resolved.
Show resolved Hide resolved
scope: PropTypes.oneOf(['col', 'row', 'colgroup', 'rowgroup']),
/**
* _DEPRECATED: use `mobileOptions.only = true`_
Expand Down