-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* fix: add material-react-table * fix: upgraded Prettier * feat: added DataGrid * fix: added tsbuildinfo to speed up TS compiles * fix: removed unused dependencies from Storybook * fix: updated DataGrid story for Storybook v7 * feat: updated DataGrid with more functionality * fix: added notes to DataGrid and commented out Reports * fix: removed unused imports from DataGrid * fix: upgraded material-react-table * fix: removed unsused comment in DataGrid * fix: split DataGrid into 2 components * fix: added translation interpolation and plurality * feat: added PaginatedDataGrid * fix: minor alphabetization in Select * fix: pagination works now in DataGrid * fix: removed InfinitelyScrolledDataGrid * fix: minor code positioning * fix: renamed DataGrid to DataTable * fix: renamed table and fixed checkboxes * fix: removed unused DataGrid component * fix: incorrectly named i18nKey values * fix: removed old Table stories * fix: stories using Table now use StaticTable * fix: incorrect children type in OdysseyTranslationProvider * fix: story imports use @storybook/blocks instead of @storybook/addon-docs/blocks * fix: missing Table components in RTL stories * fix: updated TokenTables to properly render new brand tokens * fix: removed unused import * fix: added console.error where we had console.log * fix: axe testing bug
- Loading branch information
1 parent
8e06f0d
commit e7ebc73
Showing
44 changed files
with
2,568 additions
and
1,286 deletions.
There are no files selected for viewing
Binary file added
BIN
+75 KB
.yarn/cache/@tanstack-match-sorter-utils-npm-8.8.4-488b98c113-d005f50075.zip
Binary file not shown.
Binary file not shown.
Binary file added
BIN
+48.5 KB
.yarn/cache/@tanstack-react-virtual-npm-3.0.0-beta.54-58c34420c1-ddeb3cb46d.zip
Binary file not shown.
Binary file not shown.
Binary file added
BIN
+110 KB
.yarn/cache/@tanstack-virtual-core-npm-3.0.0-beta.54-e3efac248b-a58cb30e1b.zip
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
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
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,271 @@ | ||
/*! | ||
* Copyright (c) 2022-present, Okta, Inc. and/or its affiliates. All rights reserved. | ||
* The Okta software accompanied by this notice is provided pursuant to the Apache License, Version 2.0 (the "License.") | ||
* | ||
* You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0. | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* | ||
* See the License for the specific language governing permissions and limitations under the License. | ||
*/ | ||
|
||
import { AlertProps, TablePaginationProps, Typography } from "@mui/material"; | ||
import MaterialReactTable, { | ||
MRT_PaginationState, | ||
type MRT_ColumnFiltersState, | ||
type MRT_RowSelectionState, | ||
type MRT_TableInstance, | ||
type MRT_Virtualizer, | ||
} from "material-react-table"; | ||
import { | ||
FunctionComponent, | ||
memo, | ||
useCallback, | ||
useEffect, | ||
useMemo, | ||
useRef, | ||
useState, | ||
} from "react"; | ||
import { Trans, useTranslation } from "react-i18next"; | ||
|
||
import type { | ||
DefaultMaterialReactTableData, | ||
MaterialReactTableProps, | ||
} from "./materialReactTableTypes"; | ||
|
||
export type PaginatedTableProps<TData extends DefaultMaterialReactTableData> = { | ||
columns: MaterialReactTableProps<TData>["columns"]; | ||
data: MaterialReactTableProps<TData>["data"]; | ||
fetchMoreData?: () => void; | ||
getRowId?: MaterialReactTableProps<TData>["getRowId"]; | ||
hasError?: boolean; | ||
hasRowSelection?: boolean; | ||
initialState?: MaterialReactTableProps<TData>["initialState"]; | ||
isFetching?: boolean; | ||
onGlobalFilterChange?: MaterialReactTableProps<TData>["onGlobalFilterChange"]; | ||
onPaginationChange?: MaterialReactTableProps<TData>["onPaginationChange"]; | ||
onRowSelectionChange?: MaterialReactTableProps<TData>["onRowSelectionChange"]; | ||
rowsPerPage?: number; | ||
state?: MaterialReactTableProps<TData>["state"]; | ||
ToolbarButtons?: FunctionComponent< | ||
{ table: MRT_TableInstance<TData> } & unknown | ||
>; | ||
}; | ||
|
||
const PaginatedTable = <TData extends DefaultMaterialReactTableData>({ | ||
columns, | ||
data, | ||
fetchMoreData, | ||
getRowId, | ||
hasError, | ||
hasRowSelection, | ||
initialState, | ||
isFetching, | ||
onGlobalFilterChange, | ||
onPaginationChange, | ||
onRowSelectionChange: onRowSelectionChangeProp, | ||
rowsPerPage = 10, | ||
state, | ||
ToolbarButtons, | ||
}: PaginatedTableProps<TData>) => { | ||
const { t } = useTranslation(); | ||
|
||
const rowVirtualizerInstanceRef = | ||
useRef<MRT_Virtualizer<HTMLDivElement, HTMLTableRowElement>>(null); | ||
|
||
const [columnFilters, setColumnFilters] = useState<MRT_ColumnFiltersState>( | ||
[] | ||
); | ||
|
||
const [globalFilter, setGlobalFilter] = useState<string>(); | ||
|
||
useEffect(() => { | ||
if (globalFilter) { | ||
onGlobalFilterChange?.(globalFilter); | ||
} | ||
}, [globalFilter, onGlobalFilterChange]); | ||
|
||
useEffect(() => { | ||
try { | ||
// Scroll to top of table when sorting or filters change. | ||
rowVirtualizerInstanceRef.current?.scrollToIndex?.(0); | ||
} catch (error) { | ||
console.error(error); | ||
} | ||
}, [columnFilters, globalFilter]); | ||
|
||
const totalFetchedRows = data.length ?? 0; | ||
|
||
const renderBottomToolbarCustomActions = useCallback( | ||
() => | ||
fetchMoreData ? ( | ||
<Typography> | ||
<Trans | ||
count={totalFetchedRows} | ||
i18nKey="table.fetchedrows.text" | ||
values={{ | ||
totalRows: totalFetchedRows, | ||
}} | ||
/> | ||
</Typography> | ||
) : ( | ||
<Typography> | ||
<Trans | ||
count={totalFetchedRows} | ||
i18nKey="table.rows.text" | ||
values={{ | ||
totalRows: totalFetchedRows, | ||
}} | ||
/> | ||
</Typography> | ||
), | ||
[fetchMoreData, totalFetchedRows] | ||
); | ||
|
||
const renderTopToolbarCustomActions = useCallback< | ||
Exclude< | ||
MaterialReactTableProps<TData>["renderTopToolbarCustomActions"], | ||
undefined | ||
> | ||
>( | ||
({ table }) => <>{ToolbarButtons && <ToolbarButtons table={table} />}</>, | ||
[ToolbarButtons] | ||
); | ||
|
||
const [rowSelection, setRowSelection] = useState<MRT_RowSelectionState>({}); | ||
|
||
useEffect(() => { | ||
onRowSelectionChangeProp?.(rowSelection); | ||
}, [onRowSelectionChangeProp, rowSelection]); | ||
|
||
const [pagination, setPagination] = useState<MRT_PaginationState>( | ||
initialState?.pagination || { | ||
pageIndex: 0, | ||
pageSize: rowsPerPage, | ||
} | ||
); | ||
|
||
const dataLengthRef = useRef(data.length); | ||
|
||
const updatePagination = useCallback( | ||
(paginationFunction) => { | ||
if (data.length === dataLengthRef.current) { | ||
setPagination((previousPagination) => { | ||
const nextPagination = paginationFunction(previousPagination); | ||
return nextPagination; | ||
}); | ||
} else { | ||
dataLengthRef.current = data.length; | ||
} | ||
}, | ||
[data.length] | ||
); | ||
|
||
useEffect(() => { | ||
const numberOfPages = Math.floor(data.length / pagination.pageSize); | ||
|
||
if (!isFetching && pagination.pageIndex > numberOfPages - 1) { | ||
fetchMoreData?.(); | ||
} | ||
}, [ | ||
data.length, | ||
fetchMoreData, | ||
isFetching, | ||
pagination.pageIndex, | ||
pagination.pageSize, | ||
]); | ||
|
||
useEffect(() => { | ||
onPaginationChange?.({ | ||
pageIndex: pagination.pageIndex, | ||
pageSize: pagination.pageSize, | ||
}); | ||
}, [onPaginationChange, pagination.pageIndex, pagination.pageSize]); | ||
|
||
const modifiedInitialState = useMemo( | ||
() => ({ | ||
pagination, | ||
...initialState, | ||
}), | ||
[initialState, pagination] | ||
); | ||
|
||
const modifiedState = useMemo( | ||
() => ({ | ||
...state, | ||
pagination: { | ||
pageIndex: pagination.pageIndex, | ||
pageSize: pagination.pageSize, | ||
}, | ||
rowSelection, | ||
}), | ||
[pagination.pageIndex, pagination.pageSize, rowSelection, state] | ||
); | ||
|
||
const muiToolbarAlertBannerProps: AlertProps = useMemo( | ||
() => | ||
hasError | ||
? { | ||
children: t("table.error"), | ||
severity: "error", | ||
} | ||
: {}, | ||
[hasError, t] | ||
); | ||
|
||
const muiTablePaginationProps: Partial< | ||
Omit<TablePaginationProps, "rowsPerPage"> | ||
> = useMemo( | ||
() => ({ | ||
rowsPerPageOptions: [], | ||
showFirstButton: false, | ||
showLastButton: false, | ||
}), | ||
[] | ||
); | ||
|
||
const muiCheckboxStyles = useCallback( | ||
(theme) => | ||
typeof theme.components?.MuiCheckbox?.styleOverrides?.root === "function" | ||
? theme.components?.MuiCheckbox?.styleOverrides?.root?.({ | ||
ownerState: {}, | ||
theme, | ||
}) | ||
: "", | ||
[] | ||
); | ||
|
||
return ( | ||
<MaterialReactTable | ||
columns={columns} | ||
data={data} | ||
enableMultiRowSelection={hasRowSelection} | ||
enablePagination | ||
enableRowSelection={hasRowSelection} | ||
enableSorting={false} | ||
getRowId={getRowId} | ||
initialState={modifiedInitialState} | ||
muiSelectAllCheckboxProps={{ sx: muiCheckboxStyles }} | ||
muiSelectCheckboxProps={{ sx: muiCheckboxStyles }} | ||
muiTablePaginationProps={muiTablePaginationProps} | ||
muiToolbarAlertBannerProps={muiToolbarAlertBannerProps} | ||
onColumnFiltersChange={setColumnFilters} | ||
onGlobalFilterChange={setGlobalFilter} | ||
onPaginationChange={updatePagination} | ||
onRowSelectionChange={setRowSelection} | ||
renderBottomToolbarCustomActions={renderBottomToolbarCustomActions} | ||
renderTopToolbarCustomActions={renderTopToolbarCustomActions} | ||
rowVirtualizerInstanceRef={rowVirtualizerInstanceRef} | ||
rowVirtualizerProps={{ overscan: 4 }} | ||
state={modifiedState} | ||
/> | ||
); | ||
}; | ||
|
||
const MemoizedPaginatedTable = memo(PaginatedTable) as typeof PaginatedTable; | ||
|
||
// @ts-expect-error | This is going to error because the component isn't and can't be defined as a `FunctionComponent`, and therefore, doesn't have a `displayName` prop. | ||
MemoizedPaginatedTable.displayName = "PaginatedTable"; | ||
|
||
export { MemoizedPaginatedTable as PaginatedTable }; |
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
Oops, something went wrong.