Skip to content
Snippets Groups Projects
Commit ce46dbfa authored by Jacopo Gasparetto's avatar Jacopo Gasparetto
Browse files

Add onSelect function to Table component

parent 5ac94f09
No related branches found
No related tags found
No related merge requests found
// import './Table.css' // import './Table.css'
import { ChangeEvent, MouseEvent } from "react";
export interface Column { export interface Column {
id: string id: string
...@@ -12,15 +13,27 @@ export interface Value { ...@@ -12,15 +13,27 @@ export interface Value {
} }
interface TableParams { interface TableParams {
columns: Column[], columns: Column[];
data: Value[][], data: Value[][];
selectable?: boolean selectable?: boolean;
onClick?: (element: React.MouseEvent<HTMLTableRowElement>, index: number) => void selectedRows?: Set<number>;
onClick?: (element: MouseEvent<HTMLTableRowElement>, index: number) => void
onSelect?: (element: ChangeEvent<HTMLInputElement>, index: number) => void
} }
export const Table = ({ columns, data, selectable = false, onClick }: TableParams) => { export const Table = (props: TableParams) => {
const {
columns,
data,
selectable = false,
selectedRows,
onClick,
onSelect
} = props;
const thClassName = "border-b font-medium p-4 first:pl-8 last:pr-8 pt-0 pb-3 \ const thClassName = "border-b font-medium p-4 first:pl-8 last:pr-8 pt-0 pb-3 \
text-left text-slate-800"; text-left text-slate-800";
const Header = () => { const Header = () => {
return ( return (
<thead> <thead>
...@@ -56,7 +69,17 @@ export const Table = ({ columns, data, selectable = false, onClick }: TableParam ...@@ -56,7 +69,17 @@ export const Table = ({ columns, data, selectable = false, onClick }: TableParam
hover:text-slate-800 hover:cursor-pointer" hover:text-slate-800 hover:cursor-pointer"
onClick={(el) => onClick?.(el, rowIndex)} onClick={(el) => onClick?.(el, rowIndex)}
key={rowIndex}> key={rowIndex}>
{selectable ? <th className="pl-4"><input type="checkbox"></input></th> : null} {
selectable ?
<th className="pl-4">
<input
type="checkbox"
onChange={(el) => (onSelect && onSelect(el, rowIndex))}
checked={selectedRows && selectedRows.has(rowIndex)}
>
</input>
</th> : null
}
{columnIds.map((colId, index) => { {columnIds.map((colId, index) => {
return <td return <td
className="border-b border-slate-100 className="border-b border-slate-100
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment