-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fdbfb6b
commit 3070f93
Showing
18 changed files
with
147 additions
and
222 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,102 +1,87 @@ | ||
import React from "react"; | ||
import styled from "styled-components"; | ||
import PropTypes from "prop-types"; | ||
import { space } from "styled-system"; | ||
import propTypes from "@styled-system/prop-types"; | ||
import { addStyledProps } from "../StyledProps"; | ||
import TableHead from "./TableHead"; | ||
import TableBody from "./TableBody"; | ||
import TableFoot from "./TableFoot"; | ||
import { rowsPropType, RowType, Columns } from "./Table.types"; | ||
import { ColumnType } from "./Table.types"; | ||
|
||
export type BaseTableProps = { | ||
columns: Columns; | ||
rows: RowType[]; | ||
export type BaseTableProps<Row> = { | ||
columns: ColumnType<Row>[]; | ||
rows: Row[]; | ||
noRowsContent?: string; | ||
keyField?: string; | ||
id?: string; | ||
loading?: boolean; | ||
footerRows?: any; | ||
footerRows?: Row[]; | ||
rowHovers?: boolean; | ||
compact?: boolean; | ||
className?: string; | ||
stickyHeader?: boolean; | ||
onRowMouseEnter?: (...args: any[]) => any; | ||
onRowMouseLeave?: (...args: any[]) => any; | ||
onMouseEnter?: any; | ||
onMouseLeave?: any; | ||
onRowMouseEnter?: React.DOMAttributes<HTMLTableRowElement>["onMouseEnter"]; | ||
onRowMouseLeave?: React.DOMAttributes<HTMLTableRowElement>["onMouseLeave"]; | ||
onMouseEnter?: React.DOMAttributes<HTMLTableElement>["onMouseEnter"]; | ||
onMouseLeave?: React.DOMAttributes<HTMLTableElement>["onMouseLeave"]; | ||
}; | ||
|
||
const StyledTable = styled.table<any>(space, { | ||
borderCollapse: "collapse", | ||
width: "100%", | ||
background: "white", | ||
position: "relative", | ||
}); | ||
const StyledTable = styled.table( | ||
{ | ||
borderCollapse: "collapse", | ||
width: "100%", | ||
background: "white", | ||
position: "relative", | ||
}, | ||
addStyledProps | ||
); | ||
|
||
const BaseTable: React.FC<BaseTableProps> = ({ | ||
export default function BaseTable<Row>({ | ||
columns, | ||
rows, | ||
// @todo: remove this default value and internationalize it | ||
noRowsContent = "No records have been created for this table.", | ||
keyField = "id", | ||
keyField, | ||
id, | ||
loading, | ||
footerRows = [], | ||
rowHovers = true, | ||
footerRows, | ||
rowHovers, | ||
compact, | ||
className, | ||
stickyHeader, | ||
onRowMouseEnter = () => {}, | ||
onRowMouseLeave = () => {}, | ||
onRowMouseEnter, | ||
onRowMouseLeave, | ||
...props | ||
}) => ( | ||
<StyledTable id={id} className={className} {...props}> | ||
<TableHead columns={columns} compact={compact} sticky={stickyHeader} /> | ||
<TableBody | ||
columns={columns} | ||
rows={rows} | ||
keyField={keyField} | ||
noRowsContent={noRowsContent} | ||
loading={loading} | ||
rowHovers={rowHovers} | ||
compact={compact} | ||
onRowMouseLeave={onRowMouseLeave} | ||
onRowMouseEnter={onRowMouseEnter} | ||
/> | ||
{footerRows && ( | ||
<TableFoot columns={columns} rows={footerRows} keyField={keyField} loading={loading} compact={compact} /> | ||
)} | ||
</StyledTable> | ||
); | ||
}: BaseTableProps<Row>) { | ||
return ( | ||
<StyledTable id={id} className={className} {...props}> | ||
<TableHead columns={columns} compact={compact} sticky={stickyHeader} /> | ||
<TableBody | ||
columns={columns} | ||
rows={rows} | ||
keyField={keyField} | ||
noRowsContent={noRowsContent} | ||
loading={loading} | ||
rowHovers={rowHovers} | ||
compact={compact} | ||
onRowMouseLeave={onRowMouseLeave} | ||
onRowMouseEnter={onRowMouseEnter} | ||
/> | ||
{footerRows && ( | ||
<TableFoot columns={columns} rows={footerRows} keyField={keyField} loading={loading} compact={compact} /> | ||
)} | ||
</StyledTable> | ||
); | ||
} | ||
|
||
BaseTable.propTypes = { | ||
...propTypes.space, | ||
columns: PropTypes.any, | ||
rows: PropTypes.any, | ||
noRowsContent: PropTypes.string, | ||
keyField: PropTypes.string, | ||
id: PropTypes.string, | ||
loading: PropTypes.bool, | ||
footerRows: rowsPropType, | ||
rowHovers: PropTypes.bool, | ||
compact: PropTypes.bool, | ||
className: PropTypes.string, | ||
stickyHeader: PropTypes.bool, | ||
onRowMouseEnter: PropTypes.func, | ||
onRowMouseLeave: PropTypes.func, | ||
}; | ||
const noop = () => {}; | ||
|
||
BaseTable.defaultProps = { | ||
noRowsContent: "No records have been created for this table.", | ||
keyField: "id", | ||
id: undefined, | ||
loading: false, | ||
footerRows: [], | ||
rowHovers: true, | ||
compact: false, | ||
className: undefined, | ||
stickyHeader: false, | ||
onRowMouseEnter: () => {}, | ||
onRowMouseLeave: () => {}, | ||
onRowMouseEnter: noop, | ||
onRowMouseLeave: noop, | ||
}; | ||
|
||
export default BaseTable; |
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 |
---|---|---|
@@ -1,57 +1,27 @@ | ||
import type { Key } from "react"; | ||
import PropTypes from "prop-types"; | ||
|
||
export type RowType = unknown; | ||
|
||
export interface CellInfoType { | ||
export interface CellInfoType<Row> { | ||
cellData: unknown; | ||
column: ColumnType; | ||
row: RowType; | ||
column: ColumnType<Row>[]; | ||
row: Row; | ||
} | ||
|
||
interface ColumnInfoType { | ||
align?: ColumnAlignment; | ||
label: string; | ||
dataKey?: Key; | ||
width?: string | number; | ||
metadata?: Record<string, unknown>; | ||
} | ||
|
||
type ColumnAlignment = "left" | "right" | "center"; | ||
export type ColumnAlignment = "left" | "right" | "center"; | ||
|
||
export type ColumnType = { | ||
export type ColumnType<Row> = { | ||
align?: ColumnAlignment; | ||
label?: string; | ||
cellFormatter?: (cell: CellInfoType) => React.ReactNode; | ||
cellRenderer?: (cell: CellInfoType) => React.ReactNode; | ||
cellFormatter?: (cell: CellInfoType<Row>) => React.ReactNode; | ||
cellRenderer?: (cell: CellInfoType<Row>) => React.ReactNode; | ||
headerRenderer?: (column: ColumnInfoType) => React.ReactNode; | ||
headerFormatter?: (column: ColumnInfoType) => React.ReactNode; | ||
width?: string | number; | ||
} & ({ key: Key; dataKey?: never | undefined } | { dataKey: Key; key?: never | undefined }); | ||
|
||
export type Columns = ColumnType[]; | ||
|
||
export const columnPropType = PropTypes.shape({ | ||
align: PropTypes.oneOf(["right", "left", "center"]), | ||
label: PropTypes.string, | ||
dataKey: PropTypes.oneOf([PropTypes.string, PropTypes.number]), | ||
key: PropTypes.oneOf([PropTypes.string, PropTypes.number]), | ||
cellFormatter: PropTypes.func, | ||
cellRenderer: PropTypes.func, | ||
headerRenderer: PropTypes.func, | ||
width: PropTypes.string, | ||
}); | ||
|
||
export const rowPropType = PropTypes.objectOf( | ||
PropTypes.oneOfType([ | ||
PropTypes.number, | ||
PropTypes.string, | ||
PropTypes.bool, | ||
PropTypes.func, | ||
PropTypes.node, | ||
PropTypes.shape({}), | ||
]) | ||
); | ||
|
||
export const columnsPropType = PropTypes.arrayOf(columnPropType); | ||
|
||
export const rowsPropType = PropTypes.arrayOf(rowPropType); |
Oops, something went wrong.