From 9e9ae54af7c2dae702fb06316dfefeb8999762ab Mon Sep 17 00:00:00 2001 From: minnakt Date: Wed, 4 Oct 2023 14:21:07 -0400 Subject: [PATCH] EVG-19973: Improve patch parameters modal --- src/components/DisplayModal.tsx | 20 +++-------- src/pages/version/ParametersModal.test.tsx | 30 ++++++++++++++++ src/pages/version/ParametersModal.tsx | 42 ++++++++++++++-------- 3 files changed, 62 insertions(+), 30 deletions(-) create mode 100644 src/pages/version/ParametersModal.test.tsx diff --git a/src/components/DisplayModal.tsx b/src/components/DisplayModal.tsx index b82138fde3..bbd9960cd3 100644 --- a/src/components/DisplayModal.tsx +++ b/src/components/DisplayModal.tsx @@ -1,30 +1,20 @@ import styled from "@emotion/styled"; -import Modal, { ModalSize } from "@leafygreen-ui/modal"; +import Modal, { ModalProps } from "@leafygreen-ui/modal"; import { Body, BodyProps, H3 } from "@leafygreen-ui/typography"; import { size as tokenSize, zIndex } from "constants/tokens"; -export interface DisplayModalProps { - "data-cy"?: string; - open?: boolean; - setOpen?: ( - open: boolean - ) => void | React.Dispatch>; - size?: ModalSize; +type DisplayModalProps = Omit & { title?: React.ReactNode | string; - children: React.ReactNode; subtitle?: string; -} +}; export const DisplayModal: React.FC = ({ children, - "data-cy": dataCy, - open, - setOpen, - size, subtitle, title, + ...rest }) => ( - + {title &&

{title}

} {subtitle && ( {subtitle} diff --git a/src/pages/version/ParametersModal.test.tsx b/src/pages/version/ParametersModal.test.tsx new file mode 100644 index 0000000000..992e8a3573 --- /dev/null +++ b/src/pages/version/ParametersModal.test.tsx @@ -0,0 +1,30 @@ +import { render, screen, userEvent } from "test_utils"; +import { ParametersModal } from "./ParametersModal"; + +const parameters = [ + { key: "Key 1", value: "Value 1" }, + { key: "Key 2", value: "Value 2" }, +]; + +describe("parameters modal", () => { + it("modal is closed by default", () => { + render(); + expect(screen.queryByDataCy("parameters-modal")).toBeNull(); + }); + + it("link does not render if there are no parameters", () => { + render(); + expect(screen.queryByDataCy("parameters-link")).toBeNull(); + }); + + it("can click the link to open the modal and view parameters", async () => { + const user = userEvent.setup(); + render(); + await user.click(screen.getByDataCy("parameters-link")); + await screen.findByDataCy("parameters-modal"); + expect(screen.getByText(parameters[0].key)).toBeInTheDocument(); + expect(screen.getByText(parameters[0].value)).toBeInTheDocument(); + expect(screen.getByText(parameters[1].key)).toBeInTheDocument(); + expect(screen.getByText(parameters[1].value)).toBeInTheDocument(); + }); +}); diff --git a/src/pages/version/ParametersModal.tsx b/src/pages/version/ParametersModal.tsx index 869d920a89..38879412ca 100644 --- a/src/pages/version/ParametersModal.tsx +++ b/src/pages/version/ParametersModal.tsx @@ -1,10 +1,9 @@ -import { useState } from "react"; -import styled from "@emotion/styled"; -import Badge from "@leafygreen-ui/badge"; +import { useRef, useState } from "react"; +import { useLeafyGreenTable, LGColumnDef } from "@leafygreen-ui/table/new"; import { DisplayModal } from "components/DisplayModal"; import { MetadataItem } from "components/MetadataCard"; import { StyledLink } from "components/styles"; -import { size } from "constants/tokens"; +import { BaseTable } from "components/Table/BaseTable"; import { Parameter } from "gql/generated/types"; interface ParametersProps { @@ -13,6 +12,15 @@ interface ParametersProps { export const ParametersModal: React.FC = ({ parameters }) => { const [showModal, setShowModal] = useState(false); + + const tableContainerRef = useRef(null); + + const table = useLeafyGreenTable({ + containerRef: tableContainerRef, + data: parameters, + columns, + }); + return ( <> {parameters !== undefined && parameters.length > 0 && ( @@ -26,23 +34,27 @@ export const ParametersModal: React.FC = ({ parameters }) => { )} - {parameters?.map((param) => ( - - {param.key}: {param.value} - - ))} + ); }; -const StyledBadge = styled(Badge)` - :not(:last-of-type) { - margin-right: ${size.s}; - } -`; +const columns: LGColumnDef[] = [ + { + accessorKey: "key", + header: "Key", + cell: (value) => value.getValue(), + }, + { + accessorKey: "value", + header: "Value", + cell: (value) => value.getValue(), + }, +];