From 1af95550fd655b1a4bcdf32947628f01049b5f37 Mon Sep 17 00:00:00 2001 From: Chandler Prall Date: Thu, 5 Mar 2020 15:23:48 -0700 Subject: [PATCH 1/5] Added customization functionality to EuiDataGrid's toolbarVisibility.showColumnSelector --- src-docs/src/views/datagrid/datagrid.js | 5 + .../__snapshots__/data_grid.test.tsx.snap | 8 +- src/components/datagrid/column_selector.tsx | 161 +++++++++++------- src/components/datagrid/data_grid.tsx | 53 +++--- src/components/datagrid/data_grid_types.ts | 9 +- 5 files changed, 141 insertions(+), 95 deletions(-) diff --git a/src-docs/src/views/datagrid/datagrid.js b/src-docs/src/views/datagrid/datagrid.js index 62864d99fce..85e9157ff45 100644 --- a/src-docs/src/views/datagrid/datagrid.js +++ b/src-docs/src/views/datagrid/datagrid.js @@ -202,6 +202,11 @@ export default () => { return ( - Hide fields + Columns @@ -718,7 +718,7 @@ Array [ - Hide fields + Columns @@ -1435,7 +1435,7 @@ Array [ - Hide fields + Columns @@ -1956,7 +1956,7 @@ Array [ - Hide fields + Columns diff --git a/src/components/datagrid/column_selector.tsx b/src/components/datagrid/column_selector.tsx index fc776538b1f..3de233c27f2 100644 --- a/src/components/datagrid/column_selector.tsx +++ b/src/components/datagrid/column_selector.tsx @@ -10,6 +10,8 @@ import classNames from 'classnames'; import { EuiDataGridColumn, EuiDataGridColumnVisibility, + EuiDataGridToolBarVisibilityColumnSelectorOptions, + EuiDataGridToolBarVisibilityOptions, } from './data_grid_types'; import { EuiPopover, EuiPopoverFooter, EuiPopoverTitle } from '../popover'; import { EuiI18n } from '../i18n'; @@ -26,10 +28,30 @@ import { DropResult } from 'react-beautiful-dnd'; import { EuiIcon } from '../icon'; import { useDependentState } from '../../services'; +const getShowColumnSelectorValue = ( + showColumnSelector: EuiDataGridToolBarVisibilityOptions['showColumnSelector'], + valueName: keyof EuiDataGridToolBarVisibilityColumnSelectorOptions +) => { + if (showColumnSelector === false) return false; + if (showColumnSelector == null) return true; + if (showColumnSelector === true) return true; + return showColumnSelector[valueName] !== false; +}; + export const useColumnSelector = ( availableColumns: EuiDataGridColumn[], - columnVisibility: EuiDataGridColumnVisibility + columnVisibility: EuiDataGridColumnVisibility, + showColumnSelector: EuiDataGridToolBarVisibilityOptions['showColumnSelector'] ): [ReactElement, EuiDataGridColumn[]] => { + const allowColumnHiding = getShowColumnSelectorValue( + showColumnSelector, + 'allowHide' + ); + const allowColumnReorder = getShowColumnSelectorValue( + showColumnSelector, + 'allowReorder' + ); + const [sortedColumns, setSortedColumns] = useDependentState( () => availableColumns.map(({ id }) => id), [availableColumns] @@ -69,7 +91,7 @@ export const useColumnSelector = ( const filteredColumns = sortedColumns.filter( id => id.toLowerCase().indexOf(columnSearchText.toLowerCase()) !== -1 ); - const isDragEnabled = columnSearchText.length === 0; // only allow drag-and-drop when not filtering columns + const isDragEnabled = allowColumnReorder && columnSearchText.length === 0; // only allow drag-and-drop when not filtering columns const columnSelector = ( + defaults={['Columns', 'columns hidden']}> {([button, buttonActive]: ReactChild[]) => ( }>
- - - {([search, searchcolumns]: string[]) => ( - ) => - setColumnSearchText(e.currentTarget.value) - } - /> - )} - - + {allowColumnHiding && ( + + + {([search, searchcolumns]: string[]) => ( + ) => + setColumnSearchText(e.currentTarget.value) + } + /> + )} + + + )} - { - const { - target: { checked }, - } = event; - const nextVisibleColumns = sortedColumns.filter( - columnId => - checked - ? visibleColumnIds.has(columnId) || - id === columnId - : visibleColumnIds.has(columnId) && - id !== columnId - ); - setVisibleColumns(nextVisibleColumns); - }} - /> + {allowColumnHiding ? ( + { + const { + target: { checked }, + } = event; + const nextVisibleColumns = sortedColumns.filter( + columnId => + checked + ? visibleColumnIds.has(columnId) || + id === columnId + : visibleColumnIds.has(columnId) && + id !== columnId + ); + setVisibleColumns(nextVisibleColumns); + }} + /> + ) : ( + id + )} {isDragEnabled && ( @@ -177,26 +205,31 @@ export const useColumnSelector = (
- - - - setVisibleColumns(sortedColumns)}> - - - - - setVisibleColumns([])}> - - - - - + {allowColumnHiding && ( + + + + setVisibleColumns(sortedColumns)}> + + + + + setVisibleColumns([])}> + + + + + + )}
); diff --git a/src/components/datagrid/data_grid.tsx b/src/components/datagrid/data_grid.tsx index 598ff5786fc..b0e2ba941c7 100644 --- a/src/components/datagrid/data_grid.tsx +++ b/src/components/datagrid/data_grid.tsx @@ -485,6 +485,31 @@ const useFocus = ( return [focusProps, focusedCell, setFocusedCell]; }; +// Typeguards to see if toolbarVisibility has a certain boolean property assigned +// If not, just set it to true and assume it's OK to show +function objectHasKey, ObjectKey extends keyof O>( + object: O, + key: ObjectKey +): object is Required { + return object.hasOwnProperty(key); +} +function checkOrDefaultToolBarDiplayOptions< + OptionKey extends keyof EuiDataGridToolBarVisibilityOptions +>( + arg: EuiDataGridProps['toolbarVisibility'], + option: OptionKey +): Required[OptionKey] { + if (arg === undefined) { + return true; + } else if (typeof arg === 'boolean') { + return arg as boolean; + } else if (objectHasKey(arg, option)) { + return arg[option]; + } else { + return true; + } +} + export const EuiDataGrid: FunctionComponent = props => { const [isFullScreen, setIsFullScreen] = useState(false); const [hasRoomForGridControls, setHasRoomForGridControls] = useState(true); @@ -605,7 +630,8 @@ export const EuiDataGrid: FunctionComponent = props => { const [columnSelector, orderedVisibleColumns] = useColumnSelector( columns, - columnVisibility + columnVisibility, + checkOrDefaultToolBarDiplayOptions(toolbarVisibility, 'showColumnSelector') ); const columnSorting = useColumnSorting( orderedVisibleColumns, @@ -653,31 +679,6 @@ export const EuiDataGrid: FunctionComponent = props => { // By default the toolbar appears const showToolbar = !!toolbarVisibility; - // Typeguards to see if toolbarVisibility has a certain boolean property assigned - // If not, just set it to true and assume it's OK to show - function objectHasKey< - O extends Record, - ObjectKey extends keyof O - >(object: O, key: ObjectKey): object is Required { - return object.hasOwnProperty(key); - } - function checkOrDefaultToolBarDiplayOptions< - OptionKey extends keyof EuiDataGridToolBarVisibilityOptions - >( - arg: EuiDataGridProps['toolbarVisibility'], - option: OptionKey - ): Required[OptionKey] { - if (arg === undefined) { - return true; - } else if (typeof arg === 'boolean') { - return arg as boolean; - } else if (objectHasKey(arg, option)) { - return arg[option]; - } else { - return true; - } - } - // These grid controls will only show when there is room. Check the resize observer callback // They can also be optionally turned off individually by using toolbarVisibility const gridControls = ( diff --git a/src/components/datagrid/data_grid_types.ts b/src/components/datagrid/data_grid_types.ts index 58b5165095e..26e7f0b4928 100644 --- a/src/components/datagrid/data_grid_types.ts +++ b/src/components/datagrid/data_grid_types.ts @@ -99,11 +99,18 @@ export interface EuiDataGridStyle { cellPadding?: EuiDataGridStyleCellPaddings; } +export interface EuiDataGridToolBarVisibilityColumnSelectorOptions { + allowHide?: boolean; + allowReorder?: boolean; +} + export interface EuiDataGridToolBarVisibilityOptions { /** * Allows the ability for the user to hide fields and sort columns */ - showColumnSelector?: boolean; + showColumnSelector?: + | boolean + | EuiDataGridToolBarVisibilityColumnSelectorOptions; /** * Allows the ability for the user to set the grid density. If on, this merges against what is provided in #EuiDataGridStyle */ From 67e542de90851934e013496840a4376c115601dc Mon Sep 17 00:00:00 2001 From: Dave Snider Date: Tue, 10 Mar 2020 13:11:34 -0700 Subject: [PATCH 2/5] design updates --- .../__snapshots__/data_grid.test.tsx.snap | 16 +++--- .../datagrid/_data_grid_column_selector.scss | 4 ++ src/components/datagrid/column_selector.tsx | 54 +++++++++++-------- 3 files changed, 44 insertions(+), 30 deletions(-) diff --git a/src/components/datagrid/__snapshots__/data_grid.test.tsx.snap b/src/components/datagrid/__snapshots__/data_grid.test.tsx.snap index 08c55097015..6dd0ae95c2a 100644 --- a/src/components/datagrid/__snapshots__/data_grid.test.tsx.snap +++ b/src/components/datagrid/__snapshots__/data_grid.test.tsx.snap @@ -191,12 +191,12 @@ Array [