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

View File

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

View File

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

View File

@@ -6,6 +6,7 @@ import {
} from "api/npm"; } from "api/npm";
import { Table } from "components"; import { Table } from "components";
import { SuspenseLoader } from "components"; import { SuspenseLoader } from "components";
import { Badge } from "components";
import { intl } from "locale"; import { intl } from "locale";
import { useInterval } from "rooks"; import { useInterval } from "rooks";
import styled from "styled-components"; import styled from "styled-components";
@@ -68,7 +69,21 @@ function CertificateAuthorities() {
id: "column.status", id: "column.status",
defaultMessage: "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} columns={cols}
data={data.items} data={data.items}
sortBy={data.sort[0].field} sortBy={data.sort[0].field}
hasActions={true}
pagination={{ pagination={{
limit: data.limit, limit: data.limit,
offset: data.offset, offset: data.offset,