generated from EyeSeeTea/dhis2-app-skeleton
-
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.
Merge pull request #3 from EyeSeeTea/feature/create-basic-table-compo…
…nent feat: Create basic table component
- Loading branch information
Showing
5 changed files
with
173 additions
and
7 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
import React from "react"; | ||
import { Table, TableBody, TableCell, TableHead, TableRow } from "@material-ui/core"; | ||
import styled from "styled-components"; | ||
import { Maybe } from "../../../utils/ts-utils"; | ||
import i18n from "../../../utils/i18n"; | ||
import { Option } from "../utils/option"; | ||
import { Cell } from "./Cell"; | ||
|
||
const noop = () => {}; | ||
|
||
interface BaseColumn { | ||
value: string; | ||
label: string; | ||
} | ||
interface TextColumn extends BaseColumn { | ||
type: "text"; | ||
underline?: boolean; | ||
bold?: boolean; | ||
} | ||
interface LinkColumn extends BaseColumn { | ||
type: "link"; | ||
} | ||
interface SelectorColumn extends BaseColumn { | ||
type: "selector"; | ||
options: Option[]; | ||
} | ||
|
||
export type TableColumn = TextColumn | LinkColumn | SelectorColumn; | ||
interface BasicTableProps { | ||
columns: TableColumn[]; | ||
rows: { | ||
[key: TableColumn["value"]]: string; | ||
}[]; | ||
onChange?: (cell: Maybe<string>, rowIndex: number, column: TableColumn["value"]) => void; | ||
showRowIndex?: boolean; | ||
} | ||
|
||
export const BasicTable: React.FC<BasicTableProps> = React.memo( | ||
({ columns, rows, onChange = noop, showRowIndex = false }) => { | ||
return ( | ||
<StyledTable stickyHeader> | ||
<TableHead> | ||
<TableRow> | ||
{showRowIndex && <TableCell />} | ||
{columns.map(({ value, label }) => ( | ||
<TableCell key={value}>{i18n.t(label)}</TableCell> | ||
))} | ||
</TableRow> | ||
</TableHead> | ||
<TableBody> | ||
{rows.map((row, rowIndex) => ( | ||
<TableRow key={rowIndex}> | ||
{showRowIndex && <IndexTableCell>{rowIndex + 1}</IndexTableCell>} | ||
{columns.map(column => ( | ||
<Cell | ||
key={`${rowIndex}-${column.value}`} | ||
value={row[column.value] || ""} | ||
rowIndex={rowIndex} | ||
column={column} | ||
onChange={onChange} | ||
/> | ||
))} | ||
</TableRow> | ||
))} | ||
</TableBody> | ||
</StyledTable> | ||
); | ||
} | ||
); | ||
|
||
const StyledTable = styled(Table)` | ||
border-collapse: collapse; | ||
& .MuiTableHead-root { | ||
color: ${props => props.theme.palette.common.grey1}; | ||
font-weight: 600; | ||
height: 2.25rem; | ||
& .MuiTableCell-root { | ||
white-space: nowrap; | ||
background-color: ${props => props.theme.palette.common.greyLight}; | ||
} | ||
} | ||
& .MuiTableBody-root { | ||
color: ${props => props.theme.palette.common.grey}; | ||
} | ||
& .MuiTableCell-root { | ||
font-size: 0.75rem; | ||
padding-block: 0.375rem; | ||
border: 1px solid ${props => props.theme.palette.common.grey4}; | ||
height: 1.875rem; | ||
} | ||
`; | ||
|
||
const IndexTableCell = styled(TableCell)` | ||
min-width: 2.25rem; | ||
padding-inline: 0.375rem; | ||
text-align: center; | ||
`; |
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,67 @@ | ||
import React, { useCallback } from "react"; | ||
import { TableCell, Link } from "@material-ui/core"; | ||
import styled from "styled-components"; | ||
import { Selector } from "../selector/Selector"; | ||
import { TableColumn } from "./BasicTable"; | ||
|
||
const noop = () => {}; | ||
|
||
type CellProps = { | ||
value: string; | ||
rowIndex: number; | ||
column: TableColumn; | ||
onChange?: (value: string, rowIndex: number, column: TableColumn["value"]) => void; | ||
}; | ||
|
||
export const Cell: React.FC<CellProps> = React.memo( | ||
({ value, rowIndex, column, onChange = noop }) => { | ||
const [selectorValue, setSelectorValue] = React.useState<string>(value); | ||
const handleChange = useCallback( | ||
(value: string) => { | ||
setSelectorValue(value); | ||
onChange(value, rowIndex, column.value); | ||
}, | ||
[onChange, rowIndex, column.value] | ||
); | ||
|
||
switch (column.type) { | ||
case "link": | ||
return ( | ||
<StyledTableCell> | ||
<StyledLink onClick={() => onChange(value, rowIndex, column.value)}> | ||
{value} | ||
</StyledLink> | ||
</StyledTableCell> | ||
); | ||
case "selector": | ||
return ( | ||
<StyledTableCell> | ||
<Selector | ||
id={`selector-${rowIndex}-${column.value}`} | ||
options={column.options} | ||
selected={selectorValue} | ||
onChange={handleChange} | ||
/> | ||
</StyledTableCell> | ||
); | ||
case "text": | ||
default: | ||
return ( | ||
<StyledTableCell $underline={column.underline} $bold={column.bold}> | ||
{value} | ||
</StyledTableCell> | ||
); | ||
} | ||
} | ||
); | ||
|
||
const StyledTableCell = styled(TableCell)<{ $underline?: boolean; $bold?: boolean }>` | ||
text-decoration: ${props => (props.$underline ? "underline" : "initial")}; | ||
font-weight: ${props => (props.$bold ? 700 : 400)}; | ||
`; | ||
|
||
const StyledLink = styled(Link)` | ||
color: ${props => props.theme.palette.common.blue600}; | ||
text-decoration: underline; | ||
cursor: pointer; | ||
`; |
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