-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Discover] In-table search (#206454)
- Closes #192360 ## Summary The default browser Find-in-page does not work great with the grid virtualization and our pagination and it can only find matches in rows which are currently displayed. This PR adds in-table search support to the grid so users can find matches in all grid rows (up to `500` sample docs/rows by default) and jump between them with "Previous"/"Next" buttons. ![Jan-24-2025 22-03-54](https://github.com/user-attachments/assets/95b31fb8-4740-4c5f-ba91-8e1c19066e02) The implementation is extracted in a new package `@kbn/data-grid-in-table-search`. This would allow to use in-table search with `EuiDataGrid` on other pages of Kibana too. `Cmd+F` shortcut is overridden when one of grid elements is in focus otherwise we keep the browser default behaviour. ### Checklist - [x] Any text added follows [EUI's writing guidelines](https://elastic.github.io/eui/#/guidelines/writing), uses sentence case text and includes [i18n support](https://github.com/elastic/kibana/blob/main/src/platform/packages/shared/kbn-i18n/README.md) - [ ] [Documentation](https://www.elastic.co/guide/en/kibana/master/development-documentation.html) was added for features that require explanation or tutorials - [x] [Unit or functional tests](https://www.elastic.co/guide/en/kibana/master/development-tests.html) were updated or added to match the most common scenarios - [ ] If a plugin configuration key changed, check if it needs to be allowlisted in the cloud and added to the [docker list](https://github.com/elastic/kibana/blob/main/src/dev/build/tasks/os_packages/docker_generator/resources/base/bin/kibana-docker) - [ ] This was checked for breaking HTTP API changes, and any breaking changes have been approved by the breaking-change committee. The `release_note:breaking` label should be applied in these situations. - [ ] [Flaky Test Runner](https://ci-stats.kibana.dev/trigger_flaky_test_runner/1) was used on any tests changed - [x] The PR description includes the appropriate Release Notes section, and the correct `release_note:*` label is applied per the [guidelines](https://www.elastic.co/guide/en/kibana/master/contributing.html#kibana-release-notes-process) --------- Co-authored-by: kibanamachine <[email protected]> Co-authored-by: florent-leborgne <[email protected]>
- Loading branch information
1 parent
a060bae
commit 8ffb2ff
Showing
48 changed files
with
2,934 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
src/platform/packages/shared/kbn-data-grid-in-table-search/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
# @kbn/data-grid-in-table-search | ||
|
||
This package allows to extend `EuiDataGrid` with in-table search. | ||
|
||
If you are already using `UnifiedDataTable` component, you can enable in-table search simply by passing `enableInTableSearch` prop to it. | ||
|
||
```tsx | ||
<UnifiedDataTable | ||
enableInTableSearch={true} | ||
// ... other props | ||
/> | ||
``` | ||
|
||
If you are using `EuiDataGrid` directly, you can enable in-table search by importing this package and following these steps: | ||
|
||
1. include `useDataGridInTableSearch` hook in your component | ||
2. pass `inTableSearchControl` to `EuiDataGrid` inside `additionalControls.right` prop or `renderCustomToolbar` | ||
3. pass `inTableSearchCss` to the grid container element as `css` prop | ||
4. update your `cellContext` prop with `cellContextWithInTableSearchSupport` | ||
5. update your `renderCellValue` prop with `renderCellValueWithInTableSearchSupport`. | ||
|
||
```tsx | ||
import { useDataGridInTableSearch } from '@kbn/data-grid-in-table-search'; | ||
|
||
// ... | ||
|
||
const dataGridRef = useRef<EuiDataGridRefProps>(null); | ||
const [dataGridWrapper, setDataGridWrapper] = useState<HTMLElement | null>(null); | ||
|
||
// ... | ||
|
||
const { inTableSearchTermCss, inTableSearchControl, cellContextWithInTableSearchSupport, renderCellValueWithInTableSearchSupport } = | ||
useDataGridInTableSearch({ | ||
dataGridWrapper, | ||
dataGridRef, | ||
visibleColumns, | ||
rows, | ||
cellContext, | ||
renderCellValue, | ||
pagination, | ||
}); | ||
|
||
const toolbarVisibility: EuiDataGridProps['toolbarVisibility'] = useMemo( | ||
() => ({ | ||
additionalControls: inTableSearchControl ? { right: inTableSearchControl } : false, | ||
// ... | ||
}), | ||
[inTableSearchControl] | ||
); | ||
|
||
// ... | ||
<div ref={(node) => setDataGridWrapper(node)} css={inTableSearchCss}> | ||
<EuiDataGrid | ||
ref={dataGridRef} | ||
toolbarVisibility={toolbarVisibility} | ||
renderCellValue={renderCellValueWithInTableSearchSupport} | ||
cellContext={cellContextWithInTableSearchSupport} | ||
pagination={pagination} | ||
// ... other props | ||
/> | ||
</div> | ||
``` | ||
|
||
An example of how to use this package can be found in `kbn-data-grid-in-table-search/__mocks__/data_grid_example.tsx` | ||
or in `kbn-unified-data-table` package. | ||
|
||
|
||
|
||
|
||
|
||
|
23 changes: 23 additions & 0 deletions
23
src/platform/packages/shared/kbn-data-grid-in-table-search/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
/* | ||
* 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", the "GNU Affero General Public License v3.0 only", 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", the "GNU Affero General Public | ||
* License v3.0 only", or the "Server Side Public License, v 1". | ||
*/ | ||
|
||
export { | ||
useDataGridInTableSearch, | ||
type UseDataGridInTableSearchProps, | ||
type UseDataGridInTableSearchReturn, | ||
} from './src'; | ||
|
||
export { | ||
BUTTON_TEST_SUBJ, | ||
COUNTER_TEST_SUBJ, | ||
BUTTON_PREV_TEST_SUBJ, | ||
BUTTON_NEXT_TEST_SUBJ, | ||
INPUT_TEST_SUBJ, | ||
HIGHLIGHT_CLASS_NAME, | ||
} from './src/constants'; |
14 changes: 14 additions & 0 deletions
14
src/platform/packages/shared/kbn-data-grid-in-table-search/jest.config.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
/* | ||
* 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", the "GNU Affero General Public License v3.0 only", 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", the "GNU Affero General Public | ||
* License v3.0 only", or the "Server Side Public License, v 1". | ||
*/ | ||
|
||
module.exports = { | ||
preset: '@kbn/test', | ||
rootDir: '../../../../..', | ||
roots: ['<rootDir>/src/platform/packages/shared/kbn-data-grid-in-table-search'], | ||
}; |
7 changes: 7 additions & 0 deletions
7
src/platform/packages/shared/kbn-data-grid-in-table-search/kibana.jsonc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"type": "shared-browser", | ||
"id": "@kbn/data-grid-in-table-search", | ||
"owner": "@elastic/kibana-data-discovery", | ||
"group": "platform", | ||
"visibility": "shared" | ||
} |
7 changes: 7 additions & 0 deletions
7
src/platform/packages/shared/kbn-data-grid-in-table-search/package.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"name": "@kbn/data-grid-in-table-search", | ||
"private": true, | ||
"version": "1.0.0", | ||
"license": "Elastic License 2.0 OR AGPL-3.0-only OR SSPL-1.0", | ||
"sideEffects": false | ||
} |
22 changes: 22 additions & 0 deletions
22
src/platform/packages/shared/kbn-data-grid-in-table-search/src/__mocks__/data.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
/* | ||
* 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", the "GNU Affero General Public License v3.0 only", 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", the "GNU Affero General Public | ||
* License v3.0 only", or the "Server Side Public License, v 1". | ||
*/ | ||
|
||
export const generateMockData = (rowsCount: number, columnsCount: number) => { | ||
const testData: string[][] = []; | ||
|
||
Array.from({ length: rowsCount }).forEach((_, i) => { | ||
const row: string[] = []; | ||
Array.from({ length: columnsCount }).forEach((__, j) => { | ||
row.push(`cell-in-row-${i}-col-${j}`); | ||
}); | ||
testData.push(row); | ||
}); | ||
|
||
return testData; | ||
}; |
95 changes: 95 additions & 0 deletions
95
...latform/packages/shared/kbn-data-grid-in-table-search/src/__mocks__/data_grid_example.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
/* | ||
* 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", the "GNU Affero General Public License v3.0 only", 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", the "GNU Affero General Public | ||
* License v3.0 only", or the "Server Side Public License, v 1". | ||
*/ | ||
|
||
import React, { useMemo, useRef, useState } from 'react'; | ||
import { EuiDataGrid, EuiDataGridProps, EuiDataGridRefProps } from '@elastic/eui'; | ||
import { generateMockData } from './data'; | ||
import { getRenderCellValueMock } from './render_cell_value_mock'; | ||
import { useDataGridInTableSearch } from '../use_data_grid_in_table_search'; | ||
|
||
export interface DataGridWithInTableSearchExampleProps { | ||
rowsCount: number; | ||
columnsCount: number; | ||
pageSize: number | null; | ||
} | ||
|
||
export const DataGridWithInTableSearchExample: React.FC<DataGridWithInTableSearchExampleProps> = ({ | ||
rowsCount, | ||
columnsCount, | ||
pageSize, | ||
}) => { | ||
const dataGridRef = useRef<EuiDataGridRefProps>(null); | ||
const [dataGridWrapper, setDataGridWrapper] = useState<HTMLElement | null>(null); | ||
|
||
const sampleData = useMemo( | ||
() => generateMockData(rowsCount, columnsCount), | ||
[rowsCount, columnsCount] | ||
); | ||
const columns = useMemo( | ||
() => Array.from({ length: columnsCount }, (_, i) => ({ id: `column-${i}` })), | ||
[columnsCount] | ||
); | ||
|
||
const [visibleColumns, setVisibleColumns] = useState(columns.map(({ id }) => id)); | ||
|
||
const renderCellValue = useMemo(() => getRenderCellValueMock(sampleData), [sampleData]); | ||
|
||
const isPaginationEnabled = typeof pageSize === 'number'; | ||
const [pageIndex, setPageIndex] = useState(0); | ||
const pagination = useMemo(() => { | ||
return isPaginationEnabled | ||
? { | ||
onChangePage: setPageIndex, | ||
onChangeItemsPerPage: () => {}, | ||
pageIndex, | ||
pageSize, | ||
} | ||
: undefined; | ||
}, [isPaginationEnabled, setPageIndex, pageSize, pageIndex]); | ||
|
||
const { | ||
inTableSearchTermCss, | ||
inTableSearchControl, | ||
cellContextWithInTableSearchSupport, | ||
renderCellValueWithInTableSearchSupport, | ||
} = useDataGridInTableSearch({ | ||
dataGridWrapper, | ||
dataGridRef, | ||
visibleColumns, | ||
rows: sampleData, | ||
cellContext: undefined, | ||
renderCellValue, | ||
pagination, | ||
}); | ||
|
||
const toolbarVisibility: EuiDataGridProps['toolbarVisibility'] = useMemo( | ||
() => ({ | ||
additionalControls: inTableSearchControl ? { right: inTableSearchControl } : false, | ||
}), | ||
[inTableSearchControl] | ||
); | ||
|
||
return ( | ||
<div ref={(node) => setDataGridWrapper(node)} css={inTableSearchTermCss}> | ||
<EuiDataGrid | ||
ref={dataGridRef} | ||
aria-label="Data grid with in-table search" | ||
columns={columns} | ||
columnVisibility={{ visibleColumns, setVisibleColumns }} | ||
toolbarVisibility={toolbarVisibility} | ||
rowCount={rowsCount} | ||
pagination={pagination} | ||
cellContext={cellContextWithInTableSearchSupport} | ||
renderCellValue={renderCellValueWithInTableSearchSupport} | ||
width={800} | ||
height={200} | ||
/> | ||
</div> | ||
); | ||
}; |
12 changes: 12 additions & 0 deletions
12
src/platform/packages/shared/kbn-data-grid-in-table-search/src/__mocks__/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
/* | ||
* 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", the "GNU Affero General Public License v3.0 only", 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", the "GNU Affero General Public | ||
* License v3.0 only", or the "Server Side Public License, v 1". | ||
*/ | ||
|
||
export { generateMockData } from './data'; | ||
export { getRenderCellValueMock } from './render_cell_value_mock'; | ||
export { DataGridWithInTableSearchExample } from './data_grid_example'; |
26 changes: 26 additions & 0 deletions
26
...rm/packages/shared/kbn-data-grid-in-table-search/src/__mocks__/render_cell_value_mock.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/* | ||
* 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", the "GNU Affero General Public License v3.0 only", 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", the "GNU Affero General Public | ||
* License v3.0 only", or the "Server Side Public License, v 1". | ||
*/ | ||
|
||
import React from 'react'; | ||
import type { EuiDataGridCellValueElementProps } from '@elastic/eui'; | ||
|
||
export function getRenderCellValueMock(testData: string[][]) { | ||
return function OriginalRenderCellValue({ | ||
colIndex, | ||
rowIndex, | ||
}: EuiDataGridCellValueElementProps) { | ||
const cellValue = testData[rowIndex][colIndex]; | ||
|
||
if (!cellValue) { | ||
throw new Error('Testing unexpected errors'); | ||
} | ||
|
||
return <div>{cellValue}</div>; | ||
}; | ||
} |
Oops, something went wrong.