Base for table actions

This commit is contained in:
Jamie Curnow
2021-10-13 21:43:51 +10:00
parent dc97744c63
commit 185fe984c2
6 changed files with 87 additions and 11 deletions

View File

@@ -1,8 +1,8 @@
import React from "react";
import React, { ReactNode } from "react";
import cn from "classnames";
import { Badge } from "components";
import { intl } from "locale";
import { TableActionsMenu } from "components";
export interface TableColumn {
/**
@@ -47,9 +47,23 @@ export interface TableProps {
* Name of column to show sorted by
*/
sortBy?: string;
/**
* Should we render actions column at the end
*/
hasActions?: boolean;
}
export const Table = ({ columns, data, pagination, sortBy }: TableProps) => {
export const Table = ({
columns,
data,
pagination,
sortBy,
hasActions,
}: TableProps) => {
const getFormatter = (given: any) => {
if (typeof given === "function") {
return given;
}
if (typeof given === "string") {
switch (given) {
// Simple ID column has text-muted
@@ -57,34 +71,39 @@ export const Table = ({ columns, data, pagination, sortBy }: TableProps) => {
return (val: number) => {
return <span className="text-muted">{val}</span>;
};
/*
case "setup":
return (val: boolean) => {
return (
<Badge color={val ? "lime" : "red"}>
<Badge color={val ? "lime" : "yellow"}>
{val
? intl.formatMessage({
id: "ready",
defaultMessage: "Ready",
})
: intl.formatMessage({
id: "required",
defaultMessage: "Required",
id: "setup-required",
defaultMessage: "Setup Required",
})}
</Badge>
);
};
*/
case "bool":
return (val: boolean) => {
return (
<Badge color={val ? "lime" : "red"}>
<Badge color={val ? "lime" : "orange"}>
{val ? "true" : "false"}
</Badge>
);
};
default:
return () => {
return <>formatter-not-found: {given}</>;
};
}
}
return given;
};
const getPagination = (p: TablePagination) => {
@@ -223,6 +242,7 @@ export const Table = ({ columns, data, pagination, sortBy }: TableProps) => {
</th>
);
})}
{hasActions && <th />}
</tr>
</thead>
<tbody>
@@ -231,13 +251,20 @@ export const Table = ({ columns, data, pagination, sortBy }: TableProps) => {
<tr key={`table-row-${idx}`}>
{columns.map((col, idx2) => {
return (
<td key={`table-col-${idx}-${idx2}`}>
<td
key={`table-col-${idx}-${idx2}`}
className={col.className}>
{col.formatter
? getFormatter(col.formatter)(row[col.name], row)
: row[col.name]}
</td>
);
})}
{hasActions && (
<td>
<TableActionsMenu actions={data.actions} />
</td>
)}
</tr>
);
})}

View File

@@ -0,0 +1,24 @@
import React, { ReactNode } from "react";
import { DotsVertical } from "tabler-icons-react";
export interface TableActionsMenuProps {
/**
* Additional Class
*/
className?: string;
/**
* Actions
*/
actions: ReactNode[];
}
export const TableActionsMenu = ({
actions,
className,
}: TableActionsMenuProps) => {
return (
<div className={className}>
<DotsVertical />
</div>
);
};

View File

@@ -1 +1,2 @@
export * from "./TableActionsMenu";
export * from "./Table";

View File

@@ -13,6 +13,11 @@ body {
background-color: #f5f7fb;
color: rgb(73, 80, 87);
}
.text-right {
text-align: right;
}
/*
.btn {
text-transform: none;

View File

@@ -74,6 +74,9 @@
"setup.title": {
"defaultMessage": "Create your first Account"
},
"setup-required": {
"defaultMessage": "Setup Required"
},
"user.email": {
"defaultMessage": "Email"
},

View File

@@ -6,6 +6,7 @@ import {
} from "api/npm";
import { Table } from "components";
import { SuspenseLoader } from "components";
import { Badge } from "components";
import { intl } from "locale";
import { useInterval } from "rooks";
import styled from "styled-components";
@@ -68,7 +69,21 @@ function CertificateAuthorities() {
id: "column.status",
defaultMessage: "Status",
}),
formatter: "setup",
formatter: (val: boolean) => {
return (
<Badge color={val ? "lime" : "yellow"}>
{val
? intl.formatMessage({
id: "ready",
defaultMessage: "Ready",
})
: intl.formatMessage({
id: "setup-required",
defaultMessage: "Setup Required",
})}
</Badge>
);
},
},
];
@@ -89,6 +104,7 @@ function CertificateAuthorities() {
columns={cols}
data={data.items}
sortBy={data.sort[0].field}
hasActions={true}
pagination={{
limit: data.limit,
offset: data.offset,