-
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.
feat(table): add Table and Pagination Components (#40)
* feat(table): add Table and Pagination Components * feat(table): changed totalCount to totalRows
- Loading branch information
1 parent
2eefed4
commit 0cbee08
Showing
41 changed files
with
1,554 additions
and
47 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
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,41 @@ | ||
export interface CustomColumnOptions { | ||
headerCellClassName?: string; | ||
} | ||
|
||
declare module 'react-table' { | ||
// source: https://github.com/DefinitelyTyped/DefinitelyTyped/tree/master/types/react-table | ||
|
||
export interface TableOptions<D extends object> | ||
extends UseFiltersOptions<D>, | ||
UseGlobalFiltersOptions<D>, | ||
UsePaginationOptions<D>, | ||
UseSortByOptions<D>, | ||
// note that having Record here allows you to add anything to the options, this matches the spirit of the | ||
// underlying js library, but might be cleaner if it's replaced by a more specific type that matches your | ||
// feature set, this is a safe default. | ||
Record<string, any> {} | ||
|
||
export interface Hooks<D extends object = {}> extends UseSortByHooks<D> {} | ||
|
||
export interface TableInstance<D extends object = {}> | ||
extends UseFiltersInstanceProps<D>, | ||
UseGlobalFiltersInstanceProps<D>, | ||
UsePaginationInstanceProps<D>, | ||
UseSortByInstanceProps<D> {} | ||
|
||
export interface TableState<D extends object = {}> | ||
extends UseFiltersState<D>, | ||
UseGlobalFiltersState<D>, | ||
UsePaginationState<D>, | ||
UseSortByState<D> {} | ||
|
||
export interface ColumnInterface<D extends object = {}> | ||
extends UseFiltersColumnOptions<D>, | ||
UseGlobalFiltersColumnOptions<D>, | ||
UseSortByColumnOptions<D>, | ||
CustomColumnOptions {} | ||
|
||
export interface ColumnInstance<D extends object = {}> | ||
extends UseFiltersColumnProps<D>, | ||
UseSortByColumnProps<D> {} | ||
} |
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 @@ | ||
export * from './use-context-fallback.hook'; |
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,15 @@ | ||
import { Context, useContext } from 'react'; | ||
|
||
export const useContextFallback = <T>(value: Context<T | undefined>): T => { | ||
const context = useContext<T | undefined>(value); | ||
|
||
if (context === undefined) { | ||
throw new Error( | ||
`Components that require this context must be children of ${ | ||
value.displayName ?? 'the appropriate provider' | ||
}.` | ||
); | ||
} | ||
|
||
return context; | ||
}; |
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 |
---|---|---|
@@ -1,4 +1,5 @@ | ||
import './style.css'; | ||
|
||
export * from './hooks'; | ||
export * from './models'; | ||
export * from './ui'; |
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 |
---|---|---|
@@ -1,4 +1,6 @@ | ||
export * from './alert'; | ||
export * from './label'; | ||
export * from './pagination'; | ||
export * from './property-item'; | ||
export * from './table'; | ||
export * from './typography'; |
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,2 @@ | ||
export * from './pagination-info.component'; | ||
export * from './pagination-navigation.component'; |
30 changes: 30 additions & 0 deletions
30
src/ui/pagination/components/page-size-select.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,30 @@ | ||
import React from 'react'; | ||
|
||
import { usePaginationContext } from '../context'; | ||
|
||
export const PageSizeSelect = () => { | ||
const { | ||
pageSize, | ||
pageSizeOptions = [], | ||
onPageSizeChange, | ||
} = usePaginationContext(); | ||
|
||
return ( | ||
<div className="mr-4"> | ||
<label htmlFor="pagination" className="text-neutral-400 hidden"> | ||
Rows per page: | ||
</label> | ||
<select | ||
id="pagination" | ||
value={pageSize} | ||
onChange={(event) => onPageSizeChange?.(Number(event.target.value))} | ||
> | ||
{pageSizeOptions.map((size) => ( | ||
<option key={size} value={size}> | ||
{size} | ||
</option> | ||
))} | ||
</select> | ||
</div> | ||
); | ||
}; |
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,24 @@ | ||
import React from 'react'; | ||
|
||
import { PaginationButton } from './pagination-button.component'; | ||
|
||
type PagesProps = { | ||
pages: number[]; | ||
pageIndex: number; | ||
onPageClick: (page: number) => void; | ||
}; | ||
|
||
export const Pages = ({ pages, pageIndex, onPageClick }: PagesProps) => ( | ||
<> | ||
{pages.map((page) => ( | ||
<PaginationButton | ||
isActive={pageIndex + 1 === page} | ||
key={page} | ||
label={`Page ${page}`} | ||
onClick={() => onPageClick(page - 1)} | ||
> | ||
{page} | ||
</PaginationButton> | ||
))} | ||
</> | ||
); |
29 changes: 29 additions & 0 deletions
29
src/ui/pagination/components/pagination-button.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,29 @@ | ||
import React from 'react'; | ||
import clsx from 'clsx'; | ||
|
||
type PaginationButtonProps = { | ||
label: string; | ||
children: React.ReactNode; | ||
isActive?: boolean; | ||
disabled?: boolean; | ||
onClick: () => void; | ||
}; | ||
|
||
export const PaginationButton = ({ | ||
label, | ||
children, | ||
isActive, | ||
disabled, | ||
onClick, | ||
}: PaginationButtonProps) => ( | ||
<button | ||
type="button" | ||
disabled={disabled} | ||
aria-label={label} | ||
title={label} | ||
onClick={onClick} | ||
className={clsx('p-2', { 'bg-neutral-400': isActive })} | ||
> | ||
{children} | ||
</button> | ||
); |
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,3 @@ | ||
import React from 'react'; | ||
|
||
export const PaginationDots = () => <div>...</div>; |
18 changes: 18 additions & 0 deletions
18
src/ui/pagination/components/pagination-info.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,18 @@ | ||
import React from 'react'; | ||
import { isEmpty as _isEmpty } from 'lodash'; | ||
|
||
import { usePaginationContext } from '../context'; | ||
import { PageSizeSelect } from './page-size-select.component'; | ||
import { PaginationResults } from './pagination-results.component'; | ||
|
||
export const PaginationInfo = () => { | ||
const { pageSizeOptions, totalRows } = usePaginationContext(); | ||
|
||
return ( | ||
<div className="flex"> | ||
{totalRows !== undefined && <PaginationResults />} | ||
|
||
{!_isEmpty(pageSizeOptions) && <PageSizeSelect />} | ||
</div> | ||
); | ||
}; |
29 changes: 29 additions & 0 deletions
29
src/ui/pagination/components/pagination-navigation.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,29 @@ | ||
import React from 'react'; | ||
import { MdChevronLeft, MdChevronRight } from 'react-icons/md'; | ||
|
||
import { usePaginationContext } from '../context'; | ||
import { PaginationButton } from './pagination-button.component'; | ||
import { PaginationRange } from './pagination-range.component'; | ||
|
||
export const PaginationNavigation = () => { | ||
const { previousPage, nextPage, canPreviousPage, canNextPage } = | ||
usePaginationContext(); | ||
|
||
return ( | ||
<nav className="flex items-center space-x-2" aria-label="Pagination"> | ||
<PaginationButton | ||
label="Next" | ||
disabled={!canPreviousPage} | ||
onClick={previousPage} | ||
> | ||
<MdChevronLeft /> | ||
</PaginationButton> | ||
|
||
<PaginationRange /> | ||
|
||
<PaginationButton label="Prev" disabled={!canNextPage} onClick={nextPage}> | ||
<MdChevronRight /> | ||
</PaginationButton> | ||
</nav> | ||
); | ||
}; |
Oops, something went wrong.