-
Notifications
You must be signed in to change notification settings - Fork 13.2k
Chore: Generic Table #23745
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
Merged
Merged
Chore: Generic Table #23745
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,24 @@ | ||
| import { Box, Table } from '@rocket.chat/fuselage'; | ||
| import React, { forwardRef, ReactNode } from 'react'; | ||
|
|
||
| import ScrollableContentWrapper from '../../ScrollableContentWrapper'; | ||
|
|
||
| type GenericTableProps = { | ||
| fixed?: boolean; | ||
| children: ReactNode; | ||
| }; | ||
|
|
||
| export const GenericTable = forwardRef<HTMLElement, GenericTableProps>(function GenericTable( | ||
| { fixed = true, children }, | ||
| ref, | ||
| ) { | ||
| return ( | ||
| <Box mi='neg-x24' pi='x24' flexShrink={1} flexGrow={1} ref={ref} overflow='hidden'> | ||
| <ScrollableContentWrapper overflowX> | ||
| <Table fixed={fixed} sticky> | ||
| {children} | ||
| </Table> | ||
| </ScrollableContentWrapper> | ||
| </Box> | ||
| ); | ||
| }); |
This file contains hidden or 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,4 @@ | ||
| import { Table } from '@rocket.chat/fuselage'; | ||
| import React, { FC } from 'react'; | ||
|
|
||
| export const GenericTableBody: FC = (props) => <Table.Body {...props} />; |
This file contains hidden or 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,6 @@ | ||
| import { Table } from '@rocket.chat/fuselage'; | ||
| import React, { ComponentProps, FC } from 'react'; | ||
|
|
||
| export const GenericTableCell: FC<ComponentProps<typeof Table.Cell>> = (props) => ( | ||
| <Table.Cell {...props} /> | ||
| ); |
This file contains hidden or 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,10 @@ | ||
| import { Table } from '@rocket.chat/fuselage'; | ||
| import React, { FC } from 'react'; | ||
|
|
||
| import { GenericTableRow } from './GenericTableRow'; | ||
|
|
||
| export const GenericTableHeader: FC = ({ children, ...props }) => ( | ||
| <Table.Head {...props}> | ||
| <GenericTableRow>{children}</GenericTableRow> | ||
| </Table.Head> | ||
| ); |
30 changes: 30 additions & 0 deletions
30
client/components/GenericTable/V2/GenericTableHeaderCell.tsx
This file contains hidden or 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,30 @@ | ||
| import { Box, Table } from '@rocket.chat/fuselage'; | ||
| import React, { ComponentProps, ReactElement, useCallback } from 'react'; | ||
|
|
||
| import SortIcon from '../SortIcon'; | ||
|
|
||
| type GenericTableHeaderCellProps<T extends string> = Omit<ComponentProps<typeof Box>, 'onClick'> & { | ||
| active?: boolean; | ||
| direction?: 'asc' | 'desc'; | ||
| sort?: T; | ||
| onClick?: (sort: T) => void; | ||
| }; | ||
|
|
||
| export const GenericTableHeaderCell = <T extends string = string>({ | ||
| children, | ||
| active, | ||
| direction, | ||
| sort, | ||
| onClick, | ||
| ...props | ||
| }: GenericTableHeaderCellProps<T>): ReactElement => { | ||
| const fn = useCallback(() => onClick && sort && onClick(sort), [sort, onClick]); | ||
| return ( | ||
| <Table.Cell clickable={!!sort} onClick={fn} {...props}> | ||
| <Box display='flex' alignItems='center' wrap='no-wrap'> | ||
| {children} | ||
| {sort && <SortIcon direction={active ? direction : undefined} />} | ||
| </Box> | ||
| </Table.Cell> | ||
| ); | ||
| }; |
25 changes: 25 additions & 0 deletions
25
client/components/GenericTable/V2/GenericTableLoadingRow.tsx
This file contains hidden or 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,25 @@ | ||
| import { Box, Skeleton, Table } from '@rocket.chat/fuselage'; | ||
| import React, { ReactElement } from 'react'; | ||
|
|
||
| type GenericTableLoadingRowRowProps = { | ||
| cols: number; | ||
| }; | ||
|
|
||
| export const GenericTableLoadingRow = ({ cols }: GenericTableLoadingRowRowProps): ReactElement => ( | ||
| <Table.Row> | ||
| <Table.Cell> | ||
| <Box display='flex'> | ||
| <Skeleton variant='rect' height={40} width={40} /> | ||
| <Box mi='x8' flexGrow={1}> | ||
| <Skeleton width='100%' /> | ||
| <Skeleton width='100%' /> | ||
| </Box> | ||
| </Box> | ||
| </Table.Cell> | ||
| {Array.from({ length: cols - 1 }, (_, i) => ( | ||
| <Table.Cell key={i}> | ||
| <Skeleton width='100%' /> | ||
| </Table.Cell> | ||
| ))} | ||
| </Table.Row> | ||
| ); |
15 changes: 15 additions & 0 deletions
15
client/components/GenericTable/V2/GenericTableLoadingTable.tsx
This file contains hidden or 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,15 @@ | ||
| import React, { ReactElement } from 'react'; | ||
|
|
||
| import { GenericTableLoadingRow } from './GenericTableLoadingRow'; | ||
|
|
||
| export const GenericTableLoadingTable = ({ | ||
| headerCells, | ||
| }: { | ||
| headerCells: number; | ||
| }): ReactElement => ( | ||
| <> | ||
| {Array.from({ length: 10 }, (_, i) => ( | ||
| <GenericTableLoadingRow key={i} cols={headerCells} /> | ||
| ))} | ||
| </> | ||
| ); |
This file contains hidden or 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,6 @@ | ||
| import { Table } from '@rocket.chat/fuselage'; | ||
| import React, { ComponentProps, FC } from 'react'; | ||
|
|
||
| export const GenericTableRow: FC<ComponentProps<typeof Table.Row>> = (props) => ( | ||
| <Table.Row {...props} /> | ||
| ); |
This file contains hidden or 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,9 @@ | ||
| import { useState } from 'react'; | ||
|
|
||
| export const useCurrent = ( | ||
| currentInitialValue = 0, | ||
| ): [number, React.Dispatch<React.SetStateAction<number>>] => { | ||
| const [current, setCurrent] = useState<number>(currentInitialValue); | ||
|
|
||
| return [current, setCurrent]; | ||
| }; |
This file contains hidden or 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,11 @@ | ||
| import { useState } from 'react'; | ||
|
|
||
| type UseItemsPerPageValue = 25 | 50 | 100; | ||
|
|
||
| export const useItemsPerPage = ( | ||
| itemsPerPageInitialValue: UseItemsPerPageValue = 25, | ||
| ): [UseItemsPerPageValue, React.Dispatch<React.SetStateAction<UseItemsPerPageValue>>] => { | ||
| const [itemsPerPage, setItemsPerPage] = useState<UseItemsPerPageValue>(itemsPerPageInitialValue); | ||
|
|
||
| return [itemsPerPage, setItemsPerPage]; | ||
| }; |
This file contains hidden or 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,8 @@ | ||
| import { useCallback } from 'react'; | ||
|
|
||
| import { useTranslation } from '../../../contexts/TranslationContext'; | ||
|
|
||
| export const useItemsPerPageLabel = (): (() => string) => { | ||
| const t = useTranslation(); | ||
| return useCallback(() => t('Items_per_page:'), [t]); | ||
| }; |
This file contains hidden or 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,30 @@ | ||
| import { useCurrent } from './useCurrent'; | ||
| import { useItemsPerPage } from './useItemsPerPage'; | ||
| import { useItemsPerPageLabel } from './useItemsPerPageLabel'; | ||
| import { useShowingResultsLabel } from './useShowingResultsLabel'; | ||
|
|
||
| export const usePagination = (): { | ||
| current: ReturnType<typeof useCurrent>[0]; | ||
| setCurrent: ReturnType<typeof useCurrent>[1]; | ||
| itemsPerPage: ReturnType<typeof useItemsPerPage>[0]; | ||
| setItemsPerPage: ReturnType<typeof useItemsPerPage>[1]; | ||
| itemsPerPageLabel: ReturnType<typeof useItemsPerPageLabel>; | ||
| showingResultsLabel: ReturnType<typeof useShowingResultsLabel>; | ||
| } => { | ||
| const [itemsPerPage, setItemsPerPage] = useItemsPerPage(); | ||
|
|
||
| const [current, setCurrent] = useCurrent(); | ||
|
|
||
| const itemsPerPageLabel = useItemsPerPageLabel(); | ||
|
|
||
| const showingResultsLabel = useShowingResultsLabel(); | ||
|
|
||
| return { | ||
| itemsPerPage, | ||
| setItemsPerPage, | ||
| current, | ||
| setCurrent, | ||
| itemsPerPageLabel, | ||
| showingResultsLabel, | ||
| }; | ||
| }; |
19 changes: 19 additions & 0 deletions
19
client/components/GenericTable/hooks/useShowingResultsLabel.ts
This file contains hidden or 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,19 @@ | ||
| import { Pagination } from '@rocket.chat/fuselage'; | ||
| import { ComponentProps, useCallback } from 'react'; | ||
|
|
||
| import { useTranslation } from '../../../contexts/TranslationContext'; | ||
|
|
||
| type Props< | ||
| T extends ComponentProps<typeof Pagination>['showingResultsLabel'] = ComponentProps< | ||
| typeof Pagination | ||
| >['showingResultsLabel'], | ||
| > = T extends (...args: any[]) => any ? Parameters<T> : never; | ||
|
|
||
| export const useShowingResultsLabel = (): ((...params: Props) => string) => { | ||
| const t = useTranslation(); | ||
| return useCallback( | ||
| ({ count, current, itemsPerPage }) => | ||
| t('Showing_results_of', current + 1, Math.min(current + itemsPerPage, count), count), | ||
| [t], | ||
| ); | ||
| }; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.