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

[UnifiedDataTable] Add density configuration #188495

Merged
merged 42 commits into from
Aug 19, 2024
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
8e7d5ed
[UnifiedDataTable] Add density configuration
lukasolson Jul 17, 2024
32dc686
Merge branch 'main' into unified_data_table_density
lukasolson Jul 17, 2024
61e9849
Fill out unit tests
lukasolson Jul 17, 2024
6457b74
Update comment text
lukasolson Jul 17, 2024
8e7e1c0
Merge branch 'main' into unified_data_table_density
lukasolson Jul 17, 2024
68d0408
Fix source document view
lukasolson Jul 19, 2024
96c4d65
Use dataGridStyle instead of density
lukasolson Jul 19, 2024
6c10fcd
Merge branch 'main' into unified_data_table_density
lukasolson Jul 22, 2024
9868caf
Fix tests & types
lukasolson Jul 22, 2024
1dc7401
Update snapshots & fix tests
lukasolson Jul 23, 2024
1822c7b
Merge branch 'main' into unified_data_table_density
lukasolson Jul 23, 2024
526781c
Merge branch 'main' into unified_data_table_density
lukasolson Jul 26, 2024
a1ba359
Use density key instead of data grid style
lukasolson Jul 26, 2024
185f791
Review feedback
lukasolson Jul 26, 2024
39d0734
Merge branch 'main' into unified_data_table_density
lukasolson Jul 26, 2024
b1e456b
Fix references to grid style
lukasolson Jul 26, 2024
ae547da
Merge branch 'unified_data_table_density' of github.com:lukasolson/ki…
lukasolson Jul 26, 2024
9158a2e
Fix tests
lukasolson Jul 26, 2024
101d311
Remove .only
lukasolson Jul 29, 2024
f249a19
Merge branch 'main' into unified_data_table_density
lukasolson Jul 29, 2024
0ae4b4b
Update test
lukasolson Jul 29, 2024
c3af7e2
Merge branch 'unified_data_table_density' of github.com:lukasolson/ki…
lukasolson Jul 29, 2024
3cf607a
Fix test
lukasolson Jul 30, 2024
55ad28f
Add density to saved search config
lukasolson Jul 31, 2024
ed0866d
[CI] Auto-commit changed files from 'node scripts/lint_ts_projects --…
kibanamachine Jul 31, 2024
c899fe4
[CI] Auto-commit changed files from 'node scripts/jest_integration -u…
kibanamachine Jul 31, 2024
bbbbbc3
Merge branch 'main' into unified_data_table_density
lukasolson Jul 31, 2024
b54267a
Merge branch 'unified_data_table_density' github.com:lukasolson/kiba…
lukasolson Aug 1, 2024
b9e083c
Merge branch 'main' into unified_data_table_density
lukasolson Aug 1, 2024
05e8f68
Fix additional row settings
lukasolson Aug 1, 2024
69a580a
Merge branch 'main' of github.com:elastic/kibana into unified_data_ta…
lukasolson Aug 6, 2024
73f460f
Update jest snapshots
lukasolson Aug 6, 2024
c6be703
Merge branch 'main' into unified_data_table_density
lukasolson Aug 9, 2024
a28d99a
Merge branch 'main' into unified_data_table_density
bhavyarm Aug 9, 2024
0fd4430
Merge branch 'main' into unified_data_table_density
lukasolson Aug 12, 2024
1c215ef
Review feedback & remove unnecessary state
lukasolson Aug 13, 2024
54f9c02
Merge branch 'unified_data_table_density' of github.com:lukasolson/ki…
lukasolson Aug 13, 2024
371ee20
Remove showDensitySelector prop
lukasolson Aug 13, 2024
b6b708a
Workaround for EUI bug
lukasolson Aug 13, 2024
c37f61e
Add density functional tests
lukasolson Aug 14, 2024
cdb6099
Merge branch 'main' into unified_data_table_density
lukasolson Aug 15, 2024
f6baa05
Merge branch 'main' into unified_data_table_density
lukasolson Aug 19, 2024
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
2 changes: 2 additions & 0 deletions packages/kbn-unified-data-table/README.md
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This makes me realize we have not been keeping the Unified Data Table docs updated..

Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ Props description:
| **externalCustomRenderers** | (optional)Record<string,(props: EuiDataGridCellValueElementProps) => React.ReactNode>; | An optional settings for a specified fields rendering like links. Applied only for the listed fields rendering. |
| **consumer** | (optional)string | Name of the UnifiedDataTable consumer component or application. |
| **componentsTourSteps** | (optional)Record<string,string> | Optional key/value pairs to set guided onboarding steps ids for a data table components included to guided tour. |
| **showDensitySelector** | (optional)boolean | Optional boolean to toggle whether or not to show the density selector. |
| **onUpdateDensity** | (optional)(EuiDataGridStyle) => void; | Optional callback when the density configuration is modified. |

*Required **services** list:
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ describe('CompareDocuments', () => {
"data-test-subj": "unifiedDataTableCompareDocuments",
"gridStyle": Object {
"border": "horizontal",
"cellPadding": "l",
"cellPadding": "s",
"fontSize": "s",
"header": "underline",
"rowHover": "highlight",
Expand Down
48 changes: 26 additions & 22 deletions packages/kbn-unified-data-table/src/components/data_table.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -381,17 +381,19 @@ describe('UnifiedDataTable', () => {
"additionalControls": null,
"showColumnSelector": false,
"showDisplaySelector": Object {
"additionalDisplaySettings": <UnifiedDataTableAdditionalDisplaySettings
headerRowHeight="custom"
headerRowHeightLines={1}
onChangeRowHeight={[Function]}
onChangeRowHeightLines={[Function]}
onChangeSampleSize={[MockFunction]}
rowHeight="custom"
rowHeightLines={3}
sampleSize={150}
/>,
"allowDensity": false,
"additionalDisplaySettings": <React.Fragment>

<UnifiedDataTableAdditionalDisplaySettings
headerRowHeight="custom"
headerRowHeightLines={1}
onChangeRowHeight={[Function]}
onChangeRowHeightLines={[Function]}
onChangeSampleSize={[MockFunction]}
rowHeight="custom"
rowHeightLines={3}
sampleSize={150}
/>
</React.Fragment>,
"allowResetButton": false,
"allowRowHeight": false,
},
Expand All @@ -413,16 +415,18 @@ describe('UnifiedDataTable', () => {
"additionalControls": null,
"showColumnSelector": false,
"showDisplaySelector": Object {
"additionalDisplaySettings": <UnifiedDataTableAdditionalDisplaySettings
headerRowHeight="custom"
headerRowHeightLines={1}
onChangeRowHeight={[Function]}
onChangeRowHeightLines={[Function]}
rowHeight="custom"
rowHeightLines={3}
sampleSize={200}
/>,
"allowDensity": false,
"additionalDisplaySettings": <React.Fragment>

<UnifiedDataTableAdditionalDisplaySettings
headerRowHeight="custom"
headerRowHeightLines={1}
onChangeRowHeight={[Function]}
onChangeRowHeightLines={[Function]}
rowHeight="custom"
rowHeightLines={3}
sampleSize={200}
/>
</React.Fragment>,
"allowResetButton": false,
"allowRowHeight": false,
},
Expand Down Expand Up @@ -711,7 +715,7 @@ describe('UnifiedDataTable', () => {

expect(grid.hasClass('euiDataGrid--bordersHorizontal')).toBeTruthy();
expect(grid.hasClass('euiDataGrid--fontSizeSmall')).toBeTruthy();
expect(grid.hasClass('euiDataGrid--paddingLarge')).toBeTruthy();
expect(grid.hasClass('euiDataGrid--paddingSmall')).toBeTruthy();
expect(grid.hasClass('euiDataGrid--rowHoverHighlight')).toBeTruthy();
expect(grid.hasClass('euiDataGrid--headerUnderline')).toBeTruthy();
expect(grid.hasClass('euiDataGrid--stripes')).toBeTruthy();
Expand Down
67 changes: 47 additions & 20 deletions packages/kbn-unified-data-table/src/components/data_table.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import {
EuiDataGridProps,
EuiFlexGroup,
EuiFlexItem,
EuiHorizontalRule,
} from '@elastic/eui';
import type { DataView } from '@kbn/data-views-plugin/public';
import {
Expand Down Expand Up @@ -75,7 +76,6 @@ import {
import { useRowHeightsOptions } from '../hooks/use_row_heights_options';
import {
DEFAULT_ROWS_PER_PAGE,
GRID_STYLE,
ROWS_HEIGHT_OPTIONS,
toolbarVisibility as toolbarVisibilityDefaults,
} from '../constants';
Expand All @@ -90,6 +90,7 @@ import {
getColorIndicatorControlColumn,
type ColorIndicatorControlColumnParams,
} from './custom_control_columns';
import { useDataGridStyle } from '../hooks/use_data_grid_style';

export type SortOrder = [string, string];

Expand Down Expand Up @@ -232,6 +233,14 @@ export interface UnifiedDataTableProps {
* Update row height state
*/
onUpdateRowHeight?: (rowHeight: number) => void;
/**
* Whether or not to show the density selector
*/
showDensitySelector?: boolean;
/**
* Callback when the data grid style configuration is modified
*/
onUpdateDataGridStyle?: (dataGridStyle: EuiDataGridStyle) => void;
/**
* Is text base lang mode enabled
*/
Expand Down Expand Up @@ -467,6 +476,8 @@ export const UnifiedDataTable = ({
cellContext,
renderCellPopover,
getRowIndicator,
showDensitySelector = false,
onUpdateDataGridStyle,
}: UnifiedDataTableProps) => {
const { fieldFormats, toastNotifications, dataViewFieldEditor, uiSettings, storage, data } =
services;
Expand Down Expand Up @@ -610,6 +621,12 @@ export const UnifiedDataTable = ({
return getShouldShowFieldHandler(dataViewFields, dataView, showMultiFields);
}, [dataView, showMultiFields]);

const { dataGridStyle, onChangeDataGridStyle } = useDataGridStyle({
storage,
consumer,
onUpdateDataGridStyle,
});

/**
* Cell rendering
*/
Expand All @@ -625,6 +642,7 @@ export const UnifiedDataTable = ({
maxEntries: maxDocFieldsDisplayed,
externalCustomRenderers,
isPlainRecord,
isCompressed: dataGridStyle.fontSize === 's',
}),
[
dataView,
Expand All @@ -635,6 +653,7 @@ export const UnifiedDataTable = ({
fieldFormats,
externalCustomRenderers,
isPlainRecord,
dataGridStyle,
]
);

Expand Down Expand Up @@ -940,29 +959,30 @@ export const UnifiedDataTable = ({
);

const showDisplaySelector = useMemo(() => {
const options: EuiDataGridToolBarVisibilityDisplaySelectorOptions = {};

if (onUpdateRowHeight) {
options.allowDensity = false;
}
const options: EuiDataGridToolBarVisibilityDisplaySelectorOptions = {
...(showDensitySelector && { allowDensity: showDensitySelector }),
lukasolson marked this conversation as resolved.
Show resolved Hide resolved
};

if (onUpdateRowHeight || onUpdateHeaderRowHeight || onUpdateSampleSize) {
options.allowRowHeight = false;
options.allowResetButton = false;
options.additionalDisplaySettings = (
<UnifiedDataTableAdditionalDisplaySettings
rowHeight={rowHeight}
rowHeightLines={rowHeightLines}
onChangeRowHeight={onChangeRowHeight}
onChangeRowHeightLines={onChangeRowHeightLines}
headerRowHeight={headerRowHeight}
headerRowHeightLines={headerRowHeightLines}
onChangeHeaderRowHeight={onChangeHeaderRowHeight}
onChangeHeaderRowHeightLines={onChangeHeaderRowHeightLines}
maxAllowedSampleSize={maxAllowedSampleSize}
sampleSize={sampleSizeState}
onChangeSampleSize={onUpdateSampleSize}
/>
<>
{showDensitySelector ? <EuiHorizontalRule margin="s" /> : ''}
lukasolson marked this conversation as resolved.
Show resolved Hide resolved
<UnifiedDataTableAdditionalDisplaySettings
rowHeight={rowHeight}
rowHeightLines={rowHeightLines}
onChangeRowHeight={onChangeRowHeight}
onChangeRowHeightLines={onChangeRowHeightLines}
headerRowHeight={headerRowHeight}
headerRowHeightLines={headerRowHeightLines}
onChangeHeaderRowHeight={onChangeHeaderRowHeight}
onChangeHeaderRowHeightLines={onChangeHeaderRowHeightLines}
maxAllowedSampleSize={maxAllowedSampleSize}
sampleSize={sampleSizeState}
onChangeSampleSize={onUpdateSampleSize}
/>
</>
);
}

Expand All @@ -981,6 +1001,7 @@ export const UnifiedDataTable = ({
rowHeight,
rowHeightLines,
sampleSizeState,
showDensitySelector,
]);

const inMemory = useMemo(() => {
Expand Down Expand Up @@ -1053,6 +1074,12 @@ export const UnifiedDataTable = ({
);
}

const gridStyle: EuiDataGridStyle = {
...dataGridStyle,
onChange: onChangeDataGridStyle,
...gridStyleOverride,
};
lukasolson marked this conversation as resolved.
Show resolved Hide resolved

return (
<UnifiedDataTableContext.Provider value={unifiedDataTableContextValue}>
<span className="unifiedDataTable__inner">
Expand Down Expand Up @@ -1107,7 +1134,7 @@ export const UnifiedDataTable = ({
toolbarVisibility={toolbarVisibility}
rowHeightsOptions={rowHeightsOptions}
inMemory={inMemory}
gridStyle={gridStyleOverride ?? GRID_STYLE}
gridStyle={gridStyle}
renderCustomGridBody={renderCustomGridBody}
renderCustomToolbar={renderCustomToolbarFn}
trailingControlColumns={customTrailingControlColumn}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export function SourceDocument({
fieldFormats,
dataTestSubj = 'discoverCellDescriptionList',
className,
isCompressed = true,
}: {
useTopLevelObjectColumns: boolean;
row: DataTableRecord;
Expand All @@ -48,6 +49,7 @@ export function SourceDocument({
fieldFormats: FieldFormatsStart;
dataTestSubj?: string;
className?: string;
isCompressed?: boolean;
}) {
const pairs: FormattedHit = useTopLevelObjectColumns
? getTopLevelObjectPairs(row.raw, columnId, dataView, shouldShowFieldHandler).slice(
Expand All @@ -59,7 +61,7 @@ export function SourceDocument({
return (
<EuiDescriptionList
type="inline"
compressed
compressed={isCompressed}
className={classnames('unifiedDataTable__descriptionList', CELL_CLASS, className)}
data-test-subj={dataTestSubj}
>
Expand Down
3 changes: 2 additions & 1 deletion packages/kbn-unified-data-table/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,11 @@ export const defaultMonacoEditorWidth = 370;
export const defaultTimeColumnWidth = 212;
export const kibanaJSON = 'kibana-json';

// Matches the EuiDataGrid "compact" density options
export const GRID_STYLE: EuiDataGridStyle = {
border: 'horizontal',
fontSize: 's',
cellPadding: 'l',
cellPadding: 's',
rowHover: 'highlight',
header: 'underline',
stripes: true,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

import type { EuiDataGridStyle } from '@elastic/eui';
import type { Storage } from '@kbn/kibana-utils-plugin/public';
import { renderHook } from '@testing-library/react-hooks';
import { useDataGridStyle } from './use_data_grid_style';

const localStorageMock = {
get: jest.fn(),
set: jest.fn(),
};

describe('useDataGridStyle', () => {
beforeEach(() => {
localStorageMock.get.mockClear();
localStorageMock.set.mockClear();
});

it('should read from local storage', () => {
localStorageMock.get.mockReturnValue({ foo: 'bar' });
const { result } = renderHook(() =>
useDataGridStyle({
storage: localStorageMock as unknown as Storage,
consumer: 'discover',
})
);
const {
current: { dataGridStyle },
} = result;
expect(dataGridStyle).toMatchInlineSnapshot(`
Object {
"foo": "bar",
}
`);
});

it('should update local storage when onChangeDataGridStyle is called', () => {
const { result } = renderHook(() =>
useDataGridStyle({
storage: localStorageMock as unknown as Storage,
consumer: 'discover',
})
);
const {
current: { onChangeDataGridStyle },
} = result;

const newValue: EuiDataGridStyle = { border: 'all', footer: 'shade' };
onChangeDataGridStyle(newValue);

expect(localStorageMock.set).toBeCalledWith('discover:dataGridStyle', newValue);
});

it('should call provided onUpdateDataGridStyle with the updated value', () => {
const onUpdateDataGridStyle = jest.fn();
const { result } = renderHook(() =>
useDataGridStyle({
storage: localStorageMock as unknown as Storage,
consumer: 'discover',
onUpdateDataGridStyle,
})
);
const {
current: { onChangeDataGridStyle },
} = result;

const newValue: EuiDataGridStyle = { border: 'all', footer: 'shade' };
onChangeDataGridStyle(newValue);

expect(onUpdateDataGridStyle).toBeCalledWith(newValue);
});
});
Loading