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

added default sort order to dataGrid #2987

Merged
merged 4 commits into from
Mar 6, 2020
Merged
Show file tree
Hide file tree
Changes from 3 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
@@ -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))
- Added `showOnFocus` prop to `EuiScreenReaderOnly` to force display on keyboard focus ([#2976](https://github.com/elastic/eui/pull/2976))
- Added `EuiSkipLink` component ([#2976](https://github.com/elastic/eui/pull/2976))
- Created `EuiBadgeGroup` component ([#2921](https://github.com/elastic/eui/pull/2921))
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