|
| 1 | +import { |
| 2 | + Table, |
| 3 | + TableColumn |
| 4 | +} from '@backstage/core-components'; |
| 5 | +import { BackstageTheme } from '@backstage/theme'; |
| 6 | +import { MTablePagination, MTableHeader } from '@material-table/core'; |
| 7 | +import { withStyles } from '@material-ui/core/styles'; |
| 8 | +import React from 'react'; |
| 9 | +import { UserProfile } from '../../utils/types'; |
| 10 | +import { SignInContent } from '../SignInContent'; |
| 11 | + |
| 12 | +export type CustomTableProps<T extends object> = { |
| 13 | + title: string | JSX.Element; |
| 14 | + subtitle: string; |
| 15 | + columns: TableColumn<T>[]; |
| 16 | + data: T[]; |
| 17 | + isLoading: boolean; |
| 18 | + signIn: (instantPopup: boolean) => void; |
| 19 | + isSignedIn: boolean; |
| 20 | + isInitialized: boolean; |
| 21 | + isError: boolean; |
| 22 | + error?: any; |
| 23 | + user: UserProfile | null; // github user profile |
| 24 | + detailPanel?: ({ rowData }: { rowData: T }) => React.ReactNode; |
| 25 | + columnsButton?: boolean; |
| 26 | +} & PaginationProps & |
| 27 | + SearchProps; |
| 28 | + |
| 29 | +type PaginationProps = |
| 30 | + | { |
| 31 | + paging?: never; |
| 32 | + totalCount?: never; |
| 33 | + page?: never; |
| 34 | + setPage?: never; |
| 35 | + pageSize?: never; |
| 36 | + setPageSize?: never; |
| 37 | + } |
| 38 | + | { |
| 39 | + paging: boolean; |
| 40 | + totalCount: number; |
| 41 | + page: number; |
| 42 | + setPage: (newPage: number) => void; |
| 43 | + pageSize: number; |
| 44 | + setPageSize: (newPageSize: number) => void; |
| 45 | + }; |
| 46 | + |
| 47 | +type SearchProps = |
| 48 | + | { |
| 49 | + isSearchAvailable?: never; |
| 50 | + setSearchText?: never; |
| 51 | + searchPlaceholder?: never; |
| 52 | + searchTooltip?: never; |
| 53 | + } |
| 54 | + | { |
| 55 | + isSearchAvailable: boolean; |
| 56 | + setSearchText: (searchText: string) => void; |
| 57 | + searchPlaceholder?: string; |
| 58 | + searchTooltip?: string; |
| 59 | + }; |
| 60 | + |
| 61 | +CustomTable.defaultProps = { |
| 62 | + title: '', |
| 63 | + subtitle: '', |
| 64 | + columns: [], |
| 65 | + data: [], |
| 66 | + isLoading: false, |
| 67 | + paging: false, |
| 68 | + isSearchAvailable: false, |
| 69 | + totalCount: 0, |
| 70 | + searchPlaceholder: 'search', |
| 71 | + searchTooltip: 'search', |
| 72 | + pageSize: 0, |
| 73 | + page: 1, |
| 74 | + select: false, |
| 75 | + columnsButton: false |
| 76 | +}; |
| 77 | + |
| 78 | +const StyledMTableHeader = withStyles( |
| 79 | + theme => ({ |
| 80 | + header: { |
| 81 | + padding: theme.spacing(1, 2, 1, 2.5), |
| 82 | + borderTop: `1px solid ${theme.palette.grey.A100}`, |
| 83 | + borderBottom: `1px solid ${theme.palette.grey.A100}`, |
| 84 | + // withStyles hasn't a generic overload for theme |
| 85 | + color: (theme as BackstageTheme).palette.textSubtle, |
| 86 | + fontWeight: 'bold', |
| 87 | + position: 'static', |
| 88 | + wordBreak: 'normal', |
| 89 | + }, |
| 90 | + }), |
| 91 | + { name: 'BackstageTableHeader' }, |
| 92 | +)(MTableHeader); |
| 93 | + |
| 94 | +export function CustomTable<T extends object = {}>(props: CustomTableProps<T>) { |
| 95 | + return ( |
| 96 | + <Table<T> |
| 97 | + title={props.title} |
| 98 | + subtitle={props.subtitle} |
| 99 | + columns={props.columns} |
| 100 | + isLoading={props.isLoading} |
| 101 | + options={{ |
| 102 | + search: props.isSearchAvailable, |
| 103 | + paging: props.paging, |
| 104 | + padding: 'dense', |
| 105 | + toolbar: true, |
| 106 | + filtering: false, |
| 107 | + showFirstLastPageButtons: true, |
| 108 | + paginationType: 'stepped', |
| 109 | + loadingType: 'linear', |
| 110 | + emptyRowsWhenPaging: true, |
| 111 | + tableLayout: 'auto', |
| 112 | + debounceInterval: 350, |
| 113 | + sorting: false, |
| 114 | + columnsButton: props.columnsButton, |
| 115 | + }} |
| 116 | + localization={{ |
| 117 | + toolbar: { |
| 118 | + searchPlaceholder: props.searchPlaceholder, |
| 119 | + searchTooltip: props.searchTooltip, |
| 120 | + }, |
| 121 | + }} |
| 122 | + data={props.data} |
| 123 | + onSearchChange={(searchText: string) => { |
| 124 | + if (props.isSearchAvailable) { |
| 125 | + props.setSearchText(searchText); |
| 126 | + } |
| 127 | + }} |
| 128 | + emptyContent={ |
| 129 | + <> |
| 130 | + {!props.isSignedIn && ( |
| 131 | + <SignInContent handleAuthClick={() => props.signIn(false)} /> |
| 132 | + )} |
| 133 | + </> |
| 134 | + } |
| 135 | + components={{ |
| 136 | + Pagination: paginationProps => { |
| 137 | + const { classes, ...remainingProps } = paginationProps; |
| 138 | + return ( |
| 139 | + <MTablePagination |
| 140 | + {...remainingProps} |
| 141 | + page={props.page} |
| 142 | + count={props.totalCount} |
| 143 | + // @ts-ignore |
| 144 | + onPageChange={(e: any, newPage: number) => { |
| 145 | + if (props.paging) { |
| 146 | + props.setPage(newPage); |
| 147 | + } |
| 148 | + }} |
| 149 | + /> |
| 150 | + ); |
| 151 | + }, |
| 152 | + Header: headerProps => <StyledMTableHeader {...headerProps} />, |
| 153 | + }} |
| 154 | + detailPanel={props.detailPanel} |
| 155 | + /> |
| 156 | + ); |
| 157 | +} |
0 commit comments