Skip to content

Commit

Permalink
added default sort order props to dataGrid (#2987)
Browse files Browse the repository at this point in the history
* updated

* added for schema

Co-authored-by: Chandler Prall <chandler.prall@gmail.com>
  • Loading branch information
anishagg17 and chandlerprall authored Mar 6, 2020
1 parent 4427816 commit 9a52f2f
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 45 deletions.
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 `EuiDataGrid`'s default sort order property ([#2987](https://github.com/elastic/eui/pull/2987))
- Fixed `EuiDataGrid`'s pagination visibility when changing rows per page ([#2978](https://github.com/elastic/eui/pull/2978))
- Added `highlightAll` prop to `EuiHighlight` to highlight all matches ([#2957](https://github.com/elastic/eui/pull/2957))
- Added `showOnFocus` prop to `EuiScreenReaderOnly` to force display on keyboard focus ([#2976](https://github.com/elastic/eui/pull/2976))
Expand Down
3 changes: 3 additions & 0 deletions src-docs/src/views/datagrid/datagrid.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
const columns = [
{
id: 'name',
defaultSortDirection: 'asc',
},
{
id: 'email',
Expand Down Expand Up @@ -49,6 +50,7 @@ const columns = [
},
{
id: 'date',
defaultSortDirection: 'desc',
},
{
id: 'amount',
Expand All @@ -59,6 +61,7 @@ const columns = [
},
{
id: 'version',
defaultSortDirection: 'desc',
initialWidth: 65,
isResizable: false,
},
Expand Down
101 changes: 56 additions & 45 deletions src/components/datagrid/column_sorting.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ export const useColumnSorting = (
): ReactNode => {
const [isOpen, setIsOpen] = useState(false);
const [avilableColumnsisOpen, setAvailableColumnsIsOpen] = useState(false);

// prune any non-existant/hidden columns from sorting
useEffect(() => {
if (sorting) {
Expand Down Expand Up @@ -213,50 +212,62 @@ export const useColumnSorting = (
<div
className="euiDataGridColumnSorting__fieldList"
role="listbox">
{inactiveSortableColumns.map(({ id }) => (
<button
key={id}
className="euiDataGridColumnSorting__field"
aria-label={`${sortFieldAriaLabel} ${id}`}
role="option"
aria-selected="false"
data-test-subj={`dataGridColumnSortingPopoverColumnSelection-${id}`}
onClick={() => {
const nextColumns = [...sorting.columns];
nextColumns.push({ id, direction: 'asc' });
sorting.onSort(nextColumns);
}}>
<EuiFlexGroup
alignItems="center"
gutterSize="s"
component="span"
responsive={false}>
<EuiFlexItem grow={false}>
<EuiToken
iconType={
schemaDetails(id) != null
? getDetailsForSchema(
schemaDetectors,
schema[id].columnType
).icon
: 'tokenString'
}
color={
schemaDetails(id) != null
? getDetailsForSchema(
schemaDetectors,
schema[id].columnType
).color
: undefined
}
/>
</EuiFlexItem>
<EuiFlexItem grow={false}>
<EuiText size="xs">{id}</EuiText>
</EuiFlexItem>
</EuiFlexGroup>
</button>
))}
{inactiveSortableColumns.map(
({ id, defaultSortDirection }) => {
return (
<button
key={id}
className="euiDataGridColumnSorting__field"
aria-label={`${sortFieldAriaLabel} ${id}`}
role="option"
aria-selected="false"
data-test-subj={`dataGridColumnSortingPopoverColumnSelection-${id}`}
onClick={() => {
const nextColumns = [...sorting.columns];
nextColumns.push({
id,
direction:
defaultSortDirection ||
(schemaDetails(id) &&
schemaDetails(id)!
.defaultSortDirection) ||
'asc',
});
sorting.onSort(nextColumns);
}}>
<EuiFlexGroup
alignItems="center"
gutterSize="s"
component="span"
responsive={false}>
<EuiFlexItem grow={false}>
<EuiToken
iconType={
schemaDetails(id) != null
? getDetailsForSchema(
schemaDetectors,
schema[id].columnType
).icon
: 'tokenString'
}
color={
schemaDetails(id) != null
? getDetailsForSchema(
schemaDetectors,
schema[id].columnType
).color
: undefined
}
/>
</EuiFlexItem>
<EuiFlexItem grow={false}>
<EuiText size="xs">{id}</EuiText>
</EuiFlexItem>
</EuiFlexGroup>
</button>
);
}
)}
</div>
)}
</EuiI18n>
Expand Down
4 changes: 4 additions & 0 deletions src/components/datagrid/data_grid_schema.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ export interface EuiDataGridSchemaDetector {
* Whether this column is sortable
*/
isSortable: boolean;
/**
* Default sort direction of the column
*/
defaultSortDirection?: 'asc' | 'desc';
}

const numericChars = new Set([
Expand Down
4 changes: 4 additions & 0 deletions src/components/datagrid/data_grid_types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ export interface EuiDataGridColumn {
* Whether this column is sortable
*/
isSortable?: boolean;
/**
* Default sort direction of the column
*/
defaultSortDirection?: 'asc' | 'desc';
}

export interface EuiDataGridColumnVisibility {
Expand Down

0 comments on commit 9a52f2f

Please sign in to comment.