forked from opensearch-project/OpenSearch-Dashboards
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Table Visualization] add a plain datagrid component
implement a plain OuiDataGrid component use the basic pagenation, sort and format. Partially resolve: opensearch-project#2305 Signed-off-by: Anan Zhuang <ananzh@amazon.com>
- Loading branch information
Showing
7 changed files
with
145 additions
and
17 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
45 changes: 45 additions & 0 deletions
45
src/plugins/vis_type_table/public/components/table_vis_app.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,45 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import React from 'react'; | ||
import { CoreStart } from 'opensearch-dashboards/public'; | ||
import { I18nProvider } from '@osd/i18n/react'; | ||
import { IInterpreterRenderHandlers } from 'src/plugins/expressions'; | ||
import { OpenSearchDashboardsContextProvider } from '../../../opensearch_dashboards_react/public'; | ||
|
||
import { TableContext } from '../table_vis_response_handler'; | ||
import { TableVisConfig } from '../types'; | ||
import { TableVisComponent } from './table_vis_component'; | ||
|
||
interface TableVisAppProps { | ||
visData: TableContext; | ||
visConfig: TableVisConfig; | ||
handlers: IInterpreterRenderHandlers; | ||
} | ||
|
||
const TableVisApp = ({ | ||
services, | ||
visData: { table, tableGroups, direction }, | ||
visConfig, | ||
handlers, | ||
}: TableVisAppProps & { services: CoreStart }) => { | ||
return ( | ||
<I18nProvider> | ||
<OpenSearchDashboardsContextProvider services={services}> | ||
<div className="tableVis" data-test-subj="tableVisEditor"> | ||
{tableGroups?.length !== 0 ? ( | ||
<></> | ||
) : ( | ||
<TableVisComponent table={table} visConfig={visConfig} handlers={handlers} /> | ||
)} | ||
</div> | ||
</OpenSearchDashboardsContextProvider> | ||
</I18nProvider> | ||
); | ||
}; | ||
|
||
// default export required for React.Lazy | ||
// eslint-disable-next-line import/no-default-export | ||
export { TableVisApp as default }; |
64 changes: 64 additions & 0 deletions
64
src/plugins/vis_type_table/public/components/table_vis_component.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,64 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import React, { useState, useCallback, useMemo } from 'react'; | ||
import { EuiDataGridProps, EuiDataGrid } from '@elastic/eui'; | ||
|
||
import { IInterpreterRenderHandlers } from 'src/plugins/expressions'; | ||
import { Table } from '../table_vis_response_handler'; | ||
import { TableVisConfig } from '../types'; | ||
|
||
interface TableVisComponentProps { | ||
table: Table; | ||
visConfig: TableVisConfig; | ||
handlers: IInterpreterRenderHandlers; | ||
} | ||
|
||
export const TableVisComponent = ({ table, visConfig, handlers }: TableVisComponentProps) => { | ||
const { rows, columns } = table; | ||
const [pagination, setPagination] = useState({ pageIndex: 0, pageSize: 5 }); | ||
const onChangeItemsPerPage = useCallback( | ||
(pageSize) => setPagination((p) => ({ ...p, pageSize, pageIndex: 0 })), | ||
[setPagination] | ||
); | ||
|
||
const onChangePage = useCallback((pageIndex) => setPagination((p) => ({ ...p, pageIndex })), [ | ||
setPagination, | ||
]); | ||
|
||
// toDo: it is a sample renderCellValue to render a data grid component | ||
// will check on it and it might be replaced | ||
const renderCellValue = useMemo(() => { | ||
return (({ rowIndex, columnId }) => { | ||
let adjustedRowIndex = rowIndex; | ||
|
||
// If we are doing the pagination (instead of leaving that to the grid) | ||
// then the row index must be adjusted as `data` has already been pruned to the page size | ||
adjustedRowIndex = rowIndex - pagination.pageIndex * pagination.pageSize; | ||
|
||
return rows.hasOwnProperty(adjustedRowIndex) | ||
? rows[adjustedRowIndex][columnId] || null | ||
: null; | ||
}) as EuiDataGridProps['renderCellValue']; | ||
}, [rows, pagination.pageIndex, pagination.pageSize]); | ||
|
||
return ( | ||
<EuiDataGrid | ||
aria-label="tableVis" | ||
columns={columns} | ||
columnVisibility={{ | ||
visibleColumns: columns.map(({ id }) => id), | ||
setVisibleColumns: () => {}, | ||
}} | ||
rowCount={rows.length} | ||
renderCellValue={renderCellValue} | ||
pagination={{ | ||
...pagination, | ||
onChangeItemsPerPage, | ||
onChangePage, | ||
}} | ||
/> | ||
); | ||
}; |
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
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