From 185fe984c2271b03cfaeb62c63ff31ecf9f28971 Mon Sep 17 00:00:00 2001 From: Jamie Curnow Date: Wed, 13 Oct 2021 21:43:51 +1000 Subject: [PATCH] Base for table actions --- frontend/src/components/Table/Table.tsx | 47 +++++++++++++++---- .../src/components/Table/TableActionsMenu.tsx | 24 ++++++++++ frontend/src/components/Table/index.ts | 1 + frontend/src/index.scss | 5 ++ frontend/src/locale/src/en.json | 3 ++ .../pages/CertificateAuthorities/index.tsx | 18 ++++++- 6 files changed, 87 insertions(+), 11 deletions(-) create mode 100644 frontend/src/components/Table/TableActionsMenu.tsx diff --git a/frontend/src/components/Table/Table.tsx b/frontend/src/components/Table/Table.tsx index 419a5160..0115ff21 100644 --- a/frontend/src/components/Table/Table.tsx +++ b/frontend/src/components/Table/Table.tsx @@ -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 {val}; }; + /* case "setup": return (val: boolean) => { return ( - + {val ? intl.formatMessage({ id: "ready", defaultMessage: "Ready", }) : intl.formatMessage({ - id: "required", - defaultMessage: "Required", + id: "setup-required", + defaultMessage: "Setup Required", })} ); }; + */ case "bool": return (val: boolean) => { return ( - + {val ? "true" : "false"} ); }; + + 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) => { ); })} + {hasActions && } @@ -231,13 +251,20 @@ export const Table = ({ columns, data, pagination, sortBy }: TableProps) => { {columns.map((col, idx2) => { return ( - + {col.formatter ? getFormatter(col.formatter)(row[col.name], row) : row[col.name]} ); })} + {hasActions && ( + + + + )} ); })} diff --git a/frontend/src/components/Table/TableActionsMenu.tsx b/frontend/src/components/Table/TableActionsMenu.tsx new file mode 100644 index 00000000..8e0495c1 --- /dev/null +++ b/frontend/src/components/Table/TableActionsMenu.tsx @@ -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 ( +
+ +
+ ); +}; diff --git a/frontend/src/components/Table/index.ts b/frontend/src/components/Table/index.ts index e40efa47..59432a8d 100644 --- a/frontend/src/components/Table/index.ts +++ b/frontend/src/components/Table/index.ts @@ -1 +1,2 @@ +export * from "./TableActionsMenu"; export * from "./Table"; diff --git a/frontend/src/index.scss b/frontend/src/index.scss index ac2457df..864548f4 100644 --- a/frontend/src/index.scss +++ b/frontend/src/index.scss @@ -13,6 +13,11 @@ body { background-color: #f5f7fb; color: rgb(73, 80, 87); } + +.text-right { + text-align: right; +} + /* .btn { text-transform: none; diff --git a/frontend/src/locale/src/en.json b/frontend/src/locale/src/en.json index 6cca765a..5f4a478e 100644 --- a/frontend/src/locale/src/en.json +++ b/frontend/src/locale/src/en.json @@ -74,6 +74,9 @@ "setup.title": { "defaultMessage": "Create your first Account" }, + "setup-required": { + "defaultMessage": "Setup Required" + }, "user.email": { "defaultMessage": "Email" }, diff --git a/frontend/src/pages/CertificateAuthorities/index.tsx b/frontend/src/pages/CertificateAuthorities/index.tsx index 4000568e..0596dba9 100644 --- a/frontend/src/pages/CertificateAuthorities/index.tsx +++ b/frontend/src/pages/CertificateAuthorities/index.tsx @@ -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 ( + + {val + ? intl.formatMessage({ + id: "ready", + defaultMessage: "Ready", + }) + : intl.formatMessage({ + id: "setup-required", + defaultMessage: "Setup Required", + })} + + ); + }, }, ]; @@ -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,