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

EuiDataGrid column visibility & ordering #2207

Merged
Show file tree
Hide file tree
Changes from 2 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
12 changes: 5 additions & 7 deletions src-docs/src/views/datagrid/datagrid.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,16 @@ import {

const columns = [
{
name: 'name',
id: 'name',
},
{
name: 'avatar_url',
id: 'avatar_url',
},
{
name: 'url',
id: 'url',
},
{
name: 'contributions',
id: 'contributions',
},
];

Expand Down Expand Up @@ -395,9 +395,7 @@ export default class DataGrid extends Component {
rowHover: this.state.rowHoverSelected,
header: this.state.headerSelected,
}}
renderCellValue={({ rowIndex, columnName }) =>
data[rowIndex][columnName]
}
renderCellValue={({ rowIndex, columnId }) => data[rowIndex][columnId]}
pagination={{
...pagination,
pageSizeOptions: [5, 10, 25],
Expand Down
36 changes: 34 additions & 2 deletions src/components/datagrid/__snapshots__/data_grid.test.tsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,38 @@ exports[`EuiDataGrid rendering renders with common and div attributes 1`] = `
data-test-subj="test subject string"
role="grid"
>
<div
class="euiPopover euiPopover--anchorDownCenter"
data-test-subj="dataGridColumnSelectorPopover"
>
<div
class="euiPopover__anchor"
>
<button
class="euiButton euiButton--primary"
type="button"
>
<span
class="euiButton__content"
>
<svg
aria-hidden="true"
class="euiIcon euiIcon--medium euiIcon-isLoading euiButton__icon"
focusable="false"
height="16"
viewBox="0 0 16 16"
width="16"
xmlns="http://www.w3.org/2000/svg"
/>
<span
class="euiButton__text"
>
Columns
</span>
</span>
</button>
</div>
</div>
<div
class="euiDataGrid__content"
>
Expand All @@ -134,7 +166,7 @@ exports[`EuiDataGrid rendering renders with common and div attributes 1`] = `
>
<div
class="euiDataGridHeaderCell"
data-test-subj="dataGridHeaderCell"
data-test-subj="dataGridHeaderCell-A"
role="columnheader"
style="width:100px"
>
Expand All @@ -151,7 +183,7 @@ exports[`EuiDataGrid rendering renders with common and div attributes 1`] = `
</div>
<div
class="euiDataGridHeaderCell"
data-test-subj="dataGridHeaderCell"
data-test-subj="dataGridHeaderCell-B"
role="columnheader"
style="width:100px"
>
Expand Down
99 changes: 99 additions & 0 deletions src/components/datagrid/column_selector.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
import React, { Fragment, FunctionComponent, useState } from 'react';
import { EuiDataGridColumn } from './data_grid_types';
// @ts-ignore-next-line
import { EuiPopover } from '../popover';
// @ts-ignore-next-line
import { EuiButton } from '../button';
// @ts-ignore-next-line
import { EuiSwitch } from '../form';
import {
EuiDragDropContext,
EuiDraggable,
EuiDroppable,
} from '../drag_and_drop';
import { DropResult } from 'react-beautiful-dnd';
import { EuiIcon } from '../icon';

export const useColumnSelector = (
availableColumns: EuiDataGridColumn[]
): [FunctionComponent<any>, EuiDataGridColumn[]] => {
const [sortedColumns, setSortedColumns] = useState(availableColumns);

const [visibleColumns, setVisibleColumns] = useState(availableColumns);
const visibleColumnIds = visibleColumns.reduce((visibleColumnIds, { id }) => {
visibleColumnIds.add(id);
return visibleColumnIds;
}, new Set());

const [isOpen, setIsOpen] = useState(false);

function onDragEnd({
source: { index: sourceIndex },
destination,
}: DropResult) {
const destinationIndex = destination!.index;
const nextSortedColumns = [...sortedColumns];

nextSortedColumns[sourceIndex] = sortedColumns[destinationIndex];
nextSortedColumns[destinationIndex] = sortedColumns[sourceIndex];

setSortedColumns(nextSortedColumns);

const nextVisibleColumns = nextSortedColumns.filter(({ id }) =>
visibleColumnIds.has(id)
);
setVisibleColumns(nextVisibleColumns);
}

const ColumnSelector = () => (
<EuiPopover
data-test-subj="dataGridColumnSelectorPopover"
isOpen={isOpen}
closePopover={() => setIsOpen(false)}
button={
<Fragment>
<EuiButton iconType="apps" onClick={() => setIsOpen(!isOpen)}>
Columns
</EuiButton>
</Fragment>
}>
<EuiDragDropContext onDragEnd={onDragEnd}>
<EuiDroppable droppableId="columnOrder">
<div>
{sortedColumns.map(({ id }, index) => (
<EuiDraggable key={id} draggableId={id} index={index}>
{provided => (
<Fragment>
<div {...provided.dragHandleProps}>
<EuiIcon type="grab" />
</div>
<EuiSwitch
name={id}
label={id}
checked={visibleColumnIds.has(id)}
onChange={({
currentTarget: { checked },
}: React.FormEvent<HTMLInputElement>) => {
const nextVisibleColumns = sortedColumns.filter(
({ id: columnId }) =>
checked
? visibleColumnIds.has(columnId) ||
id === columnId
: visibleColumnIds.has(columnId) &&
id !== columnId
);
setVisibleColumns(nextVisibleColumns);
}}
/>
</Fragment>
)}
</EuiDraggable>
))}
</div>
</EuiDroppable>
</EuiDragDropContext>
</EuiPopover>
);

return [ColumnSelector, visibleColumns];
};
Loading